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 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 374 | typedef minstd_rand default_random_engine; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 488 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 547 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 602 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 660 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 716 | |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 774 | |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 830 | |
Howard Hinnant | 4ff556c | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 886 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 944 | |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1002 | |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1060 | |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1118 | |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1176 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1222 | friend bool operator==(const chi_squared_distribution& x, |
| 1223 | const chi_squared_distribution& y); |
| 1224 | friend bool operator!=(const chi_squared_distribution& x, |
| 1225 | const chi_squared_distribution& y); |
| 1226 | |
| 1227 | template <class charT, class traits> |
| 1228 | friend |
| 1229 | basic_ostream<charT, traits>& |
| 1230 | operator<<(basic_ostream<charT, traits>& os, |
| 1231 | const chi_squared_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1232 | |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1233 | template <class charT, class traits> |
| 1234 | friend |
| 1235 | basic_istream<charT, traits>& |
| 1236 | operator>>(basic_istream<charT, traits>& is, |
| 1237 | chi_squared_distribution& x); |
| 1238 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1239 | |
| 1240 | template<class RealType = double> |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1241 | class cauchy_distribution |
| 1242 | { |
| 1243 | public: |
| 1244 | // types |
| 1245 | typedef RealType result_type; |
| 1246 | |
| 1247 | class param_type |
| 1248 | { |
| 1249 | public: |
| 1250 | typedef cauchy_distribution distribution_type; |
| 1251 | |
| 1252 | explicit param_type(result_type a = 0, result_type b = 1); |
| 1253 | |
| 1254 | result_type a() const; |
| 1255 | result_type b() const; |
| 1256 | |
| 1257 | friend bool operator==(const param_type& x, const param_type& y); |
| 1258 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1259 | }; |
| 1260 | |
| 1261 | // constructor and reset functions |
| 1262 | explicit cauchy_distribution(result_type a = 0, result_type b = 1); |
| 1263 | explicit cauchy_distribution(const param_type& parm); |
| 1264 | void reset(); |
| 1265 | |
| 1266 | // generating functions |
| 1267 | template<class URNG> result_type operator()(URNG& g); |
| 1268 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1269 | |
| 1270 | // property functions |
| 1271 | result_type a() const; |
| 1272 | result_type b() const; |
| 1273 | |
| 1274 | param_type param() const; |
| 1275 | void param(const param_type& parm); |
| 1276 | |
| 1277 | result_type min() const; |
| 1278 | result_type max() const; |
| 1279 | |
| 1280 | friend bool operator==(const cauchy_distribution& x, |
| 1281 | const cauchy_distribution& y); |
| 1282 | friend bool operator!=(const cauchy_distribution& x, |
| 1283 | const cauchy_distribution& y); |
| 1284 | |
| 1285 | template <class charT, class traits> |
| 1286 | friend |
| 1287 | basic_ostream<charT, traits>& |
| 1288 | operator<<(basic_ostream<charT, traits>& os, |
| 1289 | const cauchy_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1290 | |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1291 | template <class charT, class traits> |
| 1292 | friend |
| 1293 | basic_istream<charT, traits>& |
| 1294 | operator>>(basic_istream<charT, traits>& is, |
| 1295 | cauchy_distribution& x); |
| 1296 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1297 | |
| 1298 | template<class RealType = double> |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1299 | class fisher_f_distribution |
| 1300 | { |
| 1301 | public: |
| 1302 | // types |
| 1303 | typedef RealType result_type; |
| 1304 | |
| 1305 | class param_type |
| 1306 | { |
| 1307 | public: |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1308 | typedef fisher_f_distribution distribution_type; |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1309 | |
| 1310 | explicit param_type(result_type m = 1, result_type n = 1); |
| 1311 | |
| 1312 | result_type m() const; |
| 1313 | result_type n() const; |
| 1314 | |
| 1315 | friend bool operator==(const param_type& x, const param_type& y); |
| 1316 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1317 | }; |
| 1318 | |
| 1319 | // constructor and reset functions |
| 1320 | explicit fisher_f_distribution(result_type m = 1, result_type n = 1); |
| 1321 | explicit fisher_f_distribution(const param_type& parm); |
| 1322 | void reset(); |
| 1323 | |
| 1324 | // generating functions |
| 1325 | template<class URNG> result_type operator()(URNG& g); |
| 1326 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1327 | |
| 1328 | // property functions |
| 1329 | result_type m() const; |
| 1330 | result_type n() const; |
| 1331 | |
| 1332 | param_type param() const; |
| 1333 | void param(const param_type& parm); |
| 1334 | |
| 1335 | result_type min() const; |
| 1336 | result_type max() const; |
| 1337 | |
| 1338 | friend bool operator==(const fisher_f_distribution& x, |
| 1339 | const fisher_f_distribution& y); |
| 1340 | friend bool operator!=(const fisher_f_distribution& x, |
| 1341 | const fisher_f_distribution& y); |
| 1342 | |
| 1343 | template <class charT, class traits> |
| 1344 | friend |
| 1345 | basic_ostream<charT, traits>& |
| 1346 | operator<<(basic_ostream<charT, traits>& os, |
| 1347 | const fisher_f_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1348 | |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1349 | template <class charT, class traits> |
| 1350 | friend |
| 1351 | basic_istream<charT, traits>& |
| 1352 | operator>>(basic_istream<charT, traits>& is, |
| 1353 | fisher_f_distribution& x); |
| 1354 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1355 | |
| 1356 | template<class RealType = double> |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1357 | class student_t_distribution |
| 1358 | { |
| 1359 | public: |
| 1360 | // types |
| 1361 | typedef RealType result_type; |
| 1362 | |
| 1363 | class param_type |
| 1364 | { |
| 1365 | public: |
| 1366 | typedef student_t_distribution distribution_type; |
| 1367 | |
| 1368 | explicit param_type(result_type n = 1); |
| 1369 | |
| 1370 | result_type n() const; |
| 1371 | |
| 1372 | friend bool operator==(const param_type& x, const param_type& y); |
| 1373 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1374 | }; |
| 1375 | |
| 1376 | // constructor and reset functions |
| 1377 | explicit student_t_distribution(result_type n = 1); |
| 1378 | explicit student_t_distribution(const param_type& parm); |
| 1379 | void reset(); |
| 1380 | |
| 1381 | // generating functions |
| 1382 | template<class URNG> result_type operator()(URNG& g); |
| 1383 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1384 | |
| 1385 | // property functions |
| 1386 | result_type n() const; |
| 1387 | |
| 1388 | param_type param() const; |
| 1389 | void param(const param_type& parm); |
| 1390 | |
| 1391 | result_type min() const; |
| 1392 | result_type max() const; |
| 1393 | |
| 1394 | friend bool operator==(const student_t_distribution& x, |
| 1395 | const student_t_distribution& y); |
| 1396 | friend bool operator!=(const student_t_distribution& x, |
| 1397 | const student_t_distribution& y); |
| 1398 | |
| 1399 | template <class charT, class traits> |
| 1400 | friend |
| 1401 | basic_ostream<charT, traits>& |
| 1402 | operator<<(basic_ostream<charT, traits>& os, |
| 1403 | const student_t_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1404 | |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1405 | template <class charT, class traits> |
| 1406 | friend |
| 1407 | basic_istream<charT, traits>& |
| 1408 | operator>>(basic_istream<charT, traits>& is, |
| 1409 | student_t_distribution& x); |
| 1410 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1411 | |
| 1412 | template<class IntType = int> |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1413 | class discrete_distribution |
| 1414 | { |
| 1415 | public: |
| 1416 | // types |
| 1417 | typedef IntType result_type; |
| 1418 | |
| 1419 | class param_type |
| 1420 | { |
| 1421 | public: |
| 1422 | typedef discrete_distribution distribution_type; |
| 1423 | |
| 1424 | param_type(); |
| 1425 | template<class InputIterator> |
| 1426 | param_type(InputIterator firstW, InputIterator lastW); |
| 1427 | param_type(initializer_list<double> wl); |
| 1428 | template<class UnaryOperation> |
| 1429 | param_type(size_t nw, double xmin, double xmax, UnaryOperation fw); |
| 1430 | |
| 1431 | vector<double> probabilities() const; |
| 1432 | |
| 1433 | friend bool operator==(const param_type& x, const param_type& y); |
| 1434 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1435 | }; |
| 1436 | |
| 1437 | // constructor and reset functions |
| 1438 | discrete_distribution(); |
| 1439 | template<class InputIterator> |
| 1440 | discrete_distribution(InputIterator firstW, InputIterator lastW); |
| 1441 | discrete_distribution(initializer_list<double> wl); |
| 1442 | template<class UnaryOperation> |
| 1443 | discrete_distribution(size_t nw, double xmin, double xmax, |
| 1444 | UnaryOperation fw); |
| 1445 | explicit discrete_distribution(const param_type& parm); |
| 1446 | void reset(); |
| 1447 | |
| 1448 | // generating functions |
| 1449 | template<class URNG> result_type operator()(URNG& g); |
| 1450 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1451 | |
| 1452 | // property functions |
| 1453 | vector<double> probabilities() const; |
| 1454 | |
| 1455 | param_type param() const; |
| 1456 | void param(const param_type& parm); |
| 1457 | |
| 1458 | result_type min() const; |
| 1459 | result_type max() const; |
| 1460 | |
| 1461 | friend bool operator==(const discrete_distribution& x, |
| 1462 | const discrete_distribution& y); |
| 1463 | friend bool operator!=(const discrete_distribution& x, |
| 1464 | const discrete_distribution& y); |
| 1465 | |
| 1466 | template <class charT, class traits> |
| 1467 | friend |
| 1468 | basic_ostream<charT, traits>& |
| 1469 | operator<<(basic_ostream<charT, traits>& os, |
| 1470 | const discrete_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1471 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1472 | template <class charT, class traits> |
| 1473 | friend |
| 1474 | basic_istream<charT, traits>& |
| 1475 | operator>>(basic_istream<charT, traits>& is, |
| 1476 | discrete_distribution& x); |
| 1477 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1478 | |
| 1479 | template<class RealType = double> |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1480 | class piecewise_constant_distribution |
| 1481 | { |
| 1482 | // types |
| 1483 | typedef RealType result_type; |
| 1484 | |
| 1485 | class param_type |
| 1486 | { |
| 1487 | public: |
| 1488 | typedef piecewise_constant_distribution distribution_type; |
| 1489 | |
| 1490 | param_type(); |
| 1491 | template<class InputIteratorB, class InputIteratorW> |
| 1492 | param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 1493 | InputIteratorW firstW); |
| 1494 | template<class UnaryOperation> |
| 1495 | param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 1496 | template<class UnaryOperation> |
| 1497 | param_type(size_t nw, result_type xmin, result_type xmax, |
| 1498 | UnaryOperation fw); |
| 1499 | |
| 1500 | vector<result_type> intervals() const; |
| 1501 | vector<double> densities() const; |
| 1502 | |
| 1503 | friend bool operator==(const param_type& x, const param_type& y); |
| 1504 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1505 | }; |
| 1506 | |
| 1507 | // constructor and reset functions |
| 1508 | piecewise_constant_distribution(); |
| 1509 | template<class InputIteratorB, class InputIteratorW> |
| 1510 | piecewise_constant_distribution(InputIteratorB firstB, |
| 1511 | InputIteratorB lastB, |
| 1512 | InputIteratorW firstW); |
| 1513 | template<class UnaryOperation> |
| 1514 | piecewise_constant_distribution(initializer_list<result_type> bl, |
| 1515 | UnaryOperation fw); |
| 1516 | template<class UnaryOperation> |
| 1517 | piecewise_constant_distribution(size_t nw, result_type xmin, |
| 1518 | result_type xmax, UnaryOperation fw); |
| 1519 | explicit piecewise_constant_distribution(const param_type& parm); |
| 1520 | void reset(); |
| 1521 | |
| 1522 | // generating functions |
| 1523 | template<class URNG> result_type operator()(URNG& g); |
| 1524 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1525 | |
| 1526 | // property functions |
| 1527 | vector<result_type> intervals() const; |
| 1528 | vector<double> densities() const; |
| 1529 | |
| 1530 | param_type param() const; |
| 1531 | void param(const param_type& parm); |
| 1532 | |
| 1533 | result_type min() const; |
| 1534 | result_type max() const; |
| 1535 | |
| 1536 | friend bool operator==(const piecewise_constant_distribution& x, |
| 1537 | const piecewise_constant_distribution& y); |
| 1538 | friend bool operator!=(const piecewise_constant_distribution& x, |
| 1539 | const piecewise_constant_distribution& y); |
| 1540 | |
| 1541 | template <class charT, class traits> |
| 1542 | friend |
| 1543 | basic_ostream<charT, traits>& |
| 1544 | operator<<(basic_ostream<charT, traits>& os, |
| 1545 | const piecewise_constant_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1546 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1547 | template <class charT, class traits> |
| 1548 | friend |
| 1549 | basic_istream<charT, traits>& |
| 1550 | operator>>(basic_istream<charT, traits>& is, |
| 1551 | piecewise_constant_distribution& x); |
| 1552 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1553 | |
| 1554 | template<class RealType = double> |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1555 | class piecewise_linear_distribution |
| 1556 | { |
| 1557 | // types |
| 1558 | typedef RealType result_type; |
| 1559 | |
| 1560 | class param_type |
| 1561 | { |
| 1562 | public: |
| 1563 | typedef piecewise_linear_distribution distribution_type; |
| 1564 | |
| 1565 | param_type(); |
| 1566 | template<class InputIteratorB, class InputIteratorW> |
| 1567 | param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 1568 | InputIteratorW firstW); |
| 1569 | template<class UnaryOperation> |
| 1570 | param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 1571 | template<class UnaryOperation> |
| 1572 | param_type(size_t nw, result_type xmin, result_type xmax, |
| 1573 | UnaryOperation fw); |
| 1574 | |
| 1575 | vector<result_type> intervals() const; |
| 1576 | vector<double> densities() const; |
| 1577 | |
| 1578 | friend bool operator==(const param_type& x, const param_type& y); |
| 1579 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1580 | }; |
| 1581 | |
| 1582 | // constructor and reset functions |
| 1583 | piecewise_linear_distribution(); |
| 1584 | template<class InputIteratorB, class InputIteratorW> |
| 1585 | piecewise_linear_distribution(InputIteratorB firstB, |
| 1586 | InputIteratorB lastB, |
| 1587 | InputIteratorW firstW); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1588 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1589 | template<class UnaryOperation> |
| 1590 | piecewise_linear_distribution(initializer_list<result_type> bl, |
| 1591 | UnaryOperation fw); |
| 1592 | |
| 1593 | template<class UnaryOperation> |
| 1594 | piecewise_linear_distribution(size_t nw, result_type xmin, |
| 1595 | result_type xmax, UnaryOperation fw); |
| 1596 | |
| 1597 | explicit piecewise_linear_distribution(const param_type& parm); |
| 1598 | void reset(); |
| 1599 | |
| 1600 | // generating functions |
| 1601 | template<class URNG> result_type operator()(URNG& g); |
| 1602 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1603 | |
| 1604 | // property functions |
| 1605 | vector<result_type> intervals() const; |
| 1606 | vector<double> densities() const; |
| 1607 | |
| 1608 | param_type param() const; |
| 1609 | void param(const param_type& parm); |
| 1610 | |
| 1611 | result_type min() const; |
| 1612 | result_type max() const; |
| 1613 | |
| 1614 | friend bool operator==(const piecewise_linear_distribution& x, |
| 1615 | const piecewise_linear_distribution& y); |
| 1616 | friend bool operator!=(const piecewise_linear_distribution& x, |
| 1617 | const piecewise_linear_distribution& y); |
| 1618 | |
| 1619 | template <class charT, class traits> |
| 1620 | friend |
| 1621 | basic_ostream<charT, traits>& |
| 1622 | operator<<(basic_ostream<charT, traits>& os, |
| 1623 | const piecewise_linear_distribution& x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1624 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1625 | template <class charT, class traits> |
| 1626 | friend |
| 1627 | basic_istream<charT, traits>& |
| 1628 | operator>>(basic_istream<charT, traits>& is, |
| 1629 | piecewise_linear_distribution& x); |
| 1630 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1631 | |
| 1632 | } // std |
| 1633 | */ |
| 1634 | |
| 1635 | #include <__config> |
| 1636 | #include <cstddef> |
| 1637 | #include <type_traits> |
| 1638 | #include <initializer_list> |
| 1639 | #include <cstdint> |
| 1640 | #include <limits> |
| 1641 | #include <algorithm> |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1642 | #include <numeric> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1643 | #include <vector> |
| 1644 | #include <string> |
| 1645 | #include <istream> |
| 1646 | #include <ostream> |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 1647 | #include <cmath> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1648 | |
| 1649 | #pragma GCC system_header |
| 1650 | |
| 1651 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 1652 | |
| 1653 | // linear_congruential_engine |
| 1654 | |
| 1655 | template <unsigned long long __a, unsigned long long __c, |
| 1656 | unsigned long long __m, unsigned long long _M, |
| 1657 | bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)> |
| 1658 | struct __lce_ta; |
| 1659 | |
| 1660 | // 64 |
| 1661 | |
| 1662 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1663 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> |
| 1664 | { |
| 1665 | typedef unsigned long long result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1666 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1667 | static result_type next(result_type __x) |
| 1668 | { |
| 1669 | // Schrage's algorithm |
| 1670 | const result_type __q = __m / __a; |
| 1671 | const result_type __r = __m % __a; |
| 1672 | const result_type __t0 = __a * (__x % __q); |
| 1673 | const result_type __t1 = __r * (__x / __q); |
| 1674 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1675 | __x += __c - (__x >= __m - __c) * __m; |
| 1676 | return __x; |
| 1677 | } |
| 1678 | }; |
| 1679 | |
| 1680 | template <unsigned long long __a, unsigned long long __m> |
| 1681 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> |
| 1682 | { |
| 1683 | typedef unsigned long long result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1684 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1685 | static result_type next(result_type __x) |
| 1686 | { |
| 1687 | // Schrage's algorithm |
| 1688 | const result_type __q = __m / __a; |
| 1689 | const result_type __r = __m % __a; |
| 1690 | const result_type __t0 = __a * (__x % __q); |
| 1691 | const result_type __t1 = __r * (__x / __q); |
| 1692 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1693 | return __x; |
| 1694 | } |
| 1695 | }; |
| 1696 | |
| 1697 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1698 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> |
| 1699 | { |
| 1700 | typedef unsigned long long result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1701 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1702 | static result_type next(result_type __x) |
| 1703 | { |
| 1704 | return (__a * __x + __c) % __m; |
| 1705 | } |
| 1706 | }; |
| 1707 | |
| 1708 | template <unsigned long long __a, unsigned long long __c> |
| 1709 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> |
| 1710 | { |
| 1711 | typedef unsigned long long result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1712 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1713 | static result_type next(result_type __x) |
| 1714 | { |
| 1715 | return __a * __x + __c; |
| 1716 | } |
| 1717 | }; |
| 1718 | |
| 1719 | // 32 |
| 1720 | |
| 1721 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> |
| 1722 | struct __lce_ta<_A, _C, _M, unsigned(~0), true> |
| 1723 | { |
| 1724 | typedef unsigned result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1725 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1726 | static result_type next(result_type __x) |
| 1727 | { |
| 1728 | const result_type __a = static_cast<result_type>(_A); |
| 1729 | const result_type __c = static_cast<result_type>(_C); |
| 1730 | const result_type __m = static_cast<result_type>(_M); |
| 1731 | // Schrage's algorithm |
| 1732 | const result_type __q = __m / __a; |
| 1733 | const result_type __r = __m % __a; |
| 1734 | const result_type __t0 = __a * (__x % __q); |
| 1735 | const result_type __t1 = __r * (__x / __q); |
| 1736 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1737 | __x += __c - (__x >= __m - __c) * __m; |
| 1738 | return __x; |
| 1739 | } |
| 1740 | }; |
| 1741 | |
| 1742 | template <unsigned long long _A, unsigned long long _M> |
| 1743 | struct __lce_ta<_A, 0, _M, unsigned(~0), true> |
| 1744 | { |
| 1745 | typedef unsigned result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1746 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1747 | static result_type next(result_type __x) |
| 1748 | { |
| 1749 | const result_type __a = static_cast<result_type>(_A); |
| 1750 | const result_type __m = static_cast<result_type>(_M); |
| 1751 | // Schrage's algorithm |
| 1752 | const result_type __q = __m / __a; |
| 1753 | const result_type __r = __m % __a; |
| 1754 | const result_type __t0 = __a * (__x % __q); |
| 1755 | const result_type __t1 = __r * (__x / __q); |
| 1756 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1757 | return __x; |
| 1758 | } |
| 1759 | }; |
| 1760 | |
| 1761 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> |
| 1762 | struct __lce_ta<_A, _C, _M, unsigned(~0), false> |
| 1763 | { |
| 1764 | typedef unsigned result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1765 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1766 | static result_type next(result_type __x) |
| 1767 | { |
| 1768 | const result_type __a = static_cast<result_type>(_A); |
| 1769 | const result_type __c = static_cast<result_type>(_C); |
| 1770 | const result_type __m = static_cast<result_type>(_M); |
| 1771 | return (__a * __x + __c) % __m; |
| 1772 | } |
| 1773 | }; |
| 1774 | |
| 1775 | template <unsigned long long _A, unsigned long long _C> |
| 1776 | struct __lce_ta<_A, _C, 0, unsigned(~0), false> |
| 1777 | { |
| 1778 | typedef unsigned result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1779 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1780 | static result_type next(result_type __x) |
| 1781 | { |
| 1782 | const result_type __a = static_cast<result_type>(_A); |
| 1783 | const result_type __c = static_cast<result_type>(_C); |
| 1784 | return __a * __x + __c; |
| 1785 | } |
| 1786 | }; |
| 1787 | |
| 1788 | // 16 |
| 1789 | |
| 1790 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> |
| 1791 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> |
| 1792 | { |
| 1793 | typedef unsigned short result_type; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1794 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1795 | static result_type next(result_type __x) |
| 1796 | { |
| 1797 | return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); |
| 1798 | } |
| 1799 | }; |
| 1800 | |
| 1801 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1802 | class linear_congruential_engine; |
| 1803 | |
| 1804 | template <class _CharT, class _Traits, |
| 1805 | class _U, _U _A, _U _C, _U _N> |
| 1806 | basic_ostream<_CharT, _Traits>& |
| 1807 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1808 | const linear_congruential_engine<_U, _A, _C, _N>&); |
| 1809 | |
| 1810 | template <class _CharT, class _Traits, |
| 1811 | class _U, _U _A, _U _C, _U _N> |
| 1812 | basic_istream<_CharT, _Traits>& |
| 1813 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1814 | linear_congruential_engine<_U, _A, _C, _N>& __x); |
| 1815 | |
| 1816 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1817 | class _LIBCPP_VISIBLE linear_congruential_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1818 | { |
| 1819 | public: |
| 1820 | // types |
| 1821 | typedef _UIntType result_type; |
| 1822 | |
| 1823 | private: |
| 1824 | result_type __x_; |
| 1825 | |
| 1826 | static const result_type _M = result_type(~0); |
| 1827 | |
| 1828 | static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); |
| 1829 | static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); |
| 1830 | public: |
| 1831 | static const result_type _Min = __c == 0u ? 1u: 0u; |
| 1832 | static const result_type _Max = __m - 1u; |
| 1833 | static_assert(_Min < _Max, "linear_congruential_engine invalid parameters"); |
| 1834 | |
| 1835 | // engine characteristics |
| 1836 | static const/*expr*/ result_type multiplier = __a; |
| 1837 | static const/*expr*/ result_type increment = __c; |
| 1838 | static const/*expr*/ result_type modulus = __m; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1839 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1840 | static const/*expr*/ result_type min() {return _Min;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1841 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1842 | static const/*expr*/ result_type max() {return _Max;} |
| 1843 | static const/*expr*/ result_type default_seed = 1u; |
| 1844 | |
| 1845 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1846 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1847 | explicit linear_congruential_engine(result_type __s = default_seed) |
| 1848 | {seed(__s);} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 1849 | template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q, |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1850 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 1851 | typename enable_if<!is_convertible<_Sseq, result_type>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1852 | {seed(__q);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1853 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1854 | void seed(result_type __s = default_seed) |
| 1855 | {seed(integral_constant<bool, __m == 0>(), |
| 1856 | integral_constant<bool, __c == 0>(), __s);} |
| 1857 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1858 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1859 | typename enable_if |
| 1860 | < |
| 1861 | !is_convertible<_Sseq, result_type>::value, |
| 1862 | void |
| 1863 | >::type |
| 1864 | seed(_Sseq& __q) |
| 1865 | {__seed(__q, integral_constant<unsigned, |
| 1866 | 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 |
| 1867 | : (__m-1) / 0x100000000ull)>());} |
| 1868 | |
| 1869 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1870 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1871 | result_type operator()() |
| 1872 | {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1873 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1874 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1875 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1876 | friend _LIBCPP_INLINE_VISIBILITY |
| 1877 | bool operator==(const linear_congruential_engine& __x, |
| 1878 | const linear_congruential_engine& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1879 | {return __x.__x_ == __y.__x_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1880 | friend _LIBCPP_INLINE_VISIBILITY |
| 1881 | bool operator!=(const linear_congruential_engine& __x, |
| 1882 | const linear_congruential_engine& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1883 | {return !(__x == __y);} |
| 1884 | |
| 1885 | private: |
| 1886 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1887 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1888 | void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1889 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1890 | void seed(true_type, false_type, result_type __s) {__x_ = __s;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1891 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1892 | void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? |
| 1893 | 1 : __s % __m;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1894 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1895 | void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} |
| 1896 | |
| 1897 | template<class _Sseq> |
| 1898 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1899 | template<class _Sseq> |
| 1900 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1901 | |
| 1902 | template <class _CharT, class _Traits, |
| 1903 | class _U, _U _A, _U _C, _U _N> |
| 1904 | friend |
| 1905 | basic_ostream<_CharT, _Traits>& |
| 1906 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1907 | const linear_congruential_engine<_U, _A, _C, _N>&); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1908 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1909 | template <class _CharT, class _Traits, |
| 1910 | class _U, _U _A, _U _C, _U _N> |
| 1911 | friend |
| 1912 | basic_istream<_CharT, _Traits>& |
| 1913 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1914 | linear_congruential_engine<_U, _A, _C, _N>& __x); |
| 1915 | }; |
| 1916 | |
| 1917 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1918 | template<class _Sseq> |
| 1919 | void |
| 1920 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1921 | integral_constant<unsigned, 1>) |
| 1922 | { |
| 1923 | const unsigned __k = 1; |
| 1924 | uint32_t __ar[__k+3]; |
| 1925 | __q.generate(__ar, __ar + __k + 3); |
| 1926 | result_type __s = static_cast<result_type>(__ar[3] % __m); |
| 1927 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1928 | } |
| 1929 | |
| 1930 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1931 | template<class _Sseq> |
| 1932 | void |
| 1933 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1934 | integral_constant<unsigned, 2>) |
| 1935 | { |
| 1936 | const unsigned __k = 2; |
| 1937 | uint32_t __ar[__k+3]; |
| 1938 | __q.generate(__ar, __ar + __k + 3); |
| 1939 | result_type __s = static_cast<result_type>((__ar[3] + |
| 1940 | (uint64_t)__ar[4] << 32) % __m); |
| 1941 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1942 | } |
| 1943 | |
| 1944 | template <class _CharT, class _Traits> |
| 1945 | class __save_flags |
| 1946 | { |
| 1947 | typedef basic_ios<_CharT, _Traits> __stream_type; |
| 1948 | typedef typename __stream_type::fmtflags fmtflags; |
| 1949 | |
| 1950 | __stream_type& __stream_; |
| 1951 | fmtflags __fmtflags_; |
| 1952 | _CharT __fill_; |
| 1953 | |
| 1954 | __save_flags(const __save_flags&); |
| 1955 | __save_flags& operator=(const __save_flags&); |
| 1956 | public: |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1957 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1958 | explicit __save_flags(__stream_type& __stream) |
| 1959 | : __stream_(__stream), |
| 1960 | __fmtflags_(__stream.flags()), |
| 1961 | __fill_(__stream.fill()) |
| 1962 | {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1963 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1964 | ~__save_flags() |
| 1965 | { |
| 1966 | __stream_.flags(__fmtflags_); |
| 1967 | __stream_.fill(__fill_); |
| 1968 | } |
| 1969 | }; |
| 1970 | |
| 1971 | template <class _CharT, class _Traits, |
| 1972 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1973 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1974 | basic_ostream<_CharT, _Traits>& |
| 1975 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1976 | const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1977 | { |
| 1978 | __save_flags<_CharT, _Traits> _(__os); |
| 1979 | __os.flags(ios_base::dec | ios_base::left); |
| 1980 | __os.fill(__os.widen(' ')); |
| 1981 | return __os << __x.__x_; |
| 1982 | } |
| 1983 | |
| 1984 | template <class _CharT, class _Traits, |
| 1985 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1986 | basic_istream<_CharT, _Traits>& |
| 1987 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1988 | linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1989 | { |
| 1990 | __save_flags<_CharT, _Traits> _(__is); |
| 1991 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1992 | _UIntType __t; |
| 1993 | __is >> __t; |
| 1994 | if (!__is.fail()) |
| 1995 | __x.__x_ = __t; |
| 1996 | return __is; |
| 1997 | } |
| 1998 | |
| 1999 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 2000 | minstd_rand0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2001 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 2002 | minstd_rand; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 2003 | typedef minstd_rand default_random_engine; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2004 | // mersenne_twister_engine |
| 2005 | |
| 2006 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2007 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2008 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2009 | class mersenne_twister_engine; |
| 2010 | |
| 2011 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2012 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2013 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2014 | bool |
| 2015 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2016 | _B, _T, _C, _L, _F>& __x, |
| 2017 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2018 | _B, _T, _C, _L, _F>& __y); |
| 2019 | |
| 2020 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2021 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2022 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2023 | bool |
| 2024 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2025 | _B, _T, _C, _L, _F>& __x, |
| 2026 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2027 | _B, _T, _C, _L, _F>& __y); |
| 2028 | |
| 2029 | template <class _CharT, class _Traits, |
| 2030 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2031 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2032 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2033 | basic_ostream<_CharT, _Traits>& |
| 2034 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2035 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2036 | _B, _T, _C, _L, _F>& __x); |
| 2037 | |
| 2038 | template <class _CharT, class _Traits, |
| 2039 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2040 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2041 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2042 | basic_istream<_CharT, _Traits>& |
| 2043 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2044 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2045 | _B, _T, _C, _L, _F>& __x); |
| 2046 | |
| 2047 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2048 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2049 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2050 | class _LIBCPP_VISIBLE mersenne_twister_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2051 | { |
| 2052 | public: |
| 2053 | // types |
| 2054 | typedef _UIntType result_type; |
| 2055 | |
| 2056 | private: |
| 2057 | result_type __x_[__n]; |
| 2058 | size_t __i_; |
| 2059 | |
| 2060 | static_assert( 0 < __m, "mersenne_twister_engine invalid parameters"); |
| 2061 | static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); |
| 2062 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 2063 | static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); |
| 2064 | static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters"); |
| 2065 | static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); |
| 2066 | static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); |
| 2067 | static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); |
| 2068 | static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); |
| 2069 | static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); |
| 2070 | public: |
| 2071 | static const result_type _Min = 0; |
| 2072 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2073 | (result_type(1) << __w) - result_type(1); |
| 2074 | static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); |
| 2075 | static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2076 | static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2077 | static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2078 | static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2079 | static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2080 | |
| 2081 | // engine characteristics |
| 2082 | static const/*expr*/ size_t word_size = __w; |
| 2083 | static const/*expr*/ size_t state_size = __n; |
| 2084 | static const/*expr*/ size_t shift_size = __m; |
| 2085 | static const/*expr*/ size_t mask_bits = __r; |
| 2086 | static const/*expr*/ result_type xor_mask = __a; |
| 2087 | static const/*expr*/ size_t tempering_u = __u; |
| 2088 | static const/*expr*/ result_type tempering_d = __d; |
| 2089 | static const/*expr*/ size_t tempering_s = __s; |
| 2090 | static const/*expr*/ result_type tempering_b = __b; |
| 2091 | static const/*expr*/ size_t tempering_t = __t; |
| 2092 | static const/*expr*/ result_type tempering_c = __c; |
| 2093 | static const/*expr*/ size_t tempering_l = __l; |
| 2094 | static const/*expr*/ result_type initialization_multiplier = __f; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2095 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2096 | static const/*expr*/ result_type min() { return _Min; } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2097 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2098 | static const/*expr*/ result_type max() { return _Max; } |
| 2099 | static const/*expr*/ result_type default_seed = 5489u; |
| 2100 | |
| 2101 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2102 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2103 | explicit mersenne_twister_engine(result_type __sd = default_seed) |
| 2104 | {seed(__sd);} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2105 | template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q, |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2106 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2107 | typename enable_if<!is_convertible<_Sseq, result_type>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2108 | {seed(__q);} |
| 2109 | void seed(result_type __sd = default_seed); |
| 2110 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2111 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2112 | typename enable_if |
| 2113 | < |
| 2114 | !is_convertible<_Sseq, result_type>::value, |
| 2115 | void |
| 2116 | >::type |
| 2117 | seed(_Sseq& __q) |
| 2118 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2119 | |
| 2120 | // generating functions |
| 2121 | result_type operator()(); |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2122 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2123 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2124 | |
| 2125 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2126 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2127 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2128 | friend |
| 2129 | bool |
| 2130 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2131 | _B, _T, _C, _L, _F>& __x, |
| 2132 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2133 | _B, _T, _C, _L, _F>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2134 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2135 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2136 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2137 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2138 | friend |
| 2139 | bool |
| 2140 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2141 | _B, _T, _C, _L, _F>& __x, |
| 2142 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2143 | _B, _T, _C, _L, _F>& __y); |
| 2144 | |
| 2145 | template <class _CharT, class _Traits, |
| 2146 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2147 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2148 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2149 | friend |
| 2150 | basic_ostream<_CharT, _Traits>& |
| 2151 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2152 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2153 | _B, _T, _C, _L, _F>& __x); |
| 2154 | |
| 2155 | template <class _CharT, class _Traits, |
| 2156 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2157 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2158 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2159 | friend |
| 2160 | basic_istream<_CharT, _Traits>& |
| 2161 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2162 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2163 | _B, _T, _C, _L, _F>& __x); |
| 2164 | private: |
| 2165 | |
| 2166 | template<class _Sseq> |
| 2167 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2168 | template<class _Sseq> |
| 2169 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2170 | |
| 2171 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2172 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2173 | static |
| 2174 | typename enable_if |
| 2175 | < |
| 2176 | __count < __w, |
| 2177 | result_type |
| 2178 | >::type |
| 2179 | __lshift(result_type __x) {return (__x << __count) & _Max;} |
| 2180 | |
| 2181 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2182 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2183 | static |
| 2184 | typename enable_if |
| 2185 | < |
| 2186 | (__count >= __w), |
| 2187 | result_type |
| 2188 | >::type |
| 2189 | __lshift(result_type __x) {return result_type(0);} |
| 2190 | |
| 2191 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2192 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2193 | static |
| 2194 | typename enable_if |
| 2195 | < |
| 2196 | __count < _Dt, |
| 2197 | result_type |
| 2198 | >::type |
| 2199 | __rshift(result_type __x) {return __x >> __count;} |
| 2200 | |
| 2201 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2202 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2203 | static |
| 2204 | typename enable_if |
| 2205 | < |
| 2206 | (__count >= _Dt), |
| 2207 | result_type |
| 2208 | >::type |
| 2209 | __rshift(result_type __x) {return result_type(0);} |
| 2210 | }; |
| 2211 | |
| 2212 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2213 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2214 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2215 | void |
| 2216 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2217 | __t, __c, __l, __f>::seed(result_type __sd) |
| 2218 | { // __w >= 2 |
| 2219 | __x_[0] = __sd & _Max; |
| 2220 | for (size_t __i = 1; __i < __n; ++__i) |
| 2221 | __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; |
| 2222 | __i_ = 0; |
| 2223 | } |
| 2224 | |
| 2225 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2226 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2227 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2228 | template<class _Sseq> |
| 2229 | void |
| 2230 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2231 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) |
| 2232 | { |
| 2233 | const unsigned __k = 1; |
| 2234 | uint32_t __ar[__n * __k]; |
| 2235 | __q.generate(__ar, __ar + __n * __k); |
| 2236 | for (size_t __i = 0; __i < __n; ++__i) |
| 2237 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2238 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2239 | (result_type(1) << __r) - result_type(1); |
| 2240 | __i_ = 0; |
| 2241 | if ((__x_[0] & ~__mask) == 0) |
| 2242 | { |
| 2243 | for (size_t __i = 1; __i < __n; ++__i) |
| 2244 | if (__x_[__i] != 0) |
| 2245 | return; |
| 2246 | __x_[0] = _Max; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2251 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2252 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2253 | template<class _Sseq> |
| 2254 | void |
| 2255 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2256 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) |
| 2257 | { |
| 2258 | const unsigned __k = 2; |
| 2259 | uint32_t __ar[__n * __k]; |
| 2260 | __q.generate(__ar, __ar + __n * __k); |
| 2261 | for (size_t __i = 0; __i < __n; ++__i) |
| 2262 | __x_[__i] = static_cast<result_type>( |
| 2263 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2264 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2265 | (result_type(1) << __r) - result_type(1); |
| 2266 | __i_ = 0; |
| 2267 | if ((__x_[0] & ~__mask) == 0) |
| 2268 | { |
| 2269 | for (size_t __i = 1; __i < __n; ++__i) |
| 2270 | if (__x_[__i] != 0) |
| 2271 | return; |
| 2272 | __x_[0] = _Max; |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2277 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2278 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2279 | _UIntType |
| 2280 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2281 | __t, __c, __l, __f>::operator()() |
| 2282 | { |
| 2283 | const size_t __j = (__i_ + 1) % __n; |
| 2284 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2285 | (result_type(1) << __r) - result_type(1); |
| 2286 | const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); |
| 2287 | const size_t __k = (__i_ + __m) % __n; |
| 2288 | __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1)); |
| 2289 | result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); |
| 2290 | __i_ = __j; |
| 2291 | __z ^= __lshift<__s>(__z) & __b; |
| 2292 | __z ^= __lshift<__t>(__z) & __c; |
| 2293 | return __z ^ __rshift<__l>(__z); |
| 2294 | } |
| 2295 | |
| 2296 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2297 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2298 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2299 | bool |
| 2300 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2301 | _B, _T, _C, _L, _F>& __x, |
| 2302 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2303 | _B, _T, _C, _L, _F>& __y) |
| 2304 | { |
| 2305 | if (__x.__i_ == __y.__i_) |
| 2306 | return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_); |
| 2307 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2308 | { |
| 2309 | size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_); |
| 2310 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
| 2311 | __y.__x_ + __y.__i_)) |
| 2312 | return false; |
| 2313 | if (__x.__i_ == 0) |
| 2314 | return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_); |
| 2315 | return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j); |
| 2316 | } |
| 2317 | if (__x.__i_ < __y.__i_) |
| 2318 | { |
| 2319 | size_t __j = _N - __y.__i_; |
| 2320 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
| 2321 | __y.__x_ + __y.__i_)) |
| 2322 | return false; |
| 2323 | if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N, |
| 2324 | __y.__x_)) |
| 2325 | return false; |
| 2326 | return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, |
| 2327 | __y.__x_ + (_N - (__x.__i_ + __j))); |
| 2328 | } |
| 2329 | size_t __j = _N - __x.__i_; |
| 2330 | if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
| 2331 | __x.__x_ + __x.__i_)) |
| 2332 | return false; |
| 2333 | if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N, |
| 2334 | __x.__x_)) |
| 2335 | return false; |
| 2336 | return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, |
| 2337 | __x.__x_ + (_N - (__y.__i_ + __j))); |
| 2338 | } |
| 2339 | |
| 2340 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2341 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2342 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2343 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2344 | bool |
| 2345 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2346 | _B, _T, _C, _L, _F>& __x, |
| 2347 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2348 | _B, _T, _C, _L, _F>& __y) |
| 2349 | { |
| 2350 | return !(__x == __y); |
| 2351 | } |
| 2352 | |
| 2353 | template <class _CharT, class _Traits, |
| 2354 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2355 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2356 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2357 | basic_ostream<_CharT, _Traits>& |
| 2358 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2359 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2360 | _B, _T, _C, _L, _F>& __x) |
| 2361 | { |
| 2362 | __save_flags<_CharT, _Traits> _(__os); |
| 2363 | __os.flags(ios_base::dec | ios_base::left); |
| 2364 | _CharT __sp = __os.widen(' '); |
| 2365 | __os.fill(__sp); |
| 2366 | __os << __x.__x_[__x.__i_]; |
| 2367 | for (size_t __j = __x.__i_ + 1; __j < _N; ++__j) |
| 2368 | __os << __sp << __x.__x_[__j]; |
| 2369 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2370 | __os << __sp << __x.__x_[__j]; |
| 2371 | return __os; |
| 2372 | } |
| 2373 | |
| 2374 | template <class _CharT, class _Traits, |
| 2375 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 2376 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 2377 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 2378 | basic_istream<_CharT, _Traits>& |
| 2379 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2380 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 2381 | _B, _T, _C, _L, _F>& __x) |
| 2382 | { |
| 2383 | __save_flags<_CharT, _Traits> _(__is); |
| 2384 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2385 | _UI __t[_N]; |
| 2386 | for (size_t __i = 0; __i < _N; ++__i) |
| 2387 | __is >> __t[__i]; |
| 2388 | if (!__is.fail()) |
| 2389 | { |
| 2390 | for (size_t __i = 0; __i < _N; ++__i) |
| 2391 | __x.__x_[__i] = __t[__i]; |
| 2392 | __x.__i_ = 0; |
| 2393 | } |
| 2394 | return __is; |
| 2395 | } |
| 2396 | |
| 2397 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 2398 | 0x9908b0df, 11, 0xffffffff, |
| 2399 | 7, 0x9d2c5680, |
| 2400 | 15, 0xefc60000, |
| 2401 | 18, 1812433253> mt19937; |
| 2402 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 2403 | 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, |
| 2404 | 17, 0x71d67fffeda60000ULL, |
| 2405 | 37, 0xfff7eee000000000ULL, |
| 2406 | 43, 6364136223846793005ULL> mt19937_64; |
| 2407 | |
| 2408 | // subtract_with_carry_engine |
| 2409 | |
| 2410 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2411 | class subtract_with_carry_engine; |
| 2412 | |
| 2413 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2414 | bool |
| 2415 | operator==( |
| 2416 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2417 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 2418 | |
| 2419 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2420 | bool |
| 2421 | operator!=( |
| 2422 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2423 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 2424 | |
| 2425 | template <class _CharT, class _Traits, |
| 2426 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2427 | basic_ostream<_CharT, _Traits>& |
| 2428 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2429 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2430 | |
| 2431 | template <class _CharT, class _Traits, |
| 2432 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2433 | basic_istream<_CharT, _Traits>& |
| 2434 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2435 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2436 | |
| 2437 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2438 | class _LIBCPP_VISIBLE subtract_with_carry_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2439 | { |
| 2440 | public: |
| 2441 | // types |
| 2442 | typedef _UIntType result_type; |
| 2443 | |
| 2444 | private: |
| 2445 | result_type __x_[__r]; |
| 2446 | result_type __c_; |
| 2447 | size_t __i_; |
| 2448 | |
| 2449 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 2450 | static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters"); |
| 2451 | static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); |
| 2452 | static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters"); |
| 2453 | static_assert(__s < __r, "subtract_with_carry_engine invalid parameters"); |
| 2454 | public: |
| 2455 | static const result_type _Min = 0; |
| 2456 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2457 | (result_type(1) << __w) - result_type(1); |
| 2458 | static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); |
| 2459 | |
| 2460 | // engine characteristics |
| 2461 | static const/*expr*/ size_t word_size = __w; |
| 2462 | static const/*expr*/ size_t short_lag = __s; |
| 2463 | static const/*expr*/ size_t long_lag = __r; |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2464 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2465 | static const/*expr*/ result_type min() { return _Min; } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2466 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2467 | static const/*expr*/ result_type max() { return _Max; } |
| 2468 | static const/*expr*/ result_type default_seed = 19780503u; |
| 2469 | |
| 2470 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2471 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2472 | explicit subtract_with_carry_engine(result_type __sd = default_seed) |
| 2473 | {seed(__sd);} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2474 | template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q, |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2475 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2476 | typename enable_if<!is_convertible<_Sseq, result_type>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2477 | {seed(__q);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2478 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2479 | void seed(result_type __sd = default_seed) |
| 2480 | {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2481 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2482 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2483 | typename enable_if |
| 2484 | < |
| 2485 | !is_convertible<_Sseq, result_type>::value, |
| 2486 | void |
| 2487 | >::type |
| 2488 | seed(_Sseq& __q) |
| 2489 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2490 | |
| 2491 | // generating functions |
| 2492 | result_type operator()(); |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2493 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2494 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2495 | |
| 2496 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2497 | friend |
| 2498 | bool |
| 2499 | operator==( |
| 2500 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2501 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2502 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2503 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2504 | friend |
| 2505 | bool |
| 2506 | operator!=( |
| 2507 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2508 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2509 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2510 | template <class _CharT, class _Traits, |
| 2511 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2512 | friend |
| 2513 | basic_ostream<_CharT, _Traits>& |
| 2514 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2515 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2516 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2517 | template <class _CharT, class _Traits, |
| 2518 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2519 | friend |
| 2520 | basic_istream<_CharT, _Traits>& |
| 2521 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2522 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2523 | |
| 2524 | private: |
| 2525 | |
| 2526 | void seed(result_type __sd, integral_constant<unsigned, 1>); |
| 2527 | void seed(result_type __sd, integral_constant<unsigned, 2>); |
| 2528 | template<class _Sseq> |
| 2529 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2530 | template<class _Sseq> |
| 2531 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2532 | }; |
| 2533 | |
| 2534 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2535 | void |
| 2536 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2537 | integral_constant<unsigned, 1>) |
| 2538 | { |
| 2539 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2540 | __e(__sd == 0u ? default_seed : __sd); |
| 2541 | for (size_t __i = 0; __i < __r; ++__i) |
| 2542 | __x_[__i] = static_cast<result_type>(__e() & _Max); |
| 2543 | __c_ = __x_[__r-1] == 0; |
| 2544 | __i_ = 0; |
| 2545 | } |
| 2546 | |
| 2547 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2548 | void |
| 2549 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2550 | integral_constant<unsigned, 2>) |
| 2551 | { |
| 2552 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2553 | __e(__sd == 0u ? default_seed : __sd); |
| 2554 | for (size_t __i = 0; __i < __r; ++__i) |
| 2555 | __x_[__i] = static_cast<result_type>( |
| 2556 | (__e() + ((uint64_t)__e() << 32)) & _Max); |
| 2557 | __c_ = __x_[__r-1] == 0; |
| 2558 | __i_ = 0; |
| 2559 | } |
| 2560 | |
| 2561 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2562 | template<class _Sseq> |
| 2563 | void |
| 2564 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2565 | integral_constant<unsigned, 1>) |
| 2566 | { |
| 2567 | const unsigned __k = 1; |
| 2568 | uint32_t __ar[__r * __k]; |
| 2569 | __q.generate(__ar, __ar + __r * __k); |
| 2570 | for (size_t __i = 0; __i < __r; ++__i) |
| 2571 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2572 | __c_ = __x_[__r-1] == 0; |
| 2573 | __i_ = 0; |
| 2574 | } |
| 2575 | |
| 2576 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2577 | template<class _Sseq> |
| 2578 | void |
| 2579 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2580 | integral_constant<unsigned, 2>) |
| 2581 | { |
| 2582 | const unsigned __k = 2; |
| 2583 | uint32_t __ar[__r * __k]; |
| 2584 | __q.generate(__ar, __ar + __r * __k); |
| 2585 | for (size_t __i = 0; __i < __r; ++__i) |
| 2586 | __x_[__i] = static_cast<result_type>( |
| 2587 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2588 | __c_ = __x_[__r-1] == 0; |
| 2589 | __i_ = 0; |
| 2590 | } |
| 2591 | |
| 2592 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2593 | _UIntType |
| 2594 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() |
| 2595 | { |
| 2596 | const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; |
| 2597 | result_type& __xr = __x_[__i_]; |
| 2598 | result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; |
| 2599 | __xr = (__xs - __xr - __c_) & _Max; |
| 2600 | __c_ = __new_c; |
| 2601 | __i_ = (__i_ + 1) % __r; |
| 2602 | return __xr; |
| 2603 | } |
| 2604 | |
| 2605 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2606 | bool |
| 2607 | operator==( |
| 2608 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2609 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) |
| 2610 | { |
| 2611 | if (__x.__c_ != __y.__c_) |
| 2612 | return false; |
| 2613 | if (__x.__i_ == __y.__i_) |
| 2614 | return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_); |
| 2615 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2616 | { |
| 2617 | size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_); |
| 2618 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
| 2619 | __y.__x_ + __y.__i_)) |
| 2620 | return false; |
| 2621 | if (__x.__i_ == 0) |
| 2622 | return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_); |
| 2623 | return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j); |
| 2624 | } |
| 2625 | if (__x.__i_ < __y.__i_) |
| 2626 | { |
| 2627 | size_t __j = _R - __y.__i_; |
| 2628 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
| 2629 | __y.__x_ + __y.__i_)) |
| 2630 | return false; |
| 2631 | if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R, |
| 2632 | __y.__x_)) |
| 2633 | return false; |
| 2634 | return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, |
| 2635 | __y.__x_ + (_R - (__x.__i_ + __j))); |
| 2636 | } |
| 2637 | size_t __j = _R - __x.__i_; |
| 2638 | if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
| 2639 | __x.__x_ + __x.__i_)) |
| 2640 | return false; |
| 2641 | if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R, |
| 2642 | __x.__x_)) |
| 2643 | return false; |
| 2644 | return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, |
| 2645 | __x.__x_ + (_R - (__y.__i_ + __j))); |
| 2646 | } |
| 2647 | |
| 2648 | template<class _UI, size_t _W, size_t _S, size_t _R> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2649 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2650 | bool |
| 2651 | operator!=( |
| 2652 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2653 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) |
| 2654 | { |
| 2655 | return !(__x == __y); |
| 2656 | } |
| 2657 | |
| 2658 | template <class _CharT, class _Traits, |
| 2659 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2660 | basic_ostream<_CharT, _Traits>& |
| 2661 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2662 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x) |
| 2663 | { |
| 2664 | __save_flags<_CharT, _Traits> _(__os); |
| 2665 | __os.flags(ios_base::dec | ios_base::left); |
| 2666 | _CharT __sp = __os.widen(' '); |
| 2667 | __os.fill(__sp); |
| 2668 | __os << __x.__x_[__x.__i_]; |
| 2669 | for (size_t __j = __x.__i_ + 1; __j < _R; ++__j) |
| 2670 | __os << __sp << __x.__x_[__j]; |
| 2671 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2672 | __os << __sp << __x.__x_[__j]; |
| 2673 | __os << __sp << __x.__c_; |
| 2674 | return __os; |
| 2675 | } |
| 2676 | |
| 2677 | template <class _CharT, class _Traits, |
| 2678 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2679 | basic_istream<_CharT, _Traits>& |
| 2680 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2681 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x) |
| 2682 | { |
| 2683 | __save_flags<_CharT, _Traits> _(__is); |
| 2684 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2685 | _UI __t[_R+1]; |
| 2686 | for (size_t __i = 0; __i < _R+1; ++__i) |
| 2687 | __is >> __t[__i]; |
| 2688 | if (!__is.fail()) |
| 2689 | { |
| 2690 | for (size_t __i = 0; __i < _R; ++__i) |
| 2691 | __x.__x_[__i] = __t[__i]; |
| 2692 | __x.__c_ = __t[_R]; |
| 2693 | __x.__i_ = 0; |
| 2694 | } |
| 2695 | return __is; |
| 2696 | } |
| 2697 | |
| 2698 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 2699 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 2700 | |
| 2701 | // discard_block_engine |
| 2702 | |
| 2703 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2704 | class _LIBCPP_VISIBLE discard_block_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2705 | { |
| 2706 | _Engine __e_; |
| 2707 | int __n_; |
| 2708 | |
| 2709 | static_assert( 0 < __r, "discard_block_engine invalid parameters"); |
| 2710 | static_assert(__r <= __p, "discard_block_engine invalid parameters"); |
| 2711 | public: |
| 2712 | // types |
| 2713 | typedef typename _Engine::result_type result_type; |
| 2714 | |
| 2715 | // engine characteristics |
| 2716 | static const/*expr*/ size_t block_size = __p; |
| 2717 | static const/*expr*/ size_t used_block = __r; |
| 2718 | |
| 2719 | // Temporary work around for lack of constexpr |
| 2720 | static const result_type _Min = _Engine::_Min; |
| 2721 | static const result_type _Max = _Engine::_Max; |
| 2722 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2723 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2724 | static const/*expr*/ result_type min() { return _Engine::min(); } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2725 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2726 | static const/*expr*/ result_type max() { return _Engine::max(); } |
| 2727 | |
| 2728 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2729 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2730 | discard_block_engine() : __n_(0) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2731 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2732 | explicit discard_block_engine(const _Engine& __e) |
| 2733 | : __e_(__e), __n_(0) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2734 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2735 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2736 | explicit discard_block_engine(_Engine&& __e) |
| 2737 | : __e_(_STD::move(__e)), __n_(0) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2738 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2739 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2740 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2741 | template<class _Sseq> |
| 2742 | _LIBCPP_INLINE_VISIBILITY |
| 2743 | explicit discard_block_engine(_Sseq& __q, |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2744 | typename enable_if<!is_convertible<_Sseq, result_type>::value && |
| 2745 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2746 | : __e_(__q), __n_(0) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2747 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2748 | void seed() {__e_.seed(); __n_ = 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2749 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2750 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2751 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2752 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2753 | typename enable_if |
| 2754 | < |
| 2755 | !is_convertible<_Sseq, result_type>::value, |
| 2756 | void |
| 2757 | >::type |
| 2758 | seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2759 | |
| 2760 | // generating functions |
| 2761 | result_type operator()(); |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2762 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2763 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2764 | |
| 2765 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2766 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2767 | const _Engine& base() const {return __e_;} |
| 2768 | |
| 2769 | template<class _Eng, size_t _P, size_t _R> |
| 2770 | friend |
| 2771 | bool |
| 2772 | operator==( |
| 2773 | const discard_block_engine<_Eng, _P, _R>& __x, |
| 2774 | const discard_block_engine<_Eng, _P, _R>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2775 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2776 | template<class _Eng, size_t _P, size_t _R> |
| 2777 | friend |
| 2778 | bool |
| 2779 | operator!=( |
| 2780 | const discard_block_engine<_Eng, _P, _R>& __x, |
| 2781 | const discard_block_engine<_Eng, _P, _R>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2782 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2783 | template <class _CharT, class _Traits, |
| 2784 | class _Eng, size_t _P, size_t _R> |
| 2785 | friend |
| 2786 | basic_ostream<_CharT, _Traits>& |
| 2787 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2788 | const discard_block_engine<_Eng, _P, _R>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2789 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2790 | template <class _CharT, class _Traits, |
| 2791 | class _Eng, size_t _P, size_t _R> |
| 2792 | friend |
| 2793 | basic_istream<_CharT, _Traits>& |
| 2794 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2795 | discard_block_engine<_Eng, _P, _R>& __x); |
| 2796 | }; |
| 2797 | |
| 2798 | template<class _Engine, size_t __p, size_t __r> |
| 2799 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 2800 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 2801 | { |
| 2802 | if (__n_ >= __r) |
| 2803 | { |
| 2804 | __e_.discard(__p - __r); |
| 2805 | __n_ = 0; |
| 2806 | } |
| 2807 | ++__n_; |
| 2808 | return __e_(); |
| 2809 | } |
| 2810 | |
| 2811 | template<class _Eng, size_t _P, size_t _R> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2812 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2813 | bool |
| 2814 | operator==(const discard_block_engine<_Eng, _P, _R>& __x, |
| 2815 | const discard_block_engine<_Eng, _P, _R>& __y) |
| 2816 | { |
| 2817 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 2818 | } |
| 2819 | |
| 2820 | template<class _Eng, size_t _P, size_t _R> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2821 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2822 | bool |
| 2823 | operator!=(const discard_block_engine<_Eng, _P, _R>& __x, |
| 2824 | const discard_block_engine<_Eng, _P, _R>& __y) |
| 2825 | { |
| 2826 | return !(__x == __y); |
| 2827 | } |
| 2828 | |
| 2829 | template <class _CharT, class _Traits, |
| 2830 | class _Eng, size_t _P, size_t _R> |
| 2831 | basic_ostream<_CharT, _Traits>& |
| 2832 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2833 | const discard_block_engine<_Eng, _P, _R>& __x) |
| 2834 | { |
| 2835 | __save_flags<_CharT, _Traits> _(__os); |
| 2836 | __os.flags(ios_base::dec | ios_base::left); |
| 2837 | _CharT __sp = __os.widen(' '); |
| 2838 | __os.fill(__sp); |
| 2839 | return __os << __x.__e_ << __sp << __x.__n_; |
| 2840 | } |
| 2841 | |
| 2842 | template <class _CharT, class _Traits, |
| 2843 | class _Eng, size_t _P, size_t _R> |
| 2844 | basic_istream<_CharT, _Traits>& |
| 2845 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2846 | discard_block_engine<_Eng, _P, _R>& __x) |
| 2847 | { |
| 2848 | __save_flags<_CharT, _Traits> _(__is); |
| 2849 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2850 | _Eng __e; |
| 2851 | int __n; |
| 2852 | __is >> __e >> __n; |
| 2853 | if (!__is.fail()) |
| 2854 | { |
| 2855 | __x.__e_ = __e; |
| 2856 | __x.__n_ = __n; |
| 2857 | } |
| 2858 | return __is; |
| 2859 | } |
| 2860 | |
| 2861 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 2862 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 2863 | |
| 2864 | // independent_bits_engine |
| 2865 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2866 | template<class _Engine, size_t __w, class _UIntType> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2867 | class _LIBCPP_VISIBLE independent_bits_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2868 | { |
| 2869 | template <class _UI, _UI _R0, size_t _W, size_t _M> |
| 2870 | class __get_n |
| 2871 | { |
| 2872 | static const size_t _Dt = numeric_limits<_UI>::digits; |
| 2873 | static const size_t _N = _W / _M + (_W % _M != 0); |
| 2874 | static const size_t _W0 = _W / _N; |
| 2875 | static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
| 2876 | public: |
| 2877 | static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N; |
| 2878 | }; |
| 2879 | public: |
| 2880 | // types |
| 2881 | typedef _UIntType result_type; |
| 2882 | |
| 2883 | private: |
| 2884 | _Engine __e_; |
| 2885 | |
| 2886 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 2887 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 2888 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 2889 | |
| 2890 | typedef typename _Engine::result_type _Engine_result_type; |
| 2891 | typedef typename conditional |
| 2892 | < |
| 2893 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 2894 | result_type, |
| 2895 | _Engine_result_type |
| 2896 | >::type _Working_result_type; |
| 2897 | // Temporary work around for lack of constexpr |
| 2898 | static const _Working_result_type _R = _Engine::_Max - _Engine::_Min |
| 2899 | + _Working_result_type(1); |
| 2900 | static const size_t __m = __log2<_Working_result_type, _R>::value; |
| 2901 | static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value; |
| 2902 | static const size_t __w0 = __w / __n; |
| 2903 | static const size_t __n0 = __n - __w % __n; |
| 2904 | static const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 2905 | static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 2906 | static const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 2907 | (_R >> __w0) << __w0; |
| 2908 | static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 2909 | (_R >> (__w0+1)) << (__w0+1); |
| 2910 | static const _Engine_result_type __mask0 = __w0 > 0 ? |
| 2911 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 2912 | _Engine_result_type(0); |
| 2913 | static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
| 2914 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 2915 | _Engine_result_type(~0); |
| 2916 | public: |
| 2917 | static const result_type _Min = 0; |
| 2918 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2919 | (result_type(1) << __w) - result_type(1); |
| 2920 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 2921 | |
| 2922 | // engine characteristics |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2923 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2924 | static const/*expr*/ result_type min() { return _Min; } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2925 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2926 | static const/*expr*/ result_type max() { return _Max; } |
| 2927 | |
| 2928 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2929 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2930 | independent_bits_engine() {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2931 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2932 | explicit independent_bits_engine(const _Engine& __e) |
| 2933 | : __e_(__e) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2934 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2935 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2936 | explicit independent_bits_engine(_Engine&& __e) |
| 2937 | : __e_(_STD::move(__e)) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2938 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2939 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2940 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2941 | template<class _Sseq> explicit independent_bits_engine(_Sseq& __q, |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2942 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2943 | typename enable_if<!is_convertible<_Sseq, result_type>::value && |
| 2944 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2945 | : __e_(__q) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2946 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2947 | void seed() {__e_.seed();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2948 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2949 | void seed(result_type __sd) {__e_.seed(__sd);} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2950 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2951 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2952 | typename enable_if |
| 2953 | < |
| 2954 | !is_convertible<_Sseq, result_type>::value, |
| 2955 | void |
| 2956 | >::type |
| 2957 | seed(_Sseq& __q) {__e_.seed(__q);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2958 | |
| 2959 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2960 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2961 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2962 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2963 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2964 | |
| 2965 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2966 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2967 | const _Engine& base() const {return __e_;} |
| 2968 | |
| 2969 | template<class _Eng, size_t _W, class _UI> |
| 2970 | friend |
| 2971 | bool |
| 2972 | operator==( |
| 2973 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2974 | const independent_bits_engine<_Eng, _W, _UI>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2975 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2976 | template<class _Eng, size_t _W, class _UI> |
| 2977 | friend |
| 2978 | bool |
| 2979 | operator!=( |
| 2980 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2981 | const independent_bits_engine<_Eng, _W, _UI>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2982 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2983 | template <class _CharT, class _Traits, |
| 2984 | class _Eng, size_t _W, class _UI> |
| 2985 | friend |
| 2986 | basic_ostream<_CharT, _Traits>& |
| 2987 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2988 | const independent_bits_engine<_Eng, _W, _UI>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2989 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2990 | template <class _CharT, class _Traits, |
| 2991 | class _Eng, size_t _W, class _UI> |
| 2992 | friend |
| 2993 | basic_istream<_CharT, _Traits>& |
| 2994 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2995 | independent_bits_engine<_Eng, _W, _UI>& __x); |
| 2996 | |
| 2997 | private: |
| 2998 | result_type __eval(false_type); |
| 2999 | result_type __eval(true_type); |
| 3000 | |
| 3001 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3002 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3003 | static |
| 3004 | typename enable_if |
| 3005 | < |
| 3006 | __count < _Dt, |
| 3007 | result_type |
| 3008 | >::type |
| 3009 | __lshift(result_type __x) {return __x << __count;} |
| 3010 | |
| 3011 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3012 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3013 | static |
| 3014 | typename enable_if |
| 3015 | < |
| 3016 | (__count >= _Dt), |
| 3017 | result_type |
| 3018 | >::type |
| 3019 | __lshift(result_type __x) {return result_type(0);} |
| 3020 | }; |
| 3021 | |
| 3022 | template<class _Engine, size_t __w, class _UIntType> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3023 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3024 | _UIntType |
| 3025 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
| 3026 | { |
| 3027 | return static_cast<result_type>(__e_() & __mask0); |
| 3028 | } |
| 3029 | |
| 3030 | template<class _Engine, size_t __w, class _UIntType> |
| 3031 | _UIntType |
| 3032 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
| 3033 | { |
| 3034 | result_type _S = 0; |
| 3035 | for (size_t __k = 0; __k < __n0; ++__k) |
| 3036 | { |
| 3037 | _Engine_result_type __u; |
| 3038 | do |
| 3039 | { |
| 3040 | __u = __e_() - _Engine::min(); |
| 3041 | } while (__u >= __y0); |
| 3042 | _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0)); |
| 3043 | } |
| 3044 | for (size_t __k = __n0; __k < __n; ++__k) |
| 3045 | { |
| 3046 | _Engine_result_type __u; |
| 3047 | do |
| 3048 | { |
| 3049 | __u = __e_() - _Engine::min(); |
| 3050 | } while (__u >= __y1); |
| 3051 | _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1)); |
| 3052 | } |
| 3053 | return _S; |
| 3054 | } |
| 3055 | |
| 3056 | template<class _Eng, size_t _W, class _UI> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3057 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3058 | bool |
| 3059 | operator==( |
| 3060 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 3061 | const independent_bits_engine<_Eng, _W, _UI>& __y) |
| 3062 | { |
| 3063 | return __x.base() == __y.base(); |
| 3064 | } |
| 3065 | |
| 3066 | template<class _Eng, size_t _W, class _UI> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3067 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3068 | bool |
| 3069 | operator!=( |
| 3070 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 3071 | const independent_bits_engine<_Eng, _W, _UI>& __y) |
| 3072 | { |
| 3073 | return !(__x == __y); |
| 3074 | } |
| 3075 | |
| 3076 | template <class _CharT, class _Traits, |
| 3077 | class _Eng, size_t _W, class _UI> |
| 3078 | basic_ostream<_CharT, _Traits>& |
| 3079 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3080 | const independent_bits_engine<_Eng, _W, _UI>& __x) |
| 3081 | { |
| 3082 | return __os << __x.base(); |
| 3083 | } |
| 3084 | |
| 3085 | template <class _CharT, class _Traits, |
| 3086 | class _Eng, size_t _W, class _UI> |
| 3087 | basic_istream<_CharT, _Traits>& |
| 3088 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3089 | independent_bits_engine<_Eng, _W, _UI>& __x) |
| 3090 | { |
| 3091 | _Eng __e; |
| 3092 | __is >> __e; |
| 3093 | if (!__is.fail()) |
| 3094 | __x.__e_ = __e; |
| 3095 | return __is; |
| 3096 | } |
| 3097 | |
| 3098 | // shuffle_order_engine |
| 3099 | |
| 3100 | template <uint64_t _Xp, uint64_t _Yp> |
| 3101 | struct __ugcd |
| 3102 | { |
| 3103 | static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
| 3104 | }; |
| 3105 | |
| 3106 | template <uint64_t _Xp> |
| 3107 | struct __ugcd<_Xp, 0> |
| 3108 | { |
| 3109 | static const uint64_t value = _Xp; |
| 3110 | }; |
| 3111 | |
| 3112 | template <uint64_t _N, uint64_t _D> |
| 3113 | class __uratio |
| 3114 | { |
| 3115 | static_assert(_D != 0, "__uratio divide by 0"); |
| 3116 | static const uint64_t __gcd = __ugcd<_N, _D>::value; |
| 3117 | public: |
| 3118 | static const uint64_t num = _N / __gcd; |
| 3119 | static const uint64_t den = _D / __gcd; |
| 3120 | |
| 3121 | typedef __uratio<num, den> type; |
| 3122 | }; |
| 3123 | |
| 3124 | template<class _Engine, size_t __k> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3125 | class _LIBCPP_VISIBLE shuffle_order_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3126 | { |
| 3127 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 3128 | public: |
| 3129 | // types |
| 3130 | typedef typename _Engine::result_type result_type; |
| 3131 | |
| 3132 | private: |
| 3133 | _Engine __e_; |
| 3134 | result_type _V_[__k]; |
| 3135 | result_type _Y_; |
| 3136 | |
| 3137 | public: |
| 3138 | // engine characteristics |
| 3139 | static const/*expr*/ size_t table_size = __k; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3140 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3141 | static const result_type _Min = _Engine::_Min; |
| 3142 | static const result_type _Max = _Engine::_Max; |
| 3143 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3144 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3145 | static const/*expr*/ result_type min() { return _Min; } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3146 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3147 | static const/*expr*/ result_type max() { return _Max; } |
| 3148 | |
| 3149 | static const unsigned long long _R = _Max - _Min + 1ull; |
| 3150 | |
| 3151 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3152 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3153 | shuffle_order_engine() {__init();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3154 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3155 | explicit shuffle_order_engine(const _Engine& __e) |
| 3156 | : __e_(__e) {__init();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 3157 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3158 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3159 | explicit shuffle_order_engine(_Engine&& __e) |
| 3160 | : __e_(_STD::move(__e)) {__init();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 3161 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3162 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3163 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3164 | template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q, |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3165 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3166 | typename enable_if<!is_convertible<_Sseq, result_type>::value && |
| 3167 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3168 | : __e_(__q) {__init();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3169 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3170 | void seed() {__e_.seed(); __init();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3171 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3172 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3173 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3174 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3175 | typename enable_if |
| 3176 | < |
| 3177 | !is_convertible<_Sseq, result_type>::value, |
| 3178 | void |
| 3179 | >::type |
| 3180 | seed(_Sseq& __q) {__e_.seed(__q); __init();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3181 | |
| 3182 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3183 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3184 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3185 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3186 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3187 | |
| 3188 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3189 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3190 | const _Engine& base() const {return __e_;} |
| 3191 | |
| 3192 | private: |
| 3193 | template<class _Eng, size_t _K> |
| 3194 | friend |
| 3195 | bool |
| 3196 | operator==( |
| 3197 | const shuffle_order_engine<_Eng, _K>& __x, |
| 3198 | const shuffle_order_engine<_Eng, _K>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3199 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3200 | template<class _Eng, size_t _K> |
| 3201 | friend |
| 3202 | bool |
| 3203 | operator!=( |
| 3204 | const shuffle_order_engine<_Eng, _K>& __x, |
| 3205 | const shuffle_order_engine<_Eng, _K>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3206 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3207 | template <class _CharT, class _Traits, |
| 3208 | class _Eng, size_t _K> |
| 3209 | friend |
| 3210 | basic_ostream<_CharT, _Traits>& |
| 3211 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3212 | const shuffle_order_engine<_Eng, _K>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3213 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3214 | template <class _CharT, class _Traits, |
| 3215 | class _Eng, size_t _K> |
| 3216 | friend |
| 3217 | basic_istream<_CharT, _Traits>& |
| 3218 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3219 | shuffle_order_engine<_Eng, _K>& __x); |
| 3220 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3221 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3222 | void __init() |
| 3223 | { |
| 3224 | for (size_t __i = 0; __i < __k; ++__i) |
| 3225 | _V_[__i] = __e_(); |
| 3226 | _Y_ = __e_(); |
| 3227 | } |
| 3228 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3229 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3230 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3231 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3232 | result_type __eval(true_type) {return __eval(__uratio<__k, _R>());} |
| 3233 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3234 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3235 | result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3236 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3237 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 3238 | |
| 3239 | template <uint64_t _N, uint64_t _D> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3240 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3241 | typename enable_if |
| 3242 | < |
| 3243 | (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
| 3244 | result_type |
| 3245 | >::type |
| 3246 | __eval(__uratio<_N, _D>) |
| 3247 | {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();} |
| 3248 | |
| 3249 | template <uint64_t _N, uint64_t _D> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3250 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3251 | typename enable_if |
| 3252 | < |
| 3253 | __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
| 3254 | result_type |
| 3255 | >::type |
| 3256 | __eval(__uratio<_N, _D>) |
| 3257 | { |
| 3258 | const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min) |
| 3259 | / __uratio<_N, _D>::den); |
| 3260 | _Y_ = _V_[__j]; |
| 3261 | _V_[__j] = __e_(); |
| 3262 | return _Y_; |
| 3263 | } |
| 3264 | |
| 3265 | template <uint64_t __n, uint64_t __d> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3266 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3267 | result_type __evalf() |
| 3268 | { |
| 3269 | const double _F = __d == 0 ? |
| 3270 | __n / (2. * 0x8000000000000000ull) : |
| 3271 | __n / (double)__d; |
| 3272 | const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min)); |
| 3273 | _Y_ = _V_[__j]; |
| 3274 | _V_[__j] = __e_(); |
| 3275 | return _Y_; |
| 3276 | } |
| 3277 | }; |
| 3278 | |
| 3279 | template<class _Eng, size_t _K> |
| 3280 | bool |
| 3281 | operator==( |
| 3282 | const shuffle_order_engine<_Eng, _K>& __x, |
| 3283 | const shuffle_order_engine<_Eng, _K>& __y) |
| 3284 | { |
| 3285 | return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) && |
| 3286 | __x.__e_ == __y.__e_; |
| 3287 | } |
| 3288 | |
| 3289 | template<class _Eng, size_t _K> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3290 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3291 | bool |
| 3292 | operator!=( |
| 3293 | const shuffle_order_engine<_Eng, _K>& __x, |
| 3294 | const shuffle_order_engine<_Eng, _K>& __y) |
| 3295 | { |
| 3296 | return !(__x == __y); |
| 3297 | } |
| 3298 | |
| 3299 | template <class _CharT, class _Traits, |
| 3300 | class _Eng, size_t _K> |
| 3301 | basic_ostream<_CharT, _Traits>& |
| 3302 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3303 | const shuffle_order_engine<_Eng, _K>& __x) |
| 3304 | { |
| 3305 | __save_flags<_CharT, _Traits> _(__os); |
| 3306 | __os.flags(ios_base::dec | ios_base::left); |
| 3307 | _CharT __sp = __os.widen(' '); |
| 3308 | __os.fill(__sp); |
| 3309 | __os << __x.__e_ << __sp << __x._V_[0]; |
| 3310 | for (size_t __i = 1; __i < _K; ++__i) |
| 3311 | __os << __sp << __x._V_[__i]; |
| 3312 | return __os << __sp << __x._Y_; |
| 3313 | } |
| 3314 | |
| 3315 | template <class _CharT, class _Traits, |
| 3316 | class _Eng, size_t _K> |
| 3317 | basic_istream<_CharT, _Traits>& |
| 3318 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3319 | shuffle_order_engine<_Eng, _K>& __x) |
| 3320 | { |
| 3321 | typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type; |
| 3322 | __save_flags<_CharT, _Traits> _(__is); |
| 3323 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3324 | _Eng __e; |
| 3325 | result_type _V[_K+1]; |
| 3326 | __is >> __e; |
| 3327 | for (size_t __i = 0; __i < _K+1; ++__i) |
| 3328 | __is >> _V[__i]; |
| 3329 | if (!__is.fail()) |
| 3330 | { |
| 3331 | __x.__e_ = __e; |
| 3332 | for (size_t __i = 0; __i < _K; ++__i) |
| 3333 | __x._V_[__i] = _V[__i]; |
| 3334 | __x._Y_ = _V[_K]; |
| 3335 | } |
| 3336 | return __is; |
| 3337 | } |
| 3338 | |
| 3339 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 3340 | |
| 3341 | // random_device |
| 3342 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3343 | class _LIBCPP_VISIBLE random_device |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3344 | { |
| 3345 | int __f_; |
| 3346 | public: |
| 3347 | // types |
| 3348 | typedef unsigned result_type; |
| 3349 | |
| 3350 | // generator characteristics |
| 3351 | static const result_type _Min = 0; |
| 3352 | static const result_type _Max = 0xFFFFFFFFu; |
| 3353 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3354 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3355 | static const/*expr*/ result_type min() { return _Min;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3356 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3357 | static const/*expr*/ result_type max() { return _Max;} |
| 3358 | |
| 3359 | // constructors |
| 3360 | explicit random_device(const string& __token = "/dev/urandom"); |
| 3361 | ~random_device(); |
| 3362 | |
| 3363 | // generating functions |
| 3364 | result_type operator()(); |
| 3365 | |
| 3366 | // property functions |
| 3367 | double entropy() const; |
| 3368 | |
| 3369 | private: |
| 3370 | // no copy functions |
| 3371 | random_device(const random_device&); // = delete; |
| 3372 | random_device& operator=(const random_device&); // = delete; |
| 3373 | }; |
| 3374 | |
| 3375 | // seed_seq |
| 3376 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3377 | class _LIBCPP_VISIBLE seed_seq |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3378 | { |
| 3379 | public: |
| 3380 | // types |
| 3381 | typedef uint32_t result_type; |
| 3382 | |
| 3383 | private: |
| 3384 | vector<result_type> __v_; |
| 3385 | |
| 3386 | template<class _InputIterator> |
| 3387 | void init(_InputIterator __first, _InputIterator __last); |
| 3388 | public: |
| 3389 | // constructors |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3390 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3391 | seed_seq() {} |
| 3392 | template<class _Tp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3393 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3394 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3395 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3396 | template<class _InputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3397 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3398 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 3399 | {init(__first, __last);} |
| 3400 | |
| 3401 | // generating functions |
| 3402 | template<class _RandomAccessIterator> |
| 3403 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 3404 | |
| 3405 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3406 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3407 | size_t size() const {return __v_.size();} |
| 3408 | template<class _OutputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3409 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3410 | void param(_OutputIterator __dest) const |
| 3411 | {_STD::copy(__v_.begin(), __v_.end(), __dest);} |
| 3412 | |
| 3413 | private: |
| 3414 | // no copy functions |
| 3415 | seed_seq(const seed_seq&); // = delete; |
| 3416 | void operator=(const seed_seq&); // = delete; |
| 3417 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3418 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3419 | static result_type _T(result_type __x) {return __x ^ (__x >> 27);} |
| 3420 | }; |
| 3421 | |
| 3422 | template<class _InputIterator> |
| 3423 | void |
| 3424 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 3425 | { |
| 3426 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 3427 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 3428 | } |
| 3429 | |
| 3430 | template<class _RandomAccessIterator> |
| 3431 | void |
| 3432 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 3433 | { |
| 3434 | if (__first != __last) |
| 3435 | { |
| 3436 | _STD::fill(__first, __last, 0x8b8b8b8b); |
| 3437 | const size_t __n = static_cast<size_t>(__last - __first); |
| 3438 | const size_t __s = __v_.size(); |
| 3439 | const size_t __t = (__n >= 623) ? 11 |
| 3440 | : (__n >= 68) ? 7 |
| 3441 | : (__n >= 39) ? 5 |
| 3442 | : (__n >= 7) ? 3 |
| 3443 | : (__n - 1) / 2; |
| 3444 | const size_t __p = (__n - __t) / 2; |
| 3445 | const size_t __q = __p + __t; |
| 3446 | const size_t __m = _STD::max(__s + 1, __n); |
| 3447 | // __k = 0; |
| 3448 | { |
| 3449 | result_type __r = 1664525 * _T(__first[0] ^ __first[__p] |
| 3450 | ^ __first[__n - 1]); |
| 3451 | __first[__p] += __r; |
| 3452 | __r += __s; |
| 3453 | __first[__q] += __r; |
| 3454 | __first[0] = __r; |
| 3455 | } |
| 3456 | for (size_t __k = 1; __k <= __s; ++__k) |
| 3457 | { |
| 3458 | const size_t __kmodn = __k % __n; |
| 3459 | const size_t __kpmodn = (__k + __p) % __n; |
| 3460 | result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] |
| 3461 | ^ __first[(__k - 1) % __n]); |
| 3462 | __first[__kpmodn] += __r; |
| 3463 | __r += __kmodn + __v_[__k-1]; |
| 3464 | __first[(__k + __q) % __n] += __r; |
| 3465 | __first[__kmodn] = __r; |
| 3466 | } |
| 3467 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 3468 | { |
| 3469 | const size_t __kmodn = __k % __n; |
| 3470 | const size_t __kpmodn = (__k + __p) % __n; |
| 3471 | result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] |
| 3472 | ^ __first[(__k - 1) % __n]); |
| 3473 | __first[__kpmodn] += __r; |
| 3474 | __r += __kmodn; |
| 3475 | __first[(__k + __q) % __n] += __r; |
| 3476 | __first[__kmodn] = __r; |
| 3477 | } |
| 3478 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 3479 | { |
| 3480 | const size_t __kmodn = __k % __n; |
| 3481 | const size_t __kpmodn = (__k + __p) % __n; |
| 3482 | result_type __r = 1566083941 * _T(__first[__kmodn] + |
| 3483 | __first[__kpmodn] + |
| 3484 | __first[(__k - 1) % __n]); |
| 3485 | __first[__kpmodn] ^= __r; |
| 3486 | __r -= __kmodn; |
| 3487 | __first[(__k + __q) % __n] ^= __r; |
| 3488 | __first[__kmodn] = __r; |
| 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3493 | // generate_canonical |
| 3494 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3495 | template<class _RealType, size_t __bits, class _URNG> |
| 3496 | _RealType |
| 3497 | generate_canonical(_URNG& __g) |
| 3498 | { |
| 3499 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 3500 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
| 3501 | const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value; |
| 3502 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
| 3503 | const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1); |
| 3504 | _RealType __base = _R; |
| 3505 | _RealType _S = __g() - _URNG::_Min; |
| 3506 | for (size_t __i = 1; __i < __k; ++__i, __base *= _R) |
| 3507 | _S += (__g() - _URNG::_Min) * __base; |
| 3508 | return _S / __base; |
| 3509 | } |
| 3510 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3511 | // uniform_int_distribution |
| 3512 | |
Howard Hinnant | c326721 | 2010-05-26 17:49:34 +0000 | [diff] [blame] | 3513 | // in <algorithm> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3514 | |
| 3515 | template <class _CharT, class _Traits, class _IT> |
| 3516 | basic_ostream<_CharT, _Traits>& |
| 3517 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3518 | const uniform_int_distribution<_IT>& __x) |
| 3519 | { |
| 3520 | __save_flags<_CharT, _Traits> _(__os); |
| 3521 | __os.flags(ios_base::dec | ios_base::left); |
| 3522 | _CharT __sp = __os.widen(' '); |
| 3523 | __os.fill(__sp); |
| 3524 | return __os << __x.a() << __sp << __x.b(); |
| 3525 | } |
| 3526 | |
| 3527 | template <class _CharT, class _Traits, class _IT> |
| 3528 | basic_istream<_CharT, _Traits>& |
| 3529 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3530 | uniform_int_distribution<_IT>& __x) |
| 3531 | { |
| 3532 | typedef uniform_int_distribution<_IT> _Eng; |
| 3533 | typedef typename _Eng::result_type result_type; |
| 3534 | typedef typename _Eng::param_type param_type; |
| 3535 | __save_flags<_CharT, _Traits> _(__is); |
| 3536 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3537 | result_type __a; |
| 3538 | result_type __b; |
| 3539 | __is >> __a >> __b; |
| 3540 | if (!__is.fail()) |
| 3541 | __x.param(param_type(__a, __b)); |
| 3542 | return __is; |
| 3543 | } |
| 3544 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3545 | // uniform_real_distribution |
| 3546 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3547 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3548 | class _LIBCPP_VISIBLE uniform_real_distribution |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3549 | { |
| 3550 | public: |
| 3551 | // types |
| 3552 | typedef _RealType result_type; |
| 3553 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3554 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3555 | { |
| 3556 | result_type __a_; |
| 3557 | result_type __b_; |
| 3558 | public: |
| 3559 | typedef uniform_real_distribution distribution_type; |
| 3560 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3561 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3562 | explicit param_type(result_type __a = 0, |
| 3563 | result_type __b = 1) |
| 3564 | : __a_(__a), __b_(__b) {} |
| 3565 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3566 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3567 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3568 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3569 | result_type b() const {return __b_;} |
| 3570 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3571 | friend _LIBCPP_INLINE_VISIBILITY |
| 3572 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3573 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3574 | friend _LIBCPP_INLINE_VISIBILITY |
| 3575 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3576 | {return !(__x == __y);} |
| 3577 | }; |
| 3578 | |
| 3579 | private: |
| 3580 | param_type __p_; |
| 3581 | |
| 3582 | public: |
| 3583 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3584 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3585 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 3586 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3587 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3588 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3589 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3590 | void reset() {} |
| 3591 | |
| 3592 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3593 | template<class _URNG> |
| 3594 | _LIBCPP_INLINE_VISIBILITY |
| 3595 | result_type operator()(_URNG& __g) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3596 | {return (*this)(__g, __p_);} |
| 3597 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3598 | |
| 3599 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3600 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3601 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3602 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3603 | result_type b() const {return __p_.b();} |
| 3604 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3605 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3606 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3607 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3608 | void param(const param_type& __p) {__p_ = __p;} |
| 3609 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3610 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3611 | result_type min() const {return a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3612 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3613 | result_type max() const {return b();} |
| 3614 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3615 | friend _LIBCPP_INLINE_VISIBILITY |
| 3616 | bool operator==(const uniform_real_distribution& __x, |
| 3617 | const uniform_real_distribution& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3618 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3619 | friend _LIBCPP_INLINE_VISIBILITY |
| 3620 | bool operator!=(const uniform_real_distribution& __x, |
| 3621 | const uniform_real_distribution& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3622 | {return !(__x == __y);} |
| 3623 | }; |
| 3624 | |
| 3625 | template<class _RealType> |
| 3626 | template<class _URNG> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3627 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3628 | typename uniform_real_distribution<_RealType>::result_type |
| 3629 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3630 | { |
| 3631 | return (__p.b() - __p.a()) |
| 3632 | * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
| 3633 | + __p.a(); |
| 3634 | } |
| 3635 | |
| 3636 | template <class _CharT, class _Traits, class _RT> |
| 3637 | basic_ostream<_CharT, _Traits>& |
| 3638 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3639 | const uniform_real_distribution<_RT>& __x) |
| 3640 | { |
| 3641 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3642 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3643 | ios_base::scientific); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3644 | _CharT __sp = __os.widen(' '); |
| 3645 | __os.fill(__sp); |
| 3646 | return __os << __x.a() << __sp << __x.b(); |
| 3647 | } |
| 3648 | |
| 3649 | template <class _CharT, class _Traits, class _RT> |
| 3650 | basic_istream<_CharT, _Traits>& |
| 3651 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3652 | uniform_real_distribution<_RT>& __x) |
| 3653 | { |
| 3654 | typedef uniform_real_distribution<_RT> _Eng; |
| 3655 | typedef typename _Eng::result_type result_type; |
| 3656 | typedef typename _Eng::param_type param_type; |
| 3657 | __save_flags<_CharT, _Traits> _(__is); |
| 3658 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3659 | result_type __a; |
| 3660 | result_type __b; |
| 3661 | __is >> __a >> __b; |
| 3662 | if (!__is.fail()) |
| 3663 | __x.param(param_type(__a, __b)); |
| 3664 | return __is; |
| 3665 | } |
| 3666 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3667 | // bernoulli_distribution |
| 3668 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3669 | class _LIBCPP_VISIBLE bernoulli_distribution |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3670 | { |
| 3671 | public: |
| 3672 | // types |
| 3673 | typedef bool result_type; |
| 3674 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3675 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3676 | { |
| 3677 | double __p_; |
| 3678 | public: |
| 3679 | typedef bernoulli_distribution distribution_type; |
| 3680 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3681 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3682 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3683 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3684 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3685 | double p() const {return __p_;} |
| 3686 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3687 | friend _LIBCPP_INLINE_VISIBILITY |
| 3688 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3689 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3690 | friend _LIBCPP_INLINE_VISIBILITY |
| 3691 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3692 | {return !(__x == __y);} |
| 3693 | }; |
| 3694 | |
| 3695 | private: |
| 3696 | param_type __p_; |
| 3697 | |
| 3698 | public: |
| 3699 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3700 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3701 | explicit bernoulli_distribution(double __p = 0.5) |
| 3702 | : __p_(param_type(__p)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3703 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3704 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3705 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3706 | void reset() {} |
| 3707 | |
| 3708 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3709 | template<class _URNG> |
| 3710 | _LIBCPP_INLINE_VISIBILITY |
| 3711 | result_type operator()(_URNG& __g) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3712 | {return (*this)(__g, __p_);} |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3713 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3714 | |
| 3715 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3716 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3717 | double p() const {return __p_.p();} |
| 3718 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3719 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3720 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3721 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3722 | void param(const param_type& __p) {__p_ = __p;} |
| 3723 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3724 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3725 | result_type min() const {return false;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3726 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3727 | result_type max() const {return true;} |
| 3728 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3729 | friend _LIBCPP_INLINE_VISIBILITY |
| 3730 | bool operator==(const bernoulli_distribution& __x, |
| 3731 | const bernoulli_distribution& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3732 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3733 | friend _LIBCPP_INLINE_VISIBILITY |
| 3734 | bool operator!=(const bernoulli_distribution& __x, |
| 3735 | const bernoulli_distribution& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3736 | {return !(__x == __y);} |
| 3737 | }; |
| 3738 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3739 | template<class _URNG> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3740 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3741 | bernoulli_distribution::result_type |
| 3742 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 3743 | { |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 3744 | uniform_real_distribution<double> __gen; |
| 3745 | return __gen(__g) < __p.p(); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3748 | template <class _CharT, class _Traits> |
| 3749 | basic_ostream<_CharT, _Traits>& |
| 3750 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 3751 | { |
| 3752 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3753 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3754 | ios_base::scientific); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3755 | _CharT __sp = __os.widen(' '); |
| 3756 | __os.fill(__sp); |
| 3757 | return __os << __x.p(); |
| 3758 | } |
| 3759 | |
| 3760 | template <class _CharT, class _Traits> |
| 3761 | basic_istream<_CharT, _Traits>& |
| 3762 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 3763 | { |
| 3764 | typedef bernoulli_distribution _Eng; |
| 3765 | typedef typename _Eng::param_type param_type; |
| 3766 | __save_flags<_CharT, _Traits> _(__is); |
| 3767 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3768 | double __p; |
| 3769 | __is >> __p; |
| 3770 | if (!__is.fail()) |
| 3771 | __x.param(param_type(__p)); |
| 3772 | return __is; |
| 3773 | } |
| 3774 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3775 | // binomial_distribution |
| 3776 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3777 | template<class _IntType = int> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3778 | class _LIBCPP_VISIBLE binomial_distribution |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3779 | { |
| 3780 | public: |
| 3781 | // types |
| 3782 | typedef _IntType result_type; |
| 3783 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3784 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3785 | { |
| 3786 | result_type __t_; |
| 3787 | double __p_; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3788 | double __pr_; |
| 3789 | double __odds_ratio_; |
| 3790 | result_type __r0_; |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3791 | public: |
| 3792 | typedef binomial_distribution distribution_type; |
| 3793 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3794 | explicit param_type(result_type __t = 1, double __p = 0.5); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3795 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3796 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3797 | result_type t() const {return __t_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3798 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3799 | double p() const {return __p_;} |
| 3800 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3801 | friend _LIBCPP_INLINE_VISIBILITY |
| 3802 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3803 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3804 | friend _LIBCPP_INLINE_VISIBILITY |
| 3805 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3806 | {return !(__x == __y);} |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3807 | |
| 3808 | friend class binomial_distribution; |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3809 | }; |
| 3810 | |
| 3811 | private: |
| 3812 | param_type __p_; |
| 3813 | |
| 3814 | public: |
| 3815 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3816 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3817 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 3818 | : __p_(param_type(__t, __p)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3819 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3820 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3821 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3822 | void reset() {} |
| 3823 | |
| 3824 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3825 | template<class _URNG> |
| 3826 | _LIBCPP_INLINE_VISIBILITY |
| 3827 | result_type operator()(_URNG& __g) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3828 | {return (*this)(__g, __p_);} |
| 3829 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3830 | |
| 3831 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3832 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3833 | result_type t() const {return __p_.t();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3834 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3835 | double p() const {return __p_.p();} |
| 3836 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3837 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3838 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3839 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3840 | void param(const param_type& __p) {__p_ = __p;} |
| 3841 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3842 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3843 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3844 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3845 | result_type max() const {return t();} |
| 3846 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3847 | friend _LIBCPP_INLINE_VISIBILITY |
| 3848 | bool operator==(const binomial_distribution& __x, |
| 3849 | const binomial_distribution& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3850 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3851 | friend _LIBCPP_INLINE_VISIBILITY |
| 3852 | bool operator!=(const binomial_distribution& __x, |
| 3853 | const binomial_distribution& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3854 | {return !(__x == __y);} |
| 3855 | }; |
| 3856 | |
| 3857 | template<class _IntType> |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3858 | binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) |
| 3859 | : __t_(__t), __p_(__p) |
| 3860 | { |
| 3861 | if (0 < __p_ && __p_ < 1) |
| 3862 | { |
| 3863 | __r0_ = static_cast<result_type>((__t_ + 1) * __p_); |
| 3864 | __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) - |
| 3865 | _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) + |
| 3866 | (__t_ - __r0_) * _STD::log(1 - __p_)); |
| 3867 | __odds_ratio_ = __p_ / (1 - __p_); |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | template<class _IntType> |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3872 | template<class _URNG> |
| 3873 | _IntType |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3874 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3875 | { |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3876 | if (__pr.__t_ == 0 || __pr.__p_ == 0) |
| 3877 | return 0; |
| 3878 | if (__pr.__p_ == 1) |
| 3879 | return __pr.__t_; |
| 3880 | uniform_real_distribution<double> __gen; |
| 3881 | double __u = __gen(__g) - __pr.__pr_; |
| 3882 | if (__u < 0) |
| 3883 | return __pr.__r0_; |
| 3884 | double __pu = __pr.__pr_; |
| 3885 | double __pd = __pu; |
| 3886 | result_type __ru = __pr.__r0_; |
| 3887 | result_type __rd = __ru; |
| 3888 | while (true) |
| 3889 | { |
| 3890 | if (__rd >= 1) |
| 3891 | { |
| 3892 | __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); |
| 3893 | __u -= __pd; |
| 3894 | if (__u < 0) |
| 3895 | return __rd - 1; |
| 3896 | } |
| 3897 | --__rd; |
| 3898 | ++__ru; |
| 3899 | if (__ru <= __pr.__t_) |
| 3900 | { |
| 3901 | __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; |
| 3902 | __u -= __pu; |
| 3903 | if (__u < 0) |
| 3904 | return __ru; |
| 3905 | } |
| 3906 | } |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | template <class _CharT, class _Traits, class _IntType> |
| 3910 | basic_ostream<_CharT, _Traits>& |
| 3911 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3912 | const binomial_distribution<_IntType>& __x) |
| 3913 | { |
| 3914 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3915 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3916 | ios_base::scientific); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3917 | _CharT __sp = __os.widen(' '); |
| 3918 | __os.fill(__sp); |
| 3919 | return __os << __x.t() << __sp << __x.p(); |
| 3920 | } |
| 3921 | |
| 3922 | template <class _CharT, class _Traits, class _IntType> |
| 3923 | basic_istream<_CharT, _Traits>& |
| 3924 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3925 | binomial_distribution<_IntType>& __x) |
| 3926 | { |
| 3927 | typedef binomial_distribution<_IntType> _Eng; |
| 3928 | typedef typename _Eng::result_type result_type; |
| 3929 | typedef typename _Eng::param_type param_type; |
| 3930 | __save_flags<_CharT, _Traits> _(__is); |
| 3931 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3932 | result_type __t; |
| 3933 | double __p; |
| 3934 | __is >> __t >> __p; |
| 3935 | if (!__is.fail()) |
| 3936 | __x.param(param_type(__t, __p)); |
| 3937 | return __is; |
| 3938 | } |
| 3939 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3940 | // exponential_distribution |
| 3941 | |
| 3942 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3943 | class _LIBCPP_VISIBLE exponential_distribution |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3944 | { |
| 3945 | public: |
| 3946 | // types |
| 3947 | typedef _RealType result_type; |
| 3948 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3949 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3950 | { |
| 3951 | result_type __lambda_; |
| 3952 | public: |
| 3953 | typedef exponential_distribution distribution_type; |
| 3954 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3955 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3956 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 3957 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3958 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3959 | result_type lambda() const {return __lambda_;} |
| 3960 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3961 | friend _LIBCPP_INLINE_VISIBILITY |
| 3962 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3963 | {return __x.__lambda_ == __y.__lambda_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3964 | friend _LIBCPP_INLINE_VISIBILITY |
| 3965 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3966 | {return !(__x == __y);} |
| 3967 | }; |
| 3968 | |
| 3969 | private: |
| 3970 | param_type __p_; |
| 3971 | |
| 3972 | public: |
| 3973 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3974 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3975 | explicit exponential_distribution(result_type __lambda = 1) |
| 3976 | : __p_(param_type(__lambda)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3977 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3978 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3979 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3980 | void reset() {} |
| 3981 | |
| 3982 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3983 | template<class _URNG> |
| 3984 | _LIBCPP_INLINE_VISIBILITY |
| 3985 | result_type operator()(_URNG& __g) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3986 | {return (*this)(__g, __p_);} |
| 3987 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3988 | |
| 3989 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3991 | result_type lambda() const {return __p_.lambda();} |
| 3992 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3993 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3994 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3995 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3996 | void param(const param_type& __p) {__p_ = __p;} |
| 3997 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3998 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3999 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4000 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df40dc6 | 2010-05-16 17:56:20 +0000 | [diff] [blame] | 4001 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4002 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4003 | friend _LIBCPP_INLINE_VISIBILITY |
| 4004 | bool operator==(const exponential_distribution& __x, |
| 4005 | const exponential_distribution& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4006 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4007 | friend _LIBCPP_INLINE_VISIBILITY |
| 4008 | bool operator!=(const exponential_distribution& __x, |
| 4009 | const exponential_distribution& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4010 | {return !(__x == __y);} |
| 4011 | }; |
| 4012 | |
| 4013 | template <class _RealType> |
| 4014 | template<class _URNG> |
| 4015 | _RealType |
| 4016 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4017 | { |
| 4018 | return -_STD::log |
| 4019 | ( |
| 4020 | result_type(1) - |
| 4021 | _STD::generate_canonical<result_type, |
| 4022 | numeric_limits<result_type>::digits>(__g) |
| 4023 | ) |
| 4024 | / __p.lambda(); |
| 4025 | } |
| 4026 | |
| 4027 | template <class _CharT, class _Traits, class _RealType> |
| 4028 | basic_ostream<_CharT, _Traits>& |
| 4029 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4030 | const exponential_distribution<_RealType>& __x) |
| 4031 | { |
| 4032 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4033 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4034 | ios_base::scientific); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4035 | return __os << __x.lambda(); |
| 4036 | } |
| 4037 | |
| 4038 | template <class _CharT, class _Traits, class _RealType> |
| 4039 | basic_istream<_CharT, _Traits>& |
| 4040 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4041 | exponential_distribution<_RealType>& __x) |
| 4042 | { |
| 4043 | typedef exponential_distribution<_RealType> _Eng; |
| 4044 | typedef typename _Eng::result_type result_type; |
| 4045 | typedef typename _Eng::param_type param_type; |
| 4046 | __save_flags<_CharT, _Traits> _(__is); |
| 4047 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4048 | result_type __lambda; |
| 4049 | __is >> __lambda; |
| 4050 | if (!__is.fail()) |
| 4051 | __x.param(param_type(__lambda)); |
| 4052 | return __is; |
| 4053 | } |
| 4054 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4055 | // normal_distribution |
| 4056 | |
| 4057 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4058 | class _LIBCPP_VISIBLE normal_distribution |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4059 | { |
| 4060 | public: |
| 4061 | // types |
| 4062 | typedef _RealType result_type; |
| 4063 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4064 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4065 | { |
| 4066 | result_type __mean_; |
| 4067 | result_type __stddev_; |
| 4068 | public: |
| 4069 | typedef normal_distribution distribution_type; |
| 4070 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4071 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4072 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 4073 | : __mean_(__mean), __stddev_(__stddev) {} |
| 4074 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4075 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4076 | result_type mean() const {return __mean_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4077 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4078 | result_type stddev() const {return __stddev_;} |
| 4079 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4080 | friend _LIBCPP_INLINE_VISIBILITY |
| 4081 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4082 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4083 | friend _LIBCPP_INLINE_VISIBILITY |
| 4084 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4085 | {return !(__x == __y);} |
| 4086 | }; |
| 4087 | |
| 4088 | private: |
| 4089 | param_type __p_; |
| 4090 | result_type _V_; |
| 4091 | bool _V_hot_; |
| 4092 | |
| 4093 | public: |
| 4094 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4095 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4096 | explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1) |
| 4097 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4098 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4099 | explicit normal_distribution(const param_type& __p) |
| 4100 | : __p_(__p), _V_hot_(false) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4101 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4102 | void reset() {_V_hot_ = false;} |
| 4103 | |
| 4104 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4105 | template<class _URNG> |
| 4106 | _LIBCPP_INLINE_VISIBILITY |
| 4107 | result_type operator()(_URNG& __g) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4108 | {return (*this)(__g, __p_);} |
| 4109 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4110 | |
| 4111 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4112 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4113 | result_type mean() const {return __p_.mean();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4114 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4115 | result_type stddev() const {return __p_.stddev();} |
| 4116 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4117 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4118 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4119 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4120 | void param(const param_type& __p) {__p_ = __p;} |
| 4121 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4122 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4123 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4124 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4125 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4126 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4127 | friend _LIBCPP_INLINE_VISIBILITY |
| 4128 | bool operator==(const normal_distribution& __x, |
| 4129 | const normal_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4130 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 4131 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4132 | friend _LIBCPP_INLINE_VISIBILITY |
| 4133 | bool operator!=(const normal_distribution& __x, |
| 4134 | const normal_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4135 | {return !(__x == __y);} |
| 4136 | |
| 4137 | template <class _CharT, class _Traits, class _RT> |
| 4138 | friend |
| 4139 | basic_ostream<_CharT, _Traits>& |
| 4140 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4141 | const normal_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4142 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4143 | template <class _CharT, class _Traits, class _RT> |
| 4144 | friend |
| 4145 | basic_istream<_CharT, _Traits>& |
| 4146 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4147 | normal_distribution<_RT>& __x); |
| 4148 | }; |
| 4149 | |
| 4150 | template <class _RealType> |
| 4151 | template<class _URNG> |
| 4152 | _RealType |
| 4153 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4154 | { |
| 4155 | result_type _U; |
| 4156 | if (_V_hot_) |
| 4157 | { |
| 4158 | _V_hot_ = false; |
| 4159 | _U = _V_; |
| 4160 | } |
| 4161 | else |
| 4162 | { |
| 4163 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 4164 | result_type __u; |
| 4165 | result_type __v; |
| 4166 | result_type __s; |
| 4167 | do |
| 4168 | { |
| 4169 | __u = _Uni(__g); |
| 4170 | __v = _Uni(__g); |
| 4171 | __s = __u * __u + __v * __v; |
| 4172 | } while (__s > 1 || __s == 0); |
| 4173 | result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s); |
| 4174 | _V_ = __v * _F; |
| 4175 | _V_hot_ = true; |
| 4176 | _U = __u * _F; |
| 4177 | } |
| 4178 | return _U * __p.stddev() + __p.mean(); |
| 4179 | } |
| 4180 | |
| 4181 | template <class _CharT, class _Traits, class _RT> |
| 4182 | basic_ostream<_CharT, _Traits>& |
| 4183 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4184 | const normal_distribution<_RT>& __x) |
| 4185 | { |
| 4186 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4187 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4188 | ios_base::scientific); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4189 | _CharT __sp = __os.widen(' '); |
| 4190 | __os.fill(__sp); |
| 4191 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 4192 | if (__x._V_hot_) |
| 4193 | __os << __sp << __x._V_; |
| 4194 | return __os; |
| 4195 | } |
| 4196 | |
| 4197 | template <class _CharT, class _Traits, class _RT> |
| 4198 | basic_istream<_CharT, _Traits>& |
| 4199 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4200 | normal_distribution<_RT>& __x) |
| 4201 | { |
| 4202 | typedef normal_distribution<_RT> _Eng; |
| 4203 | typedef typename _Eng::result_type result_type; |
| 4204 | typedef typename _Eng::param_type param_type; |
| 4205 | __save_flags<_CharT, _Traits> _(__is); |
| 4206 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4207 | result_type __mean; |
| 4208 | result_type __stddev; |
| 4209 | result_type _V = 0; |
| 4210 | bool _V_hot = false; |
| 4211 | __is >> __mean >> __stddev >> _V_hot; |
| 4212 | if (_V_hot) |
| 4213 | __is >> _V; |
| 4214 | if (!__is.fail()) |
| 4215 | { |
| 4216 | __x.param(param_type(__mean, __stddev)); |
| 4217 | __x._V_hot_ = _V_hot; |
| 4218 | __x._V_ = _V; |
| 4219 | } |
| 4220 | return __is; |
| 4221 | } |
| 4222 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4223 | // lognormal_distribution |
| 4224 | |
| 4225 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4226 | class _LIBCPP_VISIBLE lognormal_distribution |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4227 | { |
| 4228 | public: |
| 4229 | // types |
| 4230 | typedef _RealType result_type; |
| 4231 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4232 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4233 | { |
| 4234 | normal_distribution<result_type> __nd_; |
| 4235 | public: |
| 4236 | typedef lognormal_distribution distribution_type; |
| 4237 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4238 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4239 | explicit param_type(result_type __m = 0, result_type __s = 1) |
| 4240 | : __nd_(__m, __s) {} |
| 4241 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4242 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4243 | result_type m() const {return __nd_.mean();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4244 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4245 | result_type s() const {return __nd_.stddev();} |
| 4246 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4247 | friend _LIBCPP_INLINE_VISIBILITY |
| 4248 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4249 | {return __x.__nd_ == __y.__nd_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4250 | friend _LIBCPP_INLINE_VISIBILITY |
| 4251 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4252 | {return !(__x == __y);} |
| 4253 | friend class lognormal_distribution; |
| 4254 | |
| 4255 | template <class _CharT, class _Traits, class _RT> |
| 4256 | friend |
| 4257 | basic_ostream<_CharT, _Traits>& |
| 4258 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4259 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4260 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4261 | template <class _CharT, class _Traits, class _RT> |
| 4262 | friend |
| 4263 | basic_istream<_CharT, _Traits>& |
| 4264 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4265 | lognormal_distribution<_RT>& __x); |
| 4266 | }; |
| 4267 | |
| 4268 | private: |
| 4269 | param_type __p_; |
| 4270 | |
| 4271 | public: |
| 4272 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4273 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4274 | explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) |
| 4275 | : __p_(param_type(__m, __s)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4276 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4277 | explicit lognormal_distribution(const param_type& __p) |
| 4278 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4279 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4280 | void reset() {__p_.__nd_.reset();} |
| 4281 | |
| 4282 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4283 | template<class _URNG> |
| 4284 | _LIBCPP_INLINE_VISIBILITY |
| 4285 | result_type operator()(_URNG& __g) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4286 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4287 | template<class _URNG> |
| 4288 | _LIBCPP_INLINE_VISIBILITY |
| 4289 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4290 | {return _STD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));} |
| 4291 | |
| 4292 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4293 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4294 | result_type m() const {return __p_.m();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4295 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4296 | result_type s() const {return __p_.s();} |
| 4297 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4298 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4299 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4300 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 4301 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4302 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4304 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4305 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4306 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4307 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4308 | friend _LIBCPP_INLINE_VISIBILITY |
| 4309 | bool operator==(const lognormal_distribution& __x, |
| 4310 | const lognormal_distribution& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4311 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4312 | friend _LIBCPP_INLINE_VISIBILITY |
| 4313 | bool operator!=(const lognormal_distribution& __x, |
| 4314 | const lognormal_distribution& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4315 | {return !(__x == __y);} |
| 4316 | |
| 4317 | template <class _CharT, class _Traits, class _RT> |
| 4318 | friend |
| 4319 | basic_ostream<_CharT, _Traits>& |
| 4320 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4321 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4322 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4323 | template <class _CharT, class _Traits, class _RT> |
| 4324 | friend |
| 4325 | basic_istream<_CharT, _Traits>& |
| 4326 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4327 | lognormal_distribution<_RT>& __x); |
| 4328 | }; |
| 4329 | |
| 4330 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4331 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4332 | basic_ostream<_CharT, _Traits>& |
| 4333 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4334 | const lognormal_distribution<_RT>& __x) |
| 4335 | { |
| 4336 | return __os << __x.__p_.__nd_; |
| 4337 | } |
| 4338 | |
| 4339 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4340 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4341 | basic_istream<_CharT, _Traits>& |
| 4342 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4343 | lognormal_distribution<_RT>& __x) |
| 4344 | { |
| 4345 | return __is >> __x.__p_.__nd_; |
| 4346 | } |
| 4347 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4348 | // poisson_distribution |
| 4349 | |
| 4350 | template<class _IntType = int> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4351 | class _LIBCPP_VISIBLE poisson_distribution |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4352 | { |
| 4353 | public: |
| 4354 | // types |
| 4355 | typedef _IntType result_type; |
| 4356 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4357 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4358 | { |
| 4359 | double __mean_; |
| 4360 | double __s_; |
| 4361 | double __d_; |
| 4362 | double __l_; |
| 4363 | double __omega_; |
| 4364 | double __c0_; |
| 4365 | double __c1_; |
| 4366 | double __c2_; |
| 4367 | double __c3_; |
| 4368 | double __c_; |
| 4369 | |
| 4370 | public: |
| 4371 | typedef poisson_distribution distribution_type; |
| 4372 | |
| 4373 | explicit param_type(double __mean = 1.0); |
| 4374 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4375 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4376 | double mean() const {return __mean_;} |
| 4377 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4378 | friend _LIBCPP_INLINE_VISIBILITY |
| 4379 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4380 | {return __x.__mean_ == __y.__mean_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4381 | friend _LIBCPP_INLINE_VISIBILITY |
| 4382 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4383 | {return !(__x == __y);} |
| 4384 | |
| 4385 | friend class poisson_distribution; |
| 4386 | }; |
| 4387 | |
| 4388 | private: |
| 4389 | param_type __p_; |
| 4390 | |
| 4391 | public: |
| 4392 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4393 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4394 | explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4395 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4396 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4397 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4398 | void reset() {} |
| 4399 | |
| 4400 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4401 | template<class _URNG> |
| 4402 | _LIBCPP_INLINE_VISIBILITY |
| 4403 | result_type operator()(_URNG& __g) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4404 | {return (*this)(__g, __p_);} |
| 4405 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4406 | |
| 4407 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4408 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4409 | double mean() const {return __p_.mean();} |
| 4410 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4411 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4412 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4413 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4414 | void param(const param_type& __p) {__p_ = __p;} |
| 4415 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4416 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4417 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4418 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4419 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4420 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4421 | friend _LIBCPP_INLINE_VISIBILITY |
| 4422 | bool operator==(const poisson_distribution& __x, |
| 4423 | const poisson_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4424 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4425 | friend _LIBCPP_INLINE_VISIBILITY |
| 4426 | bool operator!=(const poisson_distribution& __x, |
| 4427 | const poisson_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4428 | {return !(__x == __y);} |
| 4429 | }; |
| 4430 | |
| 4431 | template<class _IntType> |
| 4432 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
| 4433 | : __mean_(__mean) |
| 4434 | { |
| 4435 | if (__mean_ < 10) |
| 4436 | { |
| 4437 | __s_ = 0; |
| 4438 | __d_ = 0; |
| 4439 | __l_ = _STD::exp(-__mean_); |
| 4440 | __omega_ = 0; |
| 4441 | __c3_ = 0; |
| 4442 | __c2_ = 0; |
| 4443 | __c1_ = 0; |
| 4444 | __c0_ = 0; |
| 4445 | __c_ = 0; |
| 4446 | } |
| 4447 | else |
| 4448 | { |
| 4449 | __s_ = _STD::sqrt(__mean_); |
| 4450 | __d_ = 6 * __mean_ * __mean_; |
| 4451 | __l_ = static_cast<result_type>(__mean_ - 1.1484); |
| 4452 | __omega_ = .3989423 / __s_; |
| 4453 | double __b1_ = .4166667E-1 / __mean_; |
| 4454 | double __b2_ = .3 * __b1_ * __b1_; |
| 4455 | __c3_ = .1428571 * __b1_ * __b2_; |
| 4456 | __c2_ = __b2_ - 15. * __c3_; |
| 4457 | __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; |
| 4458 | __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; |
| 4459 | __c_ = .1069 / __mean_; |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | template <class _IntType> |
| 4464 | template<class _URNG> |
| 4465 | _IntType |
| 4466 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4467 | { |
| 4468 | result_type __x; |
| 4469 | uniform_real_distribution<double> __urd; |
| 4470 | if (__pr.__mean_ <= 10) |
| 4471 | { |
| 4472 | __x = 0; |
| 4473 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__x) |
| 4474 | __p *= __urd(__urng); |
| 4475 | } |
| 4476 | else |
| 4477 | { |
| 4478 | double __difmuk; |
| 4479 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 4480 | double __u; |
| 4481 | if (__g > 0) |
| 4482 | { |
| 4483 | __x = static_cast<result_type>(__g); |
| 4484 | if (__x >= __pr.__l_) |
| 4485 | return __x; |
| 4486 | __difmuk = __pr.__mean_ - __x; |
| 4487 | __u = __urd(__urng); |
| 4488 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
| 4489 | return __x; |
| 4490 | } |
| 4491 | exponential_distribution<double> __edist; |
| 4492 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) |
| 4493 | { |
| 4494 | double __e; |
| 4495 | if (__using_exp_dist || __g < 0) |
| 4496 | { |
| 4497 | double __t; |
| 4498 | do |
| 4499 | { |
| 4500 | __e = __edist(__urng); |
| 4501 | __u = __urd(__urng); |
| 4502 | __u += __u - 1; |
| 4503 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 4504 | } while (__t <= -.6744); |
| 4505 | __x = __pr.__mean_ + __pr.__s_ * __t; |
| 4506 | __difmuk = __pr.__mean_ - __x; |
| 4507 | __using_exp_dist = true; |
| 4508 | } |
| 4509 | double __px; |
| 4510 | double __py; |
| 4511 | if (__x < 10) |
| 4512 | { |
| 4513 | const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, |
| 4514 | 40320, 362880}; |
| 4515 | __px = -__pr.__mean_; |
| 4516 | __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x]; |
| 4517 | } |
| 4518 | else |
| 4519 | { |
| 4520 | double __del = .8333333E-1 / __x; |
| 4521 | __del -= 4.8 * __del * __del * __del; |
| 4522 | double __v = __difmuk / __x; |
| 4523 | if (_STD::abs(__v) > 0.25) |
| 4524 | __px = __x * _STD::log(1 + __v) - __difmuk - __del; |
| 4525 | else |
| 4526 | __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) * |
| 4527 | __v + .1421878) * __v + -.1661269) * __v + .2000118) * |
| 4528 | __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; |
| 4529 | __py = .3989423 / _STD::sqrt(__x); |
| 4530 | } |
| 4531 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 4532 | double __r2 = __r * __r; |
| 4533 | double __fx = -0.5 * __r2; |
| 4534 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * |
| 4535 | __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 4536 | if (__using_exp_dist) |
| 4537 | { |
| 4538 | if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) - |
| 4539 | __fy * _STD::exp(__fx + __e)) |
| 4540 | break; |
| 4541 | } |
| 4542 | else |
| 4543 | { |
| 4544 | if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx)) |
| 4545 | break; |
| 4546 | } |
| 4547 | } |
| 4548 | } |
| 4549 | return __x; |
| 4550 | } |
| 4551 | |
| 4552 | template <class _CharT, class _Traits, class _IntType> |
| 4553 | basic_ostream<_CharT, _Traits>& |
| 4554 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4555 | const poisson_distribution<_IntType>& __x) |
| 4556 | { |
| 4557 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4558 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4559 | ios_base::scientific); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4560 | return __os << __x.mean(); |
| 4561 | } |
| 4562 | |
| 4563 | template <class _CharT, class _Traits, class _IntType> |
| 4564 | basic_istream<_CharT, _Traits>& |
| 4565 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4566 | poisson_distribution<_IntType>& __x) |
| 4567 | { |
| 4568 | typedef poisson_distribution<_IntType> _Eng; |
| 4569 | typedef typename _Eng::param_type param_type; |
| 4570 | __save_flags<_CharT, _Traits> _(__is); |
| 4571 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4572 | double __mean; |
| 4573 | __is >> __mean; |
| 4574 | if (!__is.fail()) |
| 4575 | __x.param(param_type(__mean)); |
| 4576 | return __is; |
| 4577 | } |
| 4578 | |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4579 | // weibull_distribution |
| 4580 | |
| 4581 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4582 | class _LIBCPP_VISIBLE weibull_distribution |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4583 | { |
| 4584 | public: |
| 4585 | // types |
| 4586 | typedef _RealType result_type; |
| 4587 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4588 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4589 | { |
| 4590 | result_type __a_; |
| 4591 | result_type __b_; |
| 4592 | public: |
| 4593 | typedef weibull_distribution distribution_type; |
| 4594 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4595 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4596 | explicit param_type(result_type __a = 1, result_type __b = 1) |
| 4597 | : __a_(__a), __b_(__b) {} |
| 4598 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4599 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4600 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4601 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4602 | result_type b() const {return __b_;} |
| 4603 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4604 | friend _LIBCPP_INLINE_VISIBILITY |
| 4605 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4606 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4607 | friend _LIBCPP_INLINE_VISIBILITY |
| 4608 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4609 | {return !(__x == __y);} |
| 4610 | }; |
| 4611 | |
| 4612 | private: |
| 4613 | param_type __p_; |
| 4614 | |
| 4615 | public: |
| 4616 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4617 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4618 | explicit weibull_distribution(result_type __a = 1, result_type __b = 1) |
| 4619 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4620 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4621 | explicit weibull_distribution(const param_type& __p) |
| 4622 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4623 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4624 | void reset() {} |
| 4625 | |
| 4626 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4627 | template<class _URNG> |
| 4628 | _LIBCPP_INLINE_VISIBILITY |
| 4629 | result_type operator()(_URNG& __g) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4630 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4631 | template<class _URNG> |
| 4632 | _LIBCPP_INLINE_VISIBILITY |
| 4633 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4634 | {return __p.b() * |
| 4635 | _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());} |
| 4636 | |
| 4637 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4638 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4639 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4640 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4641 | result_type b() const {return __p_.b();} |
| 4642 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4643 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4644 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4645 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4646 | void param(const param_type& __p) {__p_ = __p;} |
| 4647 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4648 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4649 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4650 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4651 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4652 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4653 | friend _LIBCPP_INLINE_VISIBILITY |
| 4654 | bool operator==(const weibull_distribution& __x, |
| 4655 | const weibull_distribution& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4656 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4657 | friend _LIBCPP_INLINE_VISIBILITY |
| 4658 | bool operator!=(const weibull_distribution& __x, |
| 4659 | const weibull_distribution& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4660 | {return !(__x == __y);} |
| 4661 | }; |
| 4662 | |
| 4663 | template <class _CharT, class _Traits, class _RT> |
| 4664 | basic_ostream<_CharT, _Traits>& |
| 4665 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4666 | const weibull_distribution<_RT>& __x) |
| 4667 | { |
| 4668 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4669 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4670 | ios_base::scientific); |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4671 | _CharT __sp = __os.widen(' '); |
| 4672 | __os.fill(__sp); |
| 4673 | __os << __x.a() << __sp << __x.b(); |
| 4674 | return __os; |
| 4675 | } |
| 4676 | |
| 4677 | template <class _CharT, class _Traits, class _RT> |
| 4678 | basic_istream<_CharT, _Traits>& |
| 4679 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4680 | weibull_distribution<_RT>& __x) |
| 4681 | { |
| 4682 | typedef weibull_distribution<_RT> _Eng; |
| 4683 | typedef typename _Eng::result_type result_type; |
| 4684 | typedef typename _Eng::param_type param_type; |
| 4685 | __save_flags<_CharT, _Traits> _(__is); |
| 4686 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4687 | result_type __a; |
| 4688 | result_type __b; |
| 4689 | __is >> __a >> __b; |
| 4690 | if (!__is.fail()) |
| 4691 | __x.param(param_type(__a, __b)); |
| 4692 | return __is; |
| 4693 | } |
| 4694 | |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4695 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4696 | class _LIBCPP_VISIBLE extreme_value_distribution |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4697 | { |
| 4698 | public: |
| 4699 | // types |
| 4700 | typedef _RealType result_type; |
| 4701 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4702 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4703 | { |
| 4704 | result_type __a_; |
| 4705 | result_type __b_; |
| 4706 | public: |
| 4707 | typedef extreme_value_distribution distribution_type; |
| 4708 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4709 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4710 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 4711 | : __a_(__a), __b_(__b) {} |
| 4712 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4713 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4714 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4715 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4716 | result_type b() const {return __b_;} |
| 4717 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4718 | friend _LIBCPP_INLINE_VISIBILITY |
| 4719 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4720 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4721 | friend _LIBCPP_INLINE_VISIBILITY |
| 4722 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4723 | {return !(__x == __y);} |
| 4724 | }; |
| 4725 | |
| 4726 | private: |
| 4727 | param_type __p_; |
| 4728 | |
| 4729 | public: |
| 4730 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4731 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4732 | explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1) |
| 4733 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4734 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4735 | explicit extreme_value_distribution(const param_type& __p) |
| 4736 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4737 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4738 | void reset() {} |
| 4739 | |
| 4740 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4741 | template<class _URNG> |
| 4742 | _LIBCPP_INLINE_VISIBILITY |
| 4743 | result_type operator()(_URNG& __g) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4744 | {return (*this)(__g, __p_);} |
| 4745 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4746 | |
| 4747 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4748 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4749 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4750 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4751 | result_type b() const {return __p_.b();} |
| 4752 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4753 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4754 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4755 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4756 | void param(const param_type& __p) {__p_ = __p;} |
| 4757 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4758 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4759 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4760 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4761 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4762 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4763 | friend _LIBCPP_INLINE_VISIBILITY |
| 4764 | bool operator==(const extreme_value_distribution& __x, |
| 4765 | const extreme_value_distribution& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4766 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4767 | friend _LIBCPP_INLINE_VISIBILITY |
| 4768 | bool operator!=(const extreme_value_distribution& __x, |
| 4769 | const extreme_value_distribution& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4770 | {return !(__x == __y);} |
| 4771 | }; |
| 4772 | |
| 4773 | template<class _RealType> |
| 4774 | template<class _URNG> |
| 4775 | _RealType |
| 4776 | extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4777 | { |
| 4778 | return __p.a() - __p.b() * |
| 4779 | _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g))); |
| 4780 | } |
| 4781 | |
| 4782 | template <class _CharT, class _Traits, class _RT> |
| 4783 | basic_ostream<_CharT, _Traits>& |
| 4784 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4785 | const extreme_value_distribution<_RT>& __x) |
| 4786 | { |
| 4787 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4788 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4789 | ios_base::scientific); |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4790 | _CharT __sp = __os.widen(' '); |
| 4791 | __os.fill(__sp); |
| 4792 | __os << __x.a() << __sp << __x.b(); |
| 4793 | return __os; |
| 4794 | } |
| 4795 | |
| 4796 | template <class _CharT, class _Traits, class _RT> |
| 4797 | basic_istream<_CharT, _Traits>& |
| 4798 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4799 | extreme_value_distribution<_RT>& __x) |
| 4800 | { |
| 4801 | typedef extreme_value_distribution<_RT> _Eng; |
| 4802 | typedef typename _Eng::result_type result_type; |
| 4803 | typedef typename _Eng::param_type param_type; |
| 4804 | __save_flags<_CharT, _Traits> _(__is); |
| 4805 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4806 | result_type __a; |
| 4807 | result_type __b; |
| 4808 | __is >> __a >> __b; |
| 4809 | if (!__is.fail()) |
| 4810 | __x.param(param_type(__a, __b)); |
| 4811 | return __is; |
| 4812 | } |
| 4813 | |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4814 | // gamma_distribution |
| 4815 | |
| 4816 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4817 | class _LIBCPP_VISIBLE gamma_distribution |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4818 | { |
| 4819 | public: |
| 4820 | // types |
| 4821 | typedef _RealType result_type; |
| 4822 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4823 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4824 | { |
| 4825 | result_type __alpha_; |
| 4826 | result_type __beta_; |
| 4827 | public: |
| 4828 | typedef gamma_distribution distribution_type; |
| 4829 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4830 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4831 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 4832 | : __alpha_(__alpha), __beta_(__beta) {} |
| 4833 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4834 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4835 | result_type alpha() const {return __alpha_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4836 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4837 | result_type beta() const {return __beta_;} |
| 4838 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4839 | friend _LIBCPP_INLINE_VISIBILITY |
| 4840 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4841 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4842 | friend _LIBCPP_INLINE_VISIBILITY |
| 4843 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4844 | {return !(__x == __y);} |
| 4845 | }; |
| 4846 | |
| 4847 | private: |
| 4848 | param_type __p_; |
| 4849 | |
| 4850 | public: |
| 4851 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4852 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4853 | explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) |
| 4854 | : __p_(param_type(__alpha, __beta)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4855 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4856 | explicit gamma_distribution(const param_type& __p) |
| 4857 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4858 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4859 | void reset() {} |
| 4860 | |
| 4861 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4862 | template<class _URNG> |
| 4863 | _LIBCPP_INLINE_VISIBILITY |
| 4864 | result_type operator()(_URNG& __g) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4865 | {return (*this)(__g, __p_);} |
| 4866 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4867 | |
| 4868 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4869 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4870 | result_type alpha() const {return __p_.alpha();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4871 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4872 | result_type beta() const {return __p_.beta();} |
| 4873 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4874 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4875 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4876 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4877 | void param(const param_type& __p) {__p_ = __p;} |
| 4878 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4879 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4880 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4881 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4882 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4883 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4884 | friend _LIBCPP_INLINE_VISIBILITY |
| 4885 | bool operator==(const gamma_distribution& __x, |
| 4886 | const gamma_distribution& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4887 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4888 | friend _LIBCPP_INLINE_VISIBILITY |
| 4889 | bool operator!=(const gamma_distribution& __x, |
| 4890 | const gamma_distribution& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4891 | {return !(__x == __y);} |
| 4892 | }; |
| 4893 | |
| 4894 | template <class _RealType> |
| 4895 | template<class _URNG> |
| 4896 | _RealType |
| 4897 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4898 | { |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4899 | result_type __a = __p.alpha(); |
| 4900 | uniform_real_distribution<result_type> __gen(0, 1); |
| 4901 | exponential_distribution<result_type> __egen; |
| 4902 | result_type __x; |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4903 | if (__a == 1) |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4904 | __x = __egen(__g); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4905 | else if (__a > 1) |
| 4906 | { |
| 4907 | const result_type __b = __a - 1; |
| 4908 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4909 | while (true) |
| 4910 | { |
| 4911 | const result_type __u = __gen(__g); |
| 4912 | const result_type __v = __gen(__g); |
| 4913 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4914 | if (__w != 0) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4915 | { |
| 4916 | const result_type __y = _STD::sqrt(__c / __w) * |
| 4917 | (__u - result_type(0.5)); |
| 4918 | __x = __b + __y; |
| 4919 | if (__x >= 0) |
| 4920 | { |
| 4921 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 4922 | if (__z <= 1 - 2 * __y * __y / __x) |
| 4923 | break; |
| 4924 | if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y)) |
| 4925 | break; |
| 4926 | } |
| 4927 | } |
| 4928 | } |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4929 | } |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4930 | else // __a < 1 |
| 4931 | { |
| 4932 | while (true) |
| 4933 | { |
| 4934 | const result_type __u = __gen(__g); |
| 4935 | const result_type __es = __egen(__g); |
| 4936 | if (__u <= 1 - __a) |
| 4937 | { |
| 4938 | __x = _STD::pow(__u, 1 / __a); |
| 4939 | if (__x <= __es) |
| 4940 | break; |
| 4941 | } |
| 4942 | else |
| 4943 | { |
| 4944 | const result_type __e = -_STD::log((1-__u)/__a); |
| 4945 | __x = _STD::pow(1 - __a + __a * __e, 1 / __a); |
| 4946 | if (__x <= __e + __es) |
| 4947 | break; |
| 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | return __x * __p.beta(); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | template <class _CharT, class _Traits, class _RT> |
| 4955 | basic_ostream<_CharT, _Traits>& |
| 4956 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4957 | const gamma_distribution<_RT>& __x) |
| 4958 | { |
| 4959 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4960 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4961 | ios_base::scientific); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4962 | _CharT __sp = __os.widen(' '); |
| 4963 | __os.fill(__sp); |
| 4964 | __os << __x.alpha() << __sp << __x.beta(); |
| 4965 | return __os; |
| 4966 | } |
| 4967 | |
| 4968 | template <class _CharT, class _Traits, class _RT> |
| 4969 | basic_istream<_CharT, _Traits>& |
| 4970 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4971 | gamma_distribution<_RT>& __x) |
| 4972 | { |
| 4973 | typedef gamma_distribution<_RT> _Eng; |
| 4974 | typedef typename _Eng::result_type result_type; |
| 4975 | typedef typename _Eng::param_type param_type; |
| 4976 | __save_flags<_CharT, _Traits> _(__is); |
| 4977 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4978 | result_type __alpha; |
| 4979 | result_type __beta; |
| 4980 | __is >> __alpha >> __beta; |
| 4981 | if (!__is.fail()) |
| 4982 | __x.param(param_type(__alpha, __beta)); |
| 4983 | return __is; |
| 4984 | } |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 4985 | |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 4986 | // negative_binomial_distribution |
| 4987 | |
| 4988 | template<class _IntType = int> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4989 | class _LIBCPP_VISIBLE negative_binomial_distribution |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 4990 | { |
| 4991 | public: |
| 4992 | // types |
| 4993 | typedef _IntType result_type; |
| 4994 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4995 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 4996 | { |
| 4997 | result_type __k_; |
| 4998 | double __p_; |
| 4999 | public: |
| 5000 | typedef negative_binomial_distribution distribution_type; |
| 5001 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5002 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5003 | explicit param_type(result_type __k = 1, double __p = 0.5) |
| 5004 | : __k_(__k), __p_(__p) {} |
| 5005 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5006 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5007 | result_type k() const {return __k_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5008 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5009 | double p() const {return __p_;} |
| 5010 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5011 | friend _LIBCPP_INLINE_VISIBILITY |
| 5012 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5013 | {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5014 | friend _LIBCPP_INLINE_VISIBILITY |
| 5015 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5016 | {return !(__x == __y);} |
| 5017 | }; |
| 5018 | |
| 5019 | private: |
| 5020 | param_type __p_; |
| 5021 | |
| 5022 | public: |
| 5023 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5024 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5025 | explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5) |
| 5026 | : __p_(__k, __p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5027 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5028 | explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5029 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5030 | void reset() {} |
| 5031 | |
| 5032 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5033 | template<class _URNG> |
| 5034 | _LIBCPP_INLINE_VISIBILITY |
| 5035 | result_type operator()(_URNG& __g) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5036 | {return (*this)(__g, __p_);} |
| 5037 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5038 | |
| 5039 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5040 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5041 | result_type k() const {return __p_.k();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5042 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5043 | double p() const {return __p_.p();} |
| 5044 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5045 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5046 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5047 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5048 | void param(const param_type& __p) {__p_ = __p;} |
| 5049 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5050 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5051 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5052 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5053 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5054 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5055 | friend _LIBCPP_INLINE_VISIBILITY |
| 5056 | bool operator==(const negative_binomial_distribution& __x, |
| 5057 | const negative_binomial_distribution& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5058 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5059 | friend _LIBCPP_INLINE_VISIBILITY |
| 5060 | bool operator!=(const negative_binomial_distribution& __x, |
| 5061 | const negative_binomial_distribution& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5062 | {return !(__x == __y);} |
| 5063 | }; |
| 5064 | |
| 5065 | template <class _IntType> |
| 5066 | template<class _URNG> |
| 5067 | _IntType |
| 5068 | negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 5069 | { |
| 5070 | result_type __k = __pr.k(); |
| 5071 | double __p = __pr.p(); |
| 5072 | if (__k <= 21 * __p) |
| 5073 | { |
| 5074 | bernoulli_distribution __gen(__p); |
| 5075 | result_type __f = 0; |
| 5076 | result_type __s = 0; |
| 5077 | while (__s < __k) |
| 5078 | { |
| 5079 | if (__gen(__urng)) |
| 5080 | ++__s; |
| 5081 | else |
| 5082 | ++__f; |
| 5083 | } |
| 5084 | return __f; |
| 5085 | } |
| 5086 | return poisson_distribution<result_type>(gamma_distribution<double> |
| 5087 | (__k, (1-__p)/__p)(__urng))(__urng); |
| 5088 | } |
| 5089 | |
| 5090 | template <class _CharT, class _Traits, class _IntType> |
| 5091 | basic_ostream<_CharT, _Traits>& |
| 5092 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5093 | const negative_binomial_distribution<_IntType>& __x) |
| 5094 | { |
| 5095 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5096 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5097 | ios_base::scientific); |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5098 | _CharT __sp = __os.widen(' '); |
| 5099 | __os.fill(__sp); |
| 5100 | return __os << __x.k() << __sp << __x.p(); |
| 5101 | } |
| 5102 | |
| 5103 | template <class _CharT, class _Traits, class _IntType> |
| 5104 | basic_istream<_CharT, _Traits>& |
| 5105 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5106 | negative_binomial_distribution<_IntType>& __x) |
| 5107 | { |
| 5108 | typedef negative_binomial_distribution<_IntType> _Eng; |
| 5109 | typedef typename _Eng::result_type result_type; |
| 5110 | typedef typename _Eng::param_type param_type; |
| 5111 | __save_flags<_CharT, _Traits> _(__is); |
| 5112 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5113 | result_type __k; |
| 5114 | double __p; |
| 5115 | __is >> __k >> __p; |
| 5116 | if (!__is.fail()) |
| 5117 | __x.param(param_type(__k, __p)); |
| 5118 | return __is; |
| 5119 | } |
| 5120 | |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5121 | // geometric_distribution |
| 5122 | |
| 5123 | template<class _IntType = int> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5124 | class _LIBCPP_VISIBLE geometric_distribution |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5125 | { |
| 5126 | public: |
| 5127 | // types |
| 5128 | typedef _IntType result_type; |
| 5129 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5130 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5131 | { |
| 5132 | double __p_; |
| 5133 | public: |
| 5134 | typedef geometric_distribution distribution_type; |
| 5135 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5136 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5137 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 5138 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5139 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5140 | double p() const {return __p_;} |
| 5141 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5142 | friend _LIBCPP_INLINE_VISIBILITY |
| 5143 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5144 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5145 | friend _LIBCPP_INLINE_VISIBILITY |
| 5146 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5147 | {return !(__x == __y);} |
| 5148 | }; |
| 5149 | |
| 5150 | private: |
| 5151 | param_type __p_; |
| 5152 | |
| 5153 | public: |
| 5154 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5155 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5156 | explicit geometric_distribution(double __p = 0.5) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5157 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5158 | explicit geometric_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5159 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5160 | void reset() {} |
| 5161 | |
| 5162 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5163 | template<class _URNG> |
| 5164 | _LIBCPP_INLINE_VISIBILITY |
| 5165 | result_type operator()(_URNG& __g) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5166 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5167 | template<class _URNG> |
| 5168 | _LIBCPP_INLINE_VISIBILITY |
| 5169 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5170 | {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} |
| 5171 | |
| 5172 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5173 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5174 | double p() const {return __p_.p();} |
| 5175 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5176 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5177 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5178 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5179 | void param(const param_type& __p) {__p_ = __p;} |
| 5180 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5181 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5182 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5183 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5184 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5185 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5186 | friend _LIBCPP_INLINE_VISIBILITY |
| 5187 | bool operator==(const geometric_distribution& __x, |
| 5188 | const geometric_distribution& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5189 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5190 | friend _LIBCPP_INLINE_VISIBILITY |
| 5191 | bool operator!=(const geometric_distribution& __x, |
| 5192 | const geometric_distribution& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5193 | {return !(__x == __y);} |
| 5194 | }; |
| 5195 | |
| 5196 | template <class _CharT, class _Traits, class _IntType> |
| 5197 | basic_ostream<_CharT, _Traits>& |
| 5198 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5199 | const geometric_distribution<_IntType>& __x) |
| 5200 | { |
| 5201 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5202 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5203 | ios_base::scientific); |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5204 | return __os << __x.p(); |
| 5205 | } |
| 5206 | |
| 5207 | template <class _CharT, class _Traits, class _IntType> |
| 5208 | basic_istream<_CharT, _Traits>& |
| 5209 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5210 | geometric_distribution<_IntType>& __x) |
| 5211 | { |
| 5212 | typedef geometric_distribution<_IntType> _Eng; |
| 5213 | typedef typename _Eng::param_type param_type; |
| 5214 | __save_flags<_CharT, _Traits> _(__is); |
| 5215 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5216 | double __p; |
| 5217 | __is >> __p; |
| 5218 | if (!__is.fail()) |
| 5219 | __x.param(param_type(__p)); |
| 5220 | return __is; |
| 5221 | } |
| 5222 | |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5223 | // chi_squared_distribution |
| 5224 | |
| 5225 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5226 | class _LIBCPP_VISIBLE chi_squared_distribution |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5227 | { |
| 5228 | public: |
| 5229 | // types |
| 5230 | typedef _RealType result_type; |
| 5231 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5232 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5233 | { |
| 5234 | result_type __n_; |
| 5235 | public: |
| 5236 | typedef chi_squared_distribution distribution_type; |
| 5237 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5238 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5239 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5240 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5241 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5242 | result_type n() const {return __n_;} |
| 5243 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5244 | friend _LIBCPP_INLINE_VISIBILITY |
| 5245 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5246 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5247 | friend _LIBCPP_INLINE_VISIBILITY |
| 5248 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5249 | {return !(__x == __y);} |
| 5250 | }; |
| 5251 | |
| 5252 | private: |
| 5253 | param_type __p_; |
| 5254 | |
| 5255 | public: |
| 5256 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5257 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5258 | explicit chi_squared_distribution(result_type __n = 1) |
| 5259 | : __p_(param_type(__n)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5260 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5261 | explicit chi_squared_distribution(const param_type& __p) |
| 5262 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5263 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5264 | void reset() {} |
| 5265 | |
| 5266 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5267 | template<class _URNG> |
| 5268 | _LIBCPP_INLINE_VISIBILITY |
| 5269 | result_type operator()(_URNG& __g) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5270 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5271 | template<class _URNG> |
| 5272 | _LIBCPP_INLINE_VISIBILITY |
| 5273 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5274 | {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);} |
| 5275 | |
| 5276 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5277 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5278 | result_type n() const {return __p_.n();} |
| 5279 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5280 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5281 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5282 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5283 | void param(const param_type& __p) {__p_ = __p;} |
| 5284 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5285 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5286 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5287 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5288 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5289 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5290 | friend _LIBCPP_INLINE_VISIBILITY |
| 5291 | bool operator==(const chi_squared_distribution& __x, |
| 5292 | const chi_squared_distribution& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5293 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5294 | friend _LIBCPP_INLINE_VISIBILITY |
| 5295 | bool operator!=(const chi_squared_distribution& __x, |
| 5296 | const chi_squared_distribution& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5297 | {return !(__x == __y);} |
| 5298 | }; |
| 5299 | |
| 5300 | template <class _CharT, class _Traits, class _RT> |
| 5301 | basic_ostream<_CharT, _Traits>& |
| 5302 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5303 | const chi_squared_distribution<_RT>& __x) |
| 5304 | { |
| 5305 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5306 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5307 | ios_base::scientific); |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5308 | __os << __x.n(); |
| 5309 | return __os; |
| 5310 | } |
| 5311 | |
| 5312 | template <class _CharT, class _Traits, class _RT> |
| 5313 | basic_istream<_CharT, _Traits>& |
| 5314 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5315 | chi_squared_distribution<_RT>& __x) |
| 5316 | { |
| 5317 | typedef chi_squared_distribution<_RT> _Eng; |
| 5318 | typedef typename _Eng::result_type result_type; |
| 5319 | typedef typename _Eng::param_type param_type; |
| 5320 | __save_flags<_CharT, _Traits> _(__is); |
| 5321 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5322 | result_type __n; |
| 5323 | __is >> __n; |
| 5324 | if (!__is.fail()) |
| 5325 | __x.param(param_type(__n)); |
| 5326 | return __is; |
| 5327 | } |
| 5328 | |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5329 | // cauchy_distribution |
| 5330 | |
| 5331 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5332 | class _LIBCPP_VISIBLE cauchy_distribution |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5333 | { |
| 5334 | public: |
| 5335 | // types |
| 5336 | typedef _RealType result_type; |
| 5337 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5338 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5339 | { |
| 5340 | result_type __a_; |
| 5341 | result_type __b_; |
| 5342 | public: |
| 5343 | typedef cauchy_distribution distribution_type; |
| 5344 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5345 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5346 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5347 | : __a_(__a), __b_(__b) {} |
| 5348 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5349 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5350 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5351 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5352 | result_type b() const {return __b_;} |
| 5353 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5354 | friend _LIBCPP_INLINE_VISIBILITY |
| 5355 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5356 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5357 | friend _LIBCPP_INLINE_VISIBILITY |
| 5358 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5359 | {return !(__x == __y);} |
| 5360 | }; |
| 5361 | |
| 5362 | private: |
| 5363 | param_type __p_; |
| 5364 | |
| 5365 | public: |
| 5366 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5367 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5368 | explicit cauchy_distribution(result_type __a = 0, result_type __b = 1) |
| 5369 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5370 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5371 | explicit cauchy_distribution(const param_type& __p) |
| 5372 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5373 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5374 | void reset() {} |
| 5375 | |
| 5376 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5377 | template<class _URNG> |
| 5378 | _LIBCPP_INLINE_VISIBILITY |
| 5379 | result_type operator()(_URNG& __g) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5380 | {return (*this)(__g, __p_);} |
| 5381 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5382 | |
| 5383 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5384 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5385 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5386 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5387 | result_type b() const {return __p_.b();} |
| 5388 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5389 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5390 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5391 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5392 | void param(const param_type& __p) {__p_ = __p;} |
| 5393 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5394 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5395 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5396 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5397 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5398 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5399 | friend _LIBCPP_INLINE_VISIBILITY |
| 5400 | bool operator==(const cauchy_distribution& __x, |
| 5401 | const cauchy_distribution& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5402 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5403 | friend _LIBCPP_INLINE_VISIBILITY |
| 5404 | bool operator!=(const cauchy_distribution& __x, |
| 5405 | const cauchy_distribution& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5406 | {return !(__x == __y);} |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5407 | }; |
| 5408 | |
| 5409 | template <class _RealType> |
| 5410 | template<class _URNG> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5411 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5412 | _RealType |
| 5413 | cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5414 | { |
| 5415 | uniform_real_distribution<result_type> __gen; |
| 5416 | // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite |
| 5417 | return __p.a() + __p.b() * _STD::tan(3.1415926535897932384626433832795 * __gen(__g)); |
| 5418 | } |
| 5419 | |
| 5420 | template <class _CharT, class _Traits, class _RT> |
| 5421 | basic_ostream<_CharT, _Traits>& |
| 5422 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5423 | const cauchy_distribution<_RT>& __x) |
| 5424 | { |
| 5425 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5426 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5427 | ios_base::scientific); |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5428 | _CharT __sp = __os.widen(' '); |
| 5429 | __os.fill(__sp); |
| 5430 | __os << __x.a() << __sp << __x.b(); |
| 5431 | return __os; |
| 5432 | } |
| 5433 | |
| 5434 | template <class _CharT, class _Traits, class _RT> |
| 5435 | basic_istream<_CharT, _Traits>& |
| 5436 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5437 | cauchy_distribution<_RT>& __x) |
| 5438 | { |
| 5439 | typedef cauchy_distribution<_RT> _Eng; |
| 5440 | typedef typename _Eng::result_type result_type; |
| 5441 | typedef typename _Eng::param_type param_type; |
| 5442 | __save_flags<_CharT, _Traits> _(__is); |
| 5443 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5444 | result_type __a; |
| 5445 | result_type __b; |
| 5446 | __is >> __a >> __b; |
| 5447 | if (!__is.fail()) |
| 5448 | __x.param(param_type(__a, __b)); |
| 5449 | return __is; |
| 5450 | } |
| 5451 | |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5452 | // fisher_f_distribution |
| 5453 | |
| 5454 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5455 | class _LIBCPP_VISIBLE fisher_f_distribution |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5456 | { |
| 5457 | public: |
| 5458 | // types |
| 5459 | typedef _RealType result_type; |
| 5460 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5461 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5462 | { |
| 5463 | result_type __m_; |
| 5464 | result_type __n_; |
| 5465 | public: |
| 5466 | typedef fisher_f_distribution distribution_type; |
| 5467 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5468 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5469 | explicit param_type(result_type __m = 1, result_type __n = 1) |
| 5470 | : __m_(__m), __n_(__n) {} |
| 5471 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5472 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5473 | result_type m() const {return __m_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5474 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5475 | result_type n() const {return __n_;} |
| 5476 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5477 | friend _LIBCPP_INLINE_VISIBILITY |
| 5478 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5479 | {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5480 | friend _LIBCPP_INLINE_VISIBILITY |
| 5481 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5482 | {return !(__x == __y);} |
| 5483 | }; |
| 5484 | |
| 5485 | private: |
| 5486 | param_type __p_; |
| 5487 | |
| 5488 | public: |
| 5489 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5490 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5491 | explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1) |
| 5492 | : __p_(param_type(__m, __n)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5493 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5494 | explicit fisher_f_distribution(const param_type& __p) |
| 5495 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5496 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5497 | void reset() {} |
| 5498 | |
| 5499 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5500 | template<class _URNG> |
| 5501 | _LIBCPP_INLINE_VISIBILITY |
| 5502 | result_type operator()(_URNG& __g) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5503 | {return (*this)(__g, __p_);} |
| 5504 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5505 | |
| 5506 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5507 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5508 | result_type m() const {return __p_.m();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5509 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5510 | result_type n() const {return __p_.n();} |
| 5511 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5512 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5513 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5514 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5515 | void param(const param_type& __p) {__p_ = __p;} |
| 5516 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5517 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5518 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5519 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5520 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5521 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5522 | friend _LIBCPP_INLINE_VISIBILITY |
| 5523 | bool operator==(const fisher_f_distribution& __x, |
| 5524 | const fisher_f_distribution& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5525 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5526 | friend _LIBCPP_INLINE_VISIBILITY |
| 5527 | bool operator!=(const fisher_f_distribution& __x, |
| 5528 | const fisher_f_distribution& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5529 | {return !(__x == __y);} |
| 5530 | }; |
| 5531 | |
| 5532 | template <class _RealType> |
| 5533 | template<class _URNG> |
| 5534 | _RealType |
| 5535 | fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5536 | { |
| 5537 | gamma_distribution<result_type> __gdm(__p.m() * result_type(.5)); |
| 5538 | gamma_distribution<result_type> __gdn(__p.n() * result_type(.5)); |
| 5539 | return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g)); |
| 5540 | } |
| 5541 | |
| 5542 | template <class _CharT, class _Traits, class _RT> |
| 5543 | basic_ostream<_CharT, _Traits>& |
| 5544 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5545 | const fisher_f_distribution<_RT>& __x) |
| 5546 | { |
| 5547 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5548 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5549 | ios_base::scientific); |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5550 | _CharT __sp = __os.widen(' '); |
| 5551 | __os.fill(__sp); |
| 5552 | __os << __x.m() << __sp << __x.n(); |
| 5553 | return __os; |
| 5554 | } |
| 5555 | |
| 5556 | template <class _CharT, class _Traits, class _RT> |
| 5557 | basic_istream<_CharT, _Traits>& |
| 5558 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5559 | fisher_f_distribution<_RT>& __x) |
| 5560 | { |
| 5561 | typedef fisher_f_distribution<_RT> _Eng; |
| 5562 | typedef typename _Eng::result_type result_type; |
| 5563 | typedef typename _Eng::param_type param_type; |
| 5564 | __save_flags<_CharT, _Traits> _(__is); |
| 5565 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5566 | result_type __m; |
| 5567 | result_type __n; |
| 5568 | __is >> __m >> __n; |
| 5569 | if (!__is.fail()) |
| 5570 | __x.param(param_type(__m, __n)); |
| 5571 | return __is; |
| 5572 | } |
| 5573 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5574 | // student_t_distribution |
| 5575 | |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5576 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5577 | class _LIBCPP_VISIBLE student_t_distribution |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5578 | { |
| 5579 | public: |
| 5580 | // types |
| 5581 | typedef _RealType result_type; |
| 5582 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5583 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5584 | { |
| 5585 | result_type __n_; |
| 5586 | public: |
| 5587 | typedef student_t_distribution distribution_type; |
| 5588 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5589 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5590 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5591 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5592 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5593 | result_type n() const {return __n_;} |
| 5594 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5595 | friend _LIBCPP_INLINE_VISIBILITY |
| 5596 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5597 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5598 | friend _LIBCPP_INLINE_VISIBILITY |
| 5599 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5600 | {return !(__x == __y);} |
| 5601 | }; |
| 5602 | |
| 5603 | private: |
| 5604 | param_type __p_; |
| 5605 | normal_distribution<result_type> __nd_; |
| 5606 | |
| 5607 | public: |
| 5608 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5609 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5610 | explicit student_t_distribution(result_type __n = 1) |
| 5611 | : __p_(param_type(__n)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5612 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5613 | explicit student_t_distribution(const param_type& __p) |
| 5614 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5615 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5616 | void reset() {__nd_.reset();} |
| 5617 | |
| 5618 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5619 | template<class _URNG> |
| 5620 | _LIBCPP_INLINE_VISIBILITY |
| 5621 | result_type operator()(_URNG& __g) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5622 | {return (*this)(__g, __p_);} |
| 5623 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5624 | |
| 5625 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5626 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5627 | result_type n() const {return __p_.n();} |
| 5628 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5629 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5630 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5631 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5632 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5633 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5634 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5635 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5636 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5637 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 5638 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5639 | friend _LIBCPP_INLINE_VISIBILITY |
| 5640 | bool operator==(const student_t_distribution& __x, |
| 5641 | const student_t_distribution& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5642 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5643 | friend _LIBCPP_INLINE_VISIBILITY |
| 5644 | bool operator!=(const student_t_distribution& __x, |
| 5645 | const student_t_distribution& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5646 | {return !(__x == __y);} |
| 5647 | }; |
| 5648 | |
| 5649 | template <class _RealType> |
| 5650 | template<class _URNG> |
| 5651 | _RealType |
| 5652 | student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5653 | { |
| 5654 | gamma_distribution<result_type> __gd(__p.n() * .5, 2); |
| 5655 | return __nd_(__g) * _STD::sqrt(__p.n()/__gd(__g)); |
| 5656 | } |
| 5657 | |
| 5658 | template <class _CharT, class _Traits, class _RT> |
| 5659 | basic_ostream<_CharT, _Traits>& |
| 5660 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5661 | const student_t_distribution<_RT>& __x) |
| 5662 | { |
| 5663 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5664 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5665 | ios_base::scientific); |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5666 | __os << __x.n(); |
| 5667 | return __os; |
| 5668 | } |
| 5669 | |
| 5670 | template <class _CharT, class _Traits, class _RT> |
| 5671 | basic_istream<_CharT, _Traits>& |
| 5672 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5673 | student_t_distribution<_RT>& __x) |
| 5674 | { |
| 5675 | typedef student_t_distribution<_RT> _Eng; |
| 5676 | typedef typename _Eng::result_type result_type; |
| 5677 | typedef typename _Eng::param_type param_type; |
| 5678 | __save_flags<_CharT, _Traits> _(__is); |
| 5679 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5680 | result_type __n; |
| 5681 | __is >> __n; |
| 5682 | if (!__is.fail()) |
| 5683 | __x.param(param_type(__n)); |
| 5684 | return __is; |
| 5685 | } |
| 5686 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5687 | // discrete_distribution |
| 5688 | |
| 5689 | template<class _IntType = int> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5690 | class _LIBCPP_VISIBLE discrete_distribution |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5691 | { |
| 5692 | public: |
| 5693 | // types |
| 5694 | typedef _IntType result_type; |
| 5695 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5696 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5697 | { |
| 5698 | vector<double> __p_; |
| 5699 | public: |
| 5700 | typedef discrete_distribution distribution_type; |
| 5701 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5702 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5703 | param_type() {} |
| 5704 | template<class _InputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5705 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5706 | param_type(_InputIterator __f, _InputIterator __l) |
| 5707 | : __p_(__f, __l) {__init();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5708 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5709 | param_type(initializer_list<double> __wl) |
| 5710 | : __p_(__wl.begin(), __wl.end()) {__init();} |
| 5711 | template<class _UnaryOperation> |
| 5712 | param_type(size_t __nw, double __xmin, double __xmax, |
| 5713 | _UnaryOperation __fw); |
| 5714 | |
| 5715 | vector<double> probabilities() const; |
| 5716 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5717 | friend _LIBCPP_INLINE_VISIBILITY |
| 5718 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5719 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5720 | friend _LIBCPP_INLINE_VISIBILITY |
| 5721 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5722 | {return !(__x == __y);} |
| 5723 | |
| 5724 | private: |
| 5725 | void __init(); |
| 5726 | |
| 5727 | friend class discrete_distribution; |
| 5728 | |
| 5729 | template <class _CharT, class _Traits, class _IT> |
| 5730 | friend |
| 5731 | basic_ostream<_CharT, _Traits>& |
| 5732 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5733 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5734 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5735 | template <class _CharT, class _Traits, class _IT> |
| 5736 | friend |
| 5737 | basic_istream<_CharT, _Traits>& |
| 5738 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5739 | discrete_distribution<_IT>& __x); |
| 5740 | }; |
| 5741 | |
| 5742 | private: |
| 5743 | param_type __p_; |
| 5744 | |
| 5745 | public: |
| 5746 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5747 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5748 | discrete_distribution() {} |
| 5749 | template<class _InputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5750 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5751 | discrete_distribution(_InputIterator __f, _InputIterator __l) |
| 5752 | : __p_(__f, __l) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5753 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5754 | discrete_distribution(initializer_list<double> __wl) |
| 5755 | : __p_(__wl) {} |
| 5756 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5757 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5758 | discrete_distribution(size_t __nw, double __xmin, double __xmax, |
| 5759 | _UnaryOperation __fw) |
| 5760 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 5761 | explicit discrete_distribution(const param_type& __p) |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5762 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5763 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5764 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5765 | void reset() {} |
| 5766 | |
| 5767 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5768 | template<class _URNG> |
| 5769 | _LIBCPP_INLINE_VISIBILITY |
| 5770 | result_type operator()(_URNG& __g) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5771 | {return (*this)(__g, __p_);} |
| 5772 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5773 | |
| 5774 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5775 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5776 | vector<double> probabilities() const {return __p_.probabilities();} |
| 5777 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5778 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5779 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5780 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5781 | void param(const param_type& __p) {__p_ = __p;} |
| 5782 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5783 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5784 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5785 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5786 | result_type max() const {return __p_.__p_.size();} |
| 5787 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5788 | friend _LIBCPP_INLINE_VISIBILITY |
| 5789 | bool operator==(const discrete_distribution& __x, |
| 5790 | const discrete_distribution& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5791 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5792 | friend _LIBCPP_INLINE_VISIBILITY |
| 5793 | bool operator!=(const discrete_distribution& __x, |
| 5794 | const discrete_distribution& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5795 | {return !(__x == __y);} |
| 5796 | |
| 5797 | template <class _CharT, class _Traits, class _IT> |
| 5798 | friend |
| 5799 | basic_ostream<_CharT, _Traits>& |
| 5800 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5801 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5802 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5803 | template <class _CharT, class _Traits, class _IT> |
| 5804 | friend |
| 5805 | basic_istream<_CharT, _Traits>& |
| 5806 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5807 | discrete_distribution<_IT>& __x); |
| 5808 | }; |
| 5809 | |
| 5810 | template<class _IntType> |
| 5811 | template<class _UnaryOperation> |
| 5812 | discrete_distribution<_IntType>::param_type::param_type(size_t __nw, |
| 5813 | double __xmin, |
| 5814 | double __xmax, |
| 5815 | _UnaryOperation __fw) |
| 5816 | { |
| 5817 | if (__nw > 1) |
| 5818 | { |
| 5819 | __p_.reserve(__nw - 1); |
| 5820 | double __d = (__xmax - __xmin) / __nw; |
| 5821 | double __d2 = __d / 2; |
| 5822 | for (size_t __k = 0; __k < __nw; ++__k) |
| 5823 | __p_.push_back(__fw(__xmin + __k * __d + __d2)); |
| 5824 | __init(); |
| 5825 | } |
| 5826 | } |
| 5827 | |
| 5828 | template<class _IntType> |
| 5829 | void |
| 5830 | discrete_distribution<_IntType>::param_type::__init() |
| 5831 | { |
| 5832 | if (!__p_.empty()) |
| 5833 | { |
| 5834 | if (__p_.size() > 1) |
| 5835 | { |
| 5836 | double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0); |
| 5837 | for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); |
| 5838 | __i < __e; ++__i) |
| 5839 | *__i /= __s; |
| 5840 | vector<double> __t(__p_.size() - 1); |
| 5841 | _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); |
| 5842 | swap(__p_, __t); |
| 5843 | } |
| 5844 | else |
| 5845 | { |
| 5846 | __p_.clear(); |
| 5847 | __p_.shrink_to_fit(); |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | template<class _IntType> |
| 5853 | vector<double> |
| 5854 | discrete_distribution<_IntType>::param_type::probabilities() const |
| 5855 | { |
| 5856 | size_t __n = __p_.size(); |
| 5857 | _STD::vector<double> __p(__n+1); |
| 5858 | _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); |
| 5859 | if (__n > 0) |
| 5860 | __p[__n] = 1 - __p_[__n-1]; |
| 5861 | else |
| 5862 | __p[0] = 1; |
| 5863 | return __p; |
| 5864 | } |
| 5865 | |
| 5866 | template<class _IntType> |
| 5867 | template<class _URNG> |
| 5868 | _IntType |
| 5869 | discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 5870 | { |
| 5871 | uniform_real_distribution<double> __gen; |
| 5872 | return static_cast<_IntType>( |
| 5873 | _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - |
| 5874 | __p.__p_.begin()); |
| 5875 | } |
| 5876 | |
| 5877 | template <class _CharT, class _Traits, class _IT> |
| 5878 | basic_ostream<_CharT, _Traits>& |
| 5879 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5880 | const discrete_distribution<_IT>& __x) |
| 5881 | { |
| 5882 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5883 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5884 | ios_base::scientific); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5885 | _CharT __sp = __os.widen(' '); |
| 5886 | __os.fill(__sp); |
| 5887 | size_t __n = __x.__p_.__p_.size(); |
| 5888 | __os << __n; |
| 5889 | for (size_t __i = 0; __i < __n; ++__i) |
| 5890 | __os << __sp << __x.__p_.__p_[__i]; |
| 5891 | return __os; |
| 5892 | } |
| 5893 | |
| 5894 | template <class _CharT, class _Traits, class _IT> |
| 5895 | basic_istream<_CharT, _Traits>& |
| 5896 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5897 | discrete_distribution<_IT>& __x) |
| 5898 | { |
| 5899 | typedef discrete_distribution<_IT> _Eng; |
| 5900 | typedef typename _Eng::result_type result_type; |
| 5901 | typedef typename _Eng::param_type param_type; |
| 5902 | __save_flags<_CharT, _Traits> _(__is); |
| 5903 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5904 | size_t __n; |
| 5905 | __is >> __n; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5906 | vector<double> __p(__n); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5907 | for (size_t __i = 0; __i < __n; ++__i) |
| 5908 | __is >> __p[__i]; |
| 5909 | if (!__is.fail()) |
| 5910 | swap(__x.__p_.__p_, __p); |
| 5911 | return __is; |
| 5912 | } |
| 5913 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5914 | // piecewise_constant_distribution |
| 5915 | |
| 5916 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5917 | class _LIBCPP_VISIBLE piecewise_constant_distribution |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5918 | { |
| 5919 | public: |
| 5920 | // types |
| 5921 | typedef _RealType result_type; |
| 5922 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5923 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5924 | { |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5925 | vector<result_type> __b_; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 5926 | vector<result_type> __densities_; |
| 5927 | vector<result_type> __areas_; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5928 | public: |
| 5929 | typedef piecewise_constant_distribution distribution_type; |
| 5930 | |
| 5931 | param_type(); |
| 5932 | template<class _InputIteratorB, class _InputIteratorW> |
| 5933 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 5934 | _InputIteratorW __fW); |
| 5935 | template<class _UnaryOperation> |
| 5936 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
| 5937 | template<class _UnaryOperation> |
| 5938 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 5939 | _UnaryOperation __fw); |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 5940 | param_type & operator=(const param_type& __rhs); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5941 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5942 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5943 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5944 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 5945 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5946 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5947 | friend _LIBCPP_INLINE_VISIBILITY |
| 5948 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5949 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5950 | friend _LIBCPP_INLINE_VISIBILITY |
| 5951 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5952 | {return !(__x == __y);} |
| 5953 | |
| 5954 | private: |
| 5955 | void __init(); |
| 5956 | |
| 5957 | friend class piecewise_constant_distribution; |
| 5958 | |
| 5959 | template <class _CharT, class _Traits, class _RT> |
| 5960 | friend |
| 5961 | basic_ostream<_CharT, _Traits>& |
| 5962 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5963 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5964 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5965 | template <class _CharT, class _Traits, class _RT> |
| 5966 | friend |
| 5967 | basic_istream<_CharT, _Traits>& |
| 5968 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5969 | piecewise_constant_distribution<_RT>& __x); |
| 5970 | }; |
| 5971 | |
| 5972 | private: |
| 5973 | param_type __p_; |
| 5974 | |
| 5975 | public: |
| 5976 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5977 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5978 | piecewise_constant_distribution() {} |
| 5979 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5980 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5981 | piecewise_constant_distribution(_InputIteratorB __fB, |
| 5982 | _InputIteratorB __lB, |
| 5983 | _InputIteratorW __fW) |
| 5984 | : __p_(__fB, __lB, __fW) {} |
| 5985 | |
| 5986 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5987 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5988 | piecewise_constant_distribution(initializer_list<result_type> __bl, |
| 5989 | _UnaryOperation __fw) |
| 5990 | : __p_(__bl, __fw) {} |
| 5991 | |
| 5992 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5993 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5994 | piecewise_constant_distribution(size_t __nw, result_type __xmin, |
| 5995 | result_type __xmax, _UnaryOperation __fw) |
| 5996 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 5997 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5998 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 5999 | explicit piecewise_constant_distribution(const param_type& __p) |
| 6000 | : __p_(__p) {} |
| 6001 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6002 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6003 | void reset() {} |
| 6004 | |
| 6005 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6006 | template<class _URNG> |
| 6007 | _LIBCPP_INLINE_VISIBILITY |
| 6008 | result_type operator()(_URNG& __g) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6009 | {return (*this)(__g, __p_);} |
| 6010 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6011 | |
| 6012 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6013 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6014 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6015 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6016 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6017 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6018 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6019 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6020 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6021 | void param(const param_type& __p) {__p_ = __p;} |
| 6022 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6023 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6024 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6025 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6026 | result_type max() const {return __p_.__b_.back();} |
| 6027 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6028 | friend _LIBCPP_INLINE_VISIBILITY |
| 6029 | bool operator==(const piecewise_constant_distribution& __x, |
| 6030 | const piecewise_constant_distribution& __y) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6031 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6032 | friend _LIBCPP_INLINE_VISIBILITY |
| 6033 | bool operator!=(const piecewise_constant_distribution& __x, |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6034 | const piecewise_constant_distribution& __y) |
| 6035 | {return !(__x == __y);} |
| 6036 | |
| 6037 | template <class _CharT, class _Traits, class _RT> |
| 6038 | friend |
| 6039 | basic_ostream<_CharT, _Traits>& |
| 6040 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6041 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6042 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6043 | template <class _CharT, class _Traits, class _RT> |
| 6044 | friend |
| 6045 | basic_istream<_CharT, _Traits>& |
| 6046 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6047 | piecewise_constant_distribution<_RT>& __x); |
| 6048 | }; |
| 6049 | |
| 6050 | template<class _RealType> |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6051 | typename piecewise_constant_distribution<_RealType>::param_type & |
| 6052 | piecewise_constant_distribution<_RealType>::param_type::operator= |
| 6053 | (const param_type& __rhs) |
| 6054 | { |
| 6055 | // These can throw |
| 6056 | __b_.reserve (__rhs.__b_.size ()); |
| 6057 | __densities_.reserve(__rhs.__densities_.size()); |
| 6058 | __areas_.reserve (__rhs.__areas_.size()); |
| 6059 | |
| 6060 | // These can not throw |
| 6061 | __b_ = __rhs.__b_; |
| 6062 | __densities_ = __rhs.__densities_; |
| 6063 | __areas_ = __rhs.__areas_; |
| 6064 | return *this; |
| 6065 | } |
| 6066 | |
| 6067 | template<class _RealType> |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6068 | void |
| 6069 | piecewise_constant_distribution<_RealType>::param_type::__init() |
| 6070 | { |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6071 | // __densities_ contains non-normalized areas |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6072 | result_type __total_area = _STD::accumulate(__densities_.begin(), |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6073 | __densities_.end(), |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6074 | result_type()); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6075 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6076 | __densities_[__i] /= __total_area; |
| 6077 | // __densities_ contains normalized areas |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6078 | __areas_.assign(__densities_.size(), result_type()); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6079 | _STD::partial_sum(__densities_.begin(), __densities_.end() - 1, |
| 6080 | __areas_.begin() + 1); |
| 6081 | // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1] |
| 6082 | __densities_.back() = 1 - __areas_.back(); // correct round off error |
| 6083 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6084 | __densities_[__i] /= (__b_[__i+1] - __b_[__i]); |
| 6085 | // __densities_ now contains __densities_ |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | template<class _RealType> |
| 6089 | piecewise_constant_distribution<_RealType>::param_type::param_type() |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6090 | : __b_(2), |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6091 | __densities_(1, 1.0), |
| 6092 | __areas_(1, 0.0) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6093 | { |
| 6094 | __b_[1] = 1; |
| 6095 | } |
| 6096 | |
| 6097 | template<class _RealType> |
| 6098 | template<class _InputIteratorB, class _InputIteratorW> |
| 6099 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6100 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6101 | : __b_(__fB, __lB) |
| 6102 | { |
| 6103 | if (__b_.size() < 2) |
| 6104 | { |
| 6105 | __b_.resize(2); |
| 6106 | __b_[0] = 0; |
| 6107 | __b_[1] = 1; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6108 | __densities_.assign(1, 1.0); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6109 | __areas_.assign(1, 0.0); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6110 | } |
| 6111 | else |
| 6112 | { |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6113 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6114 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6115 | __densities_.push_back(*__fW); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6116 | __init(); |
| 6117 | } |
| 6118 | } |
| 6119 | |
| 6120 | template<class _RealType> |
| 6121 | template<class _UnaryOperation> |
| 6122 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6123 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6124 | : __b_(__bl.begin(), __bl.end()) |
| 6125 | { |
| 6126 | if (__b_.size() < 2) |
| 6127 | { |
| 6128 | __b_.resize(2); |
| 6129 | __b_[0] = 0; |
| 6130 | __b_[1] = 1; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6131 | __densities_.assign(1, 1.0); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6132 | __areas_.assign(1, 0.0); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6133 | } |
| 6134 | else |
| 6135 | { |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6136 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6137 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6138 | __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5)); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6139 | __init(); |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | template<class _RealType> |
| 6144 | template<class _UnaryOperation> |
| 6145 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6146 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6147 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6148 | { |
| 6149 | size_t __n = __b_.size() - 1; |
| 6150 | result_type __d = (__xmax - __xmin) / __n; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6151 | __densities_.reserve(__n); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6152 | for (size_t __i = 0; __i < __n; ++__i) |
| 6153 | { |
| 6154 | __b_[__i] = __xmin + __i * __d; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6155 | __densities_.push_back(__fw(__b_[__i] + __d*.5)); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6156 | } |
| 6157 | __b_[__n] = __xmax; |
| 6158 | __init(); |
| 6159 | } |
| 6160 | |
| 6161 | template<class _RealType> |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6162 | template<class _URNG> |
| 6163 | _RealType |
| 6164 | piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6165 | { |
| 6166 | typedef uniform_real_distribution<result_type> _Gen; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6167 | result_type __u = _Gen()(__g); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6168 | ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6169 | __u) - __p.__areas_.begin() - 1; |
| 6170 | return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k]; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | template <class _CharT, class _Traits, class _RT> |
| 6174 | basic_ostream<_CharT, _Traits>& |
| 6175 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6176 | const piecewise_constant_distribution<_RT>& __x) |
| 6177 | { |
| 6178 | __save_flags<_CharT, _Traits> _(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6179 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6180 | ios_base::scientific); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6181 | _CharT __sp = __os.widen(' '); |
| 6182 | __os.fill(__sp); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6183 | size_t __n = __x.__p_.__b_.size(); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6184 | __os << __n; |
| 6185 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6186 | __os << __sp << __x.__p_.__b_[__i]; |
| 6187 | __n = __x.__p_.__densities_.size(); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6188 | __os << __sp << __n; |
| 6189 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6190 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6191 | __n = __x.__p_.__areas_.size(); |
| 6192 | __os << __sp << __n; |
| 6193 | for (size_t __i = 0; __i < __n; ++__i) |
| 6194 | __os << __sp << __x.__p_.__areas_[__i]; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6195 | return __os; |
| 6196 | } |
| 6197 | |
| 6198 | template <class _CharT, class _Traits, class _RT> |
| 6199 | basic_istream<_CharT, _Traits>& |
| 6200 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6201 | piecewise_constant_distribution<_RT>& __x) |
| 6202 | { |
| 6203 | typedef piecewise_constant_distribution<_RT> _Eng; |
| 6204 | typedef typename _Eng::result_type result_type; |
| 6205 | typedef typename _Eng::param_type param_type; |
| 6206 | __save_flags<_CharT, _Traits> _(__is); |
| 6207 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6208 | size_t __n; |
| 6209 | __is >> __n; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6210 | vector<result_type> __b(__n); |
| 6211 | for (size_t __i = 0; __i < __n; ++__i) |
| 6212 | __is >> __b[__i]; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6213 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6214 | vector<result_type> __densities(__n); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6215 | for (size_t __i = 0; __i < __n; ++__i) |
| 6216 | __is >> __densities[__i]; |
| 6217 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6218 | vector<result_type> __areas(__n); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6219 | for (size_t __i = 0; __i < __n; ++__i) |
| 6220 | __is >> __areas[__i]; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6221 | if (!__is.fail()) |
| 6222 | { |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6223 | swap(__x.__p_.__b_, __b); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6224 | swap(__x.__p_.__densities_, __densities); |
| 6225 | swap(__x.__p_.__areas_, __areas); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6226 | } |
| 6227 | return __is; |
| 6228 | } |
| 6229 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6230 | // piecewise_linear_distribution |
| 6231 | |
| 6232 | template<class _RealType = double> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6233 | class _LIBCPP_VISIBLE piecewise_linear_distribution |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6234 | { |
| 6235 | public: |
| 6236 | // types |
| 6237 | typedef _RealType result_type; |
| 6238 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6239 | class _LIBCPP_VISIBLE param_type |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6240 | { |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6241 | vector<result_type> __b_; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6242 | vector<result_type> __densities_; |
| 6243 | vector<result_type> __areas_; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6244 | public: |
| 6245 | typedef piecewise_linear_distribution distribution_type; |
| 6246 | |
| 6247 | param_type(); |
| 6248 | template<class _InputIteratorB, class _InputIteratorW> |
| 6249 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6250 | _InputIteratorW __fW); |
| 6251 | template<class _UnaryOperation> |
| 6252 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
| 6253 | template<class _UnaryOperation> |
| 6254 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6255 | _UnaryOperation __fw); |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6256 | param_type & operator=(const param_type& __rhs); |
| 6257 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6258 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6259 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6260 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6261 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6262 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6263 | friend _LIBCPP_INLINE_VISIBILITY |
| 6264 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6265 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6266 | friend _LIBCPP_INLINE_VISIBILITY |
| 6267 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6268 | {return !(__x == __y);} |
| 6269 | |
| 6270 | private: |
| 6271 | void __init(); |
| 6272 | |
| 6273 | friend class piecewise_linear_distribution; |
| 6274 | |
| 6275 | template <class _CharT, class _Traits, class _RT> |
| 6276 | friend |
| 6277 | basic_ostream<_CharT, _Traits>& |
| 6278 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6279 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6280 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6281 | template <class _CharT, class _Traits, class _RT> |
| 6282 | friend |
| 6283 | basic_istream<_CharT, _Traits>& |
| 6284 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6285 | piecewise_linear_distribution<_RT>& __x); |
| 6286 | }; |
| 6287 | |
| 6288 | private: |
| 6289 | param_type __p_; |
| 6290 | |
| 6291 | public: |
| 6292 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6293 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6294 | piecewise_linear_distribution() {} |
| 6295 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6296 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6297 | piecewise_linear_distribution(_InputIteratorB __fB, |
| 6298 | _InputIteratorB __lB, |
| 6299 | _InputIteratorW __fW) |
| 6300 | : __p_(__fB, __lB, __fW) {} |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6301 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6302 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6304 | piecewise_linear_distribution(initializer_list<result_type> __bl, |
| 6305 | _UnaryOperation __fw) |
| 6306 | : __p_(__bl, __fw) {} |
| 6307 | |
| 6308 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6309 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6310 | piecewise_linear_distribution(size_t __nw, result_type __xmin, |
| 6311 | result_type __xmax, _UnaryOperation __fw) |
| 6312 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6313 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6314 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6315 | explicit piecewise_linear_distribution(const param_type& __p) |
| 6316 | : __p_(__p) {} |
| 6317 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6318 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6319 | void reset() {} |
| 6320 | |
| 6321 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6322 | template<class _URNG> |
| 6323 | _LIBCPP_INLINE_VISIBILITY |
| 6324 | result_type operator()(_URNG& __g) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6325 | {return (*this)(__g, __p_);} |
| 6326 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6327 | |
| 6328 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6329 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6330 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6331 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6332 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6333 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6334 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6335 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6336 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6337 | void param(const param_type& __p) {__p_ = __p;} |
| 6338 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6339 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6340 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6341 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6342 | result_type max() const {return __p_.__b_.back();} |
| 6343 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6344 | friend _LIBCPP_INLINE_VISIBILITY |
| 6345 | bool operator==(const piecewise_linear_distribution& __x, |
| 6346 | const piecewise_linear_distribution& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6347 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6348 | friend _LIBCPP_INLINE_VISIBILITY |
| 6349 | bool operator!=(const piecewise_linear_distribution& __x, |
| 6350 | const piecewise_linear_distribution& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6351 | {return !(__x == __y);} |
| 6352 | |
| 6353 | template <class _CharT, class _Traits, class _RT> |
| 6354 | friend |
| 6355 | basic_ostream<_CharT, _Traits>& |
| 6356 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6357 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6358 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6359 | template <class _CharT, class _Traits, class _RT> |
| 6360 | friend |
| 6361 | basic_istream<_CharT, _Traits>& |
| 6362 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6363 | piecewise_linear_distribution<_RT>& __x); |
| 6364 | }; |
| 6365 | |
| 6366 | template<class _RealType> |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6367 | typename piecewise_linear_distribution<_RealType>::param_type & |
| 6368 | piecewise_linear_distribution<_RealType>::param_type::operator= |
| 6369 | (const param_type& __rhs) |
| 6370 | { |
| 6371 | // These can throw |
| 6372 | __b_.reserve (__rhs.__b_.size ()); |
| 6373 | __densities_.reserve(__rhs.__densities_.size()); |
| 6374 | __areas_.reserve (__rhs.__areas_.size()); |
| 6375 | |
| 6376 | // These can not throw |
| 6377 | __b_ = __rhs.__b_; |
| 6378 | __densities_ = __rhs.__densities_; |
| 6379 | __areas_ = __rhs.__areas_; |
| 6380 | return *this; |
| 6381 | } |
| 6382 | |
| 6383 | |
| 6384 | template<class _RealType> |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6385 | void |
| 6386 | piecewise_linear_distribution<_RealType>::param_type::__init() |
| 6387 | { |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6388 | __areas_.assign(__densities_.size() - 1, result_type()); |
| 6389 | result_type _S = 0; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6390 | for (size_t __i = 0; __i < __areas_.size(); ++__i) |
| 6391 | { |
| 6392 | __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) * |
| 6393 | (__b_[__i+1] - __b_[__i]) * .5; |
| 6394 | _S += __areas_[__i]; |
| 6395 | } |
| 6396 | for (size_t __i = __areas_.size(); __i > 1;) |
| 6397 | { |
| 6398 | --__i; |
| 6399 | __areas_[__i] = __areas_[__i-1] / _S; |
| 6400 | } |
| 6401 | __areas_[0] = 0; |
| 6402 | for (size_t __i = 1; __i < __areas_.size(); ++__i) |
| 6403 | __areas_[__i] += __areas_[__i-1]; |
| 6404 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6405 | __densities_[__i] /= _S; |
| 6406 | } |
| 6407 | |
| 6408 | template<class _RealType> |
| 6409 | piecewise_linear_distribution<_RealType>::param_type::param_type() |
| 6410 | : __b_(2), |
| 6411 | __densities_(2, 1.0), |
| 6412 | __areas_(1, 0.0) |
| 6413 | { |
| 6414 | __b_[1] = 1; |
| 6415 | } |
| 6416 | |
| 6417 | template<class _RealType> |
| 6418 | template<class _InputIteratorB, class _InputIteratorW> |
| 6419 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6420 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6421 | : __b_(__fB, __lB) |
| 6422 | { |
| 6423 | if (__b_.size() < 2) |
| 6424 | { |
| 6425 | __b_.resize(2); |
| 6426 | __b_[0] = 0; |
| 6427 | __b_[1] = 1; |
| 6428 | __densities_.assign(2, 1.0); |
| 6429 | __areas_.assign(1, 0.0); |
| 6430 | } |
| 6431 | else |
| 6432 | { |
| 6433 | __densities_.reserve(__b_.size()); |
| 6434 | for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW) |
| 6435 | __densities_.push_back(*__fW); |
| 6436 | __init(); |
| 6437 | } |
| 6438 | } |
| 6439 | |
| 6440 | template<class _RealType> |
| 6441 | template<class _UnaryOperation> |
| 6442 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6443 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6444 | : __b_(__bl.begin(), __bl.end()) |
| 6445 | { |
| 6446 | if (__b_.size() < 2) |
| 6447 | { |
| 6448 | __b_.resize(2); |
| 6449 | __b_[0] = 0; |
| 6450 | __b_[1] = 1; |
| 6451 | __densities_.assign(2, 1.0); |
| 6452 | __areas_.assign(1, 0.0); |
| 6453 | } |
| 6454 | else |
| 6455 | { |
| 6456 | __densities_.reserve(__b_.size()); |
| 6457 | for (size_t __i = 0; __i < __b_.size(); ++__i) |
| 6458 | __densities_.push_back(__fw(__b_[__i])); |
| 6459 | __init(); |
| 6460 | } |
| 6461 | } |
| 6462 | |
| 6463 | template<class _RealType> |
| 6464 | template<class _UnaryOperation> |
| 6465 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6466 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6467 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6468 | { |
| 6469 | size_t __n = __b_.size() - 1; |
| 6470 | result_type __d = (__xmax - __xmin) / __n; |
| 6471 | __densities_.reserve(__b_.size()); |
| 6472 | for (size_t __i = 0; __i < __n; ++__i) |
| 6473 | { |
| 6474 | __b_[__i] = __xmin + __i * __d; |
| 6475 | __densities_.push_back(__fw(__b_[__i])); |
| 6476 | } |
| 6477 | __b_[__n] = __xmax; |
| 6478 | __densities_.push_back(__fw(__b_[__n])); |
| 6479 | __init(); |
| 6480 | } |
| 6481 | |
| 6482 | template<class _RealType> |
| 6483 | template<class _URNG> |
| 6484 | _RealType |
| 6485 | piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6486 | { |
| 6487 | typedef uniform_real_distribution<result_type> _Gen; |
| 6488 | result_type __u = _Gen()(__g); |
| 6489 | ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6490 | __u) - __p.__areas_.begin() - 1; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6491 | __u -= __p.__areas_[__k]; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6492 | const result_type __dk = __p.__densities_[__k]; |
| 6493 | const result_type __dk1 = __p.__densities_[__k+1]; |
| 6494 | const result_type __deltad = __dk1 - __dk; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6495 | const result_type __bk = __p.__b_[__k]; |
| 6496 | if (__deltad == 0) |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6497 | return __u / __dk + __bk; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6498 | const result_type __bk1 = __p.__b_[__k+1]; |
| 6499 | const result_type __deltab = __bk1 - __bk; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6500 | return (__bk * __dk1 - __bk1 * __dk + |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6501 | _STD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6502 | __deltad; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | template <class _CharT, class _Traits, class _RT> |
| 6506 | basic_ostream<_CharT, _Traits>& |
| 6507 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6508 | const piecewise_linear_distribution<_RT>& __x) |
| 6509 | { |
| 6510 | __save_flags<_CharT, _Traits> _(__os); |
| 6511 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6512 | ios_base::scientific); |
| 6513 | _CharT __sp = __os.widen(' '); |
| 6514 | __os.fill(__sp); |
| 6515 | size_t __n = __x.__p_.__b_.size(); |
| 6516 | __os << __n; |
| 6517 | for (size_t __i = 0; __i < __n; ++__i) |
| 6518 | __os << __sp << __x.__p_.__b_[__i]; |
| 6519 | __n = __x.__p_.__densities_.size(); |
| 6520 | __os << __sp << __n; |
| 6521 | for (size_t __i = 0; __i < __n; ++__i) |
| 6522 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6523 | __n = __x.__p_.__areas_.size(); |
| 6524 | __os << __sp << __n; |
| 6525 | for (size_t __i = 0; __i < __n; ++__i) |
| 6526 | __os << __sp << __x.__p_.__areas_[__i]; |
| 6527 | return __os; |
| 6528 | } |
| 6529 | |
| 6530 | template <class _CharT, class _Traits, class _RT> |
| 6531 | basic_istream<_CharT, _Traits>& |
| 6532 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6533 | piecewise_linear_distribution<_RT>& __x) |
| 6534 | { |
| 6535 | typedef piecewise_linear_distribution<_RT> _Eng; |
| 6536 | typedef typename _Eng::result_type result_type; |
| 6537 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6538 | __save_flags<_CharT, _Traits> _(__is); |
| 6539 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6540 | size_t __n; |
| 6541 | __is >> __n; |
| 6542 | vector<result_type> __b(__n); |
| 6543 | for (size_t __i = 0; __i < __n; ++__i) |
| 6544 | __is >> __b[__i]; |
| 6545 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6546 | vector<result_type> __densities(__n); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6547 | for (size_t __i = 0; __i < __n; ++__i) |
| 6548 | __is >> __densities[__i]; |
| 6549 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame^] | 6550 | vector<result_type> __areas(__n); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6551 | for (size_t __i = 0; __i < __n; ++__i) |
| 6552 | __is >> __areas[__i]; |
| 6553 | if (!__is.fail()) |
| 6554 | { |
| 6555 | swap(__x.__p_.__b_, __b); |
| 6556 | swap(__x.__p_.__densities_, __densities); |
| 6557 | swap(__x.__p_.__areas_, __areas); |
| 6558 | } |
| 6559 | return __is; |
| 6560 | } |
| 6561 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6562 | _LIBCPP_END_NAMESPACE_STD |
| 6563 | |
| 6564 | #endif // _LIBCPP_RANDOM |