| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- | 
 | 2 | //===--------------------------- random -----------------------------------===// | 
 | 3 | // | 
| Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | //                     The LLVM Compiler Infrastructure | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // | 
 | 6 | // This file is distributed under the University of Illinois Open Source | 
 | 7 | // License. See LICENSE.TXT for details. | 
 | 8 | // | 
 | 9 | //===----------------------------------------------------------------------===// | 
 | 10 |  | 
 | 11 | #ifndef _LIBCPP_RANDOM | 
 | 12 | #define _LIBCPP_RANDOM | 
 | 13 |  | 
 | 14 | /* | 
 | 15 |     random synopsis | 
 | 16 |  | 
 | 17 | #include <initializer_list> | 
 | 18 |  | 
 | 19 | namespace std | 
 | 20 | { | 
 | 21 |  | 
 | 22 | // Engines | 
 | 23 |  | 
 | 24 | template <class UIntType, UIntType a, UIntType c, UIntType m> | 
 | 25 | class linear_congruential_engine | 
 | 26 | { | 
 | 27 | public: | 
 | 28 |     // types | 
 | 29 |     typedef UIntType result_type; | 
 | 30 |  | 
 | 31 |     // engine characteristics | 
 | 32 |     static constexpr result_type multiplier = a; | 
 | 33 |     static constexpr result_type increment = c; | 
 | 34 |     static constexpr result_type modulus = m; | 
 | 35 |     static constexpr result_type min() { return c == 0u ? 1u: 0u;} | 
 | 36 |     static constexpr result_type max() { return m - 1u;} | 
 | 37 |     static constexpr result_type default_seed = 1u; | 
 | 38 |  | 
 | 39 |     // constructors and seeding functions | 
 | 40 |     explicit linear_congruential_engine(result_type s = default_seed); | 
 | 41 |     template<class Sseq> explicit linear_congruential_engine(Sseq& q); | 
 | 42 |     void seed(result_type s = default_seed); | 
 | 43 |     template<class Sseq> void seed(Sseq& q); | 
 | 44 |  | 
 | 45 |     // generating functions | 
 | 46 |     result_type operator()(); | 
 | 47 |     void discard(unsigned long long z); | 
 | 48 | }; | 
 | 49 |  | 
 | 50 | template <class UIntType, UIntType a, UIntType c, UIntType m> | 
 | 51 | bool | 
 | 52 | operator==(const linear_congruential_engine<UIntType, a, c, m>& x, | 
 | 53 |            const linear_congruential_engine<UIntType, a, c, m>& y); | 
 | 54 |  | 
 | 55 | template <class UIntType, UIntType a, UIntType c, UIntType m> | 
 | 56 | bool | 
 | 57 | operator!=(const linear_congruential_engine<UIntType, a, c, m>& x, | 
 | 58 |            const linear_congruential_engine<UIntType, a, c, m>& y); | 
 | 59 |  | 
 | 60 | template <class charT, class traits, | 
 | 61 |           class UIntType, UIntType a, UIntType c, UIntType m> | 
 | 62 | basic_ostream<charT, traits>& | 
 | 63 | operator<<(basic_ostream<charT, traits>& os, | 
 | 64 |            const linear_congruential_engine<UIntType, a, c, m>& x); | 
 | 65 |  | 
 | 66 | template <class charT, class traits, | 
 | 67 |           class UIntType, UIntType a, UIntType c, UIntType m> | 
 | 68 | basic_istream<charT, traits>& | 
 | 69 | operator>>(basic_istream<charT, traits>& is, | 
 | 70 |            linear_congruential_engine<UIntType, a, c, m>& x); | 
 | 71 |  | 
 | 72 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, | 
 | 73 |           UIntType a, size_t u, UIntType d, size_t s, | 
 | 74 |           UIntType b, size_t t, UIntType c, size_t l, UIntType f> | 
 | 75 | class mersenne_twister_engine | 
 | 76 | { | 
 | 77 | public: | 
 | 78 |     // types | 
 | 79 |     typedef UIntType result_type; | 
 | 80 |  | 
 | 81 |     // engine characteristics | 
 | 82 |     static constexpr size_t word_size = w; | 
 | 83 |     static constexpr size_t state_size = n; | 
 | 84 |     static constexpr size_t shift_size = m; | 
 | 85 |     static constexpr size_t mask_bits = r; | 
 | 86 |     static constexpr result_type xor_mask = a; | 
 | 87 |     static constexpr size_t tempering_u = u; | 
 | 88 |     static constexpr result_type tempering_d = d; | 
 | 89 |     static constexpr size_t tempering_s = s; | 
 | 90 |     static constexpr result_type tempering_b = b; | 
 | 91 |     static constexpr size_t tempering_t = t; | 
 | 92 |     static constexpr result_type tempering_c = c; | 
 | 93 |     static constexpr size_t tempering_l = l; | 
 | 94 |     static constexpr result_type initialization_multiplier = f; | 
 | 95 |     static constexpr result_type min () { return 0; } | 
 | 96 |     static constexpr result_type max() { return 2^w - 1; } | 
 | 97 |     static constexpr result_type default_seed = 5489u; | 
 | 98 |  | 
 | 99 |     // constructors and seeding functions | 
 | 100 |     explicit mersenne_twister_engine(result_type value = default_seed); | 
 | 101 |     template<class Sseq> explicit mersenne_twister_engine(Sseq& q); | 
 | 102 |     void seed(result_type value = default_seed); | 
 | 103 |     template<class Sseq> void seed(Sseq& q); | 
 | 104 |  | 
 | 105 |     // generating functions | 
 | 106 |     result_type operator()(); | 
 | 107 |     void discard(unsigned long long z); | 
 | 108 | }; | 
 | 109 |  | 
 | 110 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, | 
 | 111 |           UIntType a, size_t u, UIntType d, size_t s, | 
 | 112 |           UIntType b, size_t t, UIntType c, size_t l, UIntType f> | 
 | 113 | bool | 
 | 114 | operator==( | 
 | 115 |     const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, | 
 | 116 |     const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); | 
 | 117 |  | 
 | 118 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, | 
 | 119 |           UIntType a, size_t u, UIntType d, size_t s, | 
 | 120 |           UIntType b, size_t t, UIntType c, size_t l, UIntType f> | 
 | 121 | bool | 
 | 122 | operator!=( | 
 | 123 |     const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, | 
 | 124 |     const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); | 
 | 125 |  | 
 | 126 | template <class charT, class traits, | 
 | 127 |           class UIntType, size_t w, size_t n, size_t m, size_t r, | 
 | 128 |           UIntType a, size_t u, UIntType d, size_t s, | 
 | 129 |           UIntType b, size_t t, UIntType c, size_t l, UIntType f> | 
 | 130 | basic_ostream<charT, traits>& | 
 | 131 | operator<<(basic_ostream<charT, traits>& os, | 
 | 132 |            const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); | 
 | 133 |  | 
 | 134 | template <class charT, class traits, | 
 | 135 |           class UIntType, size_t w, size_t n, size_t m, size_t r, | 
 | 136 |           UIntType a, size_t u, UIntType d, size_t s, | 
 | 137 |           UIntType b, size_t t, UIntType c, size_t l, UIntType f> | 
 | 138 | basic_istream<charT, traits>& | 
 | 139 | operator>>(basic_istream<charT, traits>& is, | 
 | 140 |            mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); | 
 | 141 |  | 
 | 142 | template<class UIntType, size_t w, size_t s, size_t r> | 
 | 143 | class subtract_with_carry_engine | 
 | 144 | { | 
 | 145 | public: | 
 | 146 |     // types | 
 | 147 |     typedef UIntType result_type; | 
 | 148 |  | 
 | 149 |     // engine characteristics | 
 | 150 |     static constexpr size_t word_size = w; | 
 | 151 |     static constexpr size_t short_lag = s; | 
 | 152 |     static constexpr size_t long_lag = r; | 
 | 153 |     static constexpr result_type min() { return 0; } | 
 | 154 |     static constexpr result_type max() { return m-1; } | 
 | 155 |     static constexpr result_type default_seed = 19780503u; | 
 | 156 |  | 
 | 157 |     // constructors and seeding functions | 
 | 158 |     explicit subtract_with_carry_engine(result_type value = default_seed); | 
 | 159 |     template<class Sseq> explicit subtract_with_carry_engine(Sseq& q); | 
 | 160 |     void seed(result_type value = default_seed); | 
 | 161 |     template<class Sseq> void seed(Sseq& q); | 
 | 162 |  | 
 | 163 |     // generating functions | 
 | 164 |     result_type operator()(); | 
 | 165 |     void discard(unsigned long long z); | 
 | 166 | }; | 
 | 167 |  | 
 | 168 | template<class UIntType, size_t w, size_t s, size_t r> | 
 | 169 | bool | 
 | 170 | operator==( | 
 | 171 |     const subtract_with_carry_engine<UIntType, w, s, r>& x, | 
 | 172 |     const subtract_with_carry_engine<UIntType, w, s, r>& y); | 
 | 173 |  | 
 | 174 | template<class UIntType, size_t w, size_t s, size_t r> | 
 | 175 | bool | 
 | 176 | operator!=( | 
 | 177 |     const subtract_with_carry_engine<UIntType, w, s, r>& x, | 
 | 178 |     const subtract_with_carry_engine<UIntType, w, s, r>& y); | 
 | 179 |  | 
 | 180 | template <class charT, class traits, | 
 | 181 |           class UIntType, size_t w, size_t s, size_t r> | 
 | 182 | basic_ostream<charT, traits>& | 
 | 183 | operator<<(basic_ostream<charT, traits>& os, | 
 | 184 |            const subtract_with_carry_engine<UIntType, w, s, r>& x); | 
 | 185 |  | 
 | 186 | template <class charT, class traits, | 
 | 187 |           class UIntType, size_t w, size_t s, size_t r> | 
 | 188 | basic_istream<charT, traits>& | 
 | 189 | operator>>(basic_istream<charT, traits>& is, | 
 | 190 |            subtract_with_carry_engine<UIntType, w, s, r>& x); | 
 | 191 |  | 
 | 192 | template<class Engine, size_t p, size_t r> | 
 | 193 | class discard_block_engine | 
 | 194 | { | 
 | 195 | public: | 
 | 196 |     // types | 
 | 197 |     typedef typename Engine::result_type result_type; | 
 | 198 |  | 
 | 199 |     // engine characteristics | 
 | 200 |     static constexpr size_t block_size = p; | 
 | 201 |     static constexpr size_t used_block = r; | 
 | 202 |     static constexpr result_type min() { return Engine::min(); } | 
 | 203 |     static constexpr result_type max() { return Engine::max(); } | 
 | 204 |  | 
 | 205 |     // constructors and seeding functions | 
 | 206 |     discard_block_engine(); | 
 | 207 |     explicit discard_block_engine(const Engine& e); | 
 | 208 |     explicit discard_block_engine(Engine&& e); | 
 | 209 |     explicit discard_block_engine(result_type s); | 
 | 210 |     template<class Sseq> explicit discard_block_engine(Sseq& q); | 
 | 211 |     void seed(); | 
 | 212 |     void seed(result_type s); | 
 | 213 |     template<class Sseq> void seed(Sseq& q); | 
 | 214 |  | 
 | 215 |     // generating functions | 
 | 216 |     result_type operator()(); | 
 | 217 |     void discard(unsigned long long z); | 
 | 218 |  | 
 | 219 |     // property functions | 
 | 220 |     const Engine& base() const; | 
 | 221 | }; | 
 | 222 |  | 
 | 223 | template<class Engine, size_t p, size_t r> | 
 | 224 | bool | 
 | 225 | operator==( | 
 | 226 |     const discard_block_engine<Engine, p, r>& x, | 
 | 227 |     const discard_block_engine<Engine, p, r>& y); | 
 | 228 |  | 
 | 229 | template<class Engine, size_t p, size_t r> | 
 | 230 | bool | 
 | 231 | operator!=( | 
 | 232 |     const discard_block_engine<Engine, p, r>& x, | 
 | 233 |     const discard_block_engine<Engine, p, r>& y); | 
 | 234 |  | 
 | 235 | template <class charT, class traits, | 
 | 236 |           class Engine, size_t p, size_t r> | 
 | 237 | basic_ostream<charT, traits>& | 
 | 238 | operator<<(basic_ostream<charT, traits>& os, | 
 | 239 |            const discard_block_engine<Engine, p, r>& x); | 
 | 240 |  | 
 | 241 | template <class charT, class traits, | 
 | 242 |           class Engine, size_t p, size_t r> | 
 | 243 | basic_istream<charT, traits>& | 
 | 244 | operator>>(basic_istream<charT, traits>& is, | 
 | 245 |            discard_block_engine<Engine, p, r>& x); | 
 | 246 |  | 
 | 247 | template<class Engine, size_t w, class UIntType> | 
 | 248 | class independent_bits_engine | 
 | 249 | { | 
 | 250 | public: | 
 | 251 |     // types | 
 | 252 |     typedef UIntType result_type; | 
 | 253 |  | 
 | 254 |     // engine characteristics | 
 | 255 |     static constexpr result_type min() { return 0; } | 
 | 256 |     static constexpr result_type max() { return 2^w - 1; } | 
 | 257 |  | 
 | 258 |     // constructors and seeding functions | 
 | 259 |     independent_bits_engine(); | 
 | 260 |     explicit independent_bits_engine(const Engine& e); | 
 | 261 |     explicit independent_bits_engine(Engine&& e); | 
 | 262 |     explicit independent_bits_engine(result_type s); | 
 | 263 |     template<class Sseq> explicit independent_bits_engine(Sseq& q); | 
 | 264 |     void seed(); | 
 | 265 |     void seed(result_type s); | 
 | 266 |     template<class Sseq> void seed(Sseq& q); | 
 | 267 |  | 
 | 268 |     // generating functions | 
 | 269 |     result_type operator()(); void discard(unsigned long long z); | 
 | 270 |  | 
 | 271 |     // property functions | 
 | 272 |     const Engine& base() const; | 
 | 273 | }; | 
 | 274 |  | 
 | 275 | template<class Engine, size_t w, class UIntType> | 
 | 276 | bool | 
 | 277 | operator==( | 
 | 278 |     const independent_bits_engine<Engine, w, UIntType>& x, | 
 | 279 |     const independent_bits_engine<Engine, w, UIntType>& y); | 
 | 280 |  | 
 | 281 | template<class Engine, size_t w, class UIntType> | 
 | 282 | bool | 
 | 283 | operator!=( | 
 | 284 |     const independent_bits_engine<Engine, w, UIntType>& x, | 
 | 285 |     const independent_bits_engine<Engine, w, UIntType>& y); | 
 | 286 |  | 
 | 287 | template <class charT, class traits, | 
 | 288 |           class Engine, size_t w, class UIntType> | 
 | 289 | basic_ostream<charT, traits>& | 
 | 290 | operator<<(basic_ostream<charT, traits>& os, | 
 | 291 |            const independent_bits_engine<Engine, w, UIntType>& x); | 
 | 292 |  | 
 | 293 | template <class charT, class traits, | 
 | 294 |           class Engine, size_t w, class UIntType> | 
 | 295 | basic_istream<charT, traits>& | 
 | 296 | operator>>(basic_istream<charT, traits>& is, | 
 | 297 |            independent_bits_engine<Engine, w, UIntType>& x); | 
 | 298 |  | 
 | 299 | template<class Engine, size_t k> | 
 | 300 | class shuffle_order_engine | 
 | 301 | { | 
 | 302 | public: | 
 | 303 |     // types | 
 | 304 |     typedef typename Engine::result_type result_type; | 
 | 305 |  | 
 | 306 |     // engine characteristics | 
 | 307 |     static constexpr size_t table_size = k; | 
 | 308 |     static constexpr result_type min() { return Engine::min; } | 
 | 309 |     static constexpr result_type max() { return Engine::max; } | 
 | 310 |  | 
 | 311 |     // constructors and seeding functions | 
 | 312 |     shuffle_order_engine(); | 
 | 313 |     explicit shuffle_order_engine(const Engine& e); | 
 | 314 |     explicit shuffle_order_engine(Engine&& e); | 
 | 315 |     explicit shuffle_order_engine(result_type s); | 
 | 316 |     template<class Sseq> explicit shuffle_order_engine(Sseq& q); | 
 | 317 |     void seed(); | 
 | 318 |     void seed(result_type s); | 
 | 319 |     template<class Sseq> void seed(Sseq& q); | 
 | 320 |  | 
 | 321 |     // generating functions | 
 | 322 |     result_type operator()(); | 
 | 323 |     void discard(unsigned long long z); | 
 | 324 |  | 
 | 325 |     // property functions | 
 | 326 |     const Engine& base() const; | 
 | 327 | }; | 
 | 328 |  | 
 | 329 | template<class Engine, size_t k> | 
 | 330 | bool | 
 | 331 | operator==( | 
 | 332 |     const shuffle_order_engine<Engine, k>& x, | 
 | 333 |     const shuffle_order_engine<Engine, k>& y); | 
 | 334 |  | 
 | 335 | template<class Engine, size_t k> | 
 | 336 | bool | 
 | 337 | operator!=( | 
 | 338 |     const shuffle_order_engine<Engine, k>& x, | 
 | 339 |     const shuffle_order_engine<Engine, k>& y); | 
 | 340 |  | 
 | 341 | template <class charT, class traits, | 
 | 342 |           class Engine, size_t k> | 
 | 343 | basic_ostream<charT, traits>& | 
 | 344 | operator<<(basic_ostream<charT, traits>& os, | 
 | 345 |            const shuffle_order_engine<Engine, k>& x); | 
 | 346 |  | 
 | 347 | template <class charT, class traits, | 
 | 348 |           class Engine, size_t k> | 
 | 349 | basic_istream<charT, traits>& | 
 | 350 | operator>>(basic_istream<charT, traits>& is, | 
 | 351 |            shuffle_order_engine<Engine, k>& x); | 
 | 352 |  | 
 | 353 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> | 
 | 354 |                                                                    minstd_rand0; | 
 | 355 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> | 
 | 356 |                                                                     minstd_rand; | 
 | 357 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, | 
 | 358 |                                 0x9908b0df, | 
 | 359 |                                 11, 0xffffffff, | 
 | 360 |                                 7,  0x9d2c5680, | 
 | 361 |                                 15, 0xefc60000, | 
 | 362 |                                 18, 1812433253>                         mt19937; | 
 | 363 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, | 
 | 364 |                                 0xb5026f5aa96619e9, | 
 | 365 |                                 29, 0x5555555555555555, | 
 | 366 |                                 17, 0x71d67fffeda60000, | 
 | 367 |                                 37, 0xfff7eee000000000, | 
 | 368 |                                 43, 6364136223846793005>             mt19937_64; | 
 | 369 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>     ranlux24_base; | 
 | 370 | typedef subtract_with_carry_engine<uint_fast64_t, 48,  5, 12>     ranlux48_base; | 
 | 371 | typedef discard_block_engine<ranlux24_base, 223, 23>                   ranlux24; | 
 | 372 | typedef discard_block_engine<ranlux48_base, 389, 11>                   ranlux48; | 
 | 373 | typedef shuffle_order_engine<minstd_rand0, 256>                         knuth_b; | 
 | 374 | typedef minstd_rand0                                      default_random_engine; | 
 | 375 |  | 
 | 376 | // Generators | 
 | 377 |  | 
 | 378 | class random_device | 
 | 379 | { | 
 | 380 | public: | 
 | 381 |     // types | 
 | 382 |     typedef unsigned int result_type; | 
 | 383 |  | 
 | 384 |     // generator characteristics | 
 | 385 |     static constexpr result_type min() { return numeric_limits<result_type>::min(); } | 
 | 386 |     static constexpr result_type max() { return numeric_limits<result_type>::max(); } | 
 | 387 |  | 
 | 388 |     // constructors | 
 | 389 |     explicit random_device(const string& token = "/dev/urandom"); | 
 | 390 |  | 
 | 391 |     // generating functions | 
 | 392 |     result_type operator()(); | 
 | 393 |  | 
 | 394 |     // property functions | 
 | 395 |     double entropy() const; | 
 | 396 |  | 
 | 397 |     // no copy functions | 
 | 398 |     random_device(const random_device& ) = delete; | 
 | 399 |     void operator=(const random_device& ) = delete; | 
 | 400 | }; | 
 | 401 |  | 
 | 402 | // Utilities | 
 | 403 |  | 
 | 404 | class seed_seq | 
 | 405 | { | 
 | 406 | public: | 
 | 407 |     // types | 
 | 408 |     typedef uint_least32_t result_type; | 
 | 409 |  | 
 | 410 |     // constructors | 
 | 411 |     seed_seq(); | 
 | 412 |     template<class T> | 
 | 413 |         seed_seq(initializer_list<T> il); | 
 | 414 |     template<class InputIterator> | 
 | 415 |         seed_seq(InputIterator begin, InputIterator end); | 
 | 416 |  | 
 | 417 |     // generating functions | 
 | 418 |     template<class RandomAccessIterator> | 
 | 419 |         void generate(RandomAccessIterator begin, RandomAccessIterator end); | 
 | 420 |  | 
 | 421 |     // property functions | 
 | 422 |     size_t size() const; | 
 | 423 |     template<class OutputIterator> | 
 | 424 |         void param(OutputIterator dest) const; | 
 | 425 |  | 
 | 426 |     // no copy functions | 
 | 427 |     seed_seq(const seed_seq&) = delete; | 
 | 428 |     void operator=(const seed_seq& ) = delete; | 
 | 429 | }; | 
 | 430 |  | 
 | 431 | template<class RealType, size_t bits, class URNG> | 
 | 432 |     RealType generate_canonical(URNG& g); | 
 | 433 |  | 
 | 434 | // Distributions | 
 | 435 |  | 
 | 436 | template<class IntType = int> | 
 | 437 | class uniform_int_distribution | 
 | 438 | { | 
 | 439 | public: | 
 | 440 |     // types | 
 | 441 |     typedef IntType result_type; | 
 | 442 |  | 
 | 443 |     class param_type | 
 | 444 |     { | 
 | 445 |     public: | 
 | 446 |         typedef uniform_int_distribution distribution_type; | 
 | 447 |  | 
 | 448 |         explicit param_type(IntType a = 0, | 
 | 449 |                                     IntType b = numeric_limits<IntType>::max()); | 
 | 450 |  | 
 | 451 |         result_type a() const; | 
 | 452 |         result_type b() const; | 
 | 453 |  | 
 | 454 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 455 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 456 |     }; | 
 | 457 |  | 
 | 458 |     // constructors and reset functions | 
 | 459 |     explicit uniform_int_distribution(IntType a = 0, | 
 | 460 |                                     IntType b = numeric_limits<IntType>::max()); | 
 | 461 |     explicit uniform_int_distribution(const param_type& parm); | 
 | 462 |     void reset(); | 
 | 463 |  | 
 | 464 |     // generating functions | 
 | 465 |     template<class URNG> result_type operator()(URNG& g); | 
 | 466 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 467 |  | 
 | 468 |     // property functions | 
 | 469 |     result_type a() const; | 
 | 470 |     result_type b() const; | 
 | 471 |  | 
 | 472 |     param_type param() const; | 
 | 473 |     void param(const param_type& parm); | 
 | 474 |  | 
 | 475 |     result_type min() const; | 
 | 476 |     result_type max() const; | 
 | 477 |  | 
 | 478 |     friend bool operator==(const uniform_int_distribution& x, | 
 | 479 |                            const uniform_int_distribution& y); | 
 | 480 |     friend bool operator!=(const uniform_int_distribution& x, | 
 | 481 |                            const uniform_int_distribution& y); | 
 | 482 |  | 
 | 483 |     template <class charT, class traits> | 
 | 484 |     friend | 
 | 485 |     basic_ostream<charT, traits>& | 
 | 486 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 487 |                const uniform_int_distribution& x); | 
 | 488 |      | 
 | 489 |     template <class charT, class traits> | 
 | 490 |     friend | 
 | 491 |     basic_istream<charT, traits>& | 
 | 492 |     operator>>(basic_istream<charT, traits>& is, | 
 | 493 |                uniform_int_distribution& x); | 
 | 494 | }; | 
 | 495 |  | 
 | 496 | template<class RealType = double> | 
 | 497 | class uniform_real_distribution | 
 | 498 | { | 
 | 499 | public: | 
 | 500 |     // types | 
 | 501 |     typedef RealType result_type; | 
 | 502 |  | 
 | 503 |     class param_type | 
 | 504 |     { | 
 | 505 |     public: | 
 | 506 |         typedef uniform_real_distribution distribution_type; | 
 | 507 |  | 
 | 508 |         explicit param_type(RealType a = 0, | 
 | 509 |                             RealType b = 1); | 
 | 510 |  | 
 | 511 |         result_type a() const; | 
 | 512 |         result_type b() const; | 
 | 513 |  | 
 | 514 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 515 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 516 |     }; | 
 | 517 |  | 
 | 518 |     // constructors and reset functions | 
 | 519 |     explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); | 
 | 520 |     explicit uniform_real_distribution(const param_type& parm); | 
 | 521 |     void reset(); | 
 | 522 |  | 
 | 523 |     // generating functions | 
 | 524 |     template<class URNG> result_type operator()(URNG& g); | 
 | 525 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 526 |  | 
 | 527 |     // property functions | 
 | 528 |     result_type a() const; | 
 | 529 |     result_type b() const; | 
 | 530 |  | 
 | 531 |     param_type param() const; | 
 | 532 |     void param(const param_type& parm); | 
 | 533 |  | 
 | 534 |     result_type min() const; | 
 | 535 |     result_type max() const; | 
 | 536 |  | 
 | 537 |     friend bool operator==(const uniform_real_distribution& x, | 
 | 538 |                            const uniform_real_distribution& y); | 
 | 539 |     friend bool operator!=(const uniform_real_distribution& x, | 
 | 540 |                            const uniform_real_distribution& y); | 
 | 541 |  | 
 | 542 |     template <class charT, class traits> | 
 | 543 |     friend | 
 | 544 |     basic_ostream<charT, traits>& | 
 | 545 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 546 |                const uniform_real_distribution& x); | 
 | 547 |      | 
 | 548 |     template <class charT, class traits> | 
 | 549 |     friend | 
 | 550 |     basic_istream<charT, traits>& | 
 | 551 |     operator>>(basic_istream<charT, traits>& is, | 
 | 552 |                uniform_real_distribution& x); | 
 | 553 | }; | 
 | 554 |  | 
 | 555 | class bernoulli_distribution | 
 | 556 | { | 
 | 557 | public: | 
 | 558 |     // types | 
 | 559 |     typedef bool result_type; | 
 | 560 |  | 
 | 561 |     class param_type | 
 | 562 |     { | 
 | 563 |     public: | 
 | 564 |         typedef bernoulli_distribution distribution_type; | 
 | 565 |  | 
 | 566 |         explicit param_type(double p = 0.5); | 
 | 567 |  | 
 | 568 |         double p() const; | 
 | 569 |  | 
 | 570 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 571 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 572 |     }; | 
 | 573 |  | 
 | 574 |     // constructors and reset functions | 
 | 575 |     explicit bernoulli_distribution(double p = 0.5); | 
 | 576 |     explicit bernoulli_distribution(const param_type& parm); | 
 | 577 |     void reset(); | 
 | 578 |  | 
 | 579 |     // generating functions | 
 | 580 |     template<class URNG> result_type operator()(URNG& g); | 
 | 581 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 582 |  | 
 | 583 |     // property functions | 
 | 584 |     double p() const; | 
 | 585 |  | 
 | 586 |     param_type param() const; | 
 | 587 |     void param(const param_type& parm); | 
 | 588 |  | 
 | 589 |     result_type min() const; | 
 | 590 |     result_type max() const; | 
 | 591 |  | 
 | 592 |     friend bool operator==(const bernoulli_distribution& x, | 
 | 593 |                            const bernoulli_distribution& y); | 
 | 594 |     friend bool operator!=(const bernoulli_distribution& x, | 
 | 595 |                            const bernoulli_distribution& y); | 
 | 596 |  | 
 | 597 |     template <class charT, class traits> | 
 | 598 |     friend | 
 | 599 |     basic_ostream<charT, traits>& | 
 | 600 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 601 |                const bernoulli_distribution& x); | 
 | 602 |      | 
 | 603 |     template <class charT, class traits> | 
 | 604 |     friend | 
 | 605 |     basic_istream<charT, traits>& | 
 | 606 |     operator>>(basic_istream<charT, traits>& is, | 
 | 607 |                bernoulli_distribution& x); | 
 | 608 | }; | 
 | 609 |  | 
 | 610 | template<class IntType = int> | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 611 | class binomial_distribution | 
 | 612 | { | 
 | 613 | public: | 
 | 614 |     // types | 
 | 615 |     typedef IntType result_type; | 
 | 616 |  | 
 | 617 |     class param_type | 
 | 618 |     { | 
 | 619 |     public: | 
 | 620 |         typedef binomial_distribution distribution_type; | 
 | 621 |  | 
 | 622 |         explicit param_type(IntType t = 1, double p = 0.5); | 
 | 623 |  | 
 | 624 |         IntType t() const; | 
 | 625 |         double p() const; | 
 | 626 |  | 
 | 627 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 628 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 629 |     }; | 
 | 630 |  | 
 | 631 |     // constructors and reset functions | 
 | 632 |     explicit binomial_distribution(IntType t = 1, double p = 0.5); | 
 | 633 |     explicit binomial_distribution(const param_type& parm); | 
 | 634 |     void reset(); | 
 | 635 |  | 
 | 636 |     // generating functions | 
 | 637 |     template<class URNG> result_type operator()(URNG& g); | 
 | 638 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 639 |  | 
 | 640 |     // property functions | 
 | 641 |     IntType t() const; | 
 | 642 |     double p() const; | 
 | 643 |  | 
 | 644 |     param_type param() const; | 
 | 645 |     void param(const param_type& parm); | 
 | 646 |  | 
 | 647 |     result_type min() const; | 
 | 648 |     result_type max() const; | 
 | 649 |  | 
 | 650 |     friend bool operator==(const binomial_distribution& x, | 
 | 651 |                            const binomial_distribution& y); | 
 | 652 |     friend bool operator!=(const binomial_distribution& x, | 
 | 653 |                            const binomial_distribution& y); | 
 | 654 |  | 
 | 655 |     template <class charT, class traits> | 
 | 656 |     friend | 
 | 657 |     basic_ostream<charT, traits>& | 
 | 658 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 659 |                const binomial_distribution& x); | 
 | 660 |      | 
 | 661 |     template <class charT, class traits> | 
 | 662 |     friend | 
 | 663 |     basic_istream<charT, traits>& | 
 | 664 |     operator>>(basic_istream<charT, traits>& is, | 
 | 665 |                binomial_distribution& x); | 
 | 666 | }; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 667 |  | 
 | 668 | template<class IntType = int> | 
 | 669 |     class geometric_distribution; | 
 | 670 |  | 
 | 671 | template<class IntType = int> | 
 | 672 |     class negative_binomial_distribution; | 
 | 673 |  | 
 | 674 | template<class IntType = int> | 
| Howard Hinnant | 4ff556c | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 675 | class poisson_distribution | 
 | 676 | { | 
 | 677 | public: | 
 | 678 |     // types | 
 | 679 |     typedef IntType result_type; | 
 | 680 |  | 
 | 681 |     class param_type | 
 | 682 |     { | 
 | 683 |     public: | 
 | 684 |         typedef poisson_distribution distribution_type; | 
 | 685 |  | 
 | 686 |         explicit param_type(double mean = 1.0); | 
 | 687 |  | 
 | 688 |         double mean() const; | 
 | 689 |  | 
 | 690 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 691 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 692 |     }; | 
 | 693 |  | 
 | 694 |     // constructors and reset functions | 
 | 695 |     explicit poisson_distribution(double mean = 1.0); | 
 | 696 |     explicit poisson_distribution(const param_type& parm); | 
 | 697 |     void reset(); | 
 | 698 |  | 
 | 699 |     // generating functions | 
 | 700 |     template<class URNG> result_type operator()(URNG& g); | 
 | 701 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 702 |  | 
 | 703 |     // property functions | 
 | 704 |     double mean() const; | 
 | 705 |  | 
 | 706 |     param_type param() const; | 
 | 707 |     void param(const param_type& parm); | 
 | 708 |  | 
 | 709 |     result_type min() const; | 
 | 710 |     result_type max() const; | 
 | 711 |  | 
 | 712 |     friend bool operator==(const poisson_distribution& x, | 
 | 713 |                            const poisson_distribution& y); | 
 | 714 |     friend bool operator!=(const poisson_distribution& x, | 
 | 715 |                            const poisson_distribution& y); | 
 | 716 |  | 
 | 717 |     template <class charT, class traits> | 
 | 718 |     friend | 
 | 719 |     basic_ostream<charT, traits>& | 
 | 720 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 721 |                const poisson_distribution& x); | 
 | 722 |      | 
 | 723 |     template <class charT, class traits> | 
 | 724 |     friend | 
 | 725 |     basic_istream<charT, traits>& | 
 | 726 |     operator>>(basic_istream<charT, traits>& is, | 
 | 727 |                poisson_distribution& x); | 
 | 728 | }; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 729 |  | 
 | 730 | template<class RealType = double> | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 731 | class exponential_distribution | 
 | 732 | { | 
 | 733 | public: | 
 | 734 |     // types | 
 | 735 |     typedef RealType result_type; | 
 | 736 |  | 
 | 737 |     class param_type | 
 | 738 |     { | 
 | 739 |     public: | 
 | 740 |         typedef exponential_distribution distribution_type; | 
 | 741 |  | 
| Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 742 |         explicit param_type(result_type lambda = 1.0); | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 743 |  | 
| Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 744 |         result_type lambda() const; | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 745 |  | 
 | 746 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 747 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 748 |     }; | 
 | 749 |  | 
 | 750 |     // constructors and reset functions | 
| Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 751 |     explicit exponential_distribution(result_type lambda = 1.0); | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 752 |     explicit exponential_distribution(const param_type& parm); | 
 | 753 |     void reset(); | 
 | 754 |  | 
 | 755 |     // generating functions | 
 | 756 |     template<class URNG> result_type operator()(URNG& g); | 
 | 757 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 758 |  | 
 | 759 |     // property functions | 
| Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 760 |     result_type lambda() const; | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 761 |  | 
 | 762 |     param_type param() const; | 
 | 763 |     void param(const param_type& parm); | 
 | 764 |  | 
 | 765 |     result_type min() const; | 
 | 766 |     result_type max() const; | 
 | 767 |  | 
 | 768 |     friend bool operator==(const exponential_distribution& x, | 
 | 769 |                            const exponential_distribution& y); | 
 | 770 |     friend bool operator!=(const exponential_distribution& x, | 
 | 771 |                            const exponential_distribution& y); | 
 | 772 |  | 
 | 773 |     template <class charT, class traits> | 
 | 774 |     friend | 
 | 775 |     basic_ostream<charT, traits>& | 
 | 776 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 777 |                const exponential_distribution& x); | 
 | 778 |      | 
 | 779 |     template <class charT, class traits> | 
 | 780 |     friend | 
 | 781 |     basic_istream<charT, traits>& | 
 | 782 |     operator>>(basic_istream<charT, traits>& is, | 
 | 783 |                exponential_distribution& x); | 
 | 784 | }; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 785 |  | 
 | 786 | template<class RealType = double> | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 787 | class gamma_distribution | 
 | 788 | { | 
 | 789 | public: | 
 | 790 |     // types | 
 | 791 |     typedef RealType result_type; | 
 | 792 |  | 
 | 793 |     class param_type | 
 | 794 |     { | 
 | 795 |     public: | 
 | 796 |         typedef gamma_distribution distribution_type; | 
 | 797 |  | 
 | 798 |         explicit param_type(result_type alpha = 1, result_type beta = 1); | 
 | 799 |  | 
 | 800 |         result_type alpha() const; | 
 | 801 |         result_type beta() const; | 
 | 802 |  | 
 | 803 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 804 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 805 |     }; | 
 | 806 |  | 
 | 807 |     // constructors and reset functions | 
 | 808 |     explicit gamma_distribution(result_type alpha = 1, result_type beta = 1); | 
 | 809 |     explicit gamma_distribution(const param_type& parm); | 
 | 810 |     void reset(); | 
 | 811 |  | 
 | 812 |     // generating functions | 
 | 813 |     template<class URNG> result_type operator()(URNG& g); | 
 | 814 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 815 |  | 
 | 816 |     // property functions | 
 | 817 |     result_type alpha() const; | 
 | 818 |     result_type beta() const; | 
 | 819 |  | 
 | 820 |     param_type param() const; | 
 | 821 |     void param(const param_type& parm); | 
 | 822 |  | 
 | 823 |     result_type min() const; | 
 | 824 |     result_type max() const; | 
 | 825 |  | 
 | 826 |     friend bool operator==(const gamma_distribution& x, | 
 | 827 |                            const gamma_distribution& y); | 
 | 828 |     friend bool operator!=(const gamma_distribution& x, | 
 | 829 |                            const gamma_distribution& y); | 
 | 830 |  | 
 | 831 |     template <class charT, class traits> | 
 | 832 |     friend | 
 | 833 |     basic_ostream<charT, traits>& | 
 | 834 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 835 |                const gamma_distribution& x); | 
 | 836 |      | 
 | 837 |     template <class charT, class traits> | 
 | 838 |     friend | 
 | 839 |     basic_istream<charT, traits>& | 
 | 840 |     operator>>(basic_istream<charT, traits>& is, | 
 | 841 |                gamma_distribution& x); | 
 | 842 | }; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 843 |  | 
 | 844 | template<class RealType = double> | 
 | 845 |     class weibull_distribution; | 
 | 846 |  | 
 | 847 | template<class RealType = double> | 
 | 848 |     class extreme_value_distribution; | 
 | 849 |  | 
 | 850 | template<class RealType = double> | 
| Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 851 | class normal_distribution | 
 | 852 | { | 
 | 853 | public: | 
 | 854 |     // types | 
 | 855 |     typedef RealType result_type; | 
 | 856 |  | 
 | 857 |     class param_type | 
 | 858 |     { | 
 | 859 |     public: | 
 | 860 |         typedef normal_distribution distribution_type; | 
 | 861 |  | 
 | 862 |         explicit param_type(result_type mean = 0, result_type stddev = 1); | 
 | 863 |  | 
 | 864 |         result_type mean() const; | 
 | 865 |         result_type stddev() const; | 
 | 866 |  | 
 | 867 |         friend bool operator==(const param_type& x, const param_type& y); | 
 | 868 |         friend bool operator!=(const param_type& x, const param_type& y); | 
 | 869 |     }; | 
 | 870 |  | 
 | 871 |     // constructors and reset functions | 
 | 872 |     explicit normal_distribution(result_type mean = 0, result_type stddev = 1); | 
 | 873 |     explicit normal_distribution(const param_type& parm); | 
 | 874 |     void reset(); | 
 | 875 |  | 
 | 876 |     // generating functions | 
 | 877 |     template<class URNG> result_type operator()(URNG& g); | 
 | 878 |     template<class URNG> result_type operator()(URNG& g, const param_type& parm); | 
 | 879 |  | 
 | 880 |     // property functions | 
 | 881 |     result_type mean() const; | 
 | 882 |     result_type stddev() const; | 
 | 883 |  | 
 | 884 |     param_type param() const; | 
 | 885 |     void param(const param_type& parm); | 
 | 886 |  | 
 | 887 |     result_type min() const; | 
 | 888 |     result_type max() const; | 
 | 889 |  | 
 | 890 |     friend bool operator==(const normal_distribution& x, | 
 | 891 |                            const normal_distribution& y); | 
 | 892 |     friend bool operator!=(const normal_distribution& x, | 
 | 893 |                            const normal_distribution& y); | 
 | 894 |  | 
 | 895 |     template <class charT, class traits> | 
 | 896 |     friend | 
 | 897 |     basic_ostream<charT, traits>& | 
 | 898 |     operator<<(basic_ostream<charT, traits>& os, | 
 | 899 |                const normal_distribution& x); | 
 | 900 |      | 
 | 901 |     template <class charT, class traits> | 
 | 902 |     friend | 
 | 903 |     basic_istream<charT, traits>& | 
 | 904 |     operator>>(basic_istream<charT, traits>& is, | 
 | 905 |                normal_distribution& x); | 
 | 906 | }; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 907 |  | 
 | 908 | template<class RealType = double> | 
 | 909 |     class lognormal_distribution; | 
 | 910 |  | 
 | 911 | template<class RealType = double> | 
 | 912 |     class chi_squared_distribution; | 
 | 913 |  | 
 | 914 | template<class RealType = double> | 
 | 915 |     class cauchy_distribution; | 
 | 916 |  | 
 | 917 | template<class RealType = double> | 
 | 918 |     class fisher_f_distribution; | 
 | 919 |  | 
 | 920 | template<class RealType = double> | 
 | 921 |     class student_t_distribution; | 
 | 922 |  | 
 | 923 | template<class IntType = int> | 
 | 924 |     class discrete_distribution; | 
 | 925 |  | 
 | 926 | template<class RealType = double> | 
 | 927 |     class piecewise_constant_distribution; | 
 | 928 |  | 
 | 929 | template<class RealType = double> | 
 | 930 |     class piecewise_linear_distribution; | 
 | 931 |  | 
 | 932 | } // std | 
 | 933 | */ | 
 | 934 |  | 
 | 935 | #include <__config> | 
 | 936 | #include <cstddef> | 
 | 937 | #include <type_traits> | 
 | 938 | #include <initializer_list> | 
 | 939 | #include <cstdint> | 
 | 940 | #include <limits> | 
 | 941 | #include <algorithm> | 
 | 942 | #include <vector> | 
 | 943 | #include <string> | 
 | 944 | #include <istream> | 
 | 945 | #include <ostream> | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 946 | #include <cmath> | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 947 |  | 
 | 948 | #pragma GCC system_header | 
 | 949 |  | 
 | 950 | _LIBCPP_BEGIN_NAMESPACE_STD | 
 | 951 |  | 
 | 952 | // linear_congruential_engine | 
 | 953 |  | 
 | 954 | template <unsigned long long __a, unsigned long long __c, | 
 | 955 |           unsigned long long __m, unsigned long long _M, | 
 | 956 |           bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)> | 
 | 957 | struct __lce_ta; | 
 | 958 |  | 
 | 959 | // 64 | 
 | 960 |  | 
 | 961 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> | 
 | 962 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> | 
 | 963 | { | 
 | 964 |     typedef unsigned long long result_type; | 
 | 965 |     static result_type next(result_type __x) | 
 | 966 |     { | 
 | 967 |         // Schrage's algorithm | 
 | 968 |         const result_type __q = __m / __a; | 
 | 969 |         const result_type __r = __m % __a; | 
 | 970 |         const result_type __t0 = __a * (__x % __q); | 
 | 971 |         const result_type __t1 = __r * (__x / __q); | 
 | 972 |         __x = __t0 + (__t0 < __t1) * __m - __t1; | 
 | 973 |         __x += __c - (__x >= __m - __c) * __m; | 
 | 974 |         return __x; | 
 | 975 |     } | 
 | 976 | }; | 
 | 977 |  | 
 | 978 | template <unsigned long long __a, unsigned long long __m> | 
 | 979 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> | 
 | 980 | { | 
 | 981 |     typedef unsigned long long result_type; | 
 | 982 |     static result_type next(result_type __x) | 
 | 983 |     { | 
 | 984 |         // Schrage's algorithm | 
 | 985 |         const result_type __q = __m / __a; | 
 | 986 |         const result_type __r = __m % __a; | 
 | 987 |         const result_type __t0 = __a * (__x % __q); | 
 | 988 |         const result_type __t1 = __r * (__x / __q); | 
 | 989 |         __x = __t0 + (__t0 < __t1) * __m - __t1; | 
 | 990 |         return __x; | 
 | 991 |     } | 
 | 992 | }; | 
 | 993 |  | 
 | 994 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> | 
 | 995 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> | 
 | 996 | { | 
 | 997 |     typedef unsigned long long result_type; | 
 | 998 |     static result_type next(result_type __x) | 
 | 999 |     { | 
 | 1000 |         return (__a * __x + __c) % __m; | 
 | 1001 |     } | 
 | 1002 | }; | 
 | 1003 |  | 
 | 1004 | template <unsigned long long __a, unsigned long long __c> | 
 | 1005 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> | 
 | 1006 | { | 
 | 1007 |     typedef unsigned long long result_type; | 
 | 1008 |     static result_type next(result_type __x) | 
 | 1009 |     { | 
 | 1010 |         return __a * __x + __c; | 
 | 1011 |     } | 
 | 1012 | }; | 
 | 1013 |  | 
 | 1014 | // 32 | 
 | 1015 |  | 
 | 1016 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> | 
 | 1017 | struct __lce_ta<_A, _C, _M, unsigned(~0), true> | 
 | 1018 | { | 
 | 1019 |     typedef unsigned result_type; | 
 | 1020 |     static result_type next(result_type __x) | 
 | 1021 |     { | 
 | 1022 |         const result_type __a = static_cast<result_type>(_A); | 
 | 1023 |         const result_type __c = static_cast<result_type>(_C); | 
 | 1024 |         const result_type __m = static_cast<result_type>(_M); | 
 | 1025 |         // Schrage's algorithm | 
 | 1026 |         const result_type __q = __m / __a; | 
 | 1027 |         const result_type __r = __m % __a; | 
 | 1028 |         const result_type __t0 = __a * (__x % __q); | 
 | 1029 |         const result_type __t1 = __r * (__x / __q); | 
 | 1030 |         __x = __t0 + (__t0 < __t1) * __m - __t1; | 
 | 1031 |         __x += __c - (__x >= __m - __c) * __m; | 
 | 1032 |         return __x; | 
 | 1033 |     } | 
 | 1034 | }; | 
 | 1035 |  | 
 | 1036 | template <unsigned long long _A, unsigned long long _M> | 
 | 1037 | struct __lce_ta<_A, 0, _M, unsigned(~0), true> | 
 | 1038 | { | 
 | 1039 |     typedef unsigned result_type; | 
 | 1040 |     static result_type next(result_type __x) | 
 | 1041 |     { | 
 | 1042 |         const result_type __a = static_cast<result_type>(_A); | 
 | 1043 |         const result_type __m = static_cast<result_type>(_M); | 
 | 1044 |         // Schrage's algorithm | 
 | 1045 |         const result_type __q = __m / __a; | 
 | 1046 |         const result_type __r = __m % __a; | 
 | 1047 |         const result_type __t0 = __a * (__x % __q); | 
 | 1048 |         const result_type __t1 = __r * (__x / __q); | 
 | 1049 |         __x = __t0 + (__t0 < __t1) * __m - __t1; | 
 | 1050 |         return __x; | 
 | 1051 |     } | 
 | 1052 | }; | 
 | 1053 |  | 
 | 1054 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> | 
 | 1055 | struct __lce_ta<_A, _C, _M, unsigned(~0), false> | 
 | 1056 | { | 
 | 1057 |     typedef unsigned result_type; | 
 | 1058 |     static result_type next(result_type __x) | 
 | 1059 |     { | 
 | 1060 |         const result_type __a = static_cast<result_type>(_A); | 
 | 1061 |         const result_type __c = static_cast<result_type>(_C); | 
 | 1062 |         const result_type __m = static_cast<result_type>(_M); | 
 | 1063 |         return (__a * __x + __c) % __m; | 
 | 1064 |     } | 
 | 1065 | }; | 
 | 1066 |  | 
 | 1067 | template <unsigned long long _A, unsigned long long _C> | 
 | 1068 | struct __lce_ta<_A, _C, 0, unsigned(~0), false> | 
 | 1069 | { | 
 | 1070 |     typedef unsigned result_type; | 
 | 1071 |     static result_type next(result_type __x) | 
 | 1072 |     { | 
 | 1073 |         const result_type __a = static_cast<result_type>(_A); | 
 | 1074 |         const result_type __c = static_cast<result_type>(_C); | 
 | 1075 |         return __a * __x + __c; | 
 | 1076 |     } | 
 | 1077 | }; | 
 | 1078 |  | 
 | 1079 | // 16 | 
 | 1080 |  | 
 | 1081 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> | 
 | 1082 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> | 
 | 1083 | { | 
 | 1084 |     typedef unsigned short result_type; | 
 | 1085 |     static result_type next(result_type __x) | 
 | 1086 |     { | 
 | 1087 |         return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); | 
 | 1088 |     } | 
 | 1089 | }; | 
 | 1090 |  | 
 | 1091 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | 
 | 1092 | class linear_congruential_engine; | 
 | 1093 |  | 
 | 1094 | template <class _CharT, class _Traits, | 
 | 1095 |           class _U, _U _A, _U _C, _U _N> | 
 | 1096 | basic_ostream<_CharT, _Traits>& | 
 | 1097 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1098 |            const linear_congruential_engine<_U, _A, _C, _N>&); | 
 | 1099 |  | 
 | 1100 | template <class _CharT, class _Traits, | 
 | 1101 |           class _U, _U _A, _U _C, _U _N> | 
 | 1102 | basic_istream<_CharT, _Traits>& | 
 | 1103 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1104 |            linear_congruential_engine<_U, _A, _C, _N>& __x); | 
 | 1105 |  | 
 | 1106 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | 
 | 1107 | class linear_congruential_engine | 
 | 1108 | { | 
 | 1109 | public: | 
 | 1110 |     // types | 
 | 1111 |     typedef _UIntType result_type; | 
 | 1112 |  | 
 | 1113 | private: | 
 | 1114 |     result_type __x_; | 
 | 1115 |  | 
 | 1116 |     static const result_type _M = result_type(~0); | 
 | 1117 |  | 
 | 1118 |     static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); | 
 | 1119 |     static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); | 
 | 1120 | public: | 
 | 1121 |     static const result_type _Min = __c == 0u ? 1u: 0u; | 
 | 1122 |     static const result_type _Max = __m - 1u; | 
 | 1123 |     static_assert(_Min < _Max,           "linear_congruential_engine invalid parameters"); | 
 | 1124 |  | 
 | 1125 |     // engine characteristics | 
 | 1126 |     static const/*expr*/ result_type multiplier = __a; | 
 | 1127 |     static const/*expr*/ result_type increment = __c; | 
 | 1128 |     static const/*expr*/ result_type modulus = __m; | 
 | 1129 |     static const/*expr*/ result_type min() {return _Min;} | 
 | 1130 |     static const/*expr*/ result_type max() {return _Max;} | 
 | 1131 |     static const/*expr*/ result_type default_seed = 1u; | 
 | 1132 |  | 
 | 1133 |     // constructors and seeding functions | 
 | 1134 |     explicit linear_congruential_engine(result_type __s = default_seed) | 
 | 1135 |         {seed(__s);} | 
 | 1136 |     template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q) | 
 | 1137 |         {seed(__q);} | 
 | 1138 |     void seed(result_type __s = default_seed) | 
 | 1139 |         {seed(integral_constant<bool, __m == 0>(), | 
 | 1140 |               integral_constant<bool, __c == 0>(), __s);} | 
 | 1141 |     template<class _Sseq> | 
 | 1142 |         typename enable_if | 
 | 1143 |         < | 
 | 1144 |             !is_convertible<_Sseq, result_type>::value, | 
 | 1145 |             void | 
 | 1146 |         >::type | 
 | 1147 |         seed(_Sseq& __q) | 
 | 1148 |             {__seed(__q, integral_constant<unsigned, | 
 | 1149 |                 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 | 
 | 1150 |                              :  (__m-1) / 0x100000000ull)>());} | 
 | 1151 |  | 
 | 1152 |     // generating functions | 
 | 1153 |     result_type operator()() | 
 | 1154 |         {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));} | 
 | 1155 |     void discard(unsigned long long __z) {for (; __z; --__z) operator()();} | 
 | 1156 |  | 
 | 1157 |     friend bool operator==(const linear_congruential_engine& __x, | 
 | 1158 |                            const linear_congruential_engine& __y) | 
 | 1159 |         {return __x.__x_ == __y.__x_;} | 
 | 1160 |     friend bool operator!=(const linear_congruential_engine& __x, | 
 | 1161 |                            const linear_congruential_engine& __y) | 
 | 1162 |         {return !(__x == __y);} | 
 | 1163 |  | 
 | 1164 | private: | 
 | 1165 |  | 
 | 1166 |     void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;} | 
 | 1167 |     void seed(true_type, false_type, result_type __s) {__x_ = __s;} | 
 | 1168 |     void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? | 
 | 1169 |                                                                  1 : __s % __m;} | 
 | 1170 |     void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} | 
 | 1171 |  | 
 | 1172 |     template<class _Sseq> | 
 | 1173 |         void __seed(_Sseq& __q, integral_constant<unsigned, 1>); | 
 | 1174 |     template<class _Sseq> | 
 | 1175 |         void __seed(_Sseq& __q, integral_constant<unsigned, 2>); | 
 | 1176 |  | 
 | 1177 |     template <class _CharT, class _Traits, | 
 | 1178 |               class _U, _U _A, _U _C, _U _N> | 
 | 1179 |     friend | 
 | 1180 |     basic_ostream<_CharT, _Traits>& | 
 | 1181 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1182 |                const linear_congruential_engine<_U, _A, _C, _N>&); | 
 | 1183 |      | 
 | 1184 |     template <class _CharT, class _Traits, | 
 | 1185 |               class _U, _U _A, _U _C, _U _N> | 
 | 1186 |     friend | 
 | 1187 |     basic_istream<_CharT, _Traits>& | 
 | 1188 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1189 |                linear_congruential_engine<_U, _A, _C, _N>& __x); | 
 | 1190 | }; | 
 | 1191 |  | 
 | 1192 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | 
 | 1193 | template<class _Sseq> | 
 | 1194 | void | 
 | 1195 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, | 
 | 1196 |                                                  integral_constant<unsigned, 1>) | 
 | 1197 | { | 
 | 1198 |     const unsigned __k = 1; | 
 | 1199 |     uint32_t __ar[__k+3]; | 
 | 1200 |     __q.generate(__ar, __ar + __k + 3); | 
 | 1201 |     result_type __s = static_cast<result_type>(__ar[3] % __m); | 
 | 1202 |     __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; | 
 | 1203 | } | 
 | 1204 |  | 
 | 1205 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | 
 | 1206 | template<class _Sseq> | 
 | 1207 | void | 
 | 1208 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, | 
 | 1209 |                                                  integral_constant<unsigned, 2>) | 
 | 1210 | { | 
 | 1211 |     const unsigned __k = 2; | 
 | 1212 |     uint32_t __ar[__k+3]; | 
 | 1213 |     __q.generate(__ar, __ar + __k + 3); | 
 | 1214 |     result_type __s = static_cast<result_type>((__ar[3] + | 
 | 1215 |                                                 (uint64_t)__ar[4] << 32) % __m); | 
 | 1216 |     __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; | 
 | 1217 | } | 
 | 1218 |  | 
 | 1219 | template <class _CharT, class _Traits> | 
 | 1220 | class __save_flags | 
 | 1221 | { | 
 | 1222 |     typedef basic_ios<_CharT, _Traits> __stream_type; | 
 | 1223 |     typedef typename __stream_type::fmtflags fmtflags; | 
 | 1224 |  | 
 | 1225 |     __stream_type& __stream_; | 
 | 1226 |     fmtflags       __fmtflags_; | 
 | 1227 |     _CharT         __fill_; | 
 | 1228 |  | 
 | 1229 |     __save_flags(const __save_flags&); | 
 | 1230 |     __save_flags& operator=(const __save_flags&); | 
 | 1231 | public: | 
 | 1232 |     explicit __save_flags(__stream_type& __stream) | 
 | 1233 |         : __stream_(__stream), | 
 | 1234 |           __fmtflags_(__stream.flags()), | 
 | 1235 |           __fill_(__stream.fill()) | 
 | 1236 |         {} | 
 | 1237 |     ~__save_flags() | 
 | 1238 |     { | 
 | 1239 |         __stream_.flags(__fmtflags_); | 
 | 1240 |         __stream_.fill(__fill_); | 
 | 1241 |     } | 
 | 1242 | }; | 
 | 1243 |  | 
 | 1244 | template <class _CharT, class _Traits, | 
 | 1245 |           class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | 
 | 1246 | inline | 
 | 1247 | basic_ostream<_CharT, _Traits>& | 
 | 1248 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1249 |            const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) | 
 | 1250 | { | 
 | 1251 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 1252 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 1253 |     __os.fill(__os.widen(' ')); | 
 | 1254 |     return __os << __x.__x_; | 
 | 1255 | } | 
 | 1256 |  | 
 | 1257 | template <class _CharT, class _Traits, | 
 | 1258 |           class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | 
 | 1259 | basic_istream<_CharT, _Traits>& | 
 | 1260 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1261 |            linear_congruential_engine<_UIntType, __a, __c, __m>& __x) | 
 | 1262 | { | 
 | 1263 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 1264 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 1265 |     _UIntType __t; | 
 | 1266 |     __is >> __t; | 
 | 1267 |     if (!__is.fail()) | 
 | 1268 |         __x.__x_ = __t; | 
 | 1269 |     return __is; | 
 | 1270 | } | 
 | 1271 |  | 
 | 1272 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> | 
 | 1273 |                                                                    minstd_rand0; | 
 | 1274 | typedef minstd_rand0                                      default_random_engine; | 
 | 1275 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> | 
 | 1276 |                                                                     minstd_rand; | 
 | 1277 | // mersenne_twister_engine | 
 | 1278 |  | 
 | 1279 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, | 
 | 1280 |           _UIntType __a, size_t __u, _UIntType __d, size_t __s, | 
 | 1281 |           _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> | 
 | 1282 | class mersenne_twister_engine; | 
 | 1283 |  | 
 | 1284 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1285 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1286 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1287 | bool | 
 | 1288 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1289 |                                          _B, _T, _C, _L, _F>& __x, | 
 | 1290 |            const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1291 |                                          _B, _T, _C, _L, _F>& __y); | 
 | 1292 |  | 
 | 1293 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1294 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1295 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1296 | bool | 
 | 1297 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1298 |                                          _B, _T, _C, _L, _F>& __x, | 
 | 1299 |            const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1300 |                                          _B, _T, _C, _L, _F>& __y); | 
 | 1301 |  | 
 | 1302 | template <class _CharT, class _Traits, | 
 | 1303 |           class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1304 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1305 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1306 | basic_ostream<_CharT, _Traits>& | 
 | 1307 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1308 |            const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1309 |                                          _B, _T, _C, _L, _F>& __x); | 
 | 1310 |  | 
 | 1311 | template <class _CharT, class _Traits, | 
 | 1312 |           class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1313 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1314 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1315 | basic_istream<_CharT, _Traits>& | 
 | 1316 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1317 |            mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1318 |                                    _B, _T, _C, _L, _F>& __x); | 
 | 1319 |  | 
 | 1320 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, | 
 | 1321 |           _UIntType __a, size_t __u, _UIntType __d, size_t __s, | 
 | 1322 |           _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> | 
 | 1323 | class mersenne_twister_engine | 
 | 1324 | { | 
 | 1325 | public: | 
 | 1326 |     // types | 
 | 1327 |     typedef _UIntType result_type; | 
 | 1328 |  | 
 | 1329 | private: | 
 | 1330 |     result_type __x_[__n]; | 
 | 1331 |     size_t      __i_; | 
 | 1332 |  | 
 | 1333 |     static_assert(  0 <  __m, "mersenne_twister_engine invalid parameters"); | 
 | 1334 |     static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); | 
 | 1335 |     static const result_type _Dt = numeric_limits<result_type>::digits; | 
 | 1336 |     static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); | 
 | 1337 |     static_assert(  2 <= __w, "mersenne_twister_engine invalid parameters"); | 
 | 1338 |     static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); | 
 | 1339 |     static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); | 
 | 1340 |     static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); | 
 | 1341 |     static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); | 
 | 1342 |     static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); | 
 | 1343 | public: | 
 | 1344 |     static const result_type _Min = 0; | 
 | 1345 |     static const result_type _Max = __w == _Dt ? result_type(~0) : | 
 | 1346 |                                    (result_type(1) << __w) - result_type(1); | 
 | 1347 |     static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); | 
 | 1348 |     static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); | 
 | 1349 |     static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); | 
 | 1350 |     static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); | 
 | 1351 |     static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); | 
 | 1352 |     static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); | 
 | 1353 |  | 
 | 1354 |     // engine characteristics | 
 | 1355 |     static const/*expr*/ size_t word_size = __w; | 
 | 1356 |     static const/*expr*/ size_t state_size = __n; | 
 | 1357 |     static const/*expr*/ size_t shift_size = __m; | 
 | 1358 |     static const/*expr*/ size_t mask_bits = __r; | 
 | 1359 |     static const/*expr*/ result_type xor_mask = __a; | 
 | 1360 |     static const/*expr*/ size_t tempering_u = __u; | 
 | 1361 |     static const/*expr*/ result_type tempering_d = __d; | 
 | 1362 |     static const/*expr*/ size_t tempering_s = __s; | 
 | 1363 |     static const/*expr*/ result_type tempering_b = __b; | 
 | 1364 |     static const/*expr*/ size_t tempering_t = __t; | 
 | 1365 |     static const/*expr*/ result_type tempering_c = __c; | 
 | 1366 |     static const/*expr*/ size_t tempering_l = __l; | 
 | 1367 |     static const/*expr*/ result_type initialization_multiplier = __f; | 
 | 1368 |     static const/*expr*/ result_type min() { return _Min; } | 
 | 1369 |     static const/*expr*/ result_type max() { return _Max; } | 
 | 1370 |     static const/*expr*/ result_type default_seed = 5489u; | 
 | 1371 |  | 
 | 1372 |     // constructors and seeding functions | 
 | 1373 |     explicit mersenne_twister_engine(result_type __sd = default_seed) | 
 | 1374 |         {seed(__sd);} | 
 | 1375 |     template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q) | 
 | 1376 |         {seed(__q);} | 
 | 1377 |     void seed(result_type __sd = default_seed); | 
 | 1378 |     template<class _Sseq> | 
 | 1379 |         typename enable_if | 
 | 1380 |         < | 
 | 1381 |             !is_convertible<_Sseq, result_type>::value, | 
 | 1382 |             void | 
 | 1383 |         >::type | 
 | 1384 |         seed(_Sseq& __q) | 
 | 1385 |             {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} | 
 | 1386 |  | 
 | 1387 |     // generating functions | 
 | 1388 |     result_type operator()(); | 
 | 1389 |     void discard(unsigned long long __z) {for (; __z; --__z) operator()();} | 
 | 1390 |  | 
 | 1391 |     template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1392 |               _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1393 |               _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1394 |     friend | 
 | 1395 |     bool | 
 | 1396 |     operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1397 |                                              _B, _T, _C, _L, _F>& __x, | 
 | 1398 |                const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1399 |                                              _B, _T, _C, _L, _F>& __y); | 
 | 1400 |      | 
 | 1401 |     template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1402 |               _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1403 |               _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1404 |     friend | 
 | 1405 |     bool | 
 | 1406 |     operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1407 |                                              _B, _T, _C, _L, _F>& __x, | 
 | 1408 |                const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1409 |                                              _B, _T, _C, _L, _F>& __y); | 
 | 1410 |  | 
 | 1411 |     template <class _CharT, class _Traits, | 
 | 1412 |               class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1413 |               _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1414 |               _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1415 |     friend | 
 | 1416 |     basic_ostream<_CharT, _Traits>& | 
 | 1417 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1418 |                const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1419 |                                              _B, _T, _C, _L, _F>& __x); | 
 | 1420 |  | 
 | 1421 |     template <class _CharT, class _Traits, | 
 | 1422 |               class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1423 |               _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1424 |               _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1425 |     friend | 
 | 1426 |     basic_istream<_CharT, _Traits>& | 
 | 1427 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1428 |                mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1429 |                                        _B, _T, _C, _L, _F>& __x); | 
 | 1430 | private: | 
 | 1431 |  | 
 | 1432 |     template<class _Sseq> | 
 | 1433 |         void __seed(_Sseq& __q, integral_constant<unsigned, 1>); | 
 | 1434 |     template<class _Sseq> | 
 | 1435 |         void __seed(_Sseq& __q, integral_constant<unsigned, 2>); | 
 | 1436 |  | 
 | 1437 |     template <size_t __count> | 
 | 1438 |         static | 
 | 1439 |         typename enable_if | 
 | 1440 |         < | 
 | 1441 |             __count < __w, | 
 | 1442 |             result_type | 
 | 1443 |         >::type | 
 | 1444 |         __lshift(result_type __x) {return (__x << __count) & _Max;} | 
 | 1445 |  | 
 | 1446 |     template <size_t __count> | 
 | 1447 |         static | 
 | 1448 |         typename enable_if | 
 | 1449 |         < | 
 | 1450 |             (__count >= __w), | 
 | 1451 |             result_type | 
 | 1452 |         >::type | 
 | 1453 |         __lshift(result_type __x) {return result_type(0);} | 
 | 1454 |  | 
 | 1455 |     template <size_t __count> | 
 | 1456 |         static | 
 | 1457 |         typename enable_if | 
 | 1458 |         < | 
 | 1459 |             __count < _Dt, | 
 | 1460 |             result_type | 
 | 1461 |         >::type | 
 | 1462 |         __rshift(result_type __x) {return __x >> __count;} | 
 | 1463 |  | 
 | 1464 |     template <size_t __count> | 
 | 1465 |         static | 
 | 1466 |         typename enable_if | 
 | 1467 |         < | 
 | 1468 |             (__count >= _Dt), | 
 | 1469 |             result_type | 
 | 1470 |         >::type | 
 | 1471 |         __rshift(result_type __x) {return result_type(0);} | 
 | 1472 | }; | 
 | 1473 |  | 
 | 1474 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, | 
 | 1475 |           _UIntType __a, size_t __u, _UIntType __d, size_t __s, | 
 | 1476 |           _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> | 
 | 1477 | void | 
 | 1478 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, | 
 | 1479 |     __t, __c, __l, __f>::seed(result_type __sd) | 
 | 1480 | {   // __w >= 2 | 
 | 1481 |     __x_[0] = __sd & _Max; | 
 | 1482 |     for (size_t __i = 1; __i < __n; ++__i) | 
 | 1483 |         __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; | 
 | 1484 |     __i_ = 0; | 
 | 1485 | } | 
 | 1486 |  | 
 | 1487 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, | 
 | 1488 |           _UIntType __a, size_t __u, _UIntType __d, size_t __s, | 
 | 1489 |           _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> | 
 | 1490 | template<class _Sseq> | 
 | 1491 | void | 
 | 1492 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, | 
 | 1493 |     __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) | 
 | 1494 | { | 
 | 1495 |     const unsigned __k = 1; | 
 | 1496 |     uint32_t __ar[__n * __k]; | 
 | 1497 |     __q.generate(__ar, __ar + __n * __k); | 
 | 1498 |     for (size_t __i = 0; __i < __n; ++__i) | 
 | 1499 |         __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); | 
 | 1500 |     const result_type __mask = __r == _Dt ? result_type(~0) : | 
 | 1501 |                                        (result_type(1) << __r) - result_type(1); | 
 | 1502 |     __i_ = 0; | 
 | 1503 |     if ((__x_[0] & ~__mask) == 0) | 
 | 1504 |     { | 
 | 1505 |         for (size_t __i = 1; __i < __n; ++__i) | 
 | 1506 |             if (__x_[__i] != 0) | 
 | 1507 |                 return; | 
 | 1508 |         __x_[0] = _Max; | 
 | 1509 |     } | 
 | 1510 | } | 
 | 1511 |  | 
 | 1512 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, | 
 | 1513 |           _UIntType __a, size_t __u, _UIntType __d, size_t __s, | 
 | 1514 |           _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> | 
 | 1515 | template<class _Sseq> | 
 | 1516 | void | 
 | 1517 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, | 
 | 1518 |     __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) | 
 | 1519 | { | 
 | 1520 |     const unsigned __k = 2; | 
 | 1521 |     uint32_t __ar[__n * __k]; | 
 | 1522 |     __q.generate(__ar, __ar + __n * __k); | 
 | 1523 |     for (size_t __i = 0; __i < __n; ++__i) | 
 | 1524 |         __x_[__i] = static_cast<result_type>( | 
 | 1525 |             (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); | 
 | 1526 |     const result_type __mask = __r == _Dt ? result_type(~0) : | 
 | 1527 |                                        (result_type(1) << __r) - result_type(1); | 
 | 1528 |     __i_ = 0; | 
 | 1529 |     if ((__x_[0] & ~__mask) == 0) | 
 | 1530 |     { | 
 | 1531 |         for (size_t __i = 1; __i < __n; ++__i) | 
 | 1532 |             if (__x_[__i] != 0) | 
 | 1533 |                 return; | 
 | 1534 |         __x_[0] = _Max; | 
 | 1535 |     } | 
 | 1536 | } | 
 | 1537 |  | 
 | 1538 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, | 
 | 1539 |           _UIntType __a, size_t __u, _UIntType __d, size_t __s, | 
 | 1540 |           _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> | 
 | 1541 | _UIntType | 
 | 1542 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, | 
 | 1543 |     __t, __c, __l, __f>::operator()() | 
 | 1544 | { | 
 | 1545 |     const size_t __j = (__i_ + 1) % __n; | 
 | 1546 |     const result_type __mask = __r == _Dt ? result_type(~0) : | 
 | 1547 |                                        (result_type(1) << __r) - result_type(1); | 
 | 1548 |     const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); | 
 | 1549 |     const size_t __k = (__i_ + __m) % __n; | 
 | 1550 |     __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1)); | 
 | 1551 |     result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); | 
 | 1552 |     __i_ = __j; | 
 | 1553 |     __z ^= __lshift<__s>(__z) & __b; | 
 | 1554 |     __z ^= __lshift<__t>(__z) & __c; | 
 | 1555 |     return __z ^ __rshift<__l>(__z); | 
 | 1556 | } | 
 | 1557 |  | 
 | 1558 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1559 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1560 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1561 | bool | 
 | 1562 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1563 |                                          _B, _T, _C, _L, _F>& __x, | 
 | 1564 |            const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1565 |                                          _B, _T, _C, _L, _F>& __y) | 
 | 1566 | { | 
 | 1567 |     if (__x.__i_ == __y.__i_) | 
 | 1568 |         return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_); | 
 | 1569 |     if (__x.__i_ == 0 || __y.__i_ == 0) | 
 | 1570 |     { | 
 | 1571 |         size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_); | 
 | 1572 |         if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, | 
 | 1573 |                          __y.__x_ + __y.__i_)) | 
 | 1574 |             return false; | 
 | 1575 |         if (__x.__i_ == 0) | 
 | 1576 |             return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_); | 
 | 1577 |         return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j); | 
 | 1578 |     } | 
 | 1579 |     if (__x.__i_ < __y.__i_) | 
 | 1580 |     { | 
 | 1581 |         size_t __j = _N - __y.__i_; | 
 | 1582 |         if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), | 
 | 1583 |                          __y.__x_ + __y.__i_)) | 
 | 1584 |             return false; | 
 | 1585 |         if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N, | 
 | 1586 |                          __y.__x_)) | 
 | 1587 |             return false; | 
 | 1588 |         return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, | 
 | 1589 |                            __y.__x_ + (_N - (__x.__i_ + __j))); | 
 | 1590 |     } | 
 | 1591 |     size_t __j = _N - __x.__i_; | 
 | 1592 |     if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), | 
 | 1593 |                      __x.__x_ + __x.__i_)) | 
 | 1594 |         return false; | 
 | 1595 |     if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N, | 
 | 1596 |                      __x.__x_)) | 
 | 1597 |         return false; | 
 | 1598 |     return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, | 
 | 1599 |                        __x.__x_ + (_N - (__y.__i_ + __j))); | 
 | 1600 | } | 
 | 1601 |  | 
 | 1602 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1603 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1604 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1605 | inline | 
 | 1606 | bool | 
 | 1607 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1608 |                                          _B, _T, _C, _L, _F>& __x, | 
 | 1609 |            const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1610 |                                          _B, _T, _C, _L, _F>& __y) | 
 | 1611 | { | 
 | 1612 |     return !(__x == __y); | 
 | 1613 | } | 
 | 1614 |  | 
 | 1615 | template <class _CharT, class _Traits, | 
 | 1616 |           class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1617 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1618 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1619 | basic_ostream<_CharT, _Traits>& | 
 | 1620 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1621 |            const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1622 |                                          _B, _T, _C, _L, _F>& __x) | 
 | 1623 | { | 
 | 1624 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 1625 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 1626 |     _CharT __sp = __os.widen(' '); | 
 | 1627 |     __os.fill(__sp); | 
 | 1628 |     __os << __x.__x_[__x.__i_]; | 
 | 1629 |     for (size_t __j = __x.__i_ + 1; __j < _N; ++__j) | 
 | 1630 |         __os << __sp << __x.__x_[__j]; | 
 | 1631 |     for (size_t __j = 0; __j < __x.__i_; ++__j) | 
 | 1632 |         __os << __sp << __x.__x_[__j]; | 
 | 1633 |     return __os; | 
 | 1634 | } | 
 | 1635 |  | 
 | 1636 | template <class _CharT, class _Traits, | 
 | 1637 |           class _UI, size_t _W, size_t _N, size_t _M, size_t _R, | 
 | 1638 |           _UI _A, size_t _U, _UI _D, size_t _S, | 
 | 1639 |           _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> | 
 | 1640 | basic_istream<_CharT, _Traits>& | 
 | 1641 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1642 |            mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, | 
 | 1643 |                                    _B, _T, _C, _L, _F>& __x) | 
 | 1644 | { | 
 | 1645 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 1646 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 1647 |     _UI __t[_N]; | 
 | 1648 |     for (size_t __i = 0; __i < _N; ++__i) | 
 | 1649 |         __is >> __t[__i]; | 
 | 1650 |     if (!__is.fail()) | 
 | 1651 |     { | 
 | 1652 |         for (size_t __i = 0; __i < _N; ++__i) | 
 | 1653 |             __x.__x_[__i] = __t[__i]; | 
 | 1654 |         __x.__i_ = 0; | 
 | 1655 |     } | 
 | 1656 |     return __is; | 
 | 1657 | } | 
 | 1658 |  | 
 | 1659 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, | 
 | 1660 |                                 0x9908b0df, 11, 0xffffffff, | 
 | 1661 |                                 7,  0x9d2c5680, | 
 | 1662 |                                 15, 0xefc60000, | 
 | 1663 |                                 18, 1812433253>                         mt19937; | 
 | 1664 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, | 
 | 1665 |                                 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, | 
 | 1666 |                                 17, 0x71d67fffeda60000ULL, | 
 | 1667 |                                 37, 0xfff7eee000000000ULL, | 
 | 1668 |                                 43, 6364136223846793005ULL>          mt19937_64; | 
 | 1669 |  | 
 | 1670 | // subtract_with_carry_engine | 
 | 1671 |  | 
 | 1672 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1673 | class subtract_with_carry_engine; | 
 | 1674 |  | 
 | 1675 | template<class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1676 | bool | 
 | 1677 | operator==( | 
 | 1678 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, | 
 | 1679 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); | 
 | 1680 |  | 
 | 1681 | template<class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1682 | bool | 
 | 1683 | operator!=( | 
 | 1684 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, | 
 | 1685 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); | 
 | 1686 |  | 
 | 1687 | template <class _CharT, class _Traits, | 
 | 1688 |           class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1689 | basic_ostream<_CharT, _Traits>& | 
 | 1690 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1691 |            const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); | 
 | 1692 |  | 
 | 1693 | template <class _CharT, class _Traits, | 
 | 1694 |           class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1695 | basic_istream<_CharT, _Traits>& | 
 | 1696 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1697 |            subtract_with_carry_engine<_UI, _W, _S, _R>& __x); | 
 | 1698 |  | 
 | 1699 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1700 | class subtract_with_carry_engine | 
 | 1701 | { | 
 | 1702 | public: | 
 | 1703 |     // types | 
 | 1704 |     typedef _UIntType result_type; | 
 | 1705 |  | 
 | 1706 | private: | 
 | 1707 |     result_type __x_[__r]; | 
 | 1708 |     result_type  __c_; | 
 | 1709 |     size_t      __i_; | 
 | 1710 |  | 
 | 1711 |     static const result_type _Dt = numeric_limits<result_type>::digits; | 
 | 1712 |     static_assert(  0 <  __w, "subtract_with_carry_engine invalid parameters"); | 
 | 1713 |     static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); | 
 | 1714 |     static_assert(  0 <  __s, "subtract_with_carry_engine invalid parameters"); | 
 | 1715 |     static_assert(__s <  __r, "subtract_with_carry_engine invalid parameters"); | 
 | 1716 | public: | 
 | 1717 |     static const result_type _Min = 0; | 
 | 1718 |     static const result_type _Max = __w == _Dt ? result_type(~0) : | 
 | 1719 |                                    (result_type(1) << __w) - result_type(1); | 
 | 1720 |     static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); | 
 | 1721 |  | 
 | 1722 |     // engine characteristics | 
 | 1723 |     static const/*expr*/ size_t word_size = __w; | 
 | 1724 |     static const/*expr*/ size_t short_lag = __s; | 
 | 1725 |     static const/*expr*/ size_t long_lag = __r; | 
 | 1726 |     static const/*expr*/ result_type min() { return _Min; } | 
 | 1727 |     static const/*expr*/ result_type max() { return _Max; } | 
 | 1728 |     static const/*expr*/ result_type default_seed = 19780503u; | 
 | 1729 |  | 
 | 1730 |     // constructors and seeding functions | 
 | 1731 |     explicit subtract_with_carry_engine(result_type __sd = default_seed) | 
 | 1732 |         {seed(__sd);} | 
 | 1733 |     template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q) | 
 | 1734 |         {seed(__q);} | 
 | 1735 |     void seed(result_type __sd = default_seed) | 
 | 1736 |         {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} | 
 | 1737 |     template<class _Sseq> | 
 | 1738 |         typename enable_if | 
 | 1739 |         < | 
 | 1740 |             !is_convertible<_Sseq, result_type>::value, | 
 | 1741 |             void | 
 | 1742 |         >::type | 
 | 1743 |         seed(_Sseq& __q) | 
 | 1744 |             {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} | 
 | 1745 |  | 
 | 1746 |     // generating functions | 
 | 1747 |     result_type operator()(); | 
 | 1748 |     void discard(unsigned long long __z) {for (; __z; --__z) operator()();} | 
 | 1749 |  | 
 | 1750 |     template<class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1751 |     friend | 
 | 1752 |     bool | 
 | 1753 |     operator==( | 
 | 1754 |         const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, | 
 | 1755 |         const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); | 
 | 1756 |      | 
 | 1757 |     template<class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1758 |     friend | 
 | 1759 |     bool | 
 | 1760 |     operator!=( | 
 | 1761 |         const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, | 
 | 1762 |         const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); | 
 | 1763 |      | 
 | 1764 |     template <class _CharT, class _Traits, | 
 | 1765 |               class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1766 |     friend | 
 | 1767 |     basic_ostream<_CharT, _Traits>& | 
 | 1768 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1769 |                const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); | 
 | 1770 |      | 
 | 1771 |     template <class _CharT, class _Traits, | 
 | 1772 |               class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1773 |     friend | 
 | 1774 |     basic_istream<_CharT, _Traits>& | 
 | 1775 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1776 |                subtract_with_carry_engine<_UI, _W, _S, _R>& __x); | 
 | 1777 |  | 
 | 1778 | private: | 
 | 1779 |  | 
 | 1780 |     void seed(result_type __sd, integral_constant<unsigned, 1>); | 
 | 1781 |     void seed(result_type __sd, integral_constant<unsigned, 2>); | 
 | 1782 |     template<class _Sseq> | 
 | 1783 |         void __seed(_Sseq& __q, integral_constant<unsigned, 1>); | 
 | 1784 |     template<class _Sseq> | 
 | 1785 |         void __seed(_Sseq& __q, integral_constant<unsigned, 2>); | 
 | 1786 | }; | 
 | 1787 |  | 
 | 1788 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1789 | void | 
 | 1790 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, | 
 | 1791 |         integral_constant<unsigned, 1>) | 
 | 1792 | { | 
 | 1793 |     linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> | 
 | 1794 |         __e(__sd == 0u ? default_seed : __sd); | 
 | 1795 |     for (size_t __i = 0; __i < __r; ++__i) | 
 | 1796 |         __x_[__i] = static_cast<result_type>(__e() & _Max); | 
 | 1797 |     __c_ = __x_[__r-1] == 0; | 
 | 1798 |     __i_ = 0; | 
 | 1799 | } | 
 | 1800 |  | 
 | 1801 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1802 | void | 
 | 1803 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, | 
 | 1804 |         integral_constant<unsigned, 2>) | 
 | 1805 | { | 
 | 1806 |     linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> | 
 | 1807 |         __e(__sd == 0u ? default_seed : __sd); | 
 | 1808 |     for (size_t __i = 0; __i < __r; ++__i) | 
 | 1809 |         __x_[__i] = static_cast<result_type>( | 
 | 1810 |                                     (__e() + ((uint64_t)__e() << 32)) & _Max); | 
 | 1811 |     __c_ = __x_[__r-1] == 0; | 
 | 1812 |     __i_ = 0; | 
 | 1813 | } | 
 | 1814 |  | 
 | 1815 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1816 | template<class _Sseq> | 
 | 1817 | void | 
 | 1818 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, | 
 | 1819 |         integral_constant<unsigned, 1>) | 
 | 1820 | { | 
 | 1821 |     const unsigned __k = 1; | 
 | 1822 |     uint32_t __ar[__r * __k]; | 
 | 1823 |     __q.generate(__ar, __ar + __r * __k); | 
 | 1824 |     for (size_t __i = 0; __i < __r; ++__i) | 
 | 1825 |         __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); | 
 | 1826 |     __c_ = __x_[__r-1] == 0; | 
 | 1827 |     __i_ = 0; | 
 | 1828 | } | 
 | 1829 |  | 
 | 1830 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1831 | template<class _Sseq> | 
 | 1832 | void | 
 | 1833 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, | 
 | 1834 |         integral_constant<unsigned, 2>) | 
 | 1835 | { | 
 | 1836 |     const unsigned __k = 2; | 
 | 1837 |     uint32_t __ar[__r * __k]; | 
 | 1838 |     __q.generate(__ar, __ar + __r * __k); | 
 | 1839 |     for (size_t __i = 0; __i < __r; ++__i) | 
 | 1840 |         __x_[__i] = static_cast<result_type>( | 
 | 1841 |                   (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); | 
 | 1842 |     __c_ = __x_[__r-1] == 0; | 
 | 1843 |     __i_ = 0; | 
 | 1844 | } | 
 | 1845 |  | 
 | 1846 | template<class _UIntType, size_t __w, size_t __s, size_t __r> | 
 | 1847 | _UIntType | 
 | 1848 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() | 
 | 1849 | { | 
 | 1850 |     const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; | 
 | 1851 |     result_type& __xr = __x_[__i_]; | 
 | 1852 |     result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; | 
 | 1853 |     __xr = (__xs - __xr - __c_) & _Max; | 
 | 1854 |     __c_ = __new_c; | 
 | 1855 |     __i_ = (__i_ + 1) % __r; | 
 | 1856 |     return __xr; | 
 | 1857 | } | 
 | 1858 |  | 
 | 1859 | template<class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1860 | bool | 
 | 1861 | operator==( | 
 | 1862 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, | 
 | 1863 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) | 
 | 1864 | { | 
 | 1865 |     if (__x.__c_ != __y.__c_) | 
 | 1866 |         return false; | 
 | 1867 |     if (__x.__i_ == __y.__i_) | 
 | 1868 |         return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_); | 
 | 1869 |     if (__x.__i_ == 0 || __y.__i_ == 0) | 
 | 1870 |     { | 
 | 1871 |         size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_); | 
 | 1872 |         if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, | 
 | 1873 |                          __y.__x_ + __y.__i_)) | 
 | 1874 |             return false; | 
 | 1875 |         if (__x.__i_ == 0) | 
 | 1876 |             return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_); | 
 | 1877 |         return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j); | 
 | 1878 |     } | 
 | 1879 |     if (__x.__i_ < __y.__i_) | 
 | 1880 |     { | 
 | 1881 |         size_t __j = _R - __y.__i_; | 
 | 1882 |         if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), | 
 | 1883 |                          __y.__x_ + __y.__i_)) | 
 | 1884 |             return false; | 
 | 1885 |         if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R, | 
 | 1886 |                          __y.__x_)) | 
 | 1887 |             return false; | 
 | 1888 |         return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, | 
 | 1889 |                            __y.__x_ + (_R - (__x.__i_ + __j))); | 
 | 1890 |     } | 
 | 1891 |     size_t __j = _R - __x.__i_; | 
 | 1892 |     if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), | 
 | 1893 |                      __x.__x_ + __x.__i_)) | 
 | 1894 |         return false; | 
 | 1895 |     if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R, | 
 | 1896 |                      __x.__x_)) | 
 | 1897 |         return false; | 
 | 1898 |     return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, | 
 | 1899 |                        __x.__x_ + (_R - (__y.__i_ + __j))); | 
 | 1900 | } | 
 | 1901 |  | 
 | 1902 | template<class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1903 | inline | 
 | 1904 | bool | 
 | 1905 | operator!=( | 
 | 1906 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, | 
 | 1907 |     const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) | 
 | 1908 | { | 
 | 1909 |     return !(__x == __y); | 
 | 1910 | } | 
 | 1911 |  | 
 | 1912 | template <class _CharT, class _Traits, | 
 | 1913 |           class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1914 | basic_ostream<_CharT, _Traits>& | 
 | 1915 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 1916 |            const subtract_with_carry_engine<_UI, _W, _S, _R>& __x) | 
 | 1917 | { | 
 | 1918 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 1919 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 1920 |     _CharT __sp = __os.widen(' '); | 
 | 1921 |     __os.fill(__sp); | 
 | 1922 |     __os << __x.__x_[__x.__i_]; | 
 | 1923 |     for (size_t __j = __x.__i_ + 1; __j < _R; ++__j) | 
 | 1924 |         __os << __sp << __x.__x_[__j]; | 
 | 1925 |     for (size_t __j = 0; __j < __x.__i_; ++__j) | 
 | 1926 |         __os << __sp << __x.__x_[__j]; | 
 | 1927 |     __os << __sp << __x.__c_; | 
 | 1928 |     return __os; | 
 | 1929 | } | 
 | 1930 |  | 
 | 1931 | template <class _CharT, class _Traits, | 
 | 1932 |           class _UI, size_t _W, size_t _S, size_t _R> | 
 | 1933 | basic_istream<_CharT, _Traits>& | 
 | 1934 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 1935 |            subtract_with_carry_engine<_UI, _W, _S, _R>& __x) | 
 | 1936 | { | 
 | 1937 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 1938 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 1939 |     _UI __t[_R+1]; | 
 | 1940 |     for (size_t __i = 0; __i < _R+1; ++__i) | 
 | 1941 |         __is >> __t[__i]; | 
 | 1942 |     if (!__is.fail()) | 
 | 1943 |     { | 
 | 1944 |         for (size_t __i = 0; __i < _R; ++__i) | 
 | 1945 |             __x.__x_[__i] = __t[__i]; | 
 | 1946 |         __x.__c_ = __t[_R]; | 
 | 1947 |         __x.__i_ = 0; | 
 | 1948 |     } | 
 | 1949 |     return __is; | 
 | 1950 | } | 
 | 1951 |  | 
 | 1952 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>     ranlux24_base; | 
 | 1953 | typedef subtract_with_carry_engine<uint_fast64_t, 48,  5, 12>     ranlux48_base; | 
 | 1954 |  | 
 | 1955 | // discard_block_engine | 
 | 1956 |  | 
 | 1957 | template<class _Engine, size_t __p, size_t __r> | 
 | 1958 | class discard_block_engine | 
 | 1959 | { | 
 | 1960 |     _Engine __e_; | 
 | 1961 |     int     __n_; | 
 | 1962 |  | 
 | 1963 |     static_assert(  0 <  __r, "discard_block_engine invalid parameters"); | 
 | 1964 |     static_assert(__r <= __p, "discard_block_engine invalid parameters"); | 
 | 1965 | public: | 
 | 1966 |     // types | 
 | 1967 |     typedef typename _Engine::result_type result_type; | 
 | 1968 |  | 
 | 1969 |     // engine characteristics | 
 | 1970 |     static const/*expr*/ size_t block_size = __p; | 
 | 1971 |     static const/*expr*/ size_t used_block = __r; | 
 | 1972 |  | 
 | 1973 |     // Temporary work around for lack of constexpr | 
 | 1974 |     static const result_type _Min = _Engine::_Min; | 
 | 1975 |     static const result_type _Max = _Engine::_Max; | 
 | 1976 |  | 
 | 1977 |     static const/*expr*/ result_type min() { return _Engine::min(); } | 
 | 1978 |     static const/*expr*/ result_type max() { return _Engine::max(); } | 
 | 1979 |  | 
 | 1980 |     // constructors and seeding functions | 
 | 1981 |     discard_block_engine() : __n_(0) {} | 
 | 1982 | //     explicit discard_block_engine(const _Engine& __e); | 
 | 1983 | //     explicit discard_block_engine(_Engine&& __e); | 
 | 1984 |     explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} | 
 | 1985 |     template<class _Sseq> explicit discard_block_engine(_Sseq& __q) | 
 | 1986 |         : __e_(__q), __n_(0) {} | 
 | 1987 |     void seed() {__e_.seed(); __n_ = 0;} | 
 | 1988 |     void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} | 
 | 1989 |     template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} | 
 | 1990 |  | 
 | 1991 |     // generating functions | 
 | 1992 |     result_type operator()(); | 
 | 1993 |     void discard(unsigned long long __z) {for (; __z; --__z) operator()();} | 
 | 1994 |  | 
 | 1995 |     // property functions | 
 | 1996 |     const _Engine& base() const {return __e_;} | 
 | 1997 |  | 
 | 1998 |     template<class _Eng, size_t _P, size_t _R> | 
 | 1999 |     friend | 
 | 2000 |     bool | 
 | 2001 |     operator==( | 
 | 2002 |         const discard_block_engine<_Eng, _P, _R>& __x, | 
 | 2003 |         const discard_block_engine<_Eng, _P, _R>& __y); | 
 | 2004 |      | 
 | 2005 |     template<class _Eng, size_t _P, size_t _R> | 
 | 2006 |     friend | 
 | 2007 |     bool | 
 | 2008 |     operator!=( | 
 | 2009 |         const discard_block_engine<_Eng, _P, _R>& __x, | 
 | 2010 |         const discard_block_engine<_Eng, _P, _R>& __y); | 
 | 2011 |      | 
 | 2012 |     template <class _CharT, class _Traits, | 
 | 2013 |               class _Eng, size_t _P, size_t _R> | 
 | 2014 |     friend | 
 | 2015 |     basic_ostream<_CharT, _Traits>& | 
 | 2016 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2017 |                const discard_block_engine<_Eng, _P, _R>& __x); | 
 | 2018 |      | 
 | 2019 |     template <class _CharT, class _Traits, | 
 | 2020 |               class _Eng, size_t _P, size_t _R> | 
 | 2021 |     friend | 
 | 2022 |     basic_istream<_CharT, _Traits>& | 
 | 2023 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2024 |                discard_block_engine<_Eng, _P, _R>& __x); | 
 | 2025 | }; | 
 | 2026 |  | 
 | 2027 | template<class _Engine, size_t __p, size_t __r> | 
 | 2028 | typename discard_block_engine<_Engine, __p, __r>::result_type | 
 | 2029 | discard_block_engine<_Engine, __p, __r>::operator()() | 
 | 2030 | { | 
 | 2031 |     if (__n_ >= __r) | 
 | 2032 |     { | 
 | 2033 |         __e_.discard(__p - __r); | 
 | 2034 |         __n_ = 0; | 
 | 2035 |     } | 
 | 2036 |     ++__n_; | 
 | 2037 |     return __e_(); | 
 | 2038 | } | 
 | 2039 |  | 
 | 2040 | template<class _Eng, size_t _P, size_t _R> | 
 | 2041 | inline | 
 | 2042 | bool | 
 | 2043 | operator==(const discard_block_engine<_Eng, _P, _R>& __x, | 
 | 2044 |            const discard_block_engine<_Eng, _P, _R>& __y) | 
 | 2045 | { | 
 | 2046 |     return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; | 
 | 2047 | } | 
 | 2048 |  | 
 | 2049 | template<class _Eng, size_t _P, size_t _R> | 
 | 2050 | inline | 
 | 2051 | bool | 
 | 2052 | operator!=(const discard_block_engine<_Eng, _P, _R>& __x, | 
 | 2053 |            const discard_block_engine<_Eng, _P, _R>& __y) | 
 | 2054 | { | 
 | 2055 |     return !(__x == __y); | 
 | 2056 | } | 
 | 2057 |  | 
 | 2058 | template <class _CharT, class _Traits, | 
 | 2059 |           class _Eng, size_t _P, size_t _R> | 
 | 2060 | basic_ostream<_CharT, _Traits>& | 
 | 2061 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2062 |            const discard_block_engine<_Eng, _P, _R>& __x) | 
 | 2063 | { | 
 | 2064 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 2065 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 2066 |     _CharT __sp = __os.widen(' '); | 
 | 2067 |     __os.fill(__sp); | 
 | 2068 |     return __os << __x.__e_ << __sp << __x.__n_; | 
 | 2069 | } | 
 | 2070 |  | 
 | 2071 | template <class _CharT, class _Traits, | 
 | 2072 |           class _Eng, size_t _P, size_t _R> | 
 | 2073 | basic_istream<_CharT, _Traits>& | 
 | 2074 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2075 |            discard_block_engine<_Eng, _P, _R>& __x) | 
 | 2076 | { | 
 | 2077 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 2078 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 2079 |     _Eng __e; | 
 | 2080 |     int __n; | 
 | 2081 |     __is >> __e >> __n; | 
 | 2082 |     if (!__is.fail()) | 
 | 2083 |     { | 
 | 2084 |         __x.__e_ = __e; | 
 | 2085 |         __x.__n_ = __n; | 
 | 2086 |     } | 
 | 2087 |     return __is; | 
 | 2088 | } | 
 | 2089 |  | 
 | 2090 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; | 
 | 2091 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; | 
 | 2092 |  | 
 | 2093 | // independent_bits_engine | 
 | 2094 |  | 
 | 2095 | template <unsigned long long _X, size_t _R> | 
 | 2096 | struct __log2_imp | 
 | 2097 | { | 
 | 2098 |     static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R | 
 | 2099 |                                            : __log2_imp<_X, _R - 1>::value; | 
 | 2100 | }; | 
 | 2101 |  | 
 | 2102 | template <unsigned long long _X> | 
 | 2103 | struct __log2_imp<_X, 0> | 
 | 2104 | { | 
 | 2105 |     static const size_t value = 0; | 
 | 2106 | }; | 
 | 2107 |  | 
 | 2108 | template <size_t _R> | 
 | 2109 | struct __log2_imp<0, _R> | 
 | 2110 | { | 
 | 2111 |     static const size_t value = _R + 1; | 
 | 2112 | }; | 
 | 2113 |  | 
 | 2114 | template <class _UI, _UI _X> | 
 | 2115 | struct __log2 | 
 | 2116 | { | 
 | 2117 |     static const size_t value = __log2_imp<_X, | 
 | 2118 |                                          sizeof(_UI) * __CHAR_BIT__ - 1>::value; | 
 | 2119 | }; | 
 | 2120 |  | 
 | 2121 | template<class _Engine, size_t __w, class _UIntType> | 
 | 2122 | class independent_bits_engine | 
 | 2123 | { | 
 | 2124 |     template <class _UI, _UI _R0, size_t _W, size_t _M> | 
 | 2125 |     class __get_n | 
 | 2126 |     { | 
 | 2127 |         static const size_t _Dt = numeric_limits<_UI>::digits; | 
 | 2128 |         static const size_t _N = _W / _M + (_W % _M != 0); | 
 | 2129 |         static const size_t _W0 = _W / _N; | 
 | 2130 |         static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; | 
 | 2131 |     public: | 
 | 2132 |         static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N; | 
 | 2133 |     }; | 
 | 2134 | public: | 
 | 2135 |     // types | 
 | 2136 |     typedef _UIntType result_type; | 
 | 2137 |  | 
 | 2138 | private: | 
 | 2139 |     _Engine __e_; | 
 | 2140 |  | 
 | 2141 |     static const result_type _Dt = numeric_limits<result_type>::digits; | 
 | 2142 |     static_assert(  0 <  __w, "independent_bits_engine invalid parameters"); | 
 | 2143 |     static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); | 
 | 2144 |  | 
 | 2145 |     typedef typename _Engine::result_type _Engine_result_type; | 
 | 2146 |     typedef typename conditional | 
 | 2147 |         < | 
 | 2148 |             sizeof(_Engine_result_type) <= sizeof(result_type), | 
 | 2149 |                 result_type, | 
 | 2150 |                 _Engine_result_type | 
 | 2151 |         >::type _Working_result_type; | 
 | 2152 |     // Temporary work around for lack of constexpr | 
 | 2153 |     static const _Working_result_type _R = _Engine::_Max - _Engine::_Min | 
 | 2154 |                                                          + _Working_result_type(1); | 
 | 2155 |     static const size_t __m = __log2<_Working_result_type, _R>::value; | 
 | 2156 |     static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value; | 
 | 2157 |     static const size_t __w0 = __w / __n; | 
 | 2158 |     static const size_t __n0 = __n - __w % __n; | 
 | 2159 |     static const size_t _WDt = numeric_limits<_Working_result_type>::digits; | 
 | 2160 |     static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; | 
 | 2161 |     static const _Working_result_type __y0 = __w0 >= _WDt ? 0 : | 
 | 2162 |                                                    (_R >> __w0) << __w0; | 
 | 2163 |     static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : | 
 | 2164 |                                                    (_R >> (__w0+1)) << (__w0+1); | 
 | 2165 |     static const _Engine_result_type __mask0 = __w0 > 0 ? | 
 | 2166 |                                 _Engine_result_type(~0) >> (_EDt - __w0) : | 
 | 2167 |                                 _Engine_result_type(0); | 
 | 2168 |     static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? | 
 | 2169 |                                 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : | 
 | 2170 |                                 _Engine_result_type(~0); | 
 | 2171 | public: | 
 | 2172 |     static const result_type _Min = 0; | 
 | 2173 |     static const result_type _Max = __w == _Dt ? result_type(~0) : | 
 | 2174 |                                    (result_type(1) << __w) - result_type(1); | 
 | 2175 |     static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); | 
 | 2176 |  | 
 | 2177 |     // engine characteristics | 
 | 2178 |     static const/*expr*/ result_type min() { return _Min; } | 
 | 2179 |     static const/*expr*/ result_type max() { return _Max; } | 
 | 2180 |  | 
 | 2181 |     // constructors and seeding functions | 
 | 2182 |     independent_bits_engine() {} | 
 | 2183 | //    explicit independent_bits_engine(const _Engine& __e); | 
 | 2184 | //    explicit independent_bits_engine(_Engine&& __e); | 
 | 2185 |     explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} | 
 | 2186 |     template<class _Sseq> explicit independent_bits_engine(_Sseq& __q) | 
 | 2187 |          : __e_(__q) {} | 
 | 2188 |     void seed() {__e_.seed();} | 
 | 2189 |     void seed(result_type __sd) {__e_.seed(__sd);} | 
 | 2190 |     template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);} | 
 | 2191 |  | 
 | 2192 |     // generating functions | 
 | 2193 |     result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} | 
 | 2194 |     void discard(unsigned long long __z) {for (; __z; --__z) operator()();} | 
 | 2195 |  | 
 | 2196 |     // property functions | 
 | 2197 |     const _Engine& base() const {return __e_;} | 
 | 2198 |  | 
 | 2199 |     template<class _Eng, size_t _W, class _UI> | 
 | 2200 |     friend | 
 | 2201 |     bool | 
 | 2202 |     operator==( | 
 | 2203 |         const independent_bits_engine<_Eng, _W, _UI>& __x, | 
 | 2204 |         const independent_bits_engine<_Eng, _W, _UI>& __y); | 
 | 2205 |      | 
 | 2206 |     template<class _Eng, size_t _W, class _UI> | 
 | 2207 |     friend | 
 | 2208 |     bool | 
 | 2209 |     operator!=( | 
 | 2210 |         const independent_bits_engine<_Eng, _W, _UI>& __x, | 
 | 2211 |         const independent_bits_engine<_Eng, _W, _UI>& __y); | 
 | 2212 |      | 
 | 2213 |     template <class _CharT, class _Traits, | 
 | 2214 |               class _Eng, size_t _W, class _UI> | 
 | 2215 |     friend | 
 | 2216 |     basic_ostream<_CharT, _Traits>& | 
 | 2217 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2218 |                const independent_bits_engine<_Eng, _W, _UI>& __x); | 
 | 2219 |      | 
 | 2220 |     template <class _CharT, class _Traits, | 
 | 2221 |               class _Eng, size_t _W, class _UI> | 
 | 2222 |     friend | 
 | 2223 |     basic_istream<_CharT, _Traits>& | 
 | 2224 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2225 |                independent_bits_engine<_Eng, _W, _UI>& __x); | 
 | 2226 |  | 
 | 2227 | private: | 
 | 2228 |     result_type __eval(false_type); | 
 | 2229 |     result_type __eval(true_type); | 
 | 2230 |  | 
 | 2231 |     template <size_t __count> | 
 | 2232 |         static | 
 | 2233 |         typename enable_if | 
 | 2234 |         < | 
 | 2235 |             __count < _Dt, | 
 | 2236 |             result_type | 
 | 2237 |         >::type | 
 | 2238 |         __lshift(result_type __x) {return __x << __count;} | 
 | 2239 |  | 
 | 2240 |     template <size_t __count> | 
 | 2241 |         static | 
 | 2242 |         typename enable_if | 
 | 2243 |         < | 
 | 2244 |             (__count >= _Dt), | 
 | 2245 |             result_type | 
 | 2246 |         >::type | 
 | 2247 |         __lshift(result_type __x) {return result_type(0);} | 
 | 2248 | }; | 
 | 2249 |  | 
 | 2250 | template<class _Engine, size_t __w, class _UIntType> | 
 | 2251 | inline | 
 | 2252 | _UIntType | 
 | 2253 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) | 
 | 2254 | { | 
 | 2255 |     return static_cast<result_type>(__e_() & __mask0); | 
 | 2256 | } | 
 | 2257 |  | 
 | 2258 | template<class _Engine, size_t __w, class _UIntType> | 
 | 2259 | _UIntType | 
 | 2260 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) | 
 | 2261 | { | 
 | 2262 |     result_type _S = 0; | 
 | 2263 |     for (size_t __k = 0; __k < __n0; ++__k) | 
 | 2264 |     { | 
 | 2265 |         _Engine_result_type __u; | 
 | 2266 |         do | 
 | 2267 |         { | 
 | 2268 |             __u = __e_() - _Engine::min(); | 
 | 2269 |         } while (__u >= __y0); | 
 | 2270 |         _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0)); | 
 | 2271 |     } | 
 | 2272 |     for (size_t __k = __n0; __k < __n; ++__k) | 
 | 2273 |     { | 
 | 2274 |         _Engine_result_type __u; | 
 | 2275 |         do | 
 | 2276 |         { | 
 | 2277 |             __u = __e_() - _Engine::min(); | 
 | 2278 |         } while (__u >= __y1); | 
 | 2279 |         _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1)); | 
 | 2280 |     } | 
 | 2281 |     return _S; | 
 | 2282 | } | 
 | 2283 |  | 
 | 2284 | template<class _Eng, size_t _W, class _UI> | 
 | 2285 | inline | 
 | 2286 | bool | 
 | 2287 | operator==( | 
 | 2288 |     const independent_bits_engine<_Eng, _W, _UI>& __x, | 
 | 2289 |     const independent_bits_engine<_Eng, _W, _UI>& __y) | 
 | 2290 | { | 
 | 2291 |     return __x.base() == __y.base(); | 
 | 2292 | } | 
 | 2293 |  | 
 | 2294 | template<class _Eng, size_t _W, class _UI> | 
 | 2295 | inline | 
 | 2296 | bool | 
 | 2297 | operator!=( | 
 | 2298 |     const independent_bits_engine<_Eng, _W, _UI>& __x, | 
 | 2299 |     const independent_bits_engine<_Eng, _W, _UI>& __y) | 
 | 2300 | { | 
 | 2301 |     return !(__x == __y); | 
 | 2302 | } | 
 | 2303 |  | 
 | 2304 | template <class _CharT, class _Traits, | 
 | 2305 |           class _Eng, size_t _W, class _UI> | 
 | 2306 | basic_ostream<_CharT, _Traits>& | 
 | 2307 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2308 |            const independent_bits_engine<_Eng, _W, _UI>& __x) | 
 | 2309 | { | 
 | 2310 |     return __os << __x.base(); | 
 | 2311 | } | 
 | 2312 |  | 
 | 2313 | template <class _CharT, class _Traits, | 
 | 2314 |           class _Eng, size_t _W, class _UI> | 
 | 2315 | basic_istream<_CharT, _Traits>& | 
 | 2316 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2317 |            independent_bits_engine<_Eng, _W, _UI>& __x) | 
 | 2318 | { | 
 | 2319 |     _Eng __e; | 
 | 2320 |     __is >> __e; | 
 | 2321 |     if (!__is.fail()) | 
 | 2322 |         __x.__e_ = __e; | 
 | 2323 |     return __is; | 
 | 2324 | } | 
 | 2325 |  | 
 | 2326 | // shuffle_order_engine | 
 | 2327 |  | 
 | 2328 | template <uint64_t _Xp, uint64_t _Yp> | 
 | 2329 | struct __ugcd | 
 | 2330 | { | 
 | 2331 |     static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; | 
 | 2332 | }; | 
 | 2333 |  | 
 | 2334 | template <uint64_t _Xp> | 
 | 2335 | struct __ugcd<_Xp, 0> | 
 | 2336 | { | 
 | 2337 |     static const uint64_t value = _Xp; | 
 | 2338 | }; | 
 | 2339 |  | 
 | 2340 | template <uint64_t _N, uint64_t _D> | 
 | 2341 | class __uratio | 
 | 2342 | { | 
 | 2343 |     static_assert(_D != 0, "__uratio divide by 0"); | 
 | 2344 |     static const uint64_t __gcd = __ugcd<_N, _D>::value; | 
 | 2345 | public: | 
 | 2346 |     static const uint64_t num = _N / __gcd; | 
 | 2347 |     static const uint64_t den = _D / __gcd; | 
 | 2348 |  | 
 | 2349 |     typedef __uratio<num, den> type; | 
 | 2350 | }; | 
 | 2351 |  | 
 | 2352 | template<class _Engine, size_t __k> | 
 | 2353 | class shuffle_order_engine | 
 | 2354 | { | 
 | 2355 |     static_assert(0 < __k, "shuffle_order_engine invalid parameters"); | 
 | 2356 | public: | 
 | 2357 |     // types | 
 | 2358 |     typedef typename _Engine::result_type result_type; | 
 | 2359 |  | 
 | 2360 | private: | 
 | 2361 |     _Engine __e_; | 
 | 2362 |     result_type _V_[__k]; | 
 | 2363 |     result_type _Y_; | 
 | 2364 |  | 
 | 2365 | public: | 
 | 2366 |     // engine characteristics | 
 | 2367 |     static const/*expr*/ size_t table_size = __k; | 
 | 2368 |      | 
 | 2369 |     static const result_type _Min = _Engine::_Min; | 
 | 2370 |     static const result_type _Max = _Engine::_Max; | 
 | 2371 |     static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); | 
 | 2372 |     static const/*expr*/ result_type min() { return _Min; } | 
 | 2373 |     static const/*expr*/ result_type max() { return _Max; } | 
 | 2374 |  | 
 | 2375 |     static const unsigned long long _R = _Max - _Min + 1ull; | 
 | 2376 |  | 
 | 2377 |     // constructors and seeding functions | 
 | 2378 |     shuffle_order_engine() {__init();} | 
 | 2379 | //    explicit shuffle_order_engine(const _Engine& __e); | 
 | 2380 | //    explicit shuffle_order_engine(_Engine&& e); | 
 | 2381 |     explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} | 
 | 2382 |     template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q) | 
 | 2383 |          : __e_(__q) {__init();} | 
 | 2384 |     void seed() {__e_.seed(); __init();} | 
 | 2385 |     void seed(result_type __sd) {__e_.seed(__sd); __init();} | 
 | 2386 |     template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();} | 
 | 2387 |  | 
 | 2388 |     // generating functions | 
 | 2389 |     result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} | 
 | 2390 |     void discard(unsigned long long __z) {for (; __z; --__z) operator()();} | 
 | 2391 |  | 
 | 2392 |     // property functions | 
 | 2393 |     const _Engine& base() const {return __e_;} | 
 | 2394 |  | 
 | 2395 | private: | 
 | 2396 |     template<class _Eng, size_t _K> | 
 | 2397 |     friend | 
 | 2398 |     bool | 
 | 2399 |     operator==( | 
 | 2400 |         const shuffle_order_engine<_Eng, _K>& __x, | 
 | 2401 |         const shuffle_order_engine<_Eng, _K>& __y); | 
 | 2402 |      | 
 | 2403 |     template<class _Eng, size_t _K> | 
 | 2404 |     friend | 
 | 2405 |     bool | 
 | 2406 |     operator!=( | 
 | 2407 |         const shuffle_order_engine<_Eng, _K>& __x, | 
 | 2408 |         const shuffle_order_engine<_Eng, _K>& __y); | 
 | 2409 |      | 
 | 2410 |     template <class _CharT, class _Traits, | 
 | 2411 |               class _Eng, size_t _K> | 
 | 2412 |     friend | 
 | 2413 |     basic_ostream<_CharT, _Traits>& | 
 | 2414 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2415 |                const shuffle_order_engine<_Eng, _K>& __x); | 
 | 2416 |      | 
 | 2417 |     template <class _CharT, class _Traits, | 
 | 2418 |               class _Eng, size_t _K> | 
 | 2419 |     friend | 
 | 2420 |     basic_istream<_CharT, _Traits>& | 
 | 2421 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2422 |                shuffle_order_engine<_Eng, _K>& __x); | 
 | 2423 |  | 
 | 2424 |     void __init() | 
 | 2425 |     { | 
 | 2426 |         for (size_t __i = 0; __i < __k; ++__i) | 
 | 2427 |             _V_[__i] = __e_(); | 
 | 2428 |         _Y_ = __e_(); | 
 | 2429 |     } | 
 | 2430 |  | 
 | 2431 |     result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} | 
 | 2432 |     result_type __eval(true_type) {return __eval(__uratio<__k, _R>());} | 
 | 2433 |  | 
 | 2434 |     result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} | 
 | 2435 |     result_type __eval2(true_type) {return __evalf<__k, 0>();} | 
 | 2436 |  | 
 | 2437 |     template <uint64_t _N, uint64_t _D> | 
 | 2438 |         typename enable_if | 
 | 2439 |         < | 
 | 2440 |             (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), | 
 | 2441 |             result_type | 
 | 2442 |         >::type | 
 | 2443 |         __eval(__uratio<_N, _D>) | 
 | 2444 |             {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();} | 
 | 2445 |  | 
 | 2446 |     template <uint64_t _N, uint64_t _D> | 
 | 2447 |         typename enable_if | 
 | 2448 |         < | 
 | 2449 |             __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), | 
 | 2450 |             result_type | 
 | 2451 |         >::type | 
 | 2452 |         __eval(__uratio<_N, _D>) | 
 | 2453 |         { | 
 | 2454 |             const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min) | 
 | 2455 |                                                    / __uratio<_N, _D>::den); | 
 | 2456 |             _Y_ = _V_[__j]; | 
 | 2457 |             _V_[__j] = __e_(); | 
 | 2458 |             return _Y_; | 
 | 2459 |         } | 
 | 2460 |  | 
 | 2461 |     template <uint64_t __n, uint64_t __d> | 
 | 2462 |         result_type __evalf() | 
 | 2463 |         { | 
 | 2464 |             const double _F = __d == 0 ? | 
 | 2465 |                 __n / (2. * 0x8000000000000000ull) : | 
 | 2466 |                 __n / (double)__d; | 
 | 2467 |             const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min)); | 
 | 2468 |             _Y_ = _V_[__j]; | 
 | 2469 |             _V_[__j] = __e_(); | 
 | 2470 |             return _Y_; | 
 | 2471 |         } | 
 | 2472 | }; | 
 | 2473 |  | 
 | 2474 | template<class _Eng, size_t _K> | 
 | 2475 | bool | 
 | 2476 | operator==( | 
 | 2477 |     const shuffle_order_engine<_Eng, _K>& __x, | 
 | 2478 |     const shuffle_order_engine<_Eng, _K>& __y) | 
 | 2479 | { | 
 | 2480 |     return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) && | 
 | 2481 |            __x.__e_ == __y.__e_; | 
 | 2482 | } | 
 | 2483 |  | 
 | 2484 | template<class _Eng, size_t _K> | 
 | 2485 | inline | 
 | 2486 | bool | 
 | 2487 | operator!=( | 
 | 2488 |     const shuffle_order_engine<_Eng, _K>& __x, | 
 | 2489 |     const shuffle_order_engine<_Eng, _K>& __y) | 
 | 2490 | { | 
 | 2491 |     return !(__x == __y); | 
 | 2492 | } | 
 | 2493 |  | 
 | 2494 | template <class _CharT, class _Traits, | 
 | 2495 |           class _Eng, size_t _K> | 
 | 2496 | basic_ostream<_CharT, _Traits>& | 
 | 2497 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2498 |            const shuffle_order_engine<_Eng, _K>& __x) | 
 | 2499 | { | 
 | 2500 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 2501 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 2502 |     _CharT __sp = __os.widen(' '); | 
 | 2503 |     __os.fill(__sp); | 
 | 2504 |     __os << __x.__e_ << __sp << __x._V_[0]; | 
 | 2505 |     for (size_t __i = 1; __i < _K; ++__i) | 
 | 2506 |         __os << __sp << __x._V_[__i]; | 
 | 2507 |     return __os << __sp << __x._Y_; | 
 | 2508 | } | 
 | 2509 |  | 
 | 2510 | template <class _CharT, class _Traits, | 
 | 2511 |           class _Eng, size_t _K> | 
 | 2512 | basic_istream<_CharT, _Traits>& | 
 | 2513 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2514 |            shuffle_order_engine<_Eng, _K>& __x) | 
 | 2515 | { | 
 | 2516 |     typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type; | 
 | 2517 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 2518 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 2519 |     _Eng __e; | 
 | 2520 |     result_type _V[_K+1]; | 
 | 2521 |     __is >> __e; | 
 | 2522 |     for (size_t __i = 0; __i < _K+1; ++__i) | 
 | 2523 |         __is >> _V[__i]; | 
 | 2524 |     if (!__is.fail()) | 
 | 2525 |     { | 
 | 2526 |         __x.__e_ = __e; | 
 | 2527 |         for (size_t __i = 0; __i < _K; ++__i) | 
 | 2528 |             __x._V_[__i] = _V[__i]; | 
 | 2529 |         __x._Y_ = _V[_K]; | 
 | 2530 |     } | 
 | 2531 |     return __is; | 
 | 2532 | } | 
 | 2533 |  | 
 | 2534 | typedef shuffle_order_engine<minstd_rand0, 256>                         knuth_b; | 
 | 2535 |  | 
 | 2536 | // random_device | 
 | 2537 |  | 
 | 2538 | class random_device | 
 | 2539 | { | 
 | 2540 |     int __f_; | 
 | 2541 | public: | 
 | 2542 |     // types | 
 | 2543 |     typedef unsigned result_type; | 
 | 2544 |  | 
 | 2545 |     // generator characteristics | 
 | 2546 |     static const result_type _Min = 0; | 
 | 2547 |     static const result_type _Max = 0xFFFFFFFFu; | 
 | 2548 |  | 
 | 2549 |     static const/*expr*/ result_type min() { return _Min;} | 
 | 2550 |     static const/*expr*/ result_type max() { return _Max;} | 
 | 2551 |  | 
 | 2552 |     // constructors | 
 | 2553 |     explicit random_device(const string& __token = "/dev/urandom"); | 
 | 2554 |     ~random_device(); | 
 | 2555 |  | 
 | 2556 |     // generating functions | 
 | 2557 |     result_type operator()(); | 
 | 2558 |  | 
 | 2559 |     // property functions | 
 | 2560 |     double entropy() const; | 
 | 2561 |  | 
 | 2562 | private: | 
 | 2563 |     // no copy functions | 
 | 2564 |     random_device(const random_device&); // = delete; | 
 | 2565 |     random_device& operator=(const random_device&); // = delete; | 
 | 2566 | }; | 
 | 2567 |  | 
 | 2568 | // seed_seq | 
 | 2569 |  | 
 | 2570 | class seed_seq | 
 | 2571 | { | 
 | 2572 | public: | 
 | 2573 |     // types | 
 | 2574 |     typedef uint32_t result_type; | 
 | 2575 |  | 
 | 2576 | private: | 
 | 2577 |     vector<result_type> __v_; | 
 | 2578 |  | 
 | 2579 |     template<class _InputIterator> | 
 | 2580 |         void init(_InputIterator __first, _InputIterator __last); | 
 | 2581 | public: | 
 | 2582 |     // constructors | 
 | 2583 |     seed_seq() {} | 
 | 2584 |     template<class _Tp> | 
 | 2585 |         seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} | 
 | 2586 |              | 
 | 2587 |     template<class _InputIterator> | 
 | 2588 |         seed_seq(_InputIterator __first, _InputIterator __last) | 
 | 2589 |              {init(__first, __last);} | 
 | 2590 |  | 
 | 2591 |     // generating functions | 
 | 2592 |     template<class _RandomAccessIterator> | 
 | 2593 |         void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); | 
 | 2594 |  | 
 | 2595 |     // property functions | 
 | 2596 |     size_t size() const {return __v_.size();} | 
 | 2597 |     template<class _OutputIterator> | 
 | 2598 |         void param(_OutputIterator __dest) const | 
 | 2599 |             {_STD::copy(__v_.begin(), __v_.end(), __dest);} | 
 | 2600 |  | 
 | 2601 | private: | 
 | 2602 |     // no copy functions | 
 | 2603 |     seed_seq(const seed_seq&); // = delete; | 
 | 2604 |     void operator=(const seed_seq&); // = delete; | 
 | 2605 |  | 
 | 2606 |     static result_type _T(result_type __x) {return __x ^ (__x >> 27);} | 
 | 2607 | }; | 
 | 2608 |  | 
 | 2609 | template<class _InputIterator> | 
 | 2610 | void | 
 | 2611 | seed_seq::init(_InputIterator __first, _InputIterator __last) | 
 | 2612 | { | 
 | 2613 |     for (_InputIterator __s = __first; __s != __last; ++__s) | 
 | 2614 |         __v_.push_back(*__s & 0xFFFFFFFF); | 
 | 2615 | } | 
 | 2616 |  | 
 | 2617 | template<class _RandomAccessIterator> | 
 | 2618 | void | 
 | 2619 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) | 
 | 2620 | { | 
 | 2621 |     if (__first != __last) | 
 | 2622 |     { | 
 | 2623 |         _STD::fill(__first, __last, 0x8b8b8b8b); | 
 | 2624 |         const size_t __n = static_cast<size_t>(__last - __first); | 
 | 2625 |         const size_t __s = __v_.size(); | 
 | 2626 |         const size_t __t = (__n >= 623) ? 11 | 
 | 2627 |                          : (__n >= 68) ? 7 | 
 | 2628 |                          : (__n >= 39) ? 5 | 
 | 2629 |                          : (__n >= 7)  ? 3 | 
 | 2630 |                          : (__n - 1) / 2; | 
 | 2631 |         const size_t __p = (__n - __t) / 2; | 
 | 2632 |         const size_t __q = __p + __t; | 
 | 2633 |         const size_t __m = _STD::max(__s + 1, __n); | 
 | 2634 |         // __k = 0; | 
 | 2635 |         { | 
 | 2636 |             result_type __r = 1664525 * _T(__first[0] ^ __first[__p] | 
 | 2637 |                                                       ^  __first[__n - 1]); | 
 | 2638 |             __first[__p] += __r; | 
 | 2639 |             __r += __s; | 
 | 2640 |             __first[__q] += __r; | 
 | 2641 |             __first[0] = __r; | 
 | 2642 |         } | 
 | 2643 |         for (size_t __k = 1; __k <= __s; ++__k) | 
 | 2644 |         { | 
 | 2645 |             const size_t __kmodn = __k % __n; | 
 | 2646 |             const size_t __kpmodn = (__k + __p) % __n; | 
 | 2647 |             result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] | 
 | 2648 |                                            ^ __first[(__k - 1) % __n]); | 
 | 2649 |             __first[__kpmodn] += __r; | 
 | 2650 |             __r +=  __kmodn + __v_[__k-1]; | 
 | 2651 |             __first[(__k + __q) % __n] += __r; | 
 | 2652 |             __first[__kmodn] = __r; | 
 | 2653 |         } | 
 | 2654 |         for (size_t __k = __s + 1; __k < __m; ++__k) | 
 | 2655 |         { | 
 | 2656 |             const size_t __kmodn = __k % __n; | 
 | 2657 |             const size_t __kpmodn = (__k + __p) % __n; | 
 | 2658 |             result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] | 
 | 2659 |                                            ^ __first[(__k - 1) % __n]); | 
 | 2660 |             __first[__kpmodn] += __r; | 
 | 2661 |             __r +=  __kmodn; | 
 | 2662 |             __first[(__k + __q) % __n] += __r; | 
 | 2663 |             __first[__kmodn] = __r; | 
 | 2664 |         } | 
 | 2665 |         for (size_t __k = __m; __k < __m + __n; ++__k) | 
 | 2666 |         { | 
 | 2667 |             const size_t __kmodn = __k % __n; | 
 | 2668 |             const size_t __kpmodn = (__k + __p) % __n; | 
 | 2669 |             result_type __r = 1566083941 * _T(__first[__kmodn] + | 
 | 2670 |                                               __first[__kpmodn] + | 
 | 2671 |                                               __first[(__k - 1) % __n]); | 
 | 2672 |             __first[__kpmodn] ^= __r; | 
 | 2673 |             __r -= __kmodn; | 
 | 2674 |             __first[(__k + __q) % __n] ^= __r; | 
 | 2675 |             __first[__kmodn] = __r; | 
 | 2676 |         } | 
 | 2677 |     } | 
 | 2678 | } | 
 | 2679 |  | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 2680 | // generate_canonical | 
 | 2681 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2682 | template<class _RealType, size_t __bits, class _URNG> | 
 | 2683 | _RealType | 
 | 2684 | generate_canonical(_URNG& __g) | 
 | 2685 | { | 
 | 2686 |     const size_t _Dt = numeric_limits<_RealType>::digits; | 
 | 2687 |     const size_t __b = _Dt < __bits ? _Dt : __bits; | 
 | 2688 |     const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value; | 
 | 2689 |     const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); | 
 | 2690 |     const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1); | 
 | 2691 |     _RealType __base = _R; | 
 | 2692 |     _RealType _S = __g() - _URNG::_Min; | 
 | 2693 |     for (size_t __i = 1; __i < __k; ++__i, __base *= _R) | 
 | 2694 |         _S += (__g() - _URNG::_Min) * __base; | 
 | 2695 |     return _S / __base; | 
 | 2696 | } | 
 | 2697 |  | 
 | 2698 | // __independent_bits_engine | 
 | 2699 |  | 
 | 2700 | template<class _Engine, class _UIntType> | 
 | 2701 | class __independent_bits_engine | 
 | 2702 | { | 
 | 2703 | public: | 
 | 2704 |     // types | 
 | 2705 |     typedef _UIntType result_type; | 
 | 2706 |  | 
 | 2707 | private: | 
 | 2708 |     typedef typename _Engine::result_type _Engine_result_type; | 
 | 2709 |     typedef typename conditional | 
 | 2710 |         < | 
 | 2711 |             sizeof(_Engine_result_type) <= sizeof(result_type), | 
 | 2712 |                 result_type, | 
 | 2713 |                 _Engine_result_type | 
 | 2714 |         >::type _Working_result_type; | 
 | 2715 |  | 
 | 2716 |     _Engine& __e_; | 
 | 2717 |     size_t __w_; | 
 | 2718 |     size_t __w0_; | 
 | 2719 |     size_t __n_; | 
 | 2720 |     size_t __n0_; | 
 | 2721 |     _Working_result_type __y0_; | 
 | 2722 |     _Working_result_type __y1_; | 
 | 2723 |     _Engine_result_type __mask0_; | 
 | 2724 |     _Engine_result_type __mask1_; | 
 | 2725 |  | 
 | 2726 |     static const _Working_result_type _R = _Engine::_Max - _Engine::_Min | 
 | 2727 |                                                          + _Working_result_type(1); | 
 | 2728 |     static const size_t __m = __log2<_Working_result_type, _R>::value; | 
 | 2729 |     static const size_t _WDt = numeric_limits<_Working_result_type>::digits; | 
 | 2730 |     static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; | 
 | 2731 |  | 
 | 2732 | public: | 
 | 2733 |     // constructors and seeding functions | 
 | 2734 |     __independent_bits_engine(_Engine& __e, size_t __w); | 
 | 2735 |  | 
 | 2736 |     // generating functions | 
 | 2737 |     result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} | 
 | 2738 |  | 
 | 2739 | private: | 
 | 2740 |     result_type __eval(false_type); | 
 | 2741 |     result_type __eval(true_type); | 
 | 2742 | }; | 
 | 2743 |  | 
 | 2744 | template<class _Engine, class _UIntType> | 
 | 2745 | __independent_bits_engine<_Engine, _UIntType> | 
 | 2746 |     ::__independent_bits_engine(_Engine& __e, size_t __w) | 
 | 2747 |         : __e_(__e), | 
 | 2748 |           __w_(__w) | 
 | 2749 | { | 
 | 2750 |     __n_ = __w_ / __m + (__w_ % __m != 0); | 
 | 2751 |     __w0_ = __w_ / __n_; | 
 | 2752 |     if (_R == 0) | 
 | 2753 |         __y0_ = _R; | 
 | 2754 |     else if (__w0_ < _WDt) | 
 | 2755 |         __y0_ = (_R >> __w0_) << __w0_; | 
 | 2756 |     else | 
 | 2757 |         __y0_ = 0; | 
 | 2758 |     if (_R - __y0_ > __y0_ / __n_) | 
 | 2759 |     { | 
 | 2760 |         ++__n_; | 
 | 2761 |         __w0_ = __w_ / __n_; | 
 | 2762 |         if (__w0_ < _WDt) | 
 | 2763 |             __y0_ = (_R >> __w0_) << __w0_; | 
 | 2764 |         else | 
 | 2765 |             __y0_ = 0; | 
 | 2766 |     } | 
 | 2767 |     __n0_ = __n_ - __w_ % __n_; | 
 | 2768 |     if (__w0_ < _WDt - 1) | 
 | 2769 |         __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1); | 
 | 2770 |     else | 
 | 2771 |         __y1_ = 0; | 
 | 2772 |     __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : | 
 | 2773 |                           _Engine_result_type(0); | 
 | 2774 |     __mask1_ = __w0_ < _EDt - 1 ? | 
 | 2775 |                                _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : | 
 | 2776 |                                _Engine_result_type(~0); | 
 | 2777 | } | 
 | 2778 |  | 
 | 2779 | template<class _Engine, class _UIntType> | 
 | 2780 | inline | 
 | 2781 | _UIntType | 
 | 2782 | __independent_bits_engine<_Engine, _UIntType>::__eval(false_type) | 
 | 2783 | { | 
 | 2784 |     return static_cast<result_type>(__e_() & __mask0_); | 
 | 2785 | } | 
 | 2786 |  | 
 | 2787 | template<class _Engine, class _UIntType> | 
 | 2788 | _UIntType | 
 | 2789 | __independent_bits_engine<_Engine, _UIntType>::__eval(true_type) | 
 | 2790 | { | 
 | 2791 |     result_type _S = 0; | 
 | 2792 |     for (size_t __k = 0; __k < __n0_; ++__k) | 
 | 2793 |     { | 
 | 2794 |         _Engine_result_type __u; | 
 | 2795 |         do | 
 | 2796 |         { | 
 | 2797 |             __u = __e_() - _Engine::min(); | 
 | 2798 |         } while (__u >= __y0_); | 
 | 2799 |         if (__w0_ < _EDt) | 
 | 2800 |             _S <<= __w0_; | 
 | 2801 |         else | 
 | 2802 |             _S = 0; | 
 | 2803 |         _S += __u & __mask0_; | 
 | 2804 |     } | 
 | 2805 |     for (size_t __k = __n0_; __k < __n_; ++__k) | 
 | 2806 |     { | 
 | 2807 |         _Engine_result_type __u; | 
 | 2808 |         do | 
 | 2809 |         { | 
 | 2810 |             __u = __e_() - _Engine::min(); | 
 | 2811 |         } while (__u >= __y1_); | 
 | 2812 |         if (__w0_ < _EDt - 1) | 
 | 2813 |             _S <<= __w0_ + 1; | 
 | 2814 |         else | 
 | 2815 |             _S = 0; | 
 | 2816 |         _S += __u & __mask1_; | 
 | 2817 |     } | 
 | 2818 |     return _S; | 
 | 2819 | } | 
 | 2820 |  | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 2821 | // uniform_int_distribution | 
 | 2822 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2823 | template<class _IntType = int> | 
 | 2824 | class uniform_int_distribution | 
 | 2825 | { | 
 | 2826 | public: | 
 | 2827 |     // types | 
 | 2828 |     typedef _IntType result_type; | 
 | 2829 |  | 
 | 2830 |     class param_type | 
 | 2831 |     { | 
 | 2832 |         result_type __a_; | 
 | 2833 |         result_type __b_; | 
 | 2834 |     public: | 
 | 2835 |         typedef uniform_int_distribution distribution_type; | 
 | 2836 |  | 
 | 2837 |         explicit param_type(result_type __a = 0, | 
 | 2838 |                             result_type __b = numeric_limits<result_type>::max()) | 
 | 2839 |             : __a_(__a), __b_(__b) {} | 
 | 2840 |  | 
 | 2841 |         result_type a() const {return __a_;} | 
 | 2842 |         result_type b() const {return __b_;} | 
 | 2843 |  | 
 | 2844 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 2845 |             {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} | 
 | 2846 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 2847 |             {return !(__x == __y);} | 
 | 2848 |     }; | 
 | 2849 |  | 
 | 2850 | private: | 
 | 2851 |     param_type __p_; | 
 | 2852 |  | 
 | 2853 | public: | 
 | 2854 |     // constructors and reset functions | 
 | 2855 |     explicit uniform_int_distribution(result_type __a = 0, | 
 | 2856 |                                       result_type __b = numeric_limits<result_type>::max()) | 
 | 2857 |         : __p_(param_type(__a, __b)) {} | 
 | 2858 |     explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {} | 
 | 2859 |     void reset() {} | 
 | 2860 |  | 
 | 2861 |     // generating functions | 
 | 2862 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 2863 |         {return (*this)(__g, __p_);} | 
 | 2864 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 2865 |  | 
 | 2866 |     // property functions | 
 | 2867 |     result_type a() const {return __p_.a();} | 
 | 2868 |     result_type b() const {return __p_.b();} | 
 | 2869 |  | 
 | 2870 |     param_type param() const {return __p_;} | 
 | 2871 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 2872 |  | 
 | 2873 |     result_type min() const {return a();} | 
 | 2874 |     result_type max() const {return b();} | 
 | 2875 |  | 
 | 2876 |     friend bool operator==(const uniform_int_distribution& __x, | 
 | 2877 |                            const uniform_int_distribution& __y) | 
 | 2878 |         {return __x.__p_ == __y.__p_;} | 
 | 2879 |     friend bool operator!=(const uniform_int_distribution& __x, | 
 | 2880 |                            const uniform_int_distribution& __y) | 
 | 2881 |             {return !(__x == __y);} | 
 | 2882 | }; | 
 | 2883 |  | 
 | 2884 | template<class _IntType> | 
 | 2885 | template<class _URNG> | 
 | 2886 | typename uniform_int_distribution<_IntType>::result_type | 
 | 2887 | uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) | 
 | 2888 | { | 
 | 2889 |     typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t), | 
 | 2890 |                                             uint32_t, uint64_t>::type _UIntType; | 
 | 2891 |     const _UIntType _R = __p.b() - __p.a() + _UIntType(1); | 
 | 2892 |     if (_R == 1) | 
 | 2893 |         return __p.a(); | 
 | 2894 |     const size_t _Dt = numeric_limits<_UIntType>::digits; | 
 | 2895 |     typedef __independent_bits_engine<_URNG, _UIntType> _Eng; | 
 | 2896 |     if (_R == 0) | 
 | 2897 |         return static_cast<result_type>(_Eng(__g, _Dt)()); | 
 | 2898 |     size_t __w = _Dt - __clz(_R) - 1; | 
 | 2899 |     if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0) | 
 | 2900 |         ++__w; | 
 | 2901 |     _Eng __e(__g, __w); | 
 | 2902 |     _UIntType __u; | 
 | 2903 |     do | 
 | 2904 |     { | 
 | 2905 |         __u = __e(); | 
 | 2906 |     } while (__u >= _R); | 
 | 2907 |     return static_cast<result_type>(__u + __p.a()); | 
 | 2908 | } | 
 | 2909 |  | 
 | 2910 | template <class _CharT, class _Traits, class _IT> | 
 | 2911 | basic_ostream<_CharT, _Traits>& | 
 | 2912 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 2913 |            const uniform_int_distribution<_IT>& __x) | 
 | 2914 | { | 
 | 2915 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 2916 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 2917 |     _CharT __sp = __os.widen(' '); | 
 | 2918 |     __os.fill(__sp); | 
 | 2919 |     return __os << __x.a() << __sp << __x.b(); | 
 | 2920 | } | 
 | 2921 |  | 
 | 2922 | template <class _CharT, class _Traits, class _IT> | 
 | 2923 | basic_istream<_CharT, _Traits>& | 
 | 2924 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 2925 |            uniform_int_distribution<_IT>& __x) | 
 | 2926 | { | 
 | 2927 |     typedef uniform_int_distribution<_IT> _Eng; | 
 | 2928 |     typedef typename _Eng::result_type result_type; | 
 | 2929 |     typedef typename _Eng::param_type param_type; | 
 | 2930 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 2931 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 2932 |     result_type __a; | 
 | 2933 |     result_type __b; | 
 | 2934 |     __is >> __a >> __b; | 
 | 2935 |     if (!__is.fail()) | 
 | 2936 |         __x.param(param_type(__a, __b)); | 
 | 2937 |     return __is; | 
 | 2938 | } | 
 | 2939 |  | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 2940 | // uniform_real_distribution | 
 | 2941 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2942 | template<class _RealType = double> | 
 | 2943 | class uniform_real_distribution | 
 | 2944 | { | 
 | 2945 | public: | 
 | 2946 |     // types | 
 | 2947 |     typedef _RealType result_type; | 
 | 2948 |  | 
 | 2949 |     class param_type | 
 | 2950 |     { | 
 | 2951 |         result_type __a_; | 
 | 2952 |         result_type __b_; | 
 | 2953 |     public: | 
 | 2954 |         typedef uniform_real_distribution distribution_type; | 
 | 2955 |  | 
 | 2956 |         explicit param_type(result_type __a = 0, | 
 | 2957 |                             result_type __b = 1) | 
 | 2958 |             : __a_(__a), __b_(__b) {} | 
 | 2959 |  | 
 | 2960 |         result_type a() const {return __a_;} | 
 | 2961 |         result_type b() const {return __b_;} | 
 | 2962 |  | 
 | 2963 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 2964 |             {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} | 
 | 2965 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 2966 |             {return !(__x == __y);} | 
 | 2967 |     }; | 
 | 2968 |  | 
 | 2969 | private: | 
 | 2970 |     param_type __p_; | 
 | 2971 |  | 
 | 2972 | public: | 
 | 2973 |     // constructors and reset functions | 
 | 2974 |     explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) | 
 | 2975 |         : __p_(param_type(__a, __b)) {} | 
 | 2976 |     explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} | 
 | 2977 |     void reset() {} | 
 | 2978 |  | 
 | 2979 |     // generating functions | 
 | 2980 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 2981 |         {return (*this)(__g, __p_);} | 
 | 2982 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 2983 |  | 
 | 2984 |     // property functions | 
 | 2985 |     result_type a() const {return __p_.a();} | 
 | 2986 |     result_type b() const {return __p_.b();} | 
 | 2987 |  | 
 | 2988 |     param_type param() const {return __p_;} | 
 | 2989 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 2990 |  | 
 | 2991 |     result_type min() const {return a();} | 
 | 2992 |     result_type max() const {return b();} | 
 | 2993 |  | 
 | 2994 |     friend bool operator==(const uniform_real_distribution& __x, | 
 | 2995 |                            const uniform_real_distribution& __y) | 
 | 2996 |         {return __x.__p_ == __y.__p_;} | 
 | 2997 |     friend bool operator!=(const uniform_real_distribution& __x, | 
 | 2998 |                            const uniform_real_distribution& __y) | 
 | 2999 |         {return !(__x == __y);} | 
 | 3000 | }; | 
 | 3001 |  | 
 | 3002 | template<class _RealType> | 
 | 3003 | template<class _URNG> | 
 | 3004 | inline | 
 | 3005 | typename uniform_real_distribution<_RealType>::result_type | 
 | 3006 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) | 
 | 3007 | { | 
 | 3008 |     return (__p.b() - __p.a()) | 
 | 3009 |         * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) | 
 | 3010 |         + __p.a(); | 
 | 3011 | } | 
 | 3012 |  | 
 | 3013 | template <class _CharT, class _Traits, class _RT> | 
 | 3014 | basic_ostream<_CharT, _Traits>& | 
 | 3015 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3016 |            const uniform_real_distribution<_RT>& __x) | 
 | 3017 | { | 
 | 3018 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3019 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3020 |     _CharT __sp = __os.widen(' '); | 
 | 3021 |     __os.fill(__sp); | 
 | 3022 |     return __os << __x.a() << __sp << __x.b(); | 
 | 3023 | } | 
 | 3024 |  | 
 | 3025 | template <class _CharT, class _Traits, class _RT> | 
 | 3026 | basic_istream<_CharT, _Traits>& | 
 | 3027 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3028 |            uniform_real_distribution<_RT>& __x) | 
 | 3029 | { | 
 | 3030 |     typedef uniform_real_distribution<_RT> _Eng; | 
 | 3031 |     typedef typename _Eng::result_type result_type; | 
 | 3032 |     typedef typename _Eng::param_type param_type; | 
 | 3033 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3034 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3035 |     result_type __a; | 
 | 3036 |     result_type __b; | 
 | 3037 |     __is >> __a >> __b; | 
 | 3038 |     if (!__is.fail()) | 
 | 3039 |         __x.param(param_type(__a, __b)); | 
 | 3040 |     return __is; | 
 | 3041 | } | 
 | 3042 |  | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3043 | // bernoulli_distribution | 
 | 3044 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3045 | class bernoulli_distribution | 
 | 3046 | { | 
 | 3047 | public: | 
 | 3048 |     // types | 
 | 3049 |     typedef bool result_type; | 
 | 3050 |  | 
 | 3051 |     class param_type | 
 | 3052 |     { | 
 | 3053 |         double __p_; | 
 | 3054 |     public: | 
 | 3055 |         typedef bernoulli_distribution distribution_type; | 
 | 3056 |  | 
 | 3057 |         explicit param_type(double __p = 0.5) : __p_(__p) {} | 
 | 3058 |  | 
 | 3059 |         double p() const {return __p_;} | 
 | 3060 |  | 
 | 3061 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 3062 |             {return __x.__p_ == __y.__p_;} | 
 | 3063 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 3064 |             {return !(__x == __y);} | 
 | 3065 |     }; | 
 | 3066 |  | 
 | 3067 | private: | 
 | 3068 |     param_type __p_; | 
 | 3069 |  | 
 | 3070 | public: | 
 | 3071 |     // constructors and reset functions | 
 | 3072 |     explicit bernoulli_distribution(double __p = 0.5) | 
 | 3073 |         : __p_(param_type(__p)) {} | 
 | 3074 |     explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} | 
 | 3075 |     void reset() {} | 
 | 3076 |  | 
 | 3077 |     // generating functions | 
 | 3078 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 3079 |         {return (*this)(__g, __p_);} | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3080 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3081 |  | 
 | 3082 |     // property functions | 
 | 3083 |     double p() const {return __p_.p();} | 
 | 3084 |  | 
 | 3085 |     param_type param() const {return __p_;} | 
 | 3086 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 3087 |  | 
 | 3088 |     result_type min() const {return false;} | 
 | 3089 |     result_type max() const {return true;} | 
 | 3090 |  | 
 | 3091 |     friend bool operator==(const bernoulli_distribution& __x, | 
 | 3092 |                           const bernoulli_distribution& __y) | 
 | 3093 |         {return __x.__p_ == __y.__p_;} | 
 | 3094 |     friend bool operator!=(const bernoulli_distribution& __x, | 
 | 3095 |                           const bernoulli_distribution& __y) | 
 | 3096 |         {return !(__x == __y);} | 
 | 3097 | }; | 
 | 3098 |  | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3099 | template<class _URNG> | 
 | 3100 | inline | 
 | 3101 | bernoulli_distribution::result_type | 
 | 3102 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) | 
 | 3103 | { | 
 | 3104 |     return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.); | 
 | 3105 | } | 
 | 3106 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3107 | template <class _CharT, class _Traits> | 
 | 3108 | basic_ostream<_CharT, _Traits>& | 
 | 3109 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) | 
 | 3110 | { | 
 | 3111 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3112 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3113 |     _CharT __sp = __os.widen(' '); | 
 | 3114 |     __os.fill(__sp); | 
 | 3115 |     return __os << __x.p(); | 
 | 3116 | } | 
 | 3117 |  | 
 | 3118 | template <class _CharT, class _Traits> | 
 | 3119 | basic_istream<_CharT, _Traits>& | 
 | 3120 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) | 
 | 3121 | { | 
 | 3122 |     typedef bernoulli_distribution _Eng; | 
 | 3123 |     typedef typename _Eng::param_type param_type; | 
 | 3124 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3125 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3126 |     double __p; | 
 | 3127 |     __is >> __p; | 
 | 3128 |     if (!__is.fail()) | 
 | 3129 |         __x.param(param_type(__p)); | 
 | 3130 |     return __is; | 
 | 3131 | } | 
 | 3132 |  | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3133 | // binomial_distribution | 
 | 3134 |  | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3135 | template<class _IntType = int> | 
 | 3136 | class binomial_distribution | 
 | 3137 | { | 
 | 3138 | public: | 
 | 3139 |     // types | 
 | 3140 |     typedef _IntType result_type; | 
 | 3141 |  | 
 | 3142 |     class param_type | 
 | 3143 |     { | 
 | 3144 |         result_type __t_; | 
 | 3145 |         double __p_; | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3146 |         double __pr_; | 
 | 3147 |         double __odds_ratio_; | 
 | 3148 |         result_type __r0_; | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3149 |     public: | 
 | 3150 |         typedef binomial_distribution distribution_type; | 
 | 3151 |  | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3152 |         explicit param_type(result_type __t = 1, double __p = 0.5); | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3153 |  | 
 | 3154 |         result_type t() const {return __t_;} | 
 | 3155 |         double p() const {return __p_;} | 
 | 3156 |  | 
 | 3157 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 3158 |             {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} | 
 | 3159 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 3160 |             {return !(__x == __y);} | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3161 |  | 
 | 3162 |         friend class binomial_distribution; | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3163 |     }; | 
 | 3164 |  | 
 | 3165 | private: | 
 | 3166 |     param_type __p_; | 
 | 3167 |  | 
 | 3168 | public: | 
 | 3169 |     // constructors and reset functions | 
 | 3170 |     explicit binomial_distribution(result_type __t = 1, double __p = 0.5) | 
 | 3171 |         : __p_(param_type(__t, __p)) {} | 
 | 3172 |     explicit binomial_distribution(const param_type& __p) : __p_(__p) {} | 
 | 3173 |     void reset() {} | 
 | 3174 |  | 
 | 3175 |     // generating functions | 
 | 3176 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 3177 |         {return (*this)(__g, __p_);} | 
 | 3178 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 3179 |  | 
 | 3180 |     // property functions | 
 | 3181 |     result_type t() const {return __p_.t();} | 
 | 3182 |     double p() const {return __p_.p();} | 
 | 3183 |  | 
 | 3184 |     param_type param() const {return __p_;} | 
 | 3185 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 3186 |  | 
 | 3187 |     result_type min() const {return 0;} | 
 | 3188 |     result_type max() const {return t();} | 
 | 3189 |  | 
 | 3190 |     friend bool operator==(const binomial_distribution& __x, | 
 | 3191 |                            const binomial_distribution& __y) | 
 | 3192 |         {return __x.__p_ == __y.__p_;} | 
 | 3193 |     friend bool operator!=(const binomial_distribution& __x, | 
 | 3194 |                            const binomial_distribution& __y) | 
 | 3195 |         {return !(__x == __y);} | 
 | 3196 | }; | 
 | 3197 |  | 
 | 3198 | template<class _IntType> | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3199 | binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) | 
 | 3200 |     : __t_(__t), __p_(__p) | 
 | 3201 | { | 
 | 3202 |     if (0 < __p_ && __p_ < 1) | 
 | 3203 |     { | 
 | 3204 |         __r0_ = static_cast<result_type>((__t_ + 1) * __p_); | 
 | 3205 |         __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) - | 
 | 3206 |                           _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) + | 
 | 3207 |                           (__t_ - __r0_) * _STD::log(1 - __p_)); | 
 | 3208 |         __odds_ratio_ = __p_ / (1 - __p_); | 
 | 3209 |     } | 
 | 3210 | } | 
 | 3211 |  | 
 | 3212 | template<class _IntType> | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3213 | template<class _URNG> | 
 | 3214 | _IntType | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3215 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3216 | { | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3217 |     if (__pr.__t_ == 0 || __pr.__p_ == 0) | 
 | 3218 |         return 0; | 
 | 3219 |     if (__pr.__p_ == 1) | 
 | 3220 |         return __pr.__t_; | 
 | 3221 |     uniform_real_distribution<double> __gen; | 
 | 3222 |     double __u = __gen(__g) - __pr.__pr_; | 
 | 3223 |     if (__u < 0) | 
 | 3224 |         return __pr.__r0_; | 
 | 3225 |     double __pu = __pr.__pr_; | 
 | 3226 |     double __pd = __pu; | 
 | 3227 |     result_type __ru = __pr.__r0_; | 
 | 3228 |     result_type __rd = __ru; | 
 | 3229 |     while (true) | 
 | 3230 |     { | 
 | 3231 |         if (__rd >= 1) | 
 | 3232 |         { | 
 | 3233 |             __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); | 
 | 3234 |             __u -= __pd; | 
 | 3235 |             if (__u < 0) | 
 | 3236 |                 return __rd - 1; | 
 | 3237 |         } | 
 | 3238 |         --__rd; | 
 | 3239 |         ++__ru; | 
 | 3240 |         if (__ru <= __pr.__t_) | 
 | 3241 |         { | 
 | 3242 |             __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; | 
 | 3243 |             __u -= __pu; | 
 | 3244 |             if (__u < 0) | 
 | 3245 |                 return __ru; | 
 | 3246 |         } | 
 | 3247 |     } | 
| Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3248 | } | 
 | 3249 |  | 
 | 3250 | template <class _CharT, class _Traits, class _IntType> | 
 | 3251 | basic_ostream<_CharT, _Traits>& | 
 | 3252 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3253 |            const binomial_distribution<_IntType>& __x) | 
 | 3254 | { | 
 | 3255 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3256 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3257 |     _CharT __sp = __os.widen(' '); | 
 | 3258 |     __os.fill(__sp); | 
 | 3259 |     return __os << __x.t() << __sp << __x.p(); | 
 | 3260 | } | 
 | 3261 |  | 
 | 3262 | template <class _CharT, class _Traits, class _IntType> | 
 | 3263 | basic_istream<_CharT, _Traits>& | 
 | 3264 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3265 |            binomial_distribution<_IntType>& __x) | 
 | 3266 | { | 
 | 3267 |     typedef binomial_distribution<_IntType> _Eng; | 
 | 3268 |     typedef typename _Eng::result_type result_type; | 
 | 3269 |     typedef typename _Eng::param_type param_type; | 
 | 3270 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3271 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3272 |     result_type __t; | 
 | 3273 |     double __p; | 
 | 3274 |     __is >> __t >> __p; | 
 | 3275 |     if (!__is.fail()) | 
 | 3276 |         __x.param(param_type(__t, __p)); | 
 | 3277 |     return __is; | 
 | 3278 | } | 
 | 3279 |  | 
| Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3280 | // exponential_distribution | 
 | 3281 |  | 
 | 3282 | template<class _RealType = double> | 
 | 3283 | class exponential_distribution | 
 | 3284 | { | 
 | 3285 | public: | 
 | 3286 |     // types | 
 | 3287 |     typedef _RealType result_type; | 
 | 3288 |  | 
 | 3289 |     class param_type | 
 | 3290 |     { | 
 | 3291 |         result_type __lambda_; | 
 | 3292 |     public: | 
 | 3293 |         typedef exponential_distribution distribution_type; | 
 | 3294 |  | 
 | 3295 |         explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} | 
 | 3296 |  | 
 | 3297 |         result_type lambda() const {return __lambda_;} | 
 | 3298 |  | 
 | 3299 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 3300 |             {return __x.__lambda_ == __y.__lambda_;} | 
 | 3301 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 3302 |             {return !(__x == __y);} | 
 | 3303 |     }; | 
 | 3304 |  | 
 | 3305 | private: | 
 | 3306 |     param_type __p_; | 
 | 3307 |  | 
 | 3308 | public: | 
 | 3309 |     // constructors and reset functions | 
 | 3310 |     explicit exponential_distribution(result_type __lambda = 1) | 
 | 3311 |         : __p_(param_type(__lambda)) {} | 
 | 3312 |     explicit exponential_distribution(const param_type& __p) : __p_(__p) {} | 
 | 3313 |     void reset() {} | 
 | 3314 |  | 
 | 3315 |     // generating functions | 
 | 3316 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 3317 |         {return (*this)(__g, __p_);} | 
 | 3318 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 3319 |  | 
 | 3320 |     // property functions | 
 | 3321 |     result_type lambda() const {return __p_.lambda();} | 
 | 3322 |  | 
 | 3323 |     param_type param() const {return __p_;} | 
 | 3324 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 3325 |  | 
 | 3326 |     result_type min() const {return 0;} | 
 | 3327 |     result_type max() const | 
 | 3328 |         {return -std::log(1-std::nextafter(result_type(1), result_type(-1))) / | 
 | 3329 |                  __p_.lambda();} | 
 | 3330 |  | 
 | 3331 |     friend bool operator==(const exponential_distribution& __x, | 
 | 3332 |                            const exponential_distribution& __y) | 
 | 3333 |         {return __x.__p_ == __y.__p_;} | 
 | 3334 |     friend bool operator!=(const exponential_distribution& __x, | 
 | 3335 |                            const exponential_distribution& __y) | 
 | 3336 |         {return !(__x == __y);} | 
 | 3337 | }; | 
 | 3338 |  | 
 | 3339 | template <class _RealType> | 
 | 3340 | template<class _URNG> | 
 | 3341 | _RealType | 
 | 3342 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) | 
 | 3343 | { | 
 | 3344 |     return -_STD::log | 
 | 3345 |                   ( | 
 | 3346 |                       result_type(1) - | 
 | 3347 |                       _STD::generate_canonical<result_type, | 
 | 3348 |                                        numeric_limits<result_type>::digits>(__g) | 
 | 3349 |                   ) | 
 | 3350 |                   / __p.lambda(); | 
 | 3351 | } | 
 | 3352 |  | 
 | 3353 | template <class _CharT, class _Traits, class _RealType> | 
 | 3354 | basic_ostream<_CharT, _Traits>& | 
 | 3355 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3356 |            const exponential_distribution<_RealType>& __x) | 
 | 3357 | { | 
 | 3358 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3359 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3360 |     return __os << __x.lambda(); | 
 | 3361 | } | 
 | 3362 |  | 
 | 3363 | template <class _CharT, class _Traits, class _RealType> | 
 | 3364 | basic_istream<_CharT, _Traits>& | 
 | 3365 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3366 |            exponential_distribution<_RealType>& __x) | 
 | 3367 | { | 
 | 3368 |     typedef exponential_distribution<_RealType> _Eng; | 
 | 3369 |     typedef typename _Eng::result_type result_type; | 
 | 3370 |     typedef typename _Eng::param_type param_type; | 
 | 3371 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3372 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3373 |     result_type __lambda; | 
 | 3374 |     __is >> __lambda; | 
 | 3375 |     if (!__is.fail()) | 
 | 3376 |         __x.param(param_type(__lambda)); | 
 | 3377 |     return __is; | 
 | 3378 | } | 
 | 3379 |  | 
| Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame^] | 3380 | // normal_distribution | 
 | 3381 |  | 
 | 3382 | template<class _RealType = double> | 
 | 3383 | class normal_distribution | 
 | 3384 | { | 
 | 3385 | public: | 
 | 3386 |     // types | 
 | 3387 |     typedef _RealType result_type; | 
 | 3388 |  | 
 | 3389 |     class param_type | 
 | 3390 |     { | 
 | 3391 |         result_type __mean_; | 
 | 3392 |         result_type __stddev_; | 
 | 3393 |     public: | 
 | 3394 |         typedef normal_distribution distribution_type; | 
 | 3395 |  | 
 | 3396 |         explicit param_type(result_type __mean = 0, result_type __stddev = 1) | 
 | 3397 |             : __mean_(__mean), __stddev_(__stddev) {} | 
 | 3398 |  | 
 | 3399 |         result_type mean() const {return __mean_;} | 
 | 3400 |         result_type stddev() const {return __stddev_;} | 
 | 3401 |  | 
 | 3402 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 3403 |             {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} | 
 | 3404 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 3405 |             {return !(__x == __y);} | 
 | 3406 |     }; | 
 | 3407 |  | 
 | 3408 | private: | 
 | 3409 |     param_type __p_; | 
 | 3410 |     result_type _V_; | 
 | 3411 |     bool _V_hot_; | 
 | 3412 |  | 
 | 3413 | public: | 
 | 3414 |     // constructors and reset functions | 
 | 3415 |     explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1) | 
 | 3416 |         : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} | 
 | 3417 |     explicit normal_distribution(const param_type& __p) | 
 | 3418 |         : __p_(__p), _V_hot_(false) {} | 
 | 3419 |     void reset() {_V_hot_ = false;} | 
 | 3420 |  | 
 | 3421 |     // generating functions | 
 | 3422 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 3423 |         {return (*this)(__g, __p_);} | 
 | 3424 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 3425 |  | 
 | 3426 |     // property functions | 
 | 3427 |     result_type mean() const {return __p_.mean();} | 
 | 3428 |     result_type stddev() const {return __p_.stddev();} | 
 | 3429 |  | 
 | 3430 |     param_type param() const {return __p_;} | 
 | 3431 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 3432 |  | 
 | 3433 |     result_type min() const {return -numeric_limits<result_type>::infinity();} | 
 | 3434 |     result_type max() const {return numeric_limits<result_type>::infinity();} | 
 | 3435 |  | 
 | 3436 |     friend bool operator==(const normal_distribution& __x, | 
 | 3437 |                            const normal_distribution& __y) | 
 | 3438 |         {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && | 
 | 3439 |                 (!__x._V_hot_ || __x._V_ == __y._V_);} | 
 | 3440 |     friend bool operator!=(const normal_distribution& __x, | 
 | 3441 |                            const normal_distribution& __y) | 
 | 3442 |         {return !(__x == __y);} | 
 | 3443 |  | 
 | 3444 |     template <class _CharT, class _Traits, class _RT> | 
 | 3445 |     friend | 
 | 3446 |     basic_ostream<_CharT, _Traits>& | 
 | 3447 |     operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3448 |                const normal_distribution<_RT>& __x); | 
 | 3449 |      | 
 | 3450 |     template <class _CharT, class _Traits, class _RT> | 
 | 3451 |     friend | 
 | 3452 |     basic_istream<_CharT, _Traits>& | 
 | 3453 |     operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3454 |                normal_distribution<_RT>& __x); | 
 | 3455 | }; | 
 | 3456 |  | 
 | 3457 | template <class _RealType> | 
 | 3458 | template<class _URNG> | 
 | 3459 | _RealType | 
 | 3460 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) | 
 | 3461 | { | 
 | 3462 |     result_type _U; | 
 | 3463 |     if (_V_hot_) | 
 | 3464 |     { | 
 | 3465 |         _V_hot_ = false; | 
 | 3466 |         _U = _V_; | 
 | 3467 |     } | 
 | 3468 |     else | 
 | 3469 |     { | 
 | 3470 |         uniform_real_distribution<result_type> _Uni(-1, 1); | 
 | 3471 |         result_type __u; | 
 | 3472 |         result_type __v; | 
 | 3473 |         result_type __s; | 
 | 3474 |         do | 
 | 3475 |         { | 
 | 3476 |             __u = _Uni(__g); | 
 | 3477 |             __v = _Uni(__g); | 
 | 3478 |             __s = __u * __u + __v * __v; | 
 | 3479 |         } while (__s > 1 || __s == 0); | 
 | 3480 |         result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s); | 
 | 3481 |         _V_ = __v * _F; | 
 | 3482 |         _V_hot_ = true; | 
 | 3483 |         _U = __u * _F; | 
 | 3484 |     } | 
 | 3485 |     return _U * __p.stddev() + __p.mean(); | 
 | 3486 | } | 
 | 3487 |  | 
 | 3488 | template <class _CharT, class _Traits, class _RT> | 
 | 3489 | basic_ostream<_CharT, _Traits>& | 
 | 3490 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3491 |            const normal_distribution<_RT>& __x) | 
 | 3492 | { | 
 | 3493 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3494 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3495 |     _CharT __sp = __os.widen(' '); | 
 | 3496 |     __os.fill(__sp); | 
 | 3497 |     __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; | 
 | 3498 |     if (__x._V_hot_) | 
 | 3499 |         __os << __sp << __x._V_; | 
 | 3500 |     return __os; | 
 | 3501 | } | 
 | 3502 |  | 
 | 3503 | template <class _CharT, class _Traits, class _RT> | 
 | 3504 | basic_istream<_CharT, _Traits>& | 
 | 3505 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3506 |            normal_distribution<_RT>& __x) | 
 | 3507 | { | 
 | 3508 |     typedef normal_distribution<_RT> _Eng; | 
 | 3509 |     typedef typename _Eng::result_type result_type; | 
 | 3510 |     typedef typename _Eng::param_type param_type; | 
 | 3511 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3512 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3513 |     result_type __mean; | 
 | 3514 |     result_type __stddev; | 
 | 3515 |     result_type _V = 0; | 
 | 3516 |     bool _V_hot = false; | 
 | 3517 |     __is >> __mean >> __stddev >> _V_hot; | 
 | 3518 |     if (_V_hot) | 
 | 3519 |         __is >> _V; | 
 | 3520 |     if (!__is.fail()) | 
 | 3521 |     { | 
 | 3522 |         __x.param(param_type(__mean, __stddev)); | 
 | 3523 |         __x._V_hot_ = _V_hot; | 
 | 3524 |         __x._V_ = _V; | 
 | 3525 |     } | 
 | 3526 |     return __is; | 
 | 3527 | } | 
 | 3528 |  | 
 | 3529 | // poisson_distribution | 
 | 3530 |  | 
 | 3531 | template<class _IntType = int> | 
 | 3532 | class poisson_distribution | 
 | 3533 | { | 
 | 3534 | public: | 
 | 3535 |     // types | 
 | 3536 |     typedef _IntType result_type; | 
 | 3537 |  | 
 | 3538 |     class param_type | 
 | 3539 |     { | 
 | 3540 |         double __mean_; | 
 | 3541 |         double __s_; | 
 | 3542 |         double __d_; | 
 | 3543 |         double __l_; | 
 | 3544 |         double __omega_; | 
 | 3545 |         double __c0_; | 
 | 3546 |         double __c1_; | 
 | 3547 |         double __c2_; | 
 | 3548 |         double __c3_; | 
 | 3549 |         double __c_; | 
 | 3550 |  | 
 | 3551 |     public: | 
 | 3552 |         typedef poisson_distribution distribution_type; | 
 | 3553 |  | 
 | 3554 |         explicit param_type(double __mean = 1.0); | 
 | 3555 |  | 
 | 3556 |         double mean() const {return __mean_;} | 
 | 3557 |  | 
 | 3558 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 3559 |             {return __x.__mean_ == __y.__mean_;} | 
 | 3560 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 3561 |             {return !(__x == __y);} | 
 | 3562 |  | 
 | 3563 |         friend class poisson_distribution; | 
 | 3564 |     }; | 
 | 3565 |  | 
 | 3566 | private: | 
 | 3567 |     param_type __p_; | 
 | 3568 |  | 
 | 3569 | public: | 
 | 3570 |     // constructors and reset functions | 
 | 3571 |     explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} | 
 | 3572 |     explicit poisson_distribution(const param_type& __p) : __p_(__p) {} | 
 | 3573 |     void reset() {} | 
 | 3574 |  | 
 | 3575 |     // generating functions | 
 | 3576 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 3577 |         {return (*this)(__g, __p_);} | 
 | 3578 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 3579 |  | 
 | 3580 |     // property functions | 
 | 3581 |     double mean() const {return __p_.mean();} | 
 | 3582 |  | 
 | 3583 |     param_type param() const {return __p_;} | 
 | 3584 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 3585 |  | 
 | 3586 |     result_type min() const {return 0;} | 
 | 3587 |     result_type max() const {return numeric_limits<result_type>::max();} | 
 | 3588 |  | 
 | 3589 |     friend bool operator==(const poisson_distribution& __x, | 
 | 3590 |                            const poisson_distribution& __y) | 
 | 3591 |         {return __x.__p_ == __y.__p_;} | 
 | 3592 |     friend bool operator!=(const poisson_distribution& __x, | 
 | 3593 |                            const poisson_distribution& __y) | 
 | 3594 |         {return !(__x == __y);} | 
 | 3595 | }; | 
 | 3596 |  | 
 | 3597 | template<class _IntType> | 
 | 3598 | poisson_distribution<_IntType>::param_type::param_type(double __mean) | 
 | 3599 |     : __mean_(__mean) | 
 | 3600 | { | 
 | 3601 |     if (__mean_ < 10) | 
 | 3602 |     { | 
 | 3603 |         __s_ = 0; | 
 | 3604 |         __d_ = 0; | 
 | 3605 |         __l_ = _STD::exp(-__mean_); | 
 | 3606 |         __omega_ = 0; | 
 | 3607 |         __c3_ = 0; | 
 | 3608 |         __c2_ = 0; | 
 | 3609 |         __c1_ = 0; | 
 | 3610 |         __c0_ = 0; | 
 | 3611 |         __c_ = 0; | 
 | 3612 |     } | 
 | 3613 |     else | 
 | 3614 |     { | 
 | 3615 |         __s_ = _STD::sqrt(__mean_); | 
 | 3616 |         __d_ = 6 * __mean_ * __mean_; | 
 | 3617 |         __l_ = static_cast<result_type>(__mean_ - 1.1484); | 
 | 3618 |         __omega_ = .3989423 / __s_; | 
 | 3619 |         double __b1_ = .4166667E-1 / __mean_; | 
 | 3620 |         double __b2_ = .3 * __b1_ * __b1_; | 
 | 3621 |         __c3_ = .1428571 * __b1_ * __b2_; | 
 | 3622 |         __c2_ = __b2_ - 15. * __c3_; | 
 | 3623 |         __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; | 
 | 3624 |         __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; | 
 | 3625 |         __c_ = .1069 / __mean_; | 
 | 3626 |     } | 
 | 3627 | } | 
 | 3628 |  | 
 | 3629 | template <class _IntType> | 
 | 3630 | template<class _URNG> | 
 | 3631 | _IntType | 
 | 3632 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) | 
 | 3633 | { | 
 | 3634 |     result_type __x; | 
 | 3635 |     uniform_real_distribution<double> __urd; | 
 | 3636 |     if (__pr.__mean_ <= 10) | 
 | 3637 |     { | 
 | 3638 |          __x = 0; | 
 | 3639 |         for (double __p = __urd(__urng); __p > __pr.__l_; ++__x) | 
 | 3640 |             __p *= __urd(__urng); | 
 | 3641 |     } | 
 | 3642 |     else | 
 | 3643 |     { | 
 | 3644 |         double __difmuk; | 
 | 3645 |         double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); | 
 | 3646 |         double __u; | 
 | 3647 |         if (__g > 0) | 
 | 3648 |         { | 
 | 3649 |             __x = static_cast<result_type>(__g); | 
 | 3650 |             if (__x >= __pr.__l_) | 
 | 3651 |                 return __x; | 
 | 3652 |             __difmuk = __pr.__mean_ - __x; | 
 | 3653 |             __u = __urd(__urng); | 
 | 3654 |             if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) | 
 | 3655 |                 return __x; | 
 | 3656 |         } | 
 | 3657 |         exponential_distribution<double> __edist; | 
 | 3658 |         for (bool __using_exp_dist = false; true; __using_exp_dist = true) | 
 | 3659 |         { | 
 | 3660 |             double __e; | 
 | 3661 |             if (__using_exp_dist || __g < 0) | 
 | 3662 |             { | 
 | 3663 |                 double __t; | 
 | 3664 |                 do | 
 | 3665 |                 { | 
 | 3666 |                     __e = __edist(__urng); | 
 | 3667 |                     __u = __urd(__urng); | 
 | 3668 |                     __u += __u - 1; | 
 | 3669 |                     __t = 1.8 + (__u < 0 ? -__e : __e); | 
 | 3670 |                 } while (__t <= -.6744); | 
 | 3671 |                 __x = __pr.__mean_ + __pr.__s_ * __t; | 
 | 3672 |                 __difmuk = __pr.__mean_ - __x; | 
 | 3673 |                 __using_exp_dist = true; | 
 | 3674 |             } | 
 | 3675 |             double __px; | 
 | 3676 |             double __py; | 
 | 3677 |             if (__x < 10) | 
 | 3678 |             { | 
 | 3679 |                 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, | 
 | 3680 |                                              40320, 362880}; | 
 | 3681 |                 __px = -__pr.__mean_; | 
 | 3682 |                 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x]; | 
 | 3683 |             } | 
 | 3684 |             else | 
 | 3685 |             { | 
 | 3686 |                 double __del = .8333333E-1 / __x; | 
 | 3687 |                 __del -= 4.8 * __del * __del * __del; | 
 | 3688 |                 double __v = __difmuk / __x; | 
 | 3689 |                 if (_STD::abs(__v) > 0.25) | 
 | 3690 |                     __px = __x * _STD::log(1 + __v) - __difmuk - __del; | 
 | 3691 |                 else | 
 | 3692 |                     __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) * | 
 | 3693 |                            __v + .1421878) * __v + -.1661269) * __v + .2000118) * | 
 | 3694 |                            __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; | 
 | 3695 |                 __py = .3989423 / _STD::sqrt(__x); | 
 | 3696 |             } | 
 | 3697 |             double __r = (0.5 - __difmuk) / __pr.__s_; | 
 | 3698 |             double __r2 = __r * __r; | 
 | 3699 |             double __fx = -0.5 * __r2; | 
 | 3700 |             double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * | 
 | 3701 |                                         __r2 + __pr.__c1_) * __r2 + __pr.__c0_); | 
 | 3702 |             if (__using_exp_dist) | 
 | 3703 |             { | 
 | 3704 |                 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) - | 
 | 3705 |                                                    __fy * _STD::exp(__fx + __e)) | 
 | 3706 |                     break; | 
 | 3707 |             } | 
 | 3708 |             else | 
 | 3709 |             { | 
 | 3710 |                 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx)) | 
 | 3711 |                     break; | 
 | 3712 |             } | 
 | 3713 |         } | 
 | 3714 |     } | 
 | 3715 |     return __x; | 
 | 3716 | } | 
 | 3717 |  | 
 | 3718 | template <class _CharT, class _Traits, class _IntType> | 
 | 3719 | basic_ostream<_CharT, _Traits>& | 
 | 3720 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3721 |            const poisson_distribution<_IntType>& __x) | 
 | 3722 | { | 
 | 3723 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3724 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3725 |     return __os << __x.mean(); | 
 | 3726 | } | 
 | 3727 |  | 
 | 3728 | template <class _CharT, class _Traits, class _IntType> | 
 | 3729 | basic_istream<_CharT, _Traits>& | 
 | 3730 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3731 |            poisson_distribution<_IntType>& __x) | 
 | 3732 | { | 
 | 3733 |     typedef poisson_distribution<_IntType> _Eng; | 
 | 3734 |     typedef typename _Eng::param_type param_type; | 
 | 3735 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3736 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3737 |     double __mean; | 
 | 3738 |     __is >> __mean; | 
 | 3739 |     if (!__is.fail()) | 
 | 3740 |         __x.param(param_type(__mean)); | 
 | 3741 |     return __is; | 
 | 3742 | } | 
 | 3743 |  | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3744 | // gamma_distribution | 
 | 3745 |  | 
 | 3746 | template<class _RealType = double> | 
 | 3747 | class gamma_distribution | 
 | 3748 | { | 
 | 3749 | public: | 
 | 3750 |     // types | 
 | 3751 |     typedef _RealType result_type; | 
 | 3752 |  | 
 | 3753 |     class param_type | 
 | 3754 |     { | 
 | 3755 |         result_type __alpha_; | 
 | 3756 |         result_type __beta_; | 
 | 3757 |     public: | 
 | 3758 |         typedef gamma_distribution distribution_type; | 
 | 3759 |  | 
 | 3760 |         explicit param_type(result_type __alpha = 1, result_type __beta = 1) | 
 | 3761 |             : __alpha_(__alpha), __beta_(__beta) {} | 
 | 3762 |  | 
 | 3763 |         result_type alpha() const {return __alpha_;} | 
 | 3764 |         result_type beta() const {return __beta_;} | 
 | 3765 |  | 
 | 3766 |         friend bool operator==(const param_type& __x, const param_type& __y) | 
 | 3767 |             {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} | 
 | 3768 |         friend bool operator!=(const param_type& __x, const param_type& __y) | 
 | 3769 |             {return !(__x == __y);} | 
 | 3770 |     }; | 
 | 3771 |  | 
 | 3772 | private: | 
 | 3773 |     param_type __p_; | 
 | 3774 |  | 
 | 3775 | public: | 
 | 3776 |     // constructors and reset functions | 
 | 3777 |     explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) | 
 | 3778 |         : __p_(param_type(__alpha, __beta)) {} | 
 | 3779 |     explicit gamma_distribution(const param_type& __p) | 
 | 3780 |         : __p_(__p) {} | 
 | 3781 |     void reset() {} | 
 | 3782 |  | 
 | 3783 |     // generating functions | 
 | 3784 |     template<class _URNG> result_type operator()(_URNG& __g) | 
 | 3785 |         {return (*this)(__g, __p_);} | 
 | 3786 |     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); | 
 | 3787 |  | 
 | 3788 |     // property functions | 
 | 3789 |     result_type alpha() const {return __p_.alpha();} | 
 | 3790 |     result_type beta() const {return __p_.beta();} | 
 | 3791 |  | 
 | 3792 |     param_type param() const {return __p_;} | 
 | 3793 |     void param(const param_type& __p) {__p_ = __p;} | 
 | 3794 |  | 
 | 3795 |     result_type min() const {return 0;} | 
 | 3796 |     result_type max() const {return numeric_limits<result_type>::infinity();}  | 
 | 3797 |  | 
 | 3798 |     friend bool operator==(const gamma_distribution& __x, | 
 | 3799 |                            const gamma_distribution& __y) | 
 | 3800 |         {return __x.__p_ == __y.__p_;} | 
 | 3801 |     friend bool operator!=(const gamma_distribution& __x, | 
 | 3802 |                            const gamma_distribution& __y) | 
 | 3803 |         {return !(__x == __y);} | 
 | 3804 | }; | 
 | 3805 |  | 
 | 3806 | template <class _RealType> | 
 | 3807 | template<class _URNG> | 
 | 3808 | _RealType | 
 | 3809 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) | 
 | 3810 | { | 
| Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3811 |     result_type __a = __p.alpha(); | 
 | 3812 |     uniform_real_distribution<result_type> __gen(0, 1); | 
 | 3813 |     exponential_distribution<result_type> __egen; | 
 | 3814 |     result_type __x; | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3815 |     if (__a == 1) | 
| Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3816 |         __x = __egen(__g); | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3817 |     else if (__a > 1) | 
 | 3818 |     { | 
 | 3819 |         const result_type __b = __a - 1; | 
 | 3820 |         const result_type __c = 3 * __a - result_type(0.75); | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3821 |         while (true) | 
 | 3822 |         { | 
 | 3823 |             const result_type __u = __gen(__g); | 
 | 3824 |             const result_type __v = __gen(__g); | 
 | 3825 |             const result_type __w = __u * (1 - __u); | 
| Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3826 |             if (__w != 0) | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3827 |             { | 
 | 3828 |                 const result_type __y = _STD::sqrt(__c / __w) * | 
 | 3829 |                                         (__u - result_type(0.5)); | 
 | 3830 |                 __x = __b + __y; | 
 | 3831 |                 if (__x >= 0) | 
 | 3832 |                 { | 
 | 3833 |                     const result_type __z = 64 * __w * __w * __w * __v * __v; | 
 | 3834 |                     if (__z <= 1 - 2 * __y * __y / __x) | 
 | 3835 |                         break; | 
 | 3836 |                     if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y)) | 
 | 3837 |                         break; | 
 | 3838 |                 } | 
 | 3839 |             } | 
 | 3840 |         } | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3841 |     } | 
| Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3842 |     else  // __a < 1 | 
 | 3843 |     { | 
 | 3844 |         while (true) | 
 | 3845 |         { | 
 | 3846 |             const result_type __u = __gen(__g); | 
 | 3847 |             const result_type __es = __egen(__g); | 
 | 3848 |             if (__u <= 1 - __a) | 
 | 3849 |             { | 
 | 3850 |                 __x = _STD::pow(__u, 1 / __a); | 
 | 3851 |                 if (__x <= __es) | 
 | 3852 |                     break; | 
 | 3853 |             } | 
 | 3854 |             else | 
 | 3855 |             { | 
 | 3856 |                 const result_type __e = -_STD::log((1-__u)/__a); | 
 | 3857 |                 __x = _STD::pow(1 - __a + __a * __e, 1 / __a); | 
 | 3858 |                 if (__x <= __e + __es) | 
 | 3859 |                     break; | 
 | 3860 |             } | 
 | 3861 |         } | 
 | 3862 |     } | 
 | 3863 |     return __x * __p.beta(); | 
| Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3864 | } | 
 | 3865 |  | 
 | 3866 | template <class _CharT, class _Traits, class _RT> | 
 | 3867 | basic_ostream<_CharT, _Traits>& | 
 | 3868 | operator<<(basic_ostream<_CharT, _Traits>& __os, | 
 | 3869 |            const gamma_distribution<_RT>& __x) | 
 | 3870 | { | 
 | 3871 |     __save_flags<_CharT, _Traits> _(__os); | 
 | 3872 |     __os.flags(ios_base::dec | ios_base::left); | 
 | 3873 |     _CharT __sp = __os.widen(' '); | 
 | 3874 |     __os.fill(__sp); | 
 | 3875 |     __os << __x.alpha() << __sp << __x.beta(); | 
 | 3876 |     return __os; | 
 | 3877 | } | 
 | 3878 |  | 
 | 3879 | template <class _CharT, class _Traits, class _RT> | 
 | 3880 | basic_istream<_CharT, _Traits>& | 
 | 3881 | operator>>(basic_istream<_CharT, _Traits>& __is, | 
 | 3882 |            gamma_distribution<_RT>& __x) | 
 | 3883 | { | 
 | 3884 |     typedef gamma_distribution<_RT> _Eng; | 
 | 3885 |     typedef typename _Eng::result_type result_type; | 
 | 3886 |     typedef typename _Eng::param_type param_type; | 
 | 3887 |     __save_flags<_CharT, _Traits> _(__is); | 
 | 3888 |     __is.flags(ios_base::dec | ios_base::skipws); | 
 | 3889 |     result_type __alpha; | 
 | 3890 |     result_type __beta; | 
 | 3891 |     __is >> __alpha >> __beta; | 
 | 3892 |     if (!__is.fail()) | 
 | 3893 |         __x.param(param_type(__alpha, __beta)); | 
 | 3894 |     return __is; | 
 | 3895 | } | 
| Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 3896 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3897 | _LIBCPP_END_NAMESPACE_STD | 
 | 3898 |  | 
 | 3899 | #endif  // _LIBCPP_RANDOM |