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> |
Eric Fiselier | f51d676 | 2015-01-23 22:22:36 +0000 | [diff] [blame] | 1637 | #include <cstdint> |
| 1638 | #include <cmath> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1639 | #include <type_traits> |
| 1640 | #include <initializer_list> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1641 | #include <limits> |
| 1642 | #include <algorithm> |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1643 | #include <numeric> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1644 | #include <vector> |
| 1645 | #include <string> |
| 1646 | #include <istream> |
| 1647 | #include <ostream> |
| 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1816 | class _LIBCPP_TEMPLATE_VIS 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1832 | class _LIBCPP_TEMPLATE_VIS 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2014 | class _LIBCPP_TEMPLATE_VIS 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2056 | class _LIBCPP_TEMPLATE_VIS 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2502 | class _LIBCPP_TEMPLATE_VIS 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2530 | class _LIBCPP_TEMPLATE_VIS 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> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2813 | class _LIBCPP_TEMPLATE_VIS 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"); |
Eric Fiselier | 50f6579 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2820 | static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters"); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2821 | public: |
| 2822 | // types |
| 2823 | typedef typename _Engine::result_type result_type; |
| 2824 | |
| 2825 | // engine characteristics |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2826 | static _LIBCPP_CONSTEXPR const size_t block_size = __p; |
| 2827 | static _LIBCPP_CONSTEXPR const size_t used_block = __r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2828 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2829 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2830 | static const result_type _Min = _Engine::_Min; |
| 2831 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2832 | #else |
| 2833 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 2834 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 2835 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2836 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2837 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2838 | static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2839 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2840 | static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2841 | |
| 2842 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2843 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2844 | discard_block_engine() : __n_(0) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2845 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2846 | explicit discard_block_engine(const _Engine& __e) |
| 2847 | : __e_(__e), __n_(0) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2848 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2849 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2850 | explicit discard_block_engine(_Engine&& __e) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2851 | : __e_(_VSTD::move(__e)), __n_(0) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2852 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2853 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2854 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2855 | template<class _Sseq> |
| 2856 | _LIBCPP_INLINE_VISIBILITY |
| 2857 | explicit discard_block_engine(_Sseq& __q, |
Howard Hinnant | ef3b2e2 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2858 | typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value && |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2859 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2860 | : __e_(__q), __n_(0) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2861 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2862 | void seed() {__e_.seed(); __n_ = 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2863 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2864 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2865 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2866 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2867 | typename enable_if |
| 2868 | < |
Howard Hinnant | ef3b2e2 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2869 | __is_seed_sequence<_Sseq, discard_block_engine>::value, |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2870 | void |
| 2871 | >::type |
| 2872 | seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2873 | |
| 2874 | // generating functions |
| 2875 | result_type operator()(); |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2876 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2877 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2878 | |
| 2879 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2880 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c83960a | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 2881 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2882 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2883 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2884 | friend |
| 2885 | bool |
| 2886 | operator==( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2887 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2888 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2889 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2890 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2891 | friend |
| 2892 | bool |
| 2893 | operator!=( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2894 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2895 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2896 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2897 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2898 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2899 | friend |
| 2900 | basic_ostream<_CharT, _Traits>& |
| 2901 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2902 | const discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2903 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2904 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2905 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2906 | friend |
| 2907 | basic_istream<_CharT, _Traits>& |
| 2908 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2909 | discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2910 | }; |
| 2911 | |
| 2912 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | 0a69fa1 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2913 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size; |
| 2914 | |
| 2915 | template<class _Engine, size_t __p, size_t __r> |
| 2916 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block; |
| 2917 | |
| 2918 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2919 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 2920 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 2921 | { |
Eric Fiselier | 50f6579 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2922 | if (__n_ >= static_cast<int>(__r)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2923 | { |
| 2924 | __e_.discard(__p - __r); |
| 2925 | __n_ = 0; |
| 2926 | } |
| 2927 | ++__n_; |
| 2928 | return __e_(); |
| 2929 | } |
| 2930 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2931 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2932 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2933 | bool |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2934 | operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2935 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2936 | { |
| 2937 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 2938 | } |
| 2939 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2940 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2941 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2942 | bool |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2943 | operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2944 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2945 | { |
| 2946 | return !(__x == __y); |
| 2947 | } |
| 2948 | |
| 2949 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2950 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2951 | basic_ostream<_CharT, _Traits>& |
| 2952 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2953 | const discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2954 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2955 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2956 | __os.flags(ios_base::dec | ios_base::left); |
| 2957 | _CharT __sp = __os.widen(' '); |
| 2958 | __os.fill(__sp); |
| 2959 | return __os << __x.__e_ << __sp << __x.__n_; |
| 2960 | } |
| 2961 | |
| 2962 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2963 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2964 | basic_istream<_CharT, _Traits>& |
| 2965 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2966 | discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2967 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2968 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2969 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2970 | _Eng __e; |
| 2971 | int __n; |
| 2972 | __is >> __e >> __n; |
| 2973 | if (!__is.fail()) |
| 2974 | { |
| 2975 | __x.__e_ = __e; |
| 2976 | __x.__n_ = __n; |
| 2977 | } |
| 2978 | return __is; |
| 2979 | } |
| 2980 | |
| 2981 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 2982 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 2983 | |
| 2984 | // independent_bits_engine |
| 2985 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2986 | template<class _Engine, size_t __w, class _UIntType> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2987 | class _LIBCPP_TEMPLATE_VIS independent_bits_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2988 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2989 | template <class _UI, _UI _R0, size_t _Wp, size_t _Mp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2990 | class __get_n |
| 2991 | { |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2992 | static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits; |
| 2993 | static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0); |
| 2994 | static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np; |
| 2995 | static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2996 | public: |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2997 | 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] | 2998 | }; |
| 2999 | public: |
| 3000 | // types |
| 3001 | typedef _UIntType result_type; |
| 3002 | |
| 3003 | private: |
| 3004 | _Engine __e_; |
| 3005 | |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3006 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3007 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 3008 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 3009 | |
| 3010 | typedef typename _Engine::result_type _Engine_result_type; |
| 3011 | typedef typename conditional |
| 3012 | < |
| 3013 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 3014 | result_type, |
| 3015 | _Engine_result_type |
| 3016 | >::type _Working_result_type; |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3017 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3018 | static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3019 | + _Working_result_type(1); |
| 3020 | #else |
| 3021 | static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() |
| 3022 | + _Working_result_type(1); |
| 3023 | #endif |
| 3024 | static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value; |
| 3025 | static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value; |
| 3026 | static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n; |
| 3027 | static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n; |
| 3028 | static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 3029 | static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 3030 | static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 3031 | (_Rp >> __w0) << __w0; |
| 3032 | static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 3033 | (_Rp >> (__w0+1)) << (__w0+1); |
| 3034 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ? |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3035 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 3036 | _Engine_result_type(0); |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3037 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3038 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 3039 | _Engine_result_type(~0); |
| 3040 | public: |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3041 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3042 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 3043 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3044 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 3045 | |
| 3046 | // engine characteristics |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3047 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3048 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3049 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3050 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3051 | |
| 3052 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3053 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3054 | independent_bits_engine() {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3055 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3056 | explicit independent_bits_engine(const _Engine& __e) |
| 3057 | : __e_(__e) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3058 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3059 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3060 | explicit independent_bits_engine(_Engine&& __e) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3061 | : __e_(_VSTD::move(__e)) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3062 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3063 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3064 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3065 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3066 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3067 | explicit independent_bits_engine(_Sseq& __q, |
Howard Hinnant | ef3b2e2 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3068 | typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value && |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3069 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3070 | : __e_(__q) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3071 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3072 | void seed() {__e_.seed();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3073 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3074 | void seed(result_type __sd) {__e_.seed(__sd);} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3075 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3076 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3077 | typename enable_if |
| 3078 | < |
Howard Hinnant | ef3b2e2 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3079 | __is_seed_sequence<_Sseq, independent_bits_engine>::value, |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3080 | void |
| 3081 | >::type |
| 3082 | seed(_Sseq& __q) {__e_.seed(__q);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3083 | |
| 3084 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3085 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3086 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3087 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3088 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3089 | |
| 3090 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3091 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c83960a | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3092 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3093 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3094 | template<class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3095 | friend |
| 3096 | bool |
| 3097 | operator==( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3098 | const independent_bits_engine<_Eng, _Wp, _UI>& __x, |
| 3099 | const independent_bits_engine<_Eng, _Wp, _UI>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3100 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3101 | template<class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3102 | friend |
| 3103 | bool |
| 3104 | operator!=( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3105 | const independent_bits_engine<_Eng, _Wp, _UI>& __x, |
| 3106 | const independent_bits_engine<_Eng, _Wp, _UI>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3107 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3108 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3109 | class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3110 | friend |
| 3111 | basic_ostream<_CharT, _Traits>& |
| 3112 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3113 | const independent_bits_engine<_Eng, _Wp, _UI>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3114 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3115 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3116 | class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3117 | friend |
| 3118 | basic_istream<_CharT, _Traits>& |
| 3119 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3120 | independent_bits_engine<_Eng, _Wp, _UI>& __x); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3121 | |
| 3122 | private: |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3123 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3124 | result_type __eval(false_type); |
| 3125 | result_type __eval(true_type); |
| 3126 | |
| 3127 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3128 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3129 | static |
| 3130 | typename enable_if |
| 3131 | < |
| 3132 | __count < _Dt, |
| 3133 | result_type |
| 3134 | >::type |
| 3135 | __lshift(result_type __x) {return __x << __count;} |
| 3136 | |
| 3137 | template <size_t __count> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3138 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3139 | static |
| 3140 | typename enable_if |
| 3141 | < |
| 3142 | (__count >= _Dt), |
| 3143 | result_type |
| 3144 | >::type |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3145 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3146 | }; |
| 3147 | |
| 3148 | template<class _Engine, size_t __w, class _UIntType> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3149 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3150 | _UIntType |
| 3151 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
| 3152 | { |
| 3153 | return static_cast<result_type>(__e_() & __mask0); |
| 3154 | } |
| 3155 | |
| 3156 | template<class _Engine, size_t __w, class _UIntType> |
| 3157 | _UIntType |
| 3158 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
| 3159 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3160 | result_type _Sp = 0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3161 | for (size_t __k = 0; __k < __n0; ++__k) |
| 3162 | { |
| 3163 | _Engine_result_type __u; |
| 3164 | do |
| 3165 | { |
| 3166 | __u = __e_() - _Engine::min(); |
| 3167 | } while (__u >= __y0); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3168 | _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3169 | } |
| 3170 | for (size_t __k = __n0; __k < __n; ++__k) |
| 3171 | { |
| 3172 | _Engine_result_type __u; |
| 3173 | do |
| 3174 | { |
| 3175 | __u = __e_() - _Engine::min(); |
| 3176 | } while (__u >= __y1); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3177 | _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3178 | } |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3179 | return _Sp; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3182 | template<class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3183 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3184 | bool |
| 3185 | operator==( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3186 | const independent_bits_engine<_Eng, _Wp, _UI>& __x, |
| 3187 | const independent_bits_engine<_Eng, _Wp, _UI>& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3188 | { |
| 3189 | return __x.base() == __y.base(); |
| 3190 | } |
| 3191 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3192 | template<class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3193 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3194 | bool |
| 3195 | operator!=( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3196 | const independent_bits_engine<_Eng, _Wp, _UI>& __x, |
| 3197 | const independent_bits_engine<_Eng, _Wp, _UI>& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3198 | { |
| 3199 | return !(__x == __y); |
| 3200 | } |
| 3201 | |
| 3202 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3203 | class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3204 | basic_ostream<_CharT, _Traits>& |
| 3205 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3206 | const independent_bits_engine<_Eng, _Wp, _UI>& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3207 | { |
| 3208 | return __os << __x.base(); |
| 3209 | } |
| 3210 | |
| 3211 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3212 | class _Eng, size_t _Wp, class _UI> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3213 | basic_istream<_CharT, _Traits>& |
| 3214 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3215 | independent_bits_engine<_Eng, _Wp, _UI>& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3216 | { |
| 3217 | _Eng __e; |
| 3218 | __is >> __e; |
| 3219 | if (!__is.fail()) |
| 3220 | __x.__e_ = __e; |
| 3221 | return __is; |
| 3222 | } |
| 3223 | |
| 3224 | // shuffle_order_engine |
| 3225 | |
| 3226 | template <uint64_t _Xp, uint64_t _Yp> |
| 3227 | struct __ugcd |
| 3228 | { |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3229 | static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3230 | }; |
| 3231 | |
| 3232 | template <uint64_t _Xp> |
| 3233 | struct __ugcd<_Xp, 0> |
| 3234 | { |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3235 | static _LIBCPP_CONSTEXPR const uint64_t value = _Xp; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3236 | }; |
| 3237 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3238 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3239 | class __uratio |
| 3240 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3241 | static_assert(_Dp != 0, "__uratio divide by 0"); |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3242 | static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3243 | public: |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3244 | static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd; |
| 3245 | static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3246 | |
| 3247 | typedef __uratio<num, den> type; |
| 3248 | }; |
| 3249 | |
| 3250 | template<class _Engine, size_t __k> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3251 | class _LIBCPP_TEMPLATE_VIS shuffle_order_engine |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3252 | { |
| 3253 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 3254 | public: |
| 3255 | // types |
| 3256 | typedef typename _Engine::result_type result_type; |
| 3257 | |
| 3258 | private: |
| 3259 | _Engine __e_; |
| 3260 | result_type _V_[__k]; |
| 3261 | result_type _Y_; |
| 3262 | |
| 3263 | public: |
| 3264 | // engine characteristics |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3265 | static _LIBCPP_CONSTEXPR const size_t table_size = __k; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3266 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3267 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3268 | static const result_type _Min = _Engine::_Min; |
| 3269 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3270 | #else |
| 3271 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 3272 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 3273 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3274 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
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 min() { return _Min; } |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3277 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3278 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3279 | |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3280 | static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3281 | |
| 3282 | // constructors and seeding functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3283 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3284 | shuffle_order_engine() {__init();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3285 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3286 | explicit shuffle_order_engine(const _Engine& __e) |
| 3287 | : __e_(__e) {__init();} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3288 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3289 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3290 | explicit shuffle_order_engine(_Engine&& __e) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3291 | : __e_(_VSTD::move(__e)) {__init();} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3292 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3293 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3294 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3295 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3296 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3297 | explicit shuffle_order_engine(_Sseq& __q, |
Howard Hinnant | ef3b2e2 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3298 | typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value && |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3299 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3300 | : __e_(__q) {__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() {__e_.seed(); __init();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3304 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3305 | template<class _Sseq> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3306 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3307 | typename enable_if |
| 3308 | < |
Howard Hinnant | ef3b2e2 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3309 | __is_seed_sequence<_Sseq, shuffle_order_engine>::value, |
Howard Hinnant | 3ec3184 | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3310 | void |
| 3311 | >::type |
| 3312 | seed(_Sseq& __q) {__e_.seed(__q); __init();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3313 | |
| 3314 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3315 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3316 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3317 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3318 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3319 | |
| 3320 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3321 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c83960a | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3322 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3323 | |
| 3324 | private: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3325 | template<class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3326 | friend |
| 3327 | bool |
| 3328 | operator==( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3329 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3330 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3331 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3332 | template<class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3333 | friend |
| 3334 | bool |
| 3335 | operator!=( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3336 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3337 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3338 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3339 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3340 | class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3341 | friend |
| 3342 | basic_ostream<_CharT, _Traits>& |
| 3343 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3344 | const shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3345 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3346 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3347 | class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3348 | friend |
| 3349 | basic_istream<_CharT, _Traits>& |
| 3350 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3351 | shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3352 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3353 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3354 | void __init() |
| 3355 | { |
| 3356 | for (size_t __i = 0; __i < __k; ++__i) |
| 3357 | _V_[__i] = __e_(); |
| 3358 | _Y_ = __e_(); |
| 3359 | } |
| 3360 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3361 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3362 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3363 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3364 | result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3365 | |
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(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3368 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3369 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 3370 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3371 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3372 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3373 | typename enable_if |
| 3374 | < |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3375 | (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3376 | result_type |
| 3377 | >::type |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3378 | __eval(__uratio<_Np, _Dp>) |
| 3379 | {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3380 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3381 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3382 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3383 | typename enable_if |
| 3384 | < |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3385 | __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3386 | result_type |
| 3387 | >::type |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3388 | __eval(__uratio<_Np, _Dp>) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3389 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3390 | const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min) |
| 3391 | / __uratio<_Np, _Dp>::den); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3392 | _Y_ = _V_[__j]; |
| 3393 | _V_[__j] = __e_(); |
| 3394 | return _Y_; |
| 3395 | } |
| 3396 | |
| 3397 | template <uint64_t __n, uint64_t __d> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3398 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3399 | result_type __evalf() |
| 3400 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3401 | const double _Fp = __d == 0 ? |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3402 | __n / (2. * 0x8000000000000000ull) : |
| 3403 | __n / (double)__d; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3404 | const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3405 | _Y_ = _V_[__j]; |
| 3406 | _V_[__j] = __e_(); |
| 3407 | return _Y_; |
| 3408 | } |
| 3409 | }; |
| 3410 | |
Howard Hinnant | 0a69fa1 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 3411 | template<class _Engine, size_t __k> |
| 3412 | _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size; |
| 3413 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3414 | template<class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3415 | bool |
| 3416 | operator==( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3417 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3418 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3419 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3420 | 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] | 3421 | __x.__e_ == __y.__e_; |
| 3422 | } |
| 3423 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3424 | template<class _Eng, size_t _Kp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3425 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3426 | bool |
| 3427 | operator!=( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3428 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3429 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3430 | { |
| 3431 | return !(__x == __y); |
| 3432 | } |
| 3433 | |
| 3434 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3435 | class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3436 | basic_ostream<_CharT, _Traits>& |
| 3437 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3438 | const shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3439 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3440 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3441 | __os.flags(ios_base::dec | ios_base::left); |
| 3442 | _CharT __sp = __os.widen(' '); |
| 3443 | __os.fill(__sp); |
| 3444 | __os << __x.__e_ << __sp << __x._V_[0]; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3445 | for (size_t __i = 1; __i < _Kp; ++__i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3446 | __os << __sp << __x._V_[__i]; |
| 3447 | return __os << __sp << __x._Y_; |
| 3448 | } |
| 3449 | |
| 3450 | template <class _CharT, class _Traits, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3451 | class _Eng, size_t _Kp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3452 | basic_istream<_CharT, _Traits>& |
| 3453 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3454 | shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3455 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3456 | typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3457 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3458 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3459 | _Eng __e; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3460 | result_type _Vp[_Kp+1]; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3461 | __is >> __e; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3462 | for (size_t __i = 0; __i < _Kp+1; ++__i) |
| 3463 | __is >> _Vp[__i]; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3464 | if (!__is.fail()) |
| 3465 | { |
| 3466 | __x.__e_ = __e; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3467 | for (size_t __i = 0; __i < _Kp; ++__i) |
| 3468 | __x._V_[__i] = _Vp[__i]; |
| 3469 | __x._Y_ = _Vp[_Kp]; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3470 | } |
| 3471 | return __is; |
| 3472 | } |
| 3473 | |
| 3474 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 3475 | |
| 3476 | // random_device |
| 3477 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3478 | class _LIBCPP_TYPE_VIS random_device |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3479 | { |
Ed Schouten | 63e70b6 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3480 | #ifdef _LIBCPP_USING_DEV_RANDOM |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3481 | int __f_; |
Ed Schouten | 63e70b6 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3482 | #endif // defined(_LIBCPP_USING_DEV_RANDOM) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3483 | public: |
| 3484 | // types |
| 3485 | typedef unsigned result_type; |
| 3486 | |
| 3487 | // generator characteristics |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3488 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3489 | static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3490 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3491 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 27b4fd3 | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3492 | static _LIBCPP_CONSTEXPR result_type min() { return _Min;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3493 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 27b4fd3 | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3494 | static _LIBCPP_CONSTEXPR result_type max() { return _Max;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3495 | |
| 3496 | // constructors |
| 3497 | explicit random_device(const string& __token = "/dev/urandom"); |
| 3498 | ~random_device(); |
| 3499 | |
| 3500 | // generating functions |
| 3501 | result_type operator()(); |
| 3502 | |
| 3503 | // property functions |
Howard Hinnant | c83960a | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3504 | double entropy() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3505 | |
| 3506 | private: |
| 3507 | // no copy functions |
| 3508 | random_device(const random_device&); // = delete; |
| 3509 | random_device& operator=(const random_device&); // = delete; |
| 3510 | }; |
| 3511 | |
| 3512 | // seed_seq |
| 3513 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3514 | class _LIBCPP_TEMPLATE_VIS seed_seq |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3515 | { |
| 3516 | public: |
| 3517 | // types |
| 3518 | typedef uint32_t result_type; |
| 3519 | |
| 3520 | private: |
| 3521 | vector<result_type> __v_; |
| 3522 | |
| 3523 | template<class _InputIterator> |
| 3524 | void init(_InputIterator __first, _InputIterator __last); |
| 3525 | public: |
| 3526 | // constructors |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3527 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 65ccddb | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3528 | seed_seq() _NOEXCEPT {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3529 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3530 | template<class _Tp> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3531 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3532 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3533 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3534 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3535 | template<class _InputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3536 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3537 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 3538 | {init(__first, __last);} |
| 3539 | |
| 3540 | // generating functions |
| 3541 | template<class _RandomAccessIterator> |
| 3542 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 3543 | |
| 3544 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3545 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 65ccddb | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3546 | size_t size() const _NOEXCEPT {return __v_.size();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3547 | template<class _OutputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3548 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3549 | void param(_OutputIterator __dest) const |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3550 | {_VSTD::copy(__v_.begin(), __v_.end(), __dest);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3551 | |
| 3552 | private: |
| 3553 | // no copy functions |
| 3554 | seed_seq(const seed_seq&); // = delete; |
| 3555 | void operator=(const seed_seq&); // = delete; |
| 3556 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3557 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3558 | static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3559 | }; |
| 3560 | |
| 3561 | template<class _InputIterator> |
| 3562 | void |
| 3563 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 3564 | { |
| 3565 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 3566 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 3567 | } |
| 3568 | |
| 3569 | template<class _RandomAccessIterator> |
| 3570 | void |
| 3571 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 3572 | { |
| 3573 | if (__first != __last) |
| 3574 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3575 | _VSTD::fill(__first, __last, 0x8b8b8b8b); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3576 | const size_t __n = static_cast<size_t>(__last - __first); |
| 3577 | const size_t __s = __v_.size(); |
| 3578 | const size_t __t = (__n >= 623) ? 11 |
| 3579 | : (__n >= 68) ? 7 |
| 3580 | : (__n >= 39) ? 5 |
| 3581 | : (__n >= 7) ? 3 |
| 3582 | : (__n - 1) / 2; |
| 3583 | const size_t __p = (__n - __t) / 2; |
| 3584 | const size_t __q = __p + __t; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3585 | const size_t __m = _VSTD::max(__s + 1, __n); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3586 | // __k = 0; |
| 3587 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3588 | result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p] |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3589 | ^ __first[__n - 1]); |
| 3590 | __first[__p] += __r; |
| 3591 | __r += __s; |
| 3592 | __first[__q] += __r; |
| 3593 | __first[0] = __r; |
| 3594 | } |
| 3595 | for (size_t __k = 1; __k <= __s; ++__k) |
| 3596 | { |
| 3597 | const size_t __kmodn = __k % __n; |
| 3598 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3599 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3600 | ^ __first[(__k - 1) % __n]); |
| 3601 | __first[__kpmodn] += __r; |
| 3602 | __r += __kmodn + __v_[__k-1]; |
| 3603 | __first[(__k + __q) % __n] += __r; |
| 3604 | __first[__kmodn] = __r; |
| 3605 | } |
| 3606 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 3607 | { |
| 3608 | const size_t __kmodn = __k % __n; |
| 3609 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3610 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3611 | ^ __first[(__k - 1) % __n]); |
| 3612 | __first[__kpmodn] += __r; |
| 3613 | __r += __kmodn; |
| 3614 | __first[(__k + __q) % __n] += __r; |
| 3615 | __first[__kmodn] = __r; |
| 3616 | } |
| 3617 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 3618 | { |
| 3619 | const size_t __kmodn = __k % __n; |
| 3620 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3621 | result_type __r = 1566083941 * _Tp(__first[__kmodn] + |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3622 | __first[__kpmodn] + |
| 3623 | __first[(__k - 1) % __n]); |
| 3624 | __first[__kpmodn] ^= __r; |
| 3625 | __r -= __kmodn; |
| 3626 | __first[(__k + __q) % __n] ^= __r; |
| 3627 | __first[__kmodn] = __r; |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3632 | // generate_canonical |
| 3633 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3634 | template<class _RealType, size_t __bits, class _URNG> |
| 3635 | _RealType |
| 3636 | generate_canonical(_URNG& __g) |
| 3637 | { |
| 3638 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 3639 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3640 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3641 | 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] | 3642 | #else |
| 3643 | const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value; |
| 3644 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3645 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3646 | const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3647 | _RealType __base = _Rp; |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3648 | _RealType _Sp = __g() - _URNG::min(); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3649 | for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp) |
Howard Hinnant | 8efd3da | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3650 | _Sp += (__g() - _URNG::min()) * __base; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3651 | return _Sp / __base; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3654 | // uniform_int_distribution |
| 3655 | |
Howard Hinnant | c326721 | 2010-05-26 17:49:34 +0000 | [diff] [blame] | 3656 | // in <algorithm> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3657 | |
| 3658 | template <class _CharT, class _Traits, class _IT> |
| 3659 | basic_ostream<_CharT, _Traits>& |
| 3660 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3661 | const uniform_int_distribution<_IT>& __x) |
| 3662 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3663 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3664 | __os.flags(ios_base::dec | ios_base::left); |
| 3665 | _CharT __sp = __os.widen(' '); |
| 3666 | __os.fill(__sp); |
| 3667 | return __os << __x.a() << __sp << __x.b(); |
| 3668 | } |
| 3669 | |
| 3670 | template <class _CharT, class _Traits, class _IT> |
| 3671 | basic_istream<_CharT, _Traits>& |
| 3672 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3673 | uniform_int_distribution<_IT>& __x) |
| 3674 | { |
| 3675 | typedef uniform_int_distribution<_IT> _Eng; |
| 3676 | typedef typename _Eng::result_type result_type; |
| 3677 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3678 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3679 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3680 | result_type __a; |
| 3681 | result_type __b; |
| 3682 | __is >> __a >> __b; |
| 3683 | if (!__is.fail()) |
| 3684 | __x.param(param_type(__a, __b)); |
| 3685 | return __is; |
| 3686 | } |
| 3687 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3688 | // uniform_real_distribution |
| 3689 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3690 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3691 | class _LIBCPP_TEMPLATE_VIS uniform_real_distribution |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3692 | { |
| 3693 | public: |
| 3694 | // types |
| 3695 | typedef _RealType result_type; |
| 3696 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3697 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3698 | { |
| 3699 | result_type __a_; |
| 3700 | result_type __b_; |
| 3701 | public: |
| 3702 | typedef uniform_real_distribution distribution_type; |
| 3703 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3704 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3705 | explicit param_type(result_type __a = 0, |
| 3706 | result_type __b = 1) |
| 3707 | : __a_(__a), __b_(__b) {} |
| 3708 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3709 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3710 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3711 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3712 | result_type b() const {return __b_;} |
| 3713 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3714 | friend _LIBCPP_INLINE_VISIBILITY |
| 3715 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3716 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3717 | friend _LIBCPP_INLINE_VISIBILITY |
| 3718 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3719 | {return !(__x == __y);} |
| 3720 | }; |
| 3721 | |
| 3722 | private: |
| 3723 | param_type __p_; |
| 3724 | |
| 3725 | public: |
| 3726 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3727 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3728 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 3729 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3730 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3731 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3732 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3733 | void reset() {} |
| 3734 | |
| 3735 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3736 | template<class _URNG> |
| 3737 | _LIBCPP_INLINE_VISIBILITY |
| 3738 | result_type operator()(_URNG& __g) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3739 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3740 | template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3741 | |
| 3742 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3743 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3744 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3745 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3746 | result_type b() const {return __p_.b();} |
| 3747 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3748 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3749 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3750 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3751 | void param(const param_type& __p) {__p_ = __p;} |
| 3752 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3753 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3754 | result_type min() const {return a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3755 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3756 | result_type max() const {return b();} |
| 3757 | |
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.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3762 | friend _LIBCPP_INLINE_VISIBILITY |
| 3763 | bool operator!=(const uniform_real_distribution& __x, |
| 3764 | const uniform_real_distribution& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3765 | {return !(__x == __y);} |
| 3766 | }; |
| 3767 | |
| 3768 | template<class _RealType> |
| 3769 | template<class _URNG> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3770 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3771 | typename uniform_real_distribution<_RealType>::result_type |
| 3772 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3773 | { |
| 3774 | return (__p.b() - __p.a()) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3775 | * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3776 | + __p.a(); |
| 3777 | } |
| 3778 | |
| 3779 | template <class _CharT, class _Traits, class _RT> |
| 3780 | basic_ostream<_CharT, _Traits>& |
| 3781 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3782 | const uniform_real_distribution<_RT>& __x) |
| 3783 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3784 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3785 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3786 | ios_base::scientific); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3787 | _CharT __sp = __os.widen(' '); |
| 3788 | __os.fill(__sp); |
| 3789 | return __os << __x.a() << __sp << __x.b(); |
| 3790 | } |
| 3791 | |
| 3792 | template <class _CharT, class _Traits, class _RT> |
| 3793 | basic_istream<_CharT, _Traits>& |
| 3794 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3795 | uniform_real_distribution<_RT>& __x) |
| 3796 | { |
| 3797 | typedef uniform_real_distribution<_RT> _Eng; |
| 3798 | typedef typename _Eng::result_type result_type; |
| 3799 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3800 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3801 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3802 | result_type __a; |
| 3803 | result_type __b; |
| 3804 | __is >> __a >> __b; |
| 3805 | if (!__is.fail()) |
| 3806 | __x.param(param_type(__a, __b)); |
| 3807 | return __is; |
| 3808 | } |
| 3809 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3810 | // bernoulli_distribution |
| 3811 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3812 | class _LIBCPP_TEMPLATE_VIS bernoulli_distribution |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3813 | { |
| 3814 | public: |
| 3815 | // types |
| 3816 | typedef bool result_type; |
| 3817 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3818 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3819 | { |
| 3820 | double __p_; |
| 3821 | public: |
| 3822 | typedef bernoulli_distribution distribution_type; |
| 3823 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3824 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3825 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3826 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3827 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3828 | double p() const {return __p_;} |
| 3829 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3830 | friend _LIBCPP_INLINE_VISIBILITY |
| 3831 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3832 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3833 | friend _LIBCPP_INLINE_VISIBILITY |
| 3834 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3835 | {return !(__x == __y);} |
| 3836 | }; |
| 3837 | |
| 3838 | private: |
| 3839 | param_type __p_; |
| 3840 | |
| 3841 | public: |
| 3842 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3843 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3844 | explicit bernoulli_distribution(double __p = 0.5) |
| 3845 | : __p_(param_type(__p)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3846 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3847 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3848 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3849 | void reset() {} |
| 3850 | |
| 3851 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3852 | template<class _URNG> |
| 3853 | _LIBCPP_INLINE_VISIBILITY |
| 3854 | result_type operator()(_URNG& __g) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3855 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3856 | template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3857 | |
| 3858 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3859 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3860 | double p() const {return __p_.p();} |
| 3861 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3862 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3863 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3864 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3865 | void param(const param_type& __p) {__p_ = __p;} |
| 3866 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3867 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3868 | result_type min() const {return false;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3869 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3870 | result_type max() const {return true;} |
| 3871 | |
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.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3876 | friend _LIBCPP_INLINE_VISIBILITY |
| 3877 | bool operator!=(const bernoulli_distribution& __x, |
| 3878 | const bernoulli_distribution& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3879 | {return !(__x == __y);} |
| 3880 | }; |
| 3881 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3882 | template<class _URNG> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3883 | inline |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3884 | bernoulli_distribution::result_type |
| 3885 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 3886 | { |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 3887 | uniform_real_distribution<double> __gen; |
| 3888 | return __gen(__g) < __p.p(); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3891 | template <class _CharT, class _Traits> |
| 3892 | basic_ostream<_CharT, _Traits>& |
| 3893 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 3894 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3895 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3896 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3897 | ios_base::scientific); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3898 | _CharT __sp = __os.widen(' '); |
| 3899 | __os.fill(__sp); |
| 3900 | return __os << __x.p(); |
| 3901 | } |
| 3902 | |
| 3903 | template <class _CharT, class _Traits> |
| 3904 | basic_istream<_CharT, _Traits>& |
| 3905 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 3906 | { |
| 3907 | typedef bernoulli_distribution _Eng; |
| 3908 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3909 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3910 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3911 | double __p; |
| 3912 | __is >> __p; |
| 3913 | if (!__is.fail()) |
| 3914 | __x.param(param_type(__p)); |
| 3915 | return __is; |
| 3916 | } |
| 3917 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3918 | // binomial_distribution |
| 3919 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3920 | template<class _IntType = int> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3921 | class _LIBCPP_TEMPLATE_VIS binomial_distribution |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3922 | { |
| 3923 | public: |
| 3924 | // types |
| 3925 | typedef _IntType result_type; |
| 3926 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3927 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3928 | { |
| 3929 | result_type __t_; |
| 3930 | double __p_; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3931 | double __pr_; |
| 3932 | double __odds_ratio_; |
| 3933 | result_type __r0_; |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3934 | public: |
| 3935 | typedef binomial_distribution distribution_type; |
| 3936 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3937 | explicit param_type(result_type __t = 1, double __p = 0.5); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3938 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3939 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3940 | result_type t() const {return __t_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3941 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3942 | double p() const {return __p_;} |
| 3943 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3944 | friend _LIBCPP_INLINE_VISIBILITY |
| 3945 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3946 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3947 | friend _LIBCPP_INLINE_VISIBILITY |
| 3948 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3949 | {return !(__x == __y);} |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3950 | |
| 3951 | friend class binomial_distribution; |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3952 | }; |
| 3953 | |
| 3954 | private: |
| 3955 | param_type __p_; |
| 3956 | |
| 3957 | public: |
| 3958 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3959 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3960 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 3961 | : __p_(param_type(__t, __p)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3962 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3963 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3964 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3965 | void reset() {} |
| 3966 | |
| 3967 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3968 | template<class _URNG> |
| 3969 | _LIBCPP_INLINE_VISIBILITY |
| 3970 | result_type operator()(_URNG& __g) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3971 | {return (*this)(__g, __p_);} |
| 3972 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3973 | |
| 3974 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3975 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3976 | result_type t() const {return __p_.t();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3977 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3978 | double p() const {return __p_.p();} |
| 3979 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3980 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3981 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3982 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3983 | void param(const param_type& __p) {__p_ = __p;} |
| 3984 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3985 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3986 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3987 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3988 | result_type max() const {return t();} |
| 3989 | |
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.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3994 | friend _LIBCPP_INLINE_VISIBILITY |
| 3995 | bool operator!=(const binomial_distribution& __x, |
| 3996 | const binomial_distribution& __y) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3997 | {return !(__x == __y);} |
| 3998 | }; |
| 3999 | |
Eric Fiselier | a9f9f0b | 2017-05-06 02:58:43 +0000 | [diff] [blame^] | 4000 | #ifndef _LIBCPP_MSVCRT |
Marshall Clow | 3320e88 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4001 | extern "C" double lgamma_r(double, int *); |
Eric Fiselier | a9f9f0b | 2017-05-06 02:58:43 +0000 | [diff] [blame^] | 4002 | #endif |
| 4003 | |
| 4004 | inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) { |
| 4005 | #if defined(_LIBCPP_MSVCRT) |
| 4006 | return lgamma(__d); |
| 4007 | #else |
| 4008 | int __sign; |
| 4009 | return lgamma_r(__d, &__sign); |
| 4010 | #endif |
| 4011 | } |
Marshall Clow | 3320e88 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4012 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4013 | template<class _IntType> |
Marshall Clow | 3320e88 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4014 | binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4015 | : __t_(__t), __p_(__p) |
| 4016 | { |
| 4017 | if (0 < __p_ && __p_ < 1) |
| 4018 | { |
| 4019 | __r0_ = static_cast<result_type>((__t_ + 1) * __p_); |
Eric Fiselier | a9f9f0b | 2017-05-06 02:58:43 +0000 | [diff] [blame^] | 4020 | __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) - |
| 4021 | __libcpp_lgamma(__r0_ + 1.) - |
| 4022 | __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) + |
Marshall Clow | 3320e88 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4023 | (__t_ - __r0_) * _VSTD::log(1 - __p_)); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4024 | __odds_ratio_ = __p_ / (1 - __p_); |
| 4025 | } |
| 4026 | } |
| 4027 | |
Marshall Clow | 5ffb8d0 | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4028 | // Reference: Kemp, C.D. (1986). `A modal method for generating binomial |
| 4029 | // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813. |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4030 | template<class _IntType> |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4031 | template<class _URNG> |
| 4032 | _IntType |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4033 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4034 | { |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4035 | if (__pr.__t_ == 0 || __pr.__p_ == 0) |
| 4036 | return 0; |
| 4037 | if (__pr.__p_ == 1) |
| 4038 | return __pr.__t_; |
| 4039 | uniform_real_distribution<double> __gen; |
| 4040 | double __u = __gen(__g) - __pr.__pr_; |
| 4041 | if (__u < 0) |
| 4042 | return __pr.__r0_; |
| 4043 | double __pu = __pr.__pr_; |
| 4044 | double __pd = __pu; |
| 4045 | result_type __ru = __pr.__r0_; |
| 4046 | result_type __rd = __ru; |
| 4047 | while (true) |
| 4048 | { |
| 4049 | if (__rd >= 1) |
| 4050 | { |
| 4051 | __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); |
| 4052 | __u -= __pd; |
| 4053 | if (__u < 0) |
| 4054 | return __rd - 1; |
| 4055 | } |
Marshall Clow | 5ffb8d0 | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4056 | if ( __rd != 0 ) |
| 4057 | --__rd; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4058 | ++__ru; |
| 4059 | if (__ru <= __pr.__t_) |
| 4060 | { |
| 4061 | __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; |
| 4062 | __u -= __pu; |
| 4063 | if (__u < 0) |
| 4064 | return __ru; |
| 4065 | } |
| 4066 | } |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | template <class _CharT, class _Traits, class _IntType> |
| 4070 | basic_ostream<_CharT, _Traits>& |
| 4071 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4072 | const binomial_distribution<_IntType>& __x) |
| 4073 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4074 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4075 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4076 | ios_base::scientific); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4077 | _CharT __sp = __os.widen(' '); |
| 4078 | __os.fill(__sp); |
| 4079 | return __os << __x.t() << __sp << __x.p(); |
| 4080 | } |
| 4081 | |
| 4082 | template <class _CharT, class _Traits, class _IntType> |
| 4083 | basic_istream<_CharT, _Traits>& |
| 4084 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4085 | binomial_distribution<_IntType>& __x) |
| 4086 | { |
| 4087 | typedef binomial_distribution<_IntType> _Eng; |
| 4088 | typedef typename _Eng::result_type result_type; |
| 4089 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4090 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4091 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4092 | result_type __t; |
| 4093 | double __p; |
| 4094 | __is >> __t >> __p; |
| 4095 | if (!__is.fail()) |
| 4096 | __x.param(param_type(__t, __p)); |
| 4097 | return __is; |
| 4098 | } |
| 4099 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4100 | // exponential_distribution |
| 4101 | |
| 4102 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4103 | class _LIBCPP_TEMPLATE_VIS exponential_distribution |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4104 | { |
| 4105 | public: |
| 4106 | // types |
| 4107 | typedef _RealType result_type; |
| 4108 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4109 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4110 | { |
| 4111 | result_type __lambda_; |
| 4112 | public: |
| 4113 | typedef exponential_distribution distribution_type; |
| 4114 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4115 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4116 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 4117 | |
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 | result_type lambda() const {return __lambda_;} |
| 4120 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4121 | friend _LIBCPP_INLINE_VISIBILITY |
| 4122 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4123 | {return __x.__lambda_ == __y.__lambda_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4124 | friend _LIBCPP_INLINE_VISIBILITY |
| 4125 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4126 | {return !(__x == __y);} |
| 4127 | }; |
| 4128 | |
| 4129 | private: |
| 4130 | param_type __p_; |
| 4131 | |
| 4132 | public: |
| 4133 | // constructors and reset functions |
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 | explicit exponential_distribution(result_type __lambda = 1) |
| 4136 | : __p_(param_type(__lambda)) {} |
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 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4139 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4140 | void reset() {} |
| 4141 | |
| 4142 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4143 | template<class _URNG> |
| 4144 | _LIBCPP_INLINE_VISIBILITY |
| 4145 | result_type operator()(_URNG& __g) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4146 | {return (*this)(__g, __p_);} |
| 4147 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4148 | |
| 4149 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4150 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4151 | result_type lambda() const {return __p_.lambda();} |
| 4152 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4153 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4154 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4155 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4156 | void param(const param_type& __p) {__p_ = __p;} |
| 4157 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4158 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4159 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4160 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df40dc6 | 2010-05-16 17:56:20 +0000 | [diff] [blame] | 4161 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4162 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4163 | friend _LIBCPP_INLINE_VISIBILITY |
| 4164 | bool operator==(const exponential_distribution& __x, |
| 4165 | const exponential_distribution& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4166 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4167 | friend _LIBCPP_INLINE_VISIBILITY |
| 4168 | bool operator!=(const exponential_distribution& __x, |
| 4169 | const exponential_distribution& __y) |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4170 | {return !(__x == __y);} |
| 4171 | }; |
| 4172 | |
| 4173 | template <class _RealType> |
| 4174 | template<class _URNG> |
| 4175 | _RealType |
| 4176 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4177 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4178 | return -_VSTD::log |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4179 | ( |
| 4180 | result_type(1) - |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4181 | _VSTD::generate_canonical<result_type, |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4182 | numeric_limits<result_type>::digits>(__g) |
| 4183 | ) |
| 4184 | / __p.lambda(); |
| 4185 | } |
| 4186 | |
| 4187 | template <class _CharT, class _Traits, class _RealType> |
| 4188 | basic_ostream<_CharT, _Traits>& |
| 4189 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4190 | const exponential_distribution<_RealType>& __x) |
| 4191 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4192 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4193 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4194 | ios_base::scientific); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4195 | return __os << __x.lambda(); |
| 4196 | } |
| 4197 | |
| 4198 | template <class _CharT, class _Traits, class _RealType> |
| 4199 | basic_istream<_CharT, _Traits>& |
| 4200 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4201 | exponential_distribution<_RealType>& __x) |
| 4202 | { |
| 4203 | typedef exponential_distribution<_RealType> _Eng; |
| 4204 | typedef typename _Eng::result_type result_type; |
| 4205 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4206 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4207 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4208 | result_type __lambda; |
| 4209 | __is >> __lambda; |
| 4210 | if (!__is.fail()) |
| 4211 | __x.param(param_type(__lambda)); |
| 4212 | return __is; |
| 4213 | } |
| 4214 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4215 | // normal_distribution |
| 4216 | |
| 4217 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4218 | class _LIBCPP_TEMPLATE_VIS normal_distribution |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4219 | { |
| 4220 | public: |
| 4221 | // types |
| 4222 | typedef _RealType result_type; |
| 4223 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4224 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4225 | { |
| 4226 | result_type __mean_; |
| 4227 | result_type __stddev_; |
| 4228 | public: |
| 4229 | typedef normal_distribution distribution_type; |
| 4230 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4231 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4232 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 4233 | : __mean_(__mean), __stddev_(__stddev) {} |
| 4234 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4235 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4236 | result_type mean() const {return __mean_;} |
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 | result_type stddev() const {return __stddev_;} |
| 4239 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4240 | friend _LIBCPP_INLINE_VISIBILITY |
| 4241 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4242 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4243 | friend _LIBCPP_INLINE_VISIBILITY |
| 4244 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4245 | {return !(__x == __y);} |
| 4246 | }; |
| 4247 | |
| 4248 | private: |
| 4249 | param_type __p_; |
| 4250 | result_type _V_; |
| 4251 | bool _V_hot_; |
| 4252 | |
| 4253 | public: |
| 4254 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4255 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4256 | explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1) |
| 4257 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
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 | explicit normal_distribution(const param_type& __p) |
| 4260 | : __p_(__p), _V_hot_(false) {} |
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 | void reset() {_V_hot_ = false;} |
| 4263 | |
| 4264 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4265 | template<class _URNG> |
| 4266 | _LIBCPP_INLINE_VISIBILITY |
| 4267 | result_type operator()(_URNG& __g) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4268 | {return (*this)(__g, __p_);} |
| 4269 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4270 | |
| 4271 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4272 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4273 | result_type mean() const {return __p_.mean();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4274 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4275 | result_type stddev() const {return __p_.stddev();} |
| 4276 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4277 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4278 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4279 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4280 | void param(const param_type& __p) {__p_ = __p;} |
| 4281 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4282 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4283 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4284 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4285 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4286 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4287 | friend _LIBCPP_INLINE_VISIBILITY |
| 4288 | bool operator==(const normal_distribution& __x, |
| 4289 | const normal_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4290 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 4291 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4292 | friend _LIBCPP_INLINE_VISIBILITY |
| 4293 | bool operator!=(const normal_distribution& __x, |
| 4294 | const normal_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4295 | {return !(__x == __y);} |
| 4296 | |
| 4297 | template <class _CharT, class _Traits, class _RT> |
| 4298 | friend |
| 4299 | basic_ostream<_CharT, _Traits>& |
| 4300 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4301 | const normal_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4302 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4303 | template <class _CharT, class _Traits, class _RT> |
| 4304 | friend |
| 4305 | basic_istream<_CharT, _Traits>& |
| 4306 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4307 | normal_distribution<_RT>& __x); |
| 4308 | }; |
| 4309 | |
| 4310 | template <class _RealType> |
| 4311 | template<class _URNG> |
| 4312 | _RealType |
| 4313 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4314 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4315 | result_type _Up; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4316 | if (_V_hot_) |
| 4317 | { |
| 4318 | _V_hot_ = false; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4319 | _Up = _V_; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4320 | } |
| 4321 | else |
| 4322 | { |
| 4323 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 4324 | result_type __u; |
| 4325 | result_type __v; |
| 4326 | result_type __s; |
| 4327 | do |
| 4328 | { |
| 4329 | __u = _Uni(__g); |
| 4330 | __v = _Uni(__g); |
| 4331 | __s = __u * __u + __v * __v; |
| 4332 | } while (__s > 1 || __s == 0); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4333 | result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s); |
| 4334 | _V_ = __v * _Fp; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4335 | _V_hot_ = true; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4336 | _Up = __u * _Fp; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4337 | } |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4338 | return _Up * __p.stddev() + __p.mean(); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | template <class _CharT, class _Traits, class _RT> |
| 4342 | basic_ostream<_CharT, _Traits>& |
| 4343 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4344 | const normal_distribution<_RT>& __x) |
| 4345 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4346 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4347 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4348 | ios_base::scientific); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4349 | _CharT __sp = __os.widen(' '); |
| 4350 | __os.fill(__sp); |
| 4351 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 4352 | if (__x._V_hot_) |
| 4353 | __os << __sp << __x._V_; |
| 4354 | return __os; |
| 4355 | } |
| 4356 | |
| 4357 | template <class _CharT, class _Traits, class _RT> |
| 4358 | basic_istream<_CharT, _Traits>& |
| 4359 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4360 | normal_distribution<_RT>& __x) |
| 4361 | { |
| 4362 | typedef normal_distribution<_RT> _Eng; |
| 4363 | typedef typename _Eng::result_type result_type; |
| 4364 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4365 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4366 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4367 | result_type __mean; |
| 4368 | result_type __stddev; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4369 | result_type _Vp = 0; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4370 | bool _V_hot = false; |
| 4371 | __is >> __mean >> __stddev >> _V_hot; |
| 4372 | if (_V_hot) |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4373 | __is >> _Vp; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4374 | if (!__is.fail()) |
| 4375 | { |
| 4376 | __x.param(param_type(__mean, __stddev)); |
| 4377 | __x._V_hot_ = _V_hot; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4378 | __x._V_ = _Vp; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4379 | } |
| 4380 | return __is; |
| 4381 | } |
| 4382 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4383 | // lognormal_distribution |
| 4384 | |
| 4385 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4386 | class _LIBCPP_TEMPLATE_VIS lognormal_distribution |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4387 | { |
| 4388 | public: |
| 4389 | // types |
| 4390 | typedef _RealType result_type; |
| 4391 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4392 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4393 | { |
| 4394 | normal_distribution<result_type> __nd_; |
| 4395 | public: |
| 4396 | typedef lognormal_distribution distribution_type; |
| 4397 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4398 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4399 | explicit param_type(result_type __m = 0, result_type __s = 1) |
| 4400 | : __nd_(__m, __s) {} |
| 4401 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4402 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4403 | result_type m() const {return __nd_.mean();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4404 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4405 | result_type s() const {return __nd_.stddev();} |
| 4406 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4407 | friend _LIBCPP_INLINE_VISIBILITY |
| 4408 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4409 | {return __x.__nd_ == __y.__nd_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4410 | friend _LIBCPP_INLINE_VISIBILITY |
| 4411 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4412 | {return !(__x == __y);} |
| 4413 | friend class lognormal_distribution; |
| 4414 | |
| 4415 | template <class _CharT, class _Traits, class _RT> |
| 4416 | friend |
| 4417 | basic_ostream<_CharT, _Traits>& |
| 4418 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4419 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4420 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4421 | template <class _CharT, class _Traits, class _RT> |
| 4422 | friend |
| 4423 | basic_istream<_CharT, _Traits>& |
| 4424 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4425 | lognormal_distribution<_RT>& __x); |
| 4426 | }; |
| 4427 | |
| 4428 | private: |
| 4429 | param_type __p_; |
| 4430 | |
| 4431 | public: |
| 4432 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4433 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4434 | explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) |
| 4435 | : __p_(param_type(__m, __s)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4436 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4437 | explicit lognormal_distribution(const param_type& __p) |
| 4438 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4439 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4440 | void reset() {__p_.__nd_.reset();} |
| 4441 | |
| 4442 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4443 | template<class _URNG> |
| 4444 | _LIBCPP_INLINE_VISIBILITY |
| 4445 | result_type operator()(_URNG& __g) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4446 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4447 | template<class _URNG> |
| 4448 | _LIBCPP_INLINE_VISIBILITY |
| 4449 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4450 | {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));} |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4451 | |
| 4452 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4453 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4454 | result_type m() const {return __p_.m();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4455 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4456 | result_type s() const {return __p_.s();} |
| 4457 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4458 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4459 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4460 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 4461 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4462 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4463 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4464 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4465 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4466 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4467 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4468 | friend _LIBCPP_INLINE_VISIBILITY |
| 4469 | bool operator==(const lognormal_distribution& __x, |
| 4470 | const lognormal_distribution& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4471 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4472 | friend _LIBCPP_INLINE_VISIBILITY |
| 4473 | bool operator!=(const lognormal_distribution& __x, |
| 4474 | const lognormal_distribution& __y) |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4475 | {return !(__x == __y);} |
| 4476 | |
| 4477 | template <class _CharT, class _Traits, class _RT> |
| 4478 | friend |
| 4479 | basic_ostream<_CharT, _Traits>& |
| 4480 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4481 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4482 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4483 | template <class _CharT, class _Traits, class _RT> |
| 4484 | friend |
| 4485 | basic_istream<_CharT, _Traits>& |
| 4486 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4487 | lognormal_distribution<_RT>& __x); |
| 4488 | }; |
| 4489 | |
| 4490 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4491 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4492 | basic_ostream<_CharT, _Traits>& |
| 4493 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4494 | const lognormal_distribution<_RT>& __x) |
| 4495 | { |
| 4496 | return __os << __x.__p_.__nd_; |
| 4497 | } |
| 4498 | |
| 4499 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4500 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4501 | basic_istream<_CharT, _Traits>& |
| 4502 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4503 | lognormal_distribution<_RT>& __x) |
| 4504 | { |
| 4505 | return __is >> __x.__p_.__nd_; |
| 4506 | } |
| 4507 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4508 | // poisson_distribution |
| 4509 | |
| 4510 | template<class _IntType = int> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4511 | class _LIBCPP_TEMPLATE_VIS poisson_distribution |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4512 | { |
| 4513 | public: |
| 4514 | // types |
| 4515 | typedef _IntType result_type; |
| 4516 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4517 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4518 | { |
| 4519 | double __mean_; |
| 4520 | double __s_; |
| 4521 | double __d_; |
| 4522 | double __l_; |
| 4523 | double __omega_; |
| 4524 | double __c0_; |
| 4525 | double __c1_; |
| 4526 | double __c2_; |
| 4527 | double __c3_; |
| 4528 | double __c_; |
| 4529 | |
| 4530 | public: |
| 4531 | typedef poisson_distribution distribution_type; |
| 4532 | |
| 4533 | explicit param_type(double __mean = 1.0); |
| 4534 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4535 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4536 | double mean() const {return __mean_;} |
| 4537 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4538 | friend _LIBCPP_INLINE_VISIBILITY |
| 4539 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4540 | {return __x.__mean_ == __y.__mean_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4541 | friend _LIBCPP_INLINE_VISIBILITY |
| 4542 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4543 | {return !(__x == __y);} |
| 4544 | |
| 4545 | friend class poisson_distribution; |
| 4546 | }; |
| 4547 | |
| 4548 | private: |
| 4549 | param_type __p_; |
| 4550 | |
| 4551 | public: |
| 4552 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4553 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4554 | explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} |
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 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
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 | void reset() {} |
| 4559 | |
| 4560 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4561 | template<class _URNG> |
| 4562 | _LIBCPP_INLINE_VISIBILITY |
| 4563 | result_type operator()(_URNG& __g) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4564 | {return (*this)(__g, __p_);} |
| 4565 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4566 | |
| 4567 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4568 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4569 | double mean() const {return __p_.mean();} |
| 4570 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4571 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4572 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4573 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4574 | void param(const param_type& __p) {__p_ = __p;} |
| 4575 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4576 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4577 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4578 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4579 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4580 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4581 | friend _LIBCPP_INLINE_VISIBILITY |
| 4582 | bool operator==(const poisson_distribution& __x, |
| 4583 | const poisson_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4584 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4585 | friend _LIBCPP_INLINE_VISIBILITY |
| 4586 | bool operator!=(const poisson_distribution& __x, |
| 4587 | const poisson_distribution& __y) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4588 | {return !(__x == __y);} |
| 4589 | }; |
| 4590 | |
| 4591 | template<class _IntType> |
| 4592 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
| 4593 | : __mean_(__mean) |
| 4594 | { |
| 4595 | if (__mean_ < 10) |
| 4596 | { |
| 4597 | __s_ = 0; |
| 4598 | __d_ = 0; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4599 | __l_ = _VSTD::exp(-__mean_); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4600 | __omega_ = 0; |
| 4601 | __c3_ = 0; |
| 4602 | __c2_ = 0; |
| 4603 | __c1_ = 0; |
| 4604 | __c0_ = 0; |
| 4605 | __c_ = 0; |
| 4606 | } |
| 4607 | else |
| 4608 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4609 | __s_ = _VSTD::sqrt(__mean_); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4610 | __d_ = 6 * __mean_ * __mean_; |
| 4611 | __l_ = static_cast<result_type>(__mean_ - 1.1484); |
| 4612 | __omega_ = .3989423 / __s_; |
| 4613 | double __b1_ = .4166667E-1 / __mean_; |
| 4614 | double __b2_ = .3 * __b1_ * __b1_; |
| 4615 | __c3_ = .1428571 * __b1_ * __b2_; |
| 4616 | __c2_ = __b2_ - 15. * __c3_; |
| 4617 | __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; |
| 4618 | __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; |
| 4619 | __c_ = .1069 / __mean_; |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | template <class _IntType> |
| 4624 | template<class _URNG> |
| 4625 | _IntType |
| 4626 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4627 | { |
| 4628 | result_type __x; |
| 4629 | uniform_real_distribution<double> __urd; |
Howard Hinnant | 75f7695 | 2011-04-14 15:59:22 +0000 | [diff] [blame] | 4630 | if (__pr.__mean_ < 10) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4631 | { |
| 4632 | __x = 0; |
| 4633 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__x) |
| 4634 | __p *= __urd(__urng); |
| 4635 | } |
| 4636 | else |
| 4637 | { |
| 4638 | double __difmuk; |
| 4639 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 4640 | double __u; |
| 4641 | if (__g > 0) |
| 4642 | { |
| 4643 | __x = static_cast<result_type>(__g); |
| 4644 | if (__x >= __pr.__l_) |
| 4645 | return __x; |
| 4646 | __difmuk = __pr.__mean_ - __x; |
| 4647 | __u = __urd(__urng); |
| 4648 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
| 4649 | return __x; |
| 4650 | } |
| 4651 | exponential_distribution<double> __edist; |
| 4652 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) |
| 4653 | { |
| 4654 | double __e; |
| 4655 | if (__using_exp_dist || __g < 0) |
| 4656 | { |
| 4657 | double __t; |
| 4658 | do |
| 4659 | { |
| 4660 | __e = __edist(__urng); |
| 4661 | __u = __urd(__urng); |
| 4662 | __u += __u - 1; |
| 4663 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 4664 | } while (__t <= -.6744); |
| 4665 | __x = __pr.__mean_ + __pr.__s_ * __t; |
| 4666 | __difmuk = __pr.__mean_ - __x; |
| 4667 | __using_exp_dist = true; |
| 4668 | } |
| 4669 | double __px; |
| 4670 | double __py; |
| 4671 | if (__x < 10) |
| 4672 | { |
| 4673 | const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, |
| 4674 | 40320, 362880}; |
| 4675 | __px = -__pr.__mean_; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4676 | __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x]; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4677 | } |
| 4678 | else |
| 4679 | { |
| 4680 | double __del = .8333333E-1 / __x; |
| 4681 | __del -= 4.8 * __del * __del * __del; |
| 4682 | double __v = __difmuk / __x; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4683 | if (_VSTD::abs(__v) > 0.25) |
| 4684 | __px = __x * _VSTD::log(1 + __v) - __difmuk - __del; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4685 | else |
| 4686 | __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) * |
| 4687 | __v + .1421878) * __v + -.1661269) * __v + .2000118) * |
| 4688 | __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4689 | __py = .3989423 / _VSTD::sqrt(__x); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4690 | } |
| 4691 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 4692 | double __r2 = __r * __r; |
| 4693 | double __fx = -0.5 * __r2; |
| 4694 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * |
| 4695 | __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 4696 | if (__using_exp_dist) |
| 4697 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4698 | if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) - |
| 4699 | __fy * _VSTD::exp(__fx + __e)) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4700 | break; |
| 4701 | } |
| 4702 | else |
| 4703 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4704 | if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx)) |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4705 | break; |
| 4706 | } |
| 4707 | } |
| 4708 | } |
| 4709 | return __x; |
| 4710 | } |
| 4711 | |
| 4712 | template <class _CharT, class _Traits, class _IntType> |
| 4713 | basic_ostream<_CharT, _Traits>& |
| 4714 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4715 | const poisson_distribution<_IntType>& __x) |
| 4716 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4717 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4718 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4719 | ios_base::scientific); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4720 | return __os << __x.mean(); |
| 4721 | } |
| 4722 | |
| 4723 | template <class _CharT, class _Traits, class _IntType> |
| 4724 | basic_istream<_CharT, _Traits>& |
| 4725 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4726 | poisson_distribution<_IntType>& __x) |
| 4727 | { |
| 4728 | typedef poisson_distribution<_IntType> _Eng; |
| 4729 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4730 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4731 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4732 | double __mean; |
| 4733 | __is >> __mean; |
| 4734 | if (!__is.fail()) |
| 4735 | __x.param(param_type(__mean)); |
| 4736 | return __is; |
| 4737 | } |
| 4738 | |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4739 | // weibull_distribution |
| 4740 | |
| 4741 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4742 | class _LIBCPP_TEMPLATE_VIS weibull_distribution |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4743 | { |
| 4744 | public: |
| 4745 | // types |
| 4746 | typedef _RealType result_type; |
| 4747 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4748 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4749 | { |
| 4750 | result_type __a_; |
| 4751 | result_type __b_; |
| 4752 | public: |
| 4753 | typedef weibull_distribution distribution_type; |
| 4754 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4755 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4756 | explicit param_type(result_type __a = 1, result_type __b = 1) |
| 4757 | : __a_(__a), __b_(__b) {} |
| 4758 | |
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 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4761 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4762 | result_type b() const {return __b_;} |
| 4763 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4764 | friend _LIBCPP_INLINE_VISIBILITY |
| 4765 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4766 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4767 | friend _LIBCPP_INLINE_VISIBILITY |
| 4768 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4769 | {return !(__x == __y);} |
| 4770 | }; |
| 4771 | |
| 4772 | private: |
| 4773 | param_type __p_; |
| 4774 | |
| 4775 | public: |
| 4776 | // constructor and reset 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 | explicit weibull_distribution(result_type __a = 1, result_type __b = 1) |
| 4779 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4780 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4781 | explicit weibull_distribution(const param_type& __p) |
| 4782 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4783 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4784 | void reset() {} |
| 4785 | |
| 4786 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4787 | template<class _URNG> |
| 4788 | _LIBCPP_INLINE_VISIBILITY |
| 4789 | result_type operator()(_URNG& __g) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4790 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4791 | template<class _URNG> |
| 4792 | _LIBCPP_INLINE_VISIBILITY |
| 4793 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4794 | {return __p.b() * |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4795 | _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());} |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4796 | |
| 4797 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4798 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4799 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4800 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4801 | result_type b() const {return __p_.b();} |
| 4802 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4803 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4804 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4805 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4806 | void param(const param_type& __p) {__p_ = __p;} |
| 4807 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4808 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4809 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4810 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4811 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4812 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4813 | friend _LIBCPP_INLINE_VISIBILITY |
| 4814 | bool operator==(const weibull_distribution& __x, |
| 4815 | const weibull_distribution& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4816 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4817 | friend _LIBCPP_INLINE_VISIBILITY |
| 4818 | bool operator!=(const weibull_distribution& __x, |
| 4819 | const weibull_distribution& __y) |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4820 | {return !(__x == __y);} |
| 4821 | }; |
| 4822 | |
| 4823 | template <class _CharT, class _Traits, class _RT> |
| 4824 | basic_ostream<_CharT, _Traits>& |
| 4825 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4826 | const weibull_distribution<_RT>& __x) |
| 4827 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4828 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4829 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4830 | ios_base::scientific); |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4831 | _CharT __sp = __os.widen(' '); |
| 4832 | __os.fill(__sp); |
| 4833 | __os << __x.a() << __sp << __x.b(); |
| 4834 | return __os; |
| 4835 | } |
| 4836 | |
| 4837 | template <class _CharT, class _Traits, class _RT> |
| 4838 | basic_istream<_CharT, _Traits>& |
| 4839 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4840 | weibull_distribution<_RT>& __x) |
| 4841 | { |
| 4842 | typedef weibull_distribution<_RT> _Eng; |
| 4843 | typedef typename _Eng::result_type result_type; |
| 4844 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4845 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4846 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4847 | result_type __a; |
| 4848 | result_type __b; |
| 4849 | __is >> __a >> __b; |
| 4850 | if (!__is.fail()) |
| 4851 | __x.param(param_type(__a, __b)); |
| 4852 | return __is; |
| 4853 | } |
| 4854 | |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4855 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4856 | class _LIBCPP_TEMPLATE_VIS extreme_value_distribution |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4857 | { |
| 4858 | public: |
| 4859 | // types |
| 4860 | typedef _RealType result_type; |
| 4861 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4862 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4863 | { |
| 4864 | result_type __a_; |
| 4865 | result_type __b_; |
| 4866 | public: |
| 4867 | typedef extreme_value_distribution distribution_type; |
| 4868 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4869 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4870 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 4871 | : __a_(__a), __b_(__b) {} |
| 4872 | |
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 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4875 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4876 | result_type b() const {return __b_;} |
| 4877 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4878 | friend _LIBCPP_INLINE_VISIBILITY |
| 4879 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4880 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4881 | friend _LIBCPP_INLINE_VISIBILITY |
| 4882 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4883 | {return !(__x == __y);} |
| 4884 | }; |
| 4885 | |
| 4886 | private: |
| 4887 | param_type __p_; |
| 4888 | |
| 4889 | public: |
| 4890 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4891 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4892 | explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1) |
| 4893 | : __p_(param_type(__a, __b)) {} |
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 | explicit extreme_value_distribution(const param_type& __p) |
| 4896 | : __p_(__p) {} |
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 | void reset() {} |
| 4899 | |
| 4900 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4901 | template<class _URNG> |
| 4902 | _LIBCPP_INLINE_VISIBILITY |
| 4903 | result_type operator()(_URNG& __g) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4904 | {return (*this)(__g, __p_);} |
| 4905 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4906 | |
| 4907 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4908 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4909 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4910 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4911 | result_type b() const {return __p_.b();} |
| 4912 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4913 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4914 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4915 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4916 | void param(const param_type& __p) {__p_ = __p;} |
| 4917 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4918 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4919 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4920 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4921 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4922 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4923 | friend _LIBCPP_INLINE_VISIBILITY |
| 4924 | bool operator==(const extreme_value_distribution& __x, |
| 4925 | const extreme_value_distribution& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4926 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4927 | friend _LIBCPP_INLINE_VISIBILITY |
| 4928 | bool operator!=(const extreme_value_distribution& __x, |
| 4929 | const extreme_value_distribution& __y) |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4930 | {return !(__x == __y);} |
| 4931 | }; |
| 4932 | |
| 4933 | template<class _RealType> |
| 4934 | template<class _URNG> |
| 4935 | _RealType |
| 4936 | extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4937 | { |
| 4938 | return __p.a() - __p.b() * |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4939 | _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g))); |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | template <class _CharT, class _Traits, class _RT> |
| 4943 | basic_ostream<_CharT, _Traits>& |
| 4944 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4945 | const extreme_value_distribution<_RT>& __x) |
| 4946 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4947 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4948 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4949 | ios_base::scientific); |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4950 | _CharT __sp = __os.widen(' '); |
| 4951 | __os.fill(__sp); |
| 4952 | __os << __x.a() << __sp << __x.b(); |
| 4953 | return __os; |
| 4954 | } |
| 4955 | |
| 4956 | template <class _CharT, class _Traits, class _RT> |
| 4957 | basic_istream<_CharT, _Traits>& |
| 4958 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4959 | extreme_value_distribution<_RT>& __x) |
| 4960 | { |
| 4961 | typedef extreme_value_distribution<_RT> _Eng; |
| 4962 | typedef typename _Eng::result_type result_type; |
| 4963 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4964 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4965 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4966 | result_type __a; |
| 4967 | result_type __b; |
| 4968 | __is >> __a >> __b; |
| 4969 | if (!__is.fail()) |
| 4970 | __x.param(param_type(__a, __b)); |
| 4971 | return __is; |
| 4972 | } |
| 4973 | |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4974 | // gamma_distribution |
| 4975 | |
| 4976 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4977 | class _LIBCPP_TEMPLATE_VIS gamma_distribution |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4978 | { |
| 4979 | public: |
| 4980 | // types |
| 4981 | typedef _RealType result_type; |
| 4982 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4983 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4984 | { |
| 4985 | result_type __alpha_; |
| 4986 | result_type __beta_; |
| 4987 | public: |
| 4988 | typedef gamma_distribution distribution_type; |
| 4989 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4991 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 4992 | : __alpha_(__alpha), __beta_(__beta) {} |
| 4993 | |
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 | result_type alpha() const {return __alpha_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4996 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4997 | result_type beta() const {return __beta_;} |
| 4998 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4999 | friend _LIBCPP_INLINE_VISIBILITY |
| 5000 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5001 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5002 | friend _LIBCPP_INLINE_VISIBILITY |
| 5003 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5004 | {return !(__x == __y);} |
| 5005 | }; |
| 5006 | |
| 5007 | private: |
| 5008 | param_type __p_; |
| 5009 | |
| 5010 | public: |
| 5011 | // constructors and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5012 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5013 | explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) |
| 5014 | : __p_(param_type(__alpha, __beta)) {} |
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 | explicit gamma_distribution(const param_type& __p) |
| 5017 | : __p_(__p) {} |
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 | void reset() {} |
| 5020 | |
| 5021 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5022 | template<class _URNG> |
| 5023 | _LIBCPP_INLINE_VISIBILITY |
| 5024 | result_type operator()(_URNG& __g) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5025 | {return (*this)(__g, __p_);} |
| 5026 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5027 | |
| 5028 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5029 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5030 | result_type alpha() const {return __p_.alpha();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5031 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5032 | result_type beta() const {return __p_.beta();} |
| 5033 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5034 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5035 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5036 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5037 | void param(const param_type& __p) {__p_ = __p;} |
| 5038 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5039 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5040 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5041 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5042 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5043 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5044 | friend _LIBCPP_INLINE_VISIBILITY |
| 5045 | bool operator==(const gamma_distribution& __x, |
| 5046 | const gamma_distribution& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5047 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5048 | friend _LIBCPP_INLINE_VISIBILITY |
| 5049 | bool operator!=(const gamma_distribution& __x, |
| 5050 | const gamma_distribution& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5051 | {return !(__x == __y);} |
| 5052 | }; |
| 5053 | |
| 5054 | template <class _RealType> |
| 5055 | template<class _URNG> |
| 5056 | _RealType |
| 5057 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5058 | { |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5059 | result_type __a = __p.alpha(); |
| 5060 | uniform_real_distribution<result_type> __gen(0, 1); |
| 5061 | exponential_distribution<result_type> __egen; |
| 5062 | result_type __x; |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5063 | if (__a == 1) |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5064 | __x = __egen(__g); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5065 | else if (__a > 1) |
| 5066 | { |
| 5067 | const result_type __b = __a - 1; |
| 5068 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5069 | while (true) |
| 5070 | { |
| 5071 | const result_type __u = __gen(__g); |
| 5072 | const result_type __v = __gen(__g); |
| 5073 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5074 | if (__w != 0) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5075 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5076 | const result_type __y = _VSTD::sqrt(__c / __w) * |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5077 | (__u - result_type(0.5)); |
| 5078 | __x = __b + __y; |
| 5079 | if (__x >= 0) |
| 5080 | { |
| 5081 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 5082 | if (__z <= 1 - 2 * __y * __y / __x) |
| 5083 | break; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5084 | if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y)) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5085 | break; |
| 5086 | } |
| 5087 | } |
| 5088 | } |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5089 | } |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5090 | else // __a < 1 |
| 5091 | { |
| 5092 | while (true) |
| 5093 | { |
| 5094 | const result_type __u = __gen(__g); |
| 5095 | const result_type __es = __egen(__g); |
| 5096 | if (__u <= 1 - __a) |
| 5097 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5098 | __x = _VSTD::pow(__u, 1 / __a); |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5099 | if (__x <= __es) |
| 5100 | break; |
| 5101 | } |
| 5102 | else |
| 5103 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5104 | const result_type __e = -_VSTD::log((1-__u)/__a); |
| 5105 | __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a); |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5106 | if (__x <= __e + __es) |
| 5107 | break; |
| 5108 | } |
| 5109 | } |
| 5110 | } |
| 5111 | return __x * __p.beta(); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | template <class _CharT, class _Traits, class _RT> |
| 5115 | basic_ostream<_CharT, _Traits>& |
| 5116 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5117 | const gamma_distribution<_RT>& __x) |
| 5118 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5119 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5120 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5121 | ios_base::scientific); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5122 | _CharT __sp = __os.widen(' '); |
| 5123 | __os.fill(__sp); |
| 5124 | __os << __x.alpha() << __sp << __x.beta(); |
| 5125 | return __os; |
| 5126 | } |
| 5127 | |
| 5128 | template <class _CharT, class _Traits, class _RT> |
| 5129 | basic_istream<_CharT, _Traits>& |
| 5130 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5131 | gamma_distribution<_RT>& __x) |
| 5132 | { |
| 5133 | typedef gamma_distribution<_RT> _Eng; |
| 5134 | typedef typename _Eng::result_type result_type; |
| 5135 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5136 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5137 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5138 | result_type __alpha; |
| 5139 | result_type __beta; |
| 5140 | __is >> __alpha >> __beta; |
| 5141 | if (!__is.fail()) |
| 5142 | __x.param(param_type(__alpha, __beta)); |
| 5143 | return __is; |
| 5144 | } |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 5145 | |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5146 | // negative_binomial_distribution |
| 5147 | |
| 5148 | template<class _IntType = int> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5149 | class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5150 | { |
| 5151 | public: |
| 5152 | // types |
| 5153 | typedef _IntType result_type; |
| 5154 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5155 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5156 | { |
| 5157 | result_type __k_; |
| 5158 | double __p_; |
| 5159 | public: |
| 5160 | typedef negative_binomial_distribution distribution_type; |
| 5161 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5162 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5163 | explicit param_type(result_type __k = 1, double __p = 0.5) |
| 5164 | : __k_(__k), __p_(__p) {} |
| 5165 | |
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 | result_type k() const {return __k_;} |
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 | double p() const {return __p_;} |
| 5170 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5171 | friend _LIBCPP_INLINE_VISIBILITY |
| 5172 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5173 | {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5174 | friend _LIBCPP_INLINE_VISIBILITY |
| 5175 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5176 | {return !(__x == __y);} |
| 5177 | }; |
| 5178 | |
| 5179 | private: |
| 5180 | param_type __p_; |
| 5181 | |
| 5182 | public: |
| 5183 | // constructor and reset functions |
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 | explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5) |
| 5186 | : __p_(__k, __p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5187 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5188 | explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {} |
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 | void reset() {} |
| 5191 | |
| 5192 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5193 | template<class _URNG> |
| 5194 | _LIBCPP_INLINE_VISIBILITY |
| 5195 | result_type operator()(_URNG& __g) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5196 | {return (*this)(__g, __p_);} |
| 5197 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5198 | |
| 5199 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5200 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5201 | result_type k() const {return __p_.k();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5202 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5203 | double p() const {return __p_.p();} |
| 5204 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5205 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5206 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5207 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5208 | void param(const param_type& __p) {__p_ = __p;} |
| 5209 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5210 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5211 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5212 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5213 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5214 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5215 | friend _LIBCPP_INLINE_VISIBILITY |
| 5216 | bool operator==(const negative_binomial_distribution& __x, |
| 5217 | const negative_binomial_distribution& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5218 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5219 | friend _LIBCPP_INLINE_VISIBILITY |
| 5220 | bool operator!=(const negative_binomial_distribution& __x, |
| 5221 | const negative_binomial_distribution& __y) |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5222 | {return !(__x == __y);} |
| 5223 | }; |
| 5224 | |
| 5225 | template <class _IntType> |
| 5226 | template<class _URNG> |
| 5227 | _IntType |
| 5228 | negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 5229 | { |
| 5230 | result_type __k = __pr.k(); |
| 5231 | double __p = __pr.p(); |
| 5232 | if (__k <= 21 * __p) |
| 5233 | { |
| 5234 | bernoulli_distribution __gen(__p); |
| 5235 | result_type __f = 0; |
| 5236 | result_type __s = 0; |
| 5237 | while (__s < __k) |
| 5238 | { |
| 5239 | if (__gen(__urng)) |
| 5240 | ++__s; |
| 5241 | else |
| 5242 | ++__f; |
| 5243 | } |
| 5244 | return __f; |
| 5245 | } |
| 5246 | return poisson_distribution<result_type>(gamma_distribution<double> |
| 5247 | (__k, (1-__p)/__p)(__urng))(__urng); |
| 5248 | } |
| 5249 | |
| 5250 | template <class _CharT, class _Traits, class _IntType> |
| 5251 | basic_ostream<_CharT, _Traits>& |
| 5252 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5253 | const negative_binomial_distribution<_IntType>& __x) |
| 5254 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5255 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5256 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5257 | ios_base::scientific); |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5258 | _CharT __sp = __os.widen(' '); |
| 5259 | __os.fill(__sp); |
| 5260 | return __os << __x.k() << __sp << __x.p(); |
| 5261 | } |
| 5262 | |
| 5263 | template <class _CharT, class _Traits, class _IntType> |
| 5264 | basic_istream<_CharT, _Traits>& |
| 5265 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5266 | negative_binomial_distribution<_IntType>& __x) |
| 5267 | { |
| 5268 | typedef negative_binomial_distribution<_IntType> _Eng; |
| 5269 | typedef typename _Eng::result_type result_type; |
| 5270 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5271 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5272 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5273 | result_type __k; |
| 5274 | double __p; |
| 5275 | __is >> __k >> __p; |
| 5276 | if (!__is.fail()) |
| 5277 | __x.param(param_type(__k, __p)); |
| 5278 | return __is; |
| 5279 | } |
| 5280 | |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5281 | // geometric_distribution |
| 5282 | |
| 5283 | template<class _IntType = int> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5284 | class _LIBCPP_TEMPLATE_VIS geometric_distribution |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5285 | { |
| 5286 | public: |
| 5287 | // types |
| 5288 | typedef _IntType result_type; |
| 5289 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5290 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5291 | { |
| 5292 | double __p_; |
| 5293 | public: |
| 5294 | typedef geometric_distribution distribution_type; |
| 5295 | |
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 param_type(double __p = 0.5) : __p_(__p) {} |
| 5298 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5299 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5300 | double p() const {return __p_;} |
| 5301 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5302 | friend _LIBCPP_INLINE_VISIBILITY |
| 5303 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5304 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5305 | friend _LIBCPP_INLINE_VISIBILITY |
| 5306 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5307 | {return !(__x == __y);} |
| 5308 | }; |
| 5309 | |
| 5310 | private: |
| 5311 | param_type __p_; |
| 5312 | |
| 5313 | public: |
| 5314 | // constructors and reset functions |
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 | explicit geometric_distribution(double __p = 0.5) : __p_(__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 | explicit geometric_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5319 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5320 | void reset() {} |
| 5321 | |
| 5322 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5323 | template<class _URNG> |
| 5324 | _LIBCPP_INLINE_VISIBILITY |
| 5325 | result_type operator()(_URNG& __g) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5326 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5327 | template<class _URNG> |
| 5328 | _LIBCPP_INLINE_VISIBILITY |
| 5329 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5330 | {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} |
| 5331 | |
| 5332 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5333 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5334 | double p() const {return __p_.p();} |
| 5335 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5336 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5337 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5338 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5339 | void param(const param_type& __p) {__p_ = __p;} |
| 5340 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5341 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5342 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5343 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5344 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5345 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5346 | friend _LIBCPP_INLINE_VISIBILITY |
| 5347 | bool operator==(const geometric_distribution& __x, |
| 5348 | const geometric_distribution& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5349 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5350 | friend _LIBCPP_INLINE_VISIBILITY |
| 5351 | bool operator!=(const geometric_distribution& __x, |
| 5352 | const geometric_distribution& __y) |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5353 | {return !(__x == __y);} |
| 5354 | }; |
| 5355 | |
| 5356 | template <class _CharT, class _Traits, class _IntType> |
| 5357 | basic_ostream<_CharT, _Traits>& |
| 5358 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5359 | const geometric_distribution<_IntType>& __x) |
| 5360 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5361 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5362 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5363 | ios_base::scientific); |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5364 | return __os << __x.p(); |
| 5365 | } |
| 5366 | |
| 5367 | template <class _CharT, class _Traits, class _IntType> |
| 5368 | basic_istream<_CharT, _Traits>& |
| 5369 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5370 | geometric_distribution<_IntType>& __x) |
| 5371 | { |
| 5372 | typedef geometric_distribution<_IntType> _Eng; |
| 5373 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5374 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5375 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5376 | double __p; |
| 5377 | __is >> __p; |
| 5378 | if (!__is.fail()) |
| 5379 | __x.param(param_type(__p)); |
| 5380 | return __is; |
| 5381 | } |
| 5382 | |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5383 | // chi_squared_distribution |
| 5384 | |
| 5385 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5386 | class _LIBCPP_TEMPLATE_VIS chi_squared_distribution |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5387 | { |
| 5388 | public: |
| 5389 | // types |
| 5390 | typedef _RealType result_type; |
| 5391 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5392 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5393 | { |
| 5394 | result_type __n_; |
| 5395 | public: |
| 5396 | typedef chi_squared_distribution distribution_type; |
| 5397 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5398 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5399 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5400 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5401 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5402 | result_type n() const {return __n_;} |
| 5403 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5404 | friend _LIBCPP_INLINE_VISIBILITY |
| 5405 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5406 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5407 | friend _LIBCPP_INLINE_VISIBILITY |
| 5408 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5409 | {return !(__x == __y);} |
| 5410 | }; |
| 5411 | |
| 5412 | private: |
| 5413 | param_type __p_; |
| 5414 | |
| 5415 | public: |
| 5416 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5417 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5418 | explicit chi_squared_distribution(result_type __n = 1) |
| 5419 | : __p_(param_type(__n)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5420 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5421 | explicit chi_squared_distribution(const param_type& __p) |
| 5422 | : __p_(__p) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5423 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5424 | void reset() {} |
| 5425 | |
| 5426 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5427 | template<class _URNG> |
| 5428 | _LIBCPP_INLINE_VISIBILITY |
| 5429 | result_type operator()(_URNG& __g) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5430 | {return (*this)(__g, __p_);} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5431 | template<class _URNG> |
| 5432 | _LIBCPP_INLINE_VISIBILITY |
| 5433 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5434 | {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);} |
| 5435 | |
| 5436 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5437 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5438 | result_type n() const {return __p_.n();} |
| 5439 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5440 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5441 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5442 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5443 | void param(const param_type& __p) {__p_ = __p;} |
| 5444 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5445 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5446 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5447 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5448 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5449 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5450 | friend _LIBCPP_INLINE_VISIBILITY |
| 5451 | bool operator==(const chi_squared_distribution& __x, |
| 5452 | const chi_squared_distribution& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5453 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5454 | friend _LIBCPP_INLINE_VISIBILITY |
| 5455 | bool operator!=(const chi_squared_distribution& __x, |
| 5456 | const chi_squared_distribution& __y) |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5457 | {return !(__x == __y);} |
| 5458 | }; |
| 5459 | |
| 5460 | template <class _CharT, class _Traits, class _RT> |
| 5461 | basic_ostream<_CharT, _Traits>& |
| 5462 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5463 | const chi_squared_distribution<_RT>& __x) |
| 5464 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5465 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5466 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5467 | ios_base::scientific); |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5468 | __os << __x.n(); |
| 5469 | return __os; |
| 5470 | } |
| 5471 | |
| 5472 | template <class _CharT, class _Traits, class _RT> |
| 5473 | basic_istream<_CharT, _Traits>& |
| 5474 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5475 | chi_squared_distribution<_RT>& __x) |
| 5476 | { |
| 5477 | typedef chi_squared_distribution<_RT> _Eng; |
| 5478 | typedef typename _Eng::result_type result_type; |
| 5479 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5480 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5481 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5482 | result_type __n; |
| 5483 | __is >> __n; |
| 5484 | if (!__is.fail()) |
| 5485 | __x.param(param_type(__n)); |
| 5486 | return __is; |
| 5487 | } |
| 5488 | |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5489 | // cauchy_distribution |
| 5490 | |
| 5491 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5492 | class _LIBCPP_TEMPLATE_VIS cauchy_distribution |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5493 | { |
| 5494 | public: |
| 5495 | // types |
| 5496 | typedef _RealType result_type; |
| 5497 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5498 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5499 | { |
| 5500 | result_type __a_; |
| 5501 | result_type __b_; |
| 5502 | public: |
| 5503 | typedef cauchy_distribution distribution_type; |
| 5504 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5505 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5506 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5507 | : __a_(__a), __b_(__b) {} |
| 5508 | |
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 | result_type a() const {return __a_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5511 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5512 | result_type b() const {return __b_;} |
| 5513 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5514 | friend _LIBCPP_INLINE_VISIBILITY |
| 5515 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5516 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5517 | friend _LIBCPP_INLINE_VISIBILITY |
| 5518 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5519 | {return !(__x == __y);} |
| 5520 | }; |
| 5521 | |
| 5522 | private: |
| 5523 | param_type __p_; |
| 5524 | |
| 5525 | public: |
| 5526 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5527 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5528 | explicit cauchy_distribution(result_type __a = 0, result_type __b = 1) |
| 5529 | : __p_(param_type(__a, __b)) {} |
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 | explicit cauchy_distribution(const param_type& __p) |
| 5532 | : __p_(__p) {} |
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 | void reset() {} |
| 5535 | |
| 5536 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5537 | template<class _URNG> |
| 5538 | _LIBCPP_INLINE_VISIBILITY |
| 5539 | result_type operator()(_URNG& __g) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5540 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5541 | template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5542 | |
| 5543 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5544 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5545 | result_type a() const {return __p_.a();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5546 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5547 | result_type b() const {return __p_.b();} |
| 5548 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5549 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5550 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5551 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5552 | void param(const param_type& __p) {__p_ = __p;} |
| 5553 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5554 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5555 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5556 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5557 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5558 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5559 | friend _LIBCPP_INLINE_VISIBILITY |
| 5560 | bool operator==(const cauchy_distribution& __x, |
| 5561 | const cauchy_distribution& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5562 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5563 | friend _LIBCPP_INLINE_VISIBILITY |
| 5564 | bool operator!=(const cauchy_distribution& __x, |
| 5565 | const cauchy_distribution& __y) |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5566 | {return !(__x == __y);} |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5567 | }; |
| 5568 | |
| 5569 | template <class _RealType> |
| 5570 | template<class _URNG> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5571 | inline |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5572 | _RealType |
| 5573 | cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5574 | { |
| 5575 | uniform_real_distribution<result_type> __gen; |
| 5576 | // 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] | 5577 | return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g)); |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | template <class _CharT, class _Traits, class _RT> |
| 5581 | basic_ostream<_CharT, _Traits>& |
| 5582 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5583 | const cauchy_distribution<_RT>& __x) |
| 5584 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5585 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5586 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5587 | ios_base::scientific); |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5588 | _CharT __sp = __os.widen(' '); |
| 5589 | __os.fill(__sp); |
| 5590 | __os << __x.a() << __sp << __x.b(); |
| 5591 | return __os; |
| 5592 | } |
| 5593 | |
| 5594 | template <class _CharT, class _Traits, class _RT> |
| 5595 | basic_istream<_CharT, _Traits>& |
| 5596 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5597 | cauchy_distribution<_RT>& __x) |
| 5598 | { |
| 5599 | typedef cauchy_distribution<_RT> _Eng; |
| 5600 | typedef typename _Eng::result_type result_type; |
| 5601 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5602 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | d7d0113 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5603 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5604 | result_type __a; |
| 5605 | result_type __b; |
| 5606 | __is >> __a >> __b; |
| 5607 | if (!__is.fail()) |
| 5608 | __x.param(param_type(__a, __b)); |
| 5609 | return __is; |
| 5610 | } |
| 5611 | |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5612 | // fisher_f_distribution |
| 5613 | |
| 5614 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5615 | class _LIBCPP_TEMPLATE_VIS fisher_f_distribution |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5616 | { |
| 5617 | public: |
| 5618 | // types |
| 5619 | typedef _RealType result_type; |
| 5620 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5621 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5622 | { |
| 5623 | result_type __m_; |
| 5624 | result_type __n_; |
| 5625 | public: |
| 5626 | typedef fisher_f_distribution distribution_type; |
| 5627 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5628 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5629 | explicit param_type(result_type __m = 1, result_type __n = 1) |
| 5630 | : __m_(__m), __n_(__n) {} |
| 5631 | |
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 | result_type m() const {return __m_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5634 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5635 | result_type n() const {return __n_;} |
| 5636 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5637 | friend _LIBCPP_INLINE_VISIBILITY |
| 5638 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5639 | {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5640 | friend _LIBCPP_INLINE_VISIBILITY |
| 5641 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5642 | {return !(__x == __y);} |
| 5643 | }; |
| 5644 | |
| 5645 | private: |
| 5646 | param_type __p_; |
| 5647 | |
| 5648 | public: |
| 5649 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5650 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5651 | explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1) |
| 5652 | : __p_(param_type(__m, __n)) {} |
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 | explicit fisher_f_distribution(const param_type& __p) |
| 5655 | : __p_(__p) {} |
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 | void reset() {} |
| 5658 | |
| 5659 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5660 | template<class _URNG> |
| 5661 | _LIBCPP_INLINE_VISIBILITY |
| 5662 | result_type operator()(_URNG& __g) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5663 | {return (*this)(__g, __p_);} |
| 5664 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5665 | |
| 5666 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5667 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5668 | result_type m() const {return __p_.m();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5669 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5670 | result_type n() const {return __p_.n();} |
| 5671 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5672 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5673 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5674 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5675 | void param(const param_type& __p) {__p_ = __p;} |
| 5676 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5677 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5678 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5679 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5680 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5681 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5682 | friend _LIBCPP_INLINE_VISIBILITY |
| 5683 | bool operator==(const fisher_f_distribution& __x, |
| 5684 | const fisher_f_distribution& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5685 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5686 | friend _LIBCPP_INLINE_VISIBILITY |
| 5687 | bool operator!=(const fisher_f_distribution& __x, |
| 5688 | const fisher_f_distribution& __y) |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5689 | {return !(__x == __y);} |
| 5690 | }; |
| 5691 | |
| 5692 | template <class _RealType> |
| 5693 | template<class _URNG> |
| 5694 | _RealType |
| 5695 | fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5696 | { |
| 5697 | gamma_distribution<result_type> __gdm(__p.m() * result_type(.5)); |
| 5698 | gamma_distribution<result_type> __gdn(__p.n() * result_type(.5)); |
| 5699 | return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g)); |
| 5700 | } |
| 5701 | |
| 5702 | template <class _CharT, class _Traits, class _RT> |
| 5703 | basic_ostream<_CharT, _Traits>& |
| 5704 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5705 | const fisher_f_distribution<_RT>& __x) |
| 5706 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5707 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5708 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5709 | ios_base::scientific); |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5710 | _CharT __sp = __os.widen(' '); |
| 5711 | __os.fill(__sp); |
| 5712 | __os << __x.m() << __sp << __x.n(); |
| 5713 | return __os; |
| 5714 | } |
| 5715 | |
| 5716 | template <class _CharT, class _Traits, class _RT> |
| 5717 | basic_istream<_CharT, _Traits>& |
| 5718 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5719 | fisher_f_distribution<_RT>& __x) |
| 5720 | { |
| 5721 | typedef fisher_f_distribution<_RT> _Eng; |
| 5722 | typedef typename _Eng::result_type result_type; |
| 5723 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5724 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5725 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5726 | result_type __m; |
| 5727 | result_type __n; |
| 5728 | __is >> __m >> __n; |
| 5729 | if (!__is.fail()) |
| 5730 | __x.param(param_type(__m, __n)); |
| 5731 | return __is; |
| 5732 | } |
| 5733 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5734 | // student_t_distribution |
| 5735 | |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5736 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5737 | class _LIBCPP_TEMPLATE_VIS student_t_distribution |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5738 | { |
| 5739 | public: |
| 5740 | // types |
| 5741 | typedef _RealType result_type; |
| 5742 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5743 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5744 | { |
| 5745 | result_type __n_; |
| 5746 | public: |
| 5747 | typedef student_t_distribution distribution_type; |
| 5748 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5749 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5750 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5751 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5752 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5753 | result_type n() const {return __n_;} |
| 5754 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5755 | friend _LIBCPP_INLINE_VISIBILITY |
| 5756 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5757 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5758 | friend _LIBCPP_INLINE_VISIBILITY |
| 5759 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5760 | {return !(__x == __y);} |
| 5761 | }; |
| 5762 | |
| 5763 | private: |
| 5764 | param_type __p_; |
| 5765 | normal_distribution<result_type> __nd_; |
| 5766 | |
| 5767 | public: |
| 5768 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5769 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5770 | explicit student_t_distribution(result_type __n = 1) |
| 5771 | : __p_(param_type(__n)) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5772 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5773 | explicit student_t_distribution(const param_type& __p) |
| 5774 | : __p_(__p) {} |
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 | void reset() {__nd_.reset();} |
| 5777 | |
| 5778 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5779 | template<class _URNG> |
| 5780 | _LIBCPP_INLINE_VISIBILITY |
| 5781 | result_type operator()(_URNG& __g) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5782 | {return (*this)(__g, __p_);} |
| 5783 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5784 | |
| 5785 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5786 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5787 | result_type n() const {return __p_.n();} |
| 5788 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5789 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5790 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5791 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5792 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5793 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5794 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5795 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5796 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5797 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 5798 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5799 | friend _LIBCPP_INLINE_VISIBILITY |
| 5800 | bool operator==(const student_t_distribution& __x, |
| 5801 | const student_t_distribution& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5802 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5803 | friend _LIBCPP_INLINE_VISIBILITY |
| 5804 | bool operator!=(const student_t_distribution& __x, |
| 5805 | const student_t_distribution& __y) |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5806 | {return !(__x == __y);} |
| 5807 | }; |
| 5808 | |
| 5809 | template <class _RealType> |
| 5810 | template<class _URNG> |
| 5811 | _RealType |
| 5812 | student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5813 | { |
| 5814 | gamma_distribution<result_type> __gd(__p.n() * .5, 2); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5815 | return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g)); |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5816 | } |
| 5817 | |
| 5818 | template <class _CharT, class _Traits, class _RT> |
| 5819 | basic_ostream<_CharT, _Traits>& |
| 5820 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5821 | const student_t_distribution<_RT>& __x) |
| 5822 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5823 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5824 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5825 | ios_base::scientific); |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5826 | __os << __x.n(); |
| 5827 | return __os; |
| 5828 | } |
| 5829 | |
| 5830 | template <class _CharT, class _Traits, class _RT> |
| 5831 | basic_istream<_CharT, _Traits>& |
| 5832 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5833 | student_t_distribution<_RT>& __x) |
| 5834 | { |
| 5835 | typedef student_t_distribution<_RT> _Eng; |
| 5836 | typedef typename _Eng::result_type result_type; |
| 5837 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5838 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 321b4bb | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5839 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5840 | result_type __n; |
| 5841 | __is >> __n; |
| 5842 | if (!__is.fail()) |
| 5843 | __x.param(param_type(__n)); |
| 5844 | return __is; |
| 5845 | } |
| 5846 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5847 | // discrete_distribution |
| 5848 | |
| 5849 | template<class _IntType = int> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5850 | class _LIBCPP_TEMPLATE_VIS discrete_distribution |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5851 | { |
| 5852 | public: |
| 5853 | // types |
| 5854 | typedef _IntType result_type; |
| 5855 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5856 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5857 | { |
| 5858 | vector<double> __p_; |
| 5859 | public: |
| 5860 | typedef discrete_distribution distribution_type; |
| 5861 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5862 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5863 | param_type() {} |
| 5864 | template<class _InputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5865 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5866 | param_type(_InputIterator __f, _InputIterator __l) |
| 5867 | : __p_(__f, __l) {__init();} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5868 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5869 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5870 | param_type(initializer_list<double> __wl) |
| 5871 | : __p_(__wl.begin(), __wl.end()) {__init();} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5872 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5873 | template<class _UnaryOperation> |
| 5874 | param_type(size_t __nw, double __xmin, double __xmax, |
| 5875 | _UnaryOperation __fw); |
| 5876 | |
| 5877 | vector<double> probabilities() const; |
| 5878 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5879 | friend _LIBCPP_INLINE_VISIBILITY |
| 5880 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5881 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5882 | friend _LIBCPP_INLINE_VISIBILITY |
| 5883 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5884 | {return !(__x == __y);} |
| 5885 | |
| 5886 | private: |
| 5887 | void __init(); |
| 5888 | |
| 5889 | friend class discrete_distribution; |
| 5890 | |
| 5891 | template <class _CharT, class _Traits, class _IT> |
| 5892 | friend |
| 5893 | basic_ostream<_CharT, _Traits>& |
| 5894 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5895 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5896 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5897 | template <class _CharT, class _Traits, class _IT> |
| 5898 | friend |
| 5899 | basic_istream<_CharT, _Traits>& |
| 5900 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5901 | discrete_distribution<_IT>& __x); |
| 5902 | }; |
| 5903 | |
| 5904 | private: |
| 5905 | param_type __p_; |
| 5906 | |
| 5907 | public: |
| 5908 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5909 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5910 | discrete_distribution() {} |
| 5911 | template<class _InputIterator> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5912 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5913 | discrete_distribution(_InputIterator __f, _InputIterator __l) |
| 5914 | : __p_(__f, __l) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5915 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5916 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5917 | discrete_distribution(initializer_list<double> __wl) |
| 5918 | : __p_(__wl) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5919 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5920 | template<class _UnaryOperation> |
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 | discrete_distribution(size_t __nw, double __xmin, double __xmax, |
| 5923 | _UnaryOperation __fw) |
| 5924 | : __p_(__nw, __xmin, __xmax, __fw) {} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5925 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7f76450 | 2013-08-14 18:00:20 +0000 | [diff] [blame] | 5926 | explicit discrete_distribution(const param_type& __p) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5927 | : __p_(__p) {} |
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 | void reset() {} |
| 5930 | |
| 5931 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5932 | template<class _URNG> |
| 5933 | _LIBCPP_INLINE_VISIBILITY |
| 5934 | result_type operator()(_URNG& __g) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5935 | {return (*this)(__g, __p_);} |
| 5936 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5937 | |
| 5938 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5939 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5940 | vector<double> probabilities() const {return __p_.probabilities();} |
| 5941 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5942 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5943 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5944 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5945 | void param(const param_type& __p) {__p_ = __p;} |
| 5946 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5947 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5948 | result_type min() const {return 0;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5949 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5950 | result_type max() const {return __p_.__p_.size();} |
| 5951 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5952 | friend _LIBCPP_INLINE_VISIBILITY |
| 5953 | bool operator==(const discrete_distribution& __x, |
| 5954 | const discrete_distribution& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5955 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5956 | friend _LIBCPP_INLINE_VISIBILITY |
| 5957 | bool operator!=(const discrete_distribution& __x, |
| 5958 | const discrete_distribution& __y) |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5959 | {return !(__x == __y);} |
| 5960 | |
| 5961 | template <class _CharT, class _Traits, class _IT> |
| 5962 | friend |
| 5963 | basic_ostream<_CharT, _Traits>& |
| 5964 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5965 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5966 | |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5967 | template <class _CharT, class _Traits, class _IT> |
| 5968 | friend |
| 5969 | basic_istream<_CharT, _Traits>& |
| 5970 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5971 | discrete_distribution<_IT>& __x); |
| 5972 | }; |
| 5973 | |
| 5974 | template<class _IntType> |
| 5975 | template<class _UnaryOperation> |
| 5976 | discrete_distribution<_IntType>::param_type::param_type(size_t __nw, |
| 5977 | double __xmin, |
| 5978 | double __xmax, |
| 5979 | _UnaryOperation __fw) |
| 5980 | { |
| 5981 | if (__nw > 1) |
| 5982 | { |
| 5983 | __p_.reserve(__nw - 1); |
| 5984 | double __d = (__xmax - __xmin) / __nw; |
| 5985 | double __d2 = __d / 2; |
| 5986 | for (size_t __k = 0; __k < __nw; ++__k) |
| 5987 | __p_.push_back(__fw(__xmin + __k * __d + __d2)); |
| 5988 | __init(); |
| 5989 | } |
| 5990 | } |
| 5991 | |
| 5992 | template<class _IntType> |
| 5993 | void |
| 5994 | discrete_distribution<_IntType>::param_type::__init() |
| 5995 | { |
| 5996 | if (!__p_.empty()) |
| 5997 | { |
| 5998 | if (__p_.size() > 1) |
| 5999 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6000 | double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0); |
| 6001 | for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6002 | __i < __e; ++__i) |
| 6003 | *__i /= __s; |
| 6004 | vector<double> __t(__p_.size() - 1); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6005 | _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6006 | swap(__p_, __t); |
| 6007 | } |
| 6008 | else |
| 6009 | { |
| 6010 | __p_.clear(); |
| 6011 | __p_.shrink_to_fit(); |
| 6012 | } |
| 6013 | } |
| 6014 | } |
| 6015 | |
| 6016 | template<class _IntType> |
| 6017 | vector<double> |
| 6018 | discrete_distribution<_IntType>::param_type::probabilities() const |
| 6019 | { |
| 6020 | size_t __n = __p_.size(); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6021 | _VSTD::vector<double> __p(__n+1); |
| 6022 | _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6023 | if (__n > 0) |
| 6024 | __p[__n] = 1 - __p_[__n-1]; |
| 6025 | else |
| 6026 | __p[0] = 1; |
| 6027 | return __p; |
| 6028 | } |
| 6029 | |
| 6030 | template<class _IntType> |
| 6031 | template<class _URNG> |
| 6032 | _IntType |
| 6033 | discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 6034 | { |
| 6035 | uniform_real_distribution<double> __gen; |
| 6036 | return static_cast<_IntType>( |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6037 | _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6038 | __p.__p_.begin()); |
| 6039 | } |
| 6040 | |
| 6041 | template <class _CharT, class _Traits, class _IT> |
| 6042 | basic_ostream<_CharT, _Traits>& |
| 6043 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6044 | const discrete_distribution<_IT>& __x) |
| 6045 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6046 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6047 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6048 | ios_base::scientific); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6049 | _CharT __sp = __os.widen(' '); |
| 6050 | __os.fill(__sp); |
| 6051 | size_t __n = __x.__p_.__p_.size(); |
| 6052 | __os << __n; |
| 6053 | for (size_t __i = 0; __i < __n; ++__i) |
| 6054 | __os << __sp << __x.__p_.__p_[__i]; |
| 6055 | return __os; |
| 6056 | } |
| 6057 | |
| 6058 | template <class _CharT, class _Traits, class _IT> |
| 6059 | basic_istream<_CharT, _Traits>& |
| 6060 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6061 | discrete_distribution<_IT>& __x) |
| 6062 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6063 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6064 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6065 | size_t __n; |
| 6066 | __is >> __n; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6067 | vector<double> __p(__n); |
Howard Hinnant | 551d8e4 | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6068 | for (size_t __i = 0; __i < __n; ++__i) |
| 6069 | __is >> __p[__i]; |
| 6070 | if (!__is.fail()) |
| 6071 | swap(__x.__p_.__p_, __p); |
| 6072 | return __is; |
| 6073 | } |
| 6074 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6075 | // piecewise_constant_distribution |
| 6076 | |
| 6077 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6078 | class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6079 | { |
| 6080 | public: |
| 6081 | // types |
| 6082 | typedef _RealType result_type; |
| 6083 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6084 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6085 | { |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6086 | vector<result_type> __b_; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6087 | vector<result_type> __densities_; |
| 6088 | vector<result_type> __areas_; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6089 | public: |
| 6090 | typedef piecewise_constant_distribution distribution_type; |
| 6091 | |
| 6092 | param_type(); |
| 6093 | template<class _InputIteratorB, class _InputIteratorW> |
| 6094 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6095 | _InputIteratorW __fW); |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6096 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6097 | template<class _UnaryOperation> |
| 6098 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6099 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6100 | template<class _UnaryOperation> |
| 6101 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6102 | _UnaryOperation __fw); |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6103 | param_type & operator=(const param_type& __rhs); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6104 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6105 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6106 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6107 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6108 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6109 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6110 | friend _LIBCPP_INLINE_VISIBILITY |
| 6111 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6112 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6113 | friend _LIBCPP_INLINE_VISIBILITY |
| 6114 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6115 | {return !(__x == __y);} |
| 6116 | |
| 6117 | private: |
| 6118 | void __init(); |
| 6119 | |
| 6120 | friend class piecewise_constant_distribution; |
| 6121 | |
| 6122 | template <class _CharT, class _Traits, class _RT> |
| 6123 | friend |
| 6124 | basic_ostream<_CharT, _Traits>& |
| 6125 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6126 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6127 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6128 | template <class _CharT, class _Traits, class _RT> |
| 6129 | friend |
| 6130 | basic_istream<_CharT, _Traits>& |
| 6131 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6132 | piecewise_constant_distribution<_RT>& __x); |
| 6133 | }; |
| 6134 | |
| 6135 | private: |
| 6136 | param_type __p_; |
| 6137 | |
| 6138 | public: |
| 6139 | // constructor and reset functions |
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() {} |
| 6142 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6143 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6144 | piecewise_constant_distribution(_InputIteratorB __fB, |
| 6145 | _InputIteratorB __lB, |
| 6146 | _InputIteratorW __fW) |
| 6147 | : __p_(__fB, __lB, __fW) {} |
| 6148 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6149 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6150 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6151 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6152 | piecewise_constant_distribution(initializer_list<result_type> __bl, |
| 6153 | _UnaryOperation __fw) |
| 6154 | : __p_(__bl, __fw) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6155 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6156 | |
| 6157 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6158 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6159 | piecewise_constant_distribution(size_t __nw, result_type __xmin, |
| 6160 | result_type __xmax, _UnaryOperation __fw) |
| 6161 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6162 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6163 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6164 | explicit piecewise_constant_distribution(const param_type& __p) |
| 6165 | : __p_(__p) {} |
| 6166 | |
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 reset() {} |
| 6169 | |
| 6170 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6171 | template<class _URNG> |
| 6172 | _LIBCPP_INLINE_VISIBILITY |
| 6173 | result_type operator()(_URNG& __g) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6174 | {return (*this)(__g, __p_);} |
| 6175 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6176 | |
| 6177 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6178 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6179 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6181 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6182 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6183 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6184 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6185 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6186 | void param(const param_type& __p) {__p_ = __p;} |
| 6187 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6188 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6189 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6190 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6191 | result_type max() const {return __p_.__b_.back();} |
| 6192 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6193 | friend _LIBCPP_INLINE_VISIBILITY |
| 6194 | bool operator==(const piecewise_constant_distribution& __x, |
| 6195 | const piecewise_constant_distribution& __y) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6196 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6197 | friend _LIBCPP_INLINE_VISIBILITY |
| 6198 | bool operator!=(const piecewise_constant_distribution& __x, |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6199 | const piecewise_constant_distribution& __y) |
| 6200 | {return !(__x == __y);} |
| 6201 | |
| 6202 | template <class _CharT, class _Traits, class _RT> |
| 6203 | friend |
| 6204 | basic_ostream<_CharT, _Traits>& |
| 6205 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6206 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6207 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6208 | template <class _CharT, class _Traits, class _RT> |
| 6209 | friend |
| 6210 | basic_istream<_CharT, _Traits>& |
| 6211 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6212 | piecewise_constant_distribution<_RT>& __x); |
| 6213 | }; |
| 6214 | |
| 6215 | template<class _RealType> |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6216 | typename piecewise_constant_distribution<_RealType>::param_type & |
| 6217 | piecewise_constant_distribution<_RealType>::param_type::operator= |
| 6218 | (const param_type& __rhs) |
| 6219 | { |
| 6220 | // These can throw |
| 6221 | __b_.reserve (__rhs.__b_.size ()); |
| 6222 | __densities_.reserve(__rhs.__densities_.size()); |
| 6223 | __areas_.reserve (__rhs.__areas_.size()); |
| 6224 | |
| 6225 | // These can not throw |
| 6226 | __b_ = __rhs.__b_; |
| 6227 | __densities_ = __rhs.__densities_; |
| 6228 | __areas_ = __rhs.__areas_; |
| 6229 | return *this; |
| 6230 | } |
| 6231 | |
| 6232 | template<class _RealType> |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6233 | void |
| 6234 | piecewise_constant_distribution<_RealType>::param_type::__init() |
| 6235 | { |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6236 | // __densities_ contains non-normalized areas |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6237 | result_type __total_area = _VSTD::accumulate(__densities_.begin(), |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6238 | __densities_.end(), |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6239 | result_type()); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6240 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6241 | __densities_[__i] /= __total_area; |
| 6242 | // __densities_ contains normalized areas |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6243 | __areas_.assign(__densities_.size(), result_type()); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6244 | _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1, |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6245 | __areas_.begin() + 1); |
| 6246 | // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1] |
| 6247 | __densities_.back() = 1 - __areas_.back(); // correct round off error |
| 6248 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6249 | __densities_[__i] /= (__b_[__i+1] - __b_[__i]); |
| 6250 | // __densities_ now contains __densities_ |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | template<class _RealType> |
| 6254 | piecewise_constant_distribution<_RealType>::param_type::param_type() |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6255 | : __b_(2), |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6256 | __densities_(1, 1.0), |
| 6257 | __areas_(1, 0.0) |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6258 | { |
| 6259 | __b_[1] = 1; |
| 6260 | } |
| 6261 | |
| 6262 | template<class _RealType> |
| 6263 | template<class _InputIteratorB, class _InputIteratorW> |
| 6264 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6265 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6266 | : __b_(__fB, __lB) |
| 6267 | { |
| 6268 | if (__b_.size() < 2) |
| 6269 | { |
| 6270 | __b_.resize(2); |
| 6271 | __b_[0] = 0; |
| 6272 | __b_[1] = 1; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6273 | __densities_.assign(1, 1.0); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6274 | __areas_.assign(1, 0.0); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6275 | } |
| 6276 | else |
| 6277 | { |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6278 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6279 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6280 | __densities_.push_back(*__fW); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6281 | __init(); |
| 6282 | } |
| 6283 | } |
| 6284 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6285 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | e3e3291 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6286 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6287 | template<class _RealType> |
| 6288 | template<class _UnaryOperation> |
| 6289 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6290 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6291 | : __b_(__bl.begin(), __bl.end()) |
| 6292 | { |
| 6293 | if (__b_.size() < 2) |
| 6294 | { |
| 6295 | __b_.resize(2); |
| 6296 | __b_[0] = 0; |
| 6297 | __b_[1] = 1; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6298 | __densities_.assign(1, 1.0); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6299 | __areas_.assign(1, 0.0); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6300 | } |
| 6301 | else |
| 6302 | { |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6303 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6304 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6305 | __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5)); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6306 | __init(); |
| 6307 | } |
| 6308 | } |
| 6309 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6310 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | e3e3291 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6311 | |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6312 | template<class _RealType> |
| 6313 | template<class _UnaryOperation> |
| 6314 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6315 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6316 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6317 | { |
| 6318 | size_t __n = __b_.size() - 1; |
| 6319 | result_type __d = (__xmax - __xmin) / __n; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6320 | __densities_.reserve(__n); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6321 | for (size_t __i = 0; __i < __n; ++__i) |
| 6322 | { |
| 6323 | __b_[__i] = __xmin + __i * __d; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6324 | __densities_.push_back(__fw(__b_[__i] + __d*.5)); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6325 | } |
| 6326 | __b_[__n] = __xmax; |
| 6327 | __init(); |
| 6328 | } |
| 6329 | |
| 6330 | template<class _RealType> |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6331 | template<class _URNG> |
| 6332 | _RealType |
| 6333 | piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6334 | { |
| 6335 | typedef uniform_real_distribution<result_type> _Gen; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6336 | result_type __u = _Gen()(__g); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6337 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6338 | __u) - __p.__areas_.begin() - 1; |
| 6339 | return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k]; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | template <class _CharT, class _Traits, class _RT> |
| 6343 | basic_ostream<_CharT, _Traits>& |
| 6344 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6345 | const piecewise_constant_distribution<_RT>& __x) |
| 6346 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6347 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6348 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6349 | ios_base::scientific); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6350 | _CharT __sp = __os.widen(' '); |
| 6351 | __os.fill(__sp); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6352 | size_t __n = __x.__p_.__b_.size(); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6353 | __os << __n; |
| 6354 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6355 | __os << __sp << __x.__p_.__b_[__i]; |
| 6356 | __n = __x.__p_.__densities_.size(); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6357 | __os << __sp << __n; |
| 6358 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6359 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6360 | __n = __x.__p_.__areas_.size(); |
| 6361 | __os << __sp << __n; |
| 6362 | for (size_t __i = 0; __i < __n; ++__i) |
| 6363 | __os << __sp << __x.__p_.__areas_[__i]; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6364 | return __os; |
| 6365 | } |
| 6366 | |
| 6367 | template <class _CharT, class _Traits, class _RT> |
| 6368 | basic_istream<_CharT, _Traits>& |
| 6369 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6370 | piecewise_constant_distribution<_RT>& __x) |
| 6371 | { |
| 6372 | typedef piecewise_constant_distribution<_RT> _Eng; |
| 6373 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6374 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6375 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6376 | size_t __n; |
| 6377 | __is >> __n; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6378 | vector<result_type> __b(__n); |
| 6379 | for (size_t __i = 0; __i < __n; ++__i) |
| 6380 | __is >> __b[__i]; |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6381 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6382 | vector<result_type> __densities(__n); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6383 | for (size_t __i = 0; __i < __n; ++__i) |
| 6384 | __is >> __densities[__i]; |
| 6385 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6386 | vector<result_type> __areas(__n); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6387 | for (size_t __i = 0; __i < __n; ++__i) |
| 6388 | __is >> __areas[__i]; |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6389 | if (!__is.fail()) |
| 6390 | { |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6391 | swap(__x.__p_.__b_, __b); |
Howard Hinnant | 2a59254 | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6392 | swap(__x.__p_.__densities_, __densities); |
| 6393 | swap(__x.__p_.__areas_, __areas); |
Howard Hinnant | d6d1171 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6394 | } |
| 6395 | return __is; |
| 6396 | } |
| 6397 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6398 | // piecewise_linear_distribution |
| 6399 | |
| 6400 | template<class _RealType = double> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6401 | class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6402 | { |
| 6403 | public: |
| 6404 | // types |
| 6405 | typedef _RealType result_type; |
| 6406 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6407 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6408 | { |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6409 | vector<result_type> __b_; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6410 | vector<result_type> __densities_; |
| 6411 | vector<result_type> __areas_; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6412 | public: |
| 6413 | typedef piecewise_linear_distribution distribution_type; |
| 6414 | |
| 6415 | param_type(); |
| 6416 | template<class _InputIteratorB, class _InputIteratorW> |
| 6417 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6418 | _InputIteratorW __fW); |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6419 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6420 | template<class _UnaryOperation> |
| 6421 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6422 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6423 | template<class _UnaryOperation> |
| 6424 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6425 | _UnaryOperation __fw); |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6426 | param_type & operator=(const param_type& __rhs); |
| 6427 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6428 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6429 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6430 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6431 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6432 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6433 | friend _LIBCPP_INLINE_VISIBILITY |
| 6434 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6435 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6436 | friend _LIBCPP_INLINE_VISIBILITY |
| 6437 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6438 | {return !(__x == __y);} |
| 6439 | |
| 6440 | private: |
| 6441 | void __init(); |
| 6442 | |
| 6443 | friend class piecewise_linear_distribution; |
| 6444 | |
| 6445 | template <class _CharT, class _Traits, class _RT> |
| 6446 | friend |
| 6447 | basic_ostream<_CharT, _Traits>& |
| 6448 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6449 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6450 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6451 | template <class _CharT, class _Traits, class _RT> |
| 6452 | friend |
| 6453 | basic_istream<_CharT, _Traits>& |
| 6454 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6455 | piecewise_linear_distribution<_RT>& __x); |
| 6456 | }; |
| 6457 | |
| 6458 | private: |
| 6459 | param_type __p_; |
| 6460 | |
| 6461 | public: |
| 6462 | // constructor and reset functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6463 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6464 | piecewise_linear_distribution() {} |
| 6465 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6466 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6467 | piecewise_linear_distribution(_InputIteratorB __fB, |
| 6468 | _InputIteratorB __lB, |
| 6469 | _InputIteratorW __fW) |
| 6470 | : __p_(__fB, __lB, __fW) {} |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6471 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6472 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6473 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6474 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6475 | piecewise_linear_distribution(initializer_list<result_type> __bl, |
| 6476 | _UnaryOperation __fw) |
| 6477 | : __p_(__bl, __fw) {} |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6478 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6479 | |
| 6480 | template<class _UnaryOperation> |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6481 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6482 | piecewise_linear_distribution(size_t __nw, result_type __xmin, |
| 6483 | result_type __xmax, _UnaryOperation __fw) |
| 6484 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6485 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6486 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6487 | explicit piecewise_linear_distribution(const param_type& __p) |
| 6488 | : __p_(__p) {} |
| 6489 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6490 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6491 | void reset() {} |
| 6492 | |
| 6493 | // generating functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6494 | template<class _URNG> |
| 6495 | _LIBCPP_INLINE_VISIBILITY |
| 6496 | result_type operator()(_URNG& __g) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6497 | {return (*this)(__g, __p_);} |
| 6498 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6499 | |
| 6500 | // property functions |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6501 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6502 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6503 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6504 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6505 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6506 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6507 | param_type param() const {return __p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6508 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6509 | void param(const param_type& __p) {__p_ = __p;} |
| 6510 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6511 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6512 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6513 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6514 | result_type max() const {return __p_.__b_.back();} |
| 6515 | |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6516 | friend _LIBCPP_INLINE_VISIBILITY |
| 6517 | bool operator==(const piecewise_linear_distribution& __x, |
| 6518 | const piecewise_linear_distribution& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6519 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | b9af2ea | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6520 | friend _LIBCPP_INLINE_VISIBILITY |
| 6521 | bool operator!=(const piecewise_linear_distribution& __x, |
| 6522 | const piecewise_linear_distribution& __y) |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6523 | {return !(__x == __y);} |
| 6524 | |
| 6525 | template <class _CharT, class _Traits, class _RT> |
| 6526 | friend |
| 6527 | basic_ostream<_CharT, _Traits>& |
| 6528 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6529 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6530 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6531 | template <class _CharT, class _Traits, class _RT> |
| 6532 | friend |
| 6533 | basic_istream<_CharT, _Traits>& |
| 6534 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6535 | piecewise_linear_distribution<_RT>& __x); |
| 6536 | }; |
| 6537 | |
| 6538 | template<class _RealType> |
Howard Hinnant | 3c143ad | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6539 | typename piecewise_linear_distribution<_RealType>::param_type & |
| 6540 | piecewise_linear_distribution<_RealType>::param_type::operator= |
| 6541 | (const param_type& __rhs) |
| 6542 | { |
| 6543 | // These can throw |
| 6544 | __b_.reserve (__rhs.__b_.size ()); |
| 6545 | __densities_.reserve(__rhs.__densities_.size()); |
| 6546 | __areas_.reserve (__rhs.__areas_.size()); |
| 6547 | |
| 6548 | // These can not throw |
| 6549 | __b_ = __rhs.__b_; |
| 6550 | __densities_ = __rhs.__densities_; |
| 6551 | __areas_ = __rhs.__areas_; |
| 6552 | return *this; |
| 6553 | } |
| 6554 | |
| 6555 | |
| 6556 | template<class _RealType> |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6557 | void |
| 6558 | piecewise_linear_distribution<_RealType>::param_type::__init() |
| 6559 | { |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6560 | __areas_.assign(__densities_.size() - 1, result_type()); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6561 | result_type _Sp = 0; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6562 | for (size_t __i = 0; __i < __areas_.size(); ++__i) |
| 6563 | { |
| 6564 | __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) * |
| 6565 | (__b_[__i+1] - __b_[__i]) * .5; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6566 | _Sp += __areas_[__i]; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6567 | } |
| 6568 | for (size_t __i = __areas_.size(); __i > 1;) |
| 6569 | { |
| 6570 | --__i; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6571 | __areas_[__i] = __areas_[__i-1] / _Sp; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6572 | } |
| 6573 | __areas_[0] = 0; |
| 6574 | for (size_t __i = 1; __i < __areas_.size(); ++__i) |
| 6575 | __areas_[__i] += __areas_[__i-1]; |
| 6576 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6577 | __densities_[__i] /= _Sp; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
| 6580 | template<class _RealType> |
| 6581 | piecewise_linear_distribution<_RealType>::param_type::param_type() |
| 6582 | : __b_(2), |
| 6583 | __densities_(2, 1.0), |
| 6584 | __areas_(1, 0.0) |
| 6585 | { |
| 6586 | __b_[1] = 1; |
| 6587 | } |
| 6588 | |
| 6589 | template<class _RealType> |
| 6590 | template<class _InputIteratorB, class _InputIteratorW> |
| 6591 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6592 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6593 | : __b_(__fB, __lB) |
| 6594 | { |
| 6595 | if (__b_.size() < 2) |
| 6596 | { |
| 6597 | __b_.resize(2); |
| 6598 | __b_[0] = 0; |
| 6599 | __b_[1] = 1; |
| 6600 | __densities_.assign(2, 1.0); |
| 6601 | __areas_.assign(1, 0.0); |
| 6602 | } |
| 6603 | else |
| 6604 | { |
| 6605 | __densities_.reserve(__b_.size()); |
| 6606 | for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW) |
| 6607 | __densities_.push_back(*__fW); |
| 6608 | __init(); |
| 6609 | } |
| 6610 | } |
| 6611 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6612 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | e3e3291 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6613 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6614 | template<class _RealType> |
| 6615 | template<class _UnaryOperation> |
| 6616 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6617 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6618 | : __b_(__bl.begin(), __bl.end()) |
| 6619 | { |
| 6620 | if (__b_.size() < 2) |
| 6621 | { |
| 6622 | __b_.resize(2); |
| 6623 | __b_[0] = 0; |
| 6624 | __b_[1] = 1; |
| 6625 | __densities_.assign(2, 1.0); |
| 6626 | __areas_.assign(1, 0.0); |
| 6627 | } |
| 6628 | else |
| 6629 | { |
| 6630 | __densities_.reserve(__b_.size()); |
| 6631 | for (size_t __i = 0; __i < __b_.size(); ++__i) |
| 6632 | __densities_.push_back(__fw(__b_[__i])); |
| 6633 | __init(); |
| 6634 | } |
| 6635 | } |
| 6636 | |
Eric Fiselier | 97db517 | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6637 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | e3e3291 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6638 | |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6639 | template<class _RealType> |
| 6640 | template<class _UnaryOperation> |
| 6641 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6642 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6643 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6644 | { |
| 6645 | size_t __n = __b_.size() - 1; |
| 6646 | result_type __d = (__xmax - __xmin) / __n; |
| 6647 | __densities_.reserve(__b_.size()); |
| 6648 | for (size_t __i = 0; __i < __n; ++__i) |
| 6649 | { |
| 6650 | __b_[__i] = __xmin + __i * __d; |
| 6651 | __densities_.push_back(__fw(__b_[__i])); |
| 6652 | } |
| 6653 | __b_[__n] = __xmax; |
| 6654 | __densities_.push_back(__fw(__b_[__n])); |
| 6655 | __init(); |
| 6656 | } |
| 6657 | |
| 6658 | template<class _RealType> |
| 6659 | template<class _URNG> |
| 6660 | _RealType |
| 6661 | piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6662 | { |
| 6663 | typedef uniform_real_distribution<result_type> _Gen; |
| 6664 | result_type __u = _Gen()(__g); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6665 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6666 | __u) - __p.__areas_.begin() - 1; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6667 | __u -= __p.__areas_[__k]; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6668 | const result_type __dk = __p.__densities_[__k]; |
| 6669 | const result_type __dk1 = __p.__densities_[__k+1]; |
| 6670 | const result_type __deltad = __dk1 - __dk; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6671 | const result_type __bk = __p.__b_[__k]; |
| 6672 | if (__deltad == 0) |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6673 | return __u / __dk + __bk; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6674 | const result_type __bk1 = __p.__b_[__k+1]; |
| 6675 | const result_type __deltab = __bk1 - __bk; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6676 | return (__bk * __dk1 - __bk1 * __dk + |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6677 | _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6678 | __deltad; |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6679 | } |
| 6680 | |
| 6681 | template <class _CharT, class _Traits, class _RT> |
| 6682 | basic_ostream<_CharT, _Traits>& |
| 6683 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6684 | const piecewise_linear_distribution<_RT>& __x) |
| 6685 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6686 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6687 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6688 | ios_base::scientific); |
| 6689 | _CharT __sp = __os.widen(' '); |
| 6690 | __os.fill(__sp); |
| 6691 | size_t __n = __x.__p_.__b_.size(); |
| 6692 | __os << __n; |
| 6693 | for (size_t __i = 0; __i < __n; ++__i) |
| 6694 | __os << __sp << __x.__p_.__b_[__i]; |
| 6695 | __n = __x.__p_.__densities_.size(); |
| 6696 | __os << __sp << __n; |
| 6697 | for (size_t __i = 0; __i < __n; ++__i) |
| 6698 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6699 | __n = __x.__p_.__areas_.size(); |
| 6700 | __os << __sp << __n; |
| 6701 | for (size_t __i = 0; __i < __n; ++__i) |
| 6702 | __os << __sp << __x.__p_.__areas_[__i]; |
| 6703 | return __os; |
| 6704 | } |
| 6705 | |
| 6706 | template <class _CharT, class _Traits, class _RT> |
| 6707 | basic_istream<_CharT, _Traits>& |
| 6708 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6709 | piecewise_linear_distribution<_RT>& __x) |
| 6710 | { |
| 6711 | typedef piecewise_linear_distribution<_RT> _Eng; |
| 6712 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6713 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6714 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6715 | size_t __n; |
| 6716 | __is >> __n; |
| 6717 | vector<result_type> __b(__n); |
| 6718 | for (size_t __i = 0; __i < __n; ++__i) |
| 6719 | __is >> __b[__i]; |
| 6720 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6721 | vector<result_type> __densities(__n); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6722 | for (size_t __i = 0; __i < __n; ++__i) |
| 6723 | __is >> __densities[__i]; |
| 6724 | __is >> __n; |
Howard Hinnant | 9650b6c | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6725 | vector<result_type> __areas(__n); |
Howard Hinnant | 5430540 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6726 | for (size_t __i = 0; __i < __n; ++__i) |
| 6727 | __is >> __areas[__i]; |
| 6728 | if (!__is.fail()) |
| 6729 | { |
| 6730 | swap(__x.__p_.__b_, __b); |
| 6731 | swap(__x.__p_.__densities_, __densities); |
| 6732 | swap(__x.__p_.__areas_, __areas); |
| 6733 | } |
| 6734 | return __is; |
| 6735 | } |
| 6736 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6737 | _LIBCPP_END_NAMESPACE_STD |
| 6738 | |
| 6739 | #endif // _LIBCPP_RANDOM |