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