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