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