blob: 849dce4c1eb71396c8194f01c825db88c2dd7207 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RANDOM
12#define _LIBCPP_RANDOM
13
14/*
15 random synopsis
16
17#include <initializer_list>
18
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;
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;
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;
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_rand0 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;
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>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class 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};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
669 class geometric_distribution;
670
671template<class IntType = int>
672 class negative_binomial_distribution;
673
674template<class IntType = int>
675 class poisson_distribution;
676
677template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000678class exponential_distribution
679{
680public:
681 // types
682 typedef RealType result_type;
683
684 class param_type
685 {
686 public:
687 typedef exponential_distribution distribution_type;
688
689 explicit param_type(RealType lambda = 1.0);
690
691 RealType lambda() const;
692
693 friend bool operator==(const param_type& x, const param_type& y);
694 friend bool operator!=(const param_type& x, const param_type& y);
695 };
696
697 // constructors and reset functions
698 explicit exponential_distribution(RealType lambda = 1.0);
699 explicit exponential_distribution(const param_type& parm);
700 void reset();
701
702 // generating functions
703 template<class URNG> result_type operator()(URNG& g);
704 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
705
706 // property functions
707 RealType lambda() const;
708
709 param_type param() const;
710 void param(const param_type& parm);
711
712 result_type min() const;
713 result_type max() const;
714
715 friend bool operator==(const exponential_distribution& x,
716 const exponential_distribution& y);
717 friend bool operator!=(const exponential_distribution& x,
718 const exponential_distribution& y);
719
720 template <class charT, class traits>
721 friend
722 basic_ostream<charT, traits>&
723 operator<<(basic_ostream<charT, traits>& os,
724 const exponential_distribution& x);
725
726 template <class charT, class traits>
727 friend
728 basic_istream<charT, traits>&
729 operator>>(basic_istream<charT, traits>& is,
730 exponential_distribution& x);
731};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733template<class RealType = double>
734 class gamma_distribution;
735
736template<class RealType = double>
737 class weibull_distribution;
738
739template<class RealType = double>
740 class extreme_value_distribution;
741
742template<class RealType = double>
743 class normal_distribution;
744
745template<class RealType = double>
746 class lognormal_distribution;
747
748template<class RealType = double>
749 class chi_squared_distribution;
750
751template<class RealType = double>
752 class cauchy_distribution;
753
754template<class RealType = double>
755 class fisher_f_distribution;
756
757template<class RealType = double>
758 class student_t_distribution;
759
760template<class IntType = int>
761 class discrete_distribution;
762
763template<class RealType = double>
764 class piecewise_constant_distribution;
765
766template<class RealType = double>
767 class piecewise_linear_distribution;
768
769} // std
770*/
771
772#include <__config>
773#include <cstddef>
774#include <type_traits>
775#include <initializer_list>
776#include <cstdint>
777#include <limits>
778#include <algorithm>
779#include <vector>
780#include <string>
781#include <istream>
782#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000783#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784
785#pragma GCC system_header
786
787_LIBCPP_BEGIN_NAMESPACE_STD
788
789// linear_congruential_engine
790
791template <unsigned long long __a, unsigned long long __c,
792 unsigned long long __m, unsigned long long _M,
793 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
794struct __lce_ta;
795
796// 64
797
798template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
799struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
800{
801 typedef unsigned long long result_type;
802 static result_type next(result_type __x)
803 {
804 // Schrage's algorithm
805 const result_type __q = __m / __a;
806 const result_type __r = __m % __a;
807 const result_type __t0 = __a * (__x % __q);
808 const result_type __t1 = __r * (__x / __q);
809 __x = __t0 + (__t0 < __t1) * __m - __t1;
810 __x += __c - (__x >= __m - __c) * __m;
811 return __x;
812 }
813};
814
815template <unsigned long long __a, unsigned long long __m>
816struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
817{
818 typedef unsigned long long result_type;
819 static result_type next(result_type __x)
820 {
821 // Schrage's algorithm
822 const result_type __q = __m / __a;
823 const result_type __r = __m % __a;
824 const result_type __t0 = __a * (__x % __q);
825 const result_type __t1 = __r * (__x / __q);
826 __x = __t0 + (__t0 < __t1) * __m - __t1;
827 return __x;
828 }
829};
830
831template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
832struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
833{
834 typedef unsigned long long result_type;
835 static result_type next(result_type __x)
836 {
837 return (__a * __x + __c) % __m;
838 }
839};
840
841template <unsigned long long __a, unsigned long long __c>
842struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
843{
844 typedef unsigned long long result_type;
845 static result_type next(result_type __x)
846 {
847 return __a * __x + __c;
848 }
849};
850
851// 32
852
853template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
854struct __lce_ta<_A, _C, _M, unsigned(~0), true>
855{
856 typedef unsigned result_type;
857 static result_type next(result_type __x)
858 {
859 const result_type __a = static_cast<result_type>(_A);
860 const result_type __c = static_cast<result_type>(_C);
861 const result_type __m = static_cast<result_type>(_M);
862 // Schrage's algorithm
863 const result_type __q = __m / __a;
864 const result_type __r = __m % __a;
865 const result_type __t0 = __a * (__x % __q);
866 const result_type __t1 = __r * (__x / __q);
867 __x = __t0 + (__t0 < __t1) * __m - __t1;
868 __x += __c - (__x >= __m - __c) * __m;
869 return __x;
870 }
871};
872
873template <unsigned long long _A, unsigned long long _M>
874struct __lce_ta<_A, 0, _M, unsigned(~0), true>
875{
876 typedef unsigned result_type;
877 static result_type next(result_type __x)
878 {
879 const result_type __a = static_cast<result_type>(_A);
880 const result_type __m = static_cast<result_type>(_M);
881 // Schrage's algorithm
882 const result_type __q = __m / __a;
883 const result_type __r = __m % __a;
884 const result_type __t0 = __a * (__x % __q);
885 const result_type __t1 = __r * (__x / __q);
886 __x = __t0 + (__t0 < __t1) * __m - __t1;
887 return __x;
888 }
889};
890
891template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
892struct __lce_ta<_A, _C, _M, unsigned(~0), false>
893{
894 typedef unsigned result_type;
895 static result_type next(result_type __x)
896 {
897 const result_type __a = static_cast<result_type>(_A);
898 const result_type __c = static_cast<result_type>(_C);
899 const result_type __m = static_cast<result_type>(_M);
900 return (__a * __x + __c) % __m;
901 }
902};
903
904template <unsigned long long _A, unsigned long long _C>
905struct __lce_ta<_A, _C, 0, unsigned(~0), false>
906{
907 typedef unsigned result_type;
908 static result_type next(result_type __x)
909 {
910 const result_type __a = static_cast<result_type>(_A);
911 const result_type __c = static_cast<result_type>(_C);
912 return __a * __x + __c;
913 }
914};
915
916// 16
917
918template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
919struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
920{
921 typedef unsigned short result_type;
922 static result_type next(result_type __x)
923 {
924 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
925 }
926};
927
928template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
929class linear_congruential_engine;
930
931template <class _CharT, class _Traits,
932 class _U, _U _A, _U _C, _U _N>
933basic_ostream<_CharT, _Traits>&
934operator<<(basic_ostream<_CharT, _Traits>& __os,
935 const linear_congruential_engine<_U, _A, _C, _N>&);
936
937template <class _CharT, class _Traits,
938 class _U, _U _A, _U _C, _U _N>
939basic_istream<_CharT, _Traits>&
940operator>>(basic_istream<_CharT, _Traits>& __is,
941 linear_congruential_engine<_U, _A, _C, _N>& __x);
942
943template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
944class linear_congruential_engine
945{
946public:
947 // types
948 typedef _UIntType result_type;
949
950private:
951 result_type __x_;
952
953 static const result_type _M = result_type(~0);
954
955 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
956 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
957public:
958 static const result_type _Min = __c == 0u ? 1u: 0u;
959 static const result_type _Max = __m - 1u;
960 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
961
962 // engine characteristics
963 static const/*expr*/ result_type multiplier = __a;
964 static const/*expr*/ result_type increment = __c;
965 static const/*expr*/ result_type modulus = __m;
966 static const/*expr*/ result_type min() {return _Min;}
967 static const/*expr*/ result_type max() {return _Max;}
968 static const/*expr*/ result_type default_seed = 1u;
969
970 // constructors and seeding functions
971 explicit linear_congruential_engine(result_type __s = default_seed)
972 {seed(__s);}
973 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
974 {seed(__q);}
975 void seed(result_type __s = default_seed)
976 {seed(integral_constant<bool, __m == 0>(),
977 integral_constant<bool, __c == 0>(), __s);}
978 template<class _Sseq>
979 typename enable_if
980 <
981 !is_convertible<_Sseq, result_type>::value,
982 void
983 >::type
984 seed(_Sseq& __q)
985 {__seed(__q, integral_constant<unsigned,
986 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
987 : (__m-1) / 0x100000000ull)>());}
988
989 // generating functions
990 result_type operator()()
991 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
992 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
993
994 friend bool operator==(const linear_congruential_engine& __x,
995 const linear_congruential_engine& __y)
996 {return __x.__x_ == __y.__x_;}
997 friend bool operator!=(const linear_congruential_engine& __x,
998 const linear_congruential_engine& __y)
999 {return !(__x == __y);}
1000
1001private:
1002
1003 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1004 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1005 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1006 1 : __s % __m;}
1007 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1008
1009 template<class _Sseq>
1010 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1011 template<class _Sseq>
1012 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1013
1014 template <class _CharT, class _Traits,
1015 class _U, _U _A, _U _C, _U _N>
1016 friend
1017 basic_ostream<_CharT, _Traits>&
1018 operator<<(basic_ostream<_CharT, _Traits>& __os,
1019 const linear_congruential_engine<_U, _A, _C, _N>&);
1020
1021 template <class _CharT, class _Traits,
1022 class _U, _U _A, _U _C, _U _N>
1023 friend
1024 basic_istream<_CharT, _Traits>&
1025 operator>>(basic_istream<_CharT, _Traits>& __is,
1026 linear_congruential_engine<_U, _A, _C, _N>& __x);
1027};
1028
1029template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1030template<class _Sseq>
1031void
1032linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1033 integral_constant<unsigned, 1>)
1034{
1035 const unsigned __k = 1;
1036 uint32_t __ar[__k+3];
1037 __q.generate(__ar, __ar + __k + 3);
1038 result_type __s = static_cast<result_type>(__ar[3] % __m);
1039 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1040}
1041
1042template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1043template<class _Sseq>
1044void
1045linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1046 integral_constant<unsigned, 2>)
1047{
1048 const unsigned __k = 2;
1049 uint32_t __ar[__k+3];
1050 __q.generate(__ar, __ar + __k + 3);
1051 result_type __s = static_cast<result_type>((__ar[3] +
1052 (uint64_t)__ar[4] << 32) % __m);
1053 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1054}
1055
1056template <class _CharT, class _Traits>
1057class __save_flags
1058{
1059 typedef basic_ios<_CharT, _Traits> __stream_type;
1060 typedef typename __stream_type::fmtflags fmtflags;
1061
1062 __stream_type& __stream_;
1063 fmtflags __fmtflags_;
1064 _CharT __fill_;
1065
1066 __save_flags(const __save_flags&);
1067 __save_flags& operator=(const __save_flags&);
1068public:
1069 explicit __save_flags(__stream_type& __stream)
1070 : __stream_(__stream),
1071 __fmtflags_(__stream.flags()),
1072 __fill_(__stream.fill())
1073 {}
1074 ~__save_flags()
1075 {
1076 __stream_.flags(__fmtflags_);
1077 __stream_.fill(__fill_);
1078 }
1079};
1080
1081template <class _CharT, class _Traits,
1082 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1083inline
1084basic_ostream<_CharT, _Traits>&
1085operator<<(basic_ostream<_CharT, _Traits>& __os,
1086 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1087{
1088 __save_flags<_CharT, _Traits> _(__os);
1089 __os.flags(ios_base::dec | ios_base::left);
1090 __os.fill(__os.widen(' '));
1091 return __os << __x.__x_;
1092}
1093
1094template <class _CharT, class _Traits,
1095 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1096basic_istream<_CharT, _Traits>&
1097operator>>(basic_istream<_CharT, _Traits>& __is,
1098 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1099{
1100 __save_flags<_CharT, _Traits> _(__is);
1101 __is.flags(ios_base::dec | ios_base::skipws);
1102 _UIntType __t;
1103 __is >> __t;
1104 if (!__is.fail())
1105 __x.__x_ = __t;
1106 return __is;
1107}
1108
1109typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1110 minstd_rand0;
1111typedef minstd_rand0 default_random_engine;
1112typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1113 minstd_rand;
1114// mersenne_twister_engine
1115
1116template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1117 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1118 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1119class mersenne_twister_engine;
1120
1121template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1122 _UI _A, size_t _U, _UI _D, size_t _S,
1123 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1124bool
1125operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1126 _B, _T, _C, _L, _F>& __x,
1127 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1128 _B, _T, _C, _L, _F>& __y);
1129
1130template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1131 _UI _A, size_t _U, _UI _D, size_t _S,
1132 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1133bool
1134operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1135 _B, _T, _C, _L, _F>& __x,
1136 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1137 _B, _T, _C, _L, _F>& __y);
1138
1139template <class _CharT, class _Traits,
1140 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1141 _UI _A, size_t _U, _UI _D, size_t _S,
1142 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1143basic_ostream<_CharT, _Traits>&
1144operator<<(basic_ostream<_CharT, _Traits>& __os,
1145 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1146 _B, _T, _C, _L, _F>& __x);
1147
1148template <class _CharT, class _Traits,
1149 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1150 _UI _A, size_t _U, _UI _D, size_t _S,
1151 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1152basic_istream<_CharT, _Traits>&
1153operator>>(basic_istream<_CharT, _Traits>& __is,
1154 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1155 _B, _T, _C, _L, _F>& __x);
1156
1157template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1158 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1159 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1160class mersenne_twister_engine
1161{
1162public:
1163 // types
1164 typedef _UIntType result_type;
1165
1166private:
1167 result_type __x_[__n];
1168 size_t __i_;
1169
1170 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1171 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1172 static const result_type _Dt = numeric_limits<result_type>::digits;
1173 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1174 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1175 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1176 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1177 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1178 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1179 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1180public:
1181 static const result_type _Min = 0;
1182 static const result_type _Max = __w == _Dt ? result_type(~0) :
1183 (result_type(1) << __w) - result_type(1);
1184 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1185 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1186 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1187 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1188 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1189 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1190
1191 // engine characteristics
1192 static const/*expr*/ size_t word_size = __w;
1193 static const/*expr*/ size_t state_size = __n;
1194 static const/*expr*/ size_t shift_size = __m;
1195 static const/*expr*/ size_t mask_bits = __r;
1196 static const/*expr*/ result_type xor_mask = __a;
1197 static const/*expr*/ size_t tempering_u = __u;
1198 static const/*expr*/ result_type tempering_d = __d;
1199 static const/*expr*/ size_t tempering_s = __s;
1200 static const/*expr*/ result_type tempering_b = __b;
1201 static const/*expr*/ size_t tempering_t = __t;
1202 static const/*expr*/ result_type tempering_c = __c;
1203 static const/*expr*/ size_t tempering_l = __l;
1204 static const/*expr*/ result_type initialization_multiplier = __f;
1205 static const/*expr*/ result_type min() { return _Min; }
1206 static const/*expr*/ result_type max() { return _Max; }
1207 static const/*expr*/ result_type default_seed = 5489u;
1208
1209 // constructors and seeding functions
1210 explicit mersenne_twister_engine(result_type __sd = default_seed)
1211 {seed(__sd);}
1212 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1213 {seed(__q);}
1214 void seed(result_type __sd = default_seed);
1215 template<class _Sseq>
1216 typename enable_if
1217 <
1218 !is_convertible<_Sseq, result_type>::value,
1219 void
1220 >::type
1221 seed(_Sseq& __q)
1222 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1223
1224 // generating functions
1225 result_type operator()();
1226 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1227
1228 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1229 _UI _A, size_t _U, _UI _D, size_t _S,
1230 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1231 friend
1232 bool
1233 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1234 _B, _T, _C, _L, _F>& __x,
1235 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1236 _B, _T, _C, _L, _F>& __y);
1237
1238 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1239 _UI _A, size_t _U, _UI _D, size_t _S,
1240 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1241 friend
1242 bool
1243 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1244 _B, _T, _C, _L, _F>& __x,
1245 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1246 _B, _T, _C, _L, _F>& __y);
1247
1248 template <class _CharT, class _Traits,
1249 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1250 _UI _A, size_t _U, _UI _D, size_t _S,
1251 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1252 friend
1253 basic_ostream<_CharT, _Traits>&
1254 operator<<(basic_ostream<_CharT, _Traits>& __os,
1255 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1256 _B, _T, _C, _L, _F>& __x);
1257
1258 template <class _CharT, class _Traits,
1259 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1260 _UI _A, size_t _U, _UI _D, size_t _S,
1261 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1262 friend
1263 basic_istream<_CharT, _Traits>&
1264 operator>>(basic_istream<_CharT, _Traits>& __is,
1265 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1266 _B, _T, _C, _L, _F>& __x);
1267private:
1268
1269 template<class _Sseq>
1270 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1271 template<class _Sseq>
1272 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1273
1274 template <size_t __count>
1275 static
1276 typename enable_if
1277 <
1278 __count < __w,
1279 result_type
1280 >::type
1281 __lshift(result_type __x) {return (__x << __count) & _Max;}
1282
1283 template <size_t __count>
1284 static
1285 typename enable_if
1286 <
1287 (__count >= __w),
1288 result_type
1289 >::type
1290 __lshift(result_type __x) {return result_type(0);}
1291
1292 template <size_t __count>
1293 static
1294 typename enable_if
1295 <
1296 __count < _Dt,
1297 result_type
1298 >::type
1299 __rshift(result_type __x) {return __x >> __count;}
1300
1301 template <size_t __count>
1302 static
1303 typename enable_if
1304 <
1305 (__count >= _Dt),
1306 result_type
1307 >::type
1308 __rshift(result_type __x) {return result_type(0);}
1309};
1310
1311template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1312 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1313 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1314void
1315mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1316 __t, __c, __l, __f>::seed(result_type __sd)
1317{ // __w >= 2
1318 __x_[0] = __sd & _Max;
1319 for (size_t __i = 1; __i < __n; ++__i)
1320 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1321 __i_ = 0;
1322}
1323
1324template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1325 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1326 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1327template<class _Sseq>
1328void
1329mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1330 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1331{
1332 const unsigned __k = 1;
1333 uint32_t __ar[__n * __k];
1334 __q.generate(__ar, __ar + __n * __k);
1335 for (size_t __i = 0; __i < __n; ++__i)
1336 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1337 const result_type __mask = __r == _Dt ? result_type(~0) :
1338 (result_type(1) << __r) - result_type(1);
1339 __i_ = 0;
1340 if ((__x_[0] & ~__mask) == 0)
1341 {
1342 for (size_t __i = 1; __i < __n; ++__i)
1343 if (__x_[__i] != 0)
1344 return;
1345 __x_[0] = _Max;
1346 }
1347}
1348
1349template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1350 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1351 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1352template<class _Sseq>
1353void
1354mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1355 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1356{
1357 const unsigned __k = 2;
1358 uint32_t __ar[__n * __k];
1359 __q.generate(__ar, __ar + __n * __k);
1360 for (size_t __i = 0; __i < __n; ++__i)
1361 __x_[__i] = static_cast<result_type>(
1362 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1363 const result_type __mask = __r == _Dt ? result_type(~0) :
1364 (result_type(1) << __r) - result_type(1);
1365 __i_ = 0;
1366 if ((__x_[0] & ~__mask) == 0)
1367 {
1368 for (size_t __i = 1; __i < __n; ++__i)
1369 if (__x_[__i] != 0)
1370 return;
1371 __x_[0] = _Max;
1372 }
1373}
1374
1375template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1376 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1377 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1378_UIntType
1379mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1380 __t, __c, __l, __f>::operator()()
1381{
1382 const size_t __j = (__i_ + 1) % __n;
1383 const result_type __mask = __r == _Dt ? result_type(~0) :
1384 (result_type(1) << __r) - result_type(1);
1385 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1386 const size_t __k = (__i_ + __m) % __n;
1387 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1388 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1389 __i_ = __j;
1390 __z ^= __lshift<__s>(__z) & __b;
1391 __z ^= __lshift<__t>(__z) & __c;
1392 return __z ^ __rshift<__l>(__z);
1393}
1394
1395template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1396 _UI _A, size_t _U, _UI _D, size_t _S,
1397 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1398bool
1399operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1400 _B, _T, _C, _L, _F>& __x,
1401 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1402 _B, _T, _C, _L, _F>& __y)
1403{
1404 if (__x.__i_ == __y.__i_)
1405 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1406 if (__x.__i_ == 0 || __y.__i_ == 0)
1407 {
1408 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1409 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1410 __y.__x_ + __y.__i_))
1411 return false;
1412 if (__x.__i_ == 0)
1413 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1414 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1415 }
1416 if (__x.__i_ < __y.__i_)
1417 {
1418 size_t __j = _N - __y.__i_;
1419 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1420 __y.__x_ + __y.__i_))
1421 return false;
1422 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1423 __y.__x_))
1424 return false;
1425 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1426 __y.__x_ + (_N - (__x.__i_ + __j)));
1427 }
1428 size_t __j = _N - __x.__i_;
1429 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1430 __x.__x_ + __x.__i_))
1431 return false;
1432 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1433 __x.__x_))
1434 return false;
1435 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1436 __x.__x_ + (_N - (__y.__i_ + __j)));
1437}
1438
1439template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1440 _UI _A, size_t _U, _UI _D, size_t _S,
1441 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1442inline
1443bool
1444operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1445 _B, _T, _C, _L, _F>& __x,
1446 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1447 _B, _T, _C, _L, _F>& __y)
1448{
1449 return !(__x == __y);
1450}
1451
1452template <class _CharT, class _Traits,
1453 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1454 _UI _A, size_t _U, _UI _D, size_t _S,
1455 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1456basic_ostream<_CharT, _Traits>&
1457operator<<(basic_ostream<_CharT, _Traits>& __os,
1458 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1459 _B, _T, _C, _L, _F>& __x)
1460{
1461 __save_flags<_CharT, _Traits> _(__os);
1462 __os.flags(ios_base::dec | ios_base::left);
1463 _CharT __sp = __os.widen(' ');
1464 __os.fill(__sp);
1465 __os << __x.__x_[__x.__i_];
1466 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1467 __os << __sp << __x.__x_[__j];
1468 for (size_t __j = 0; __j < __x.__i_; ++__j)
1469 __os << __sp << __x.__x_[__j];
1470 return __os;
1471}
1472
1473template <class _CharT, class _Traits,
1474 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1475 _UI _A, size_t _U, _UI _D, size_t _S,
1476 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1477basic_istream<_CharT, _Traits>&
1478operator>>(basic_istream<_CharT, _Traits>& __is,
1479 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1480 _B, _T, _C, _L, _F>& __x)
1481{
1482 __save_flags<_CharT, _Traits> _(__is);
1483 __is.flags(ios_base::dec | ios_base::skipws);
1484 _UI __t[_N];
1485 for (size_t __i = 0; __i < _N; ++__i)
1486 __is >> __t[__i];
1487 if (!__is.fail())
1488 {
1489 for (size_t __i = 0; __i < _N; ++__i)
1490 __x.__x_[__i] = __t[__i];
1491 __x.__i_ = 0;
1492 }
1493 return __is;
1494}
1495
1496typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1497 0x9908b0df, 11, 0xffffffff,
1498 7, 0x9d2c5680,
1499 15, 0xefc60000,
1500 18, 1812433253> mt19937;
1501typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1502 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1503 17, 0x71d67fffeda60000ULL,
1504 37, 0xfff7eee000000000ULL,
1505 43, 6364136223846793005ULL> mt19937_64;
1506
1507// subtract_with_carry_engine
1508
1509template<class _UIntType, size_t __w, size_t __s, size_t __r>
1510class subtract_with_carry_engine;
1511
1512template<class _UI, size_t _W, size_t _S, size_t _R>
1513bool
1514operator==(
1515 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1516 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1517
1518template<class _UI, size_t _W, size_t _S, size_t _R>
1519bool
1520operator!=(
1521 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1522 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1523
1524template <class _CharT, class _Traits,
1525 class _UI, size_t _W, size_t _S, size_t _R>
1526basic_ostream<_CharT, _Traits>&
1527operator<<(basic_ostream<_CharT, _Traits>& __os,
1528 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1529
1530template <class _CharT, class _Traits,
1531 class _UI, size_t _W, size_t _S, size_t _R>
1532basic_istream<_CharT, _Traits>&
1533operator>>(basic_istream<_CharT, _Traits>& __is,
1534 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1535
1536template<class _UIntType, size_t __w, size_t __s, size_t __r>
1537class subtract_with_carry_engine
1538{
1539public:
1540 // types
1541 typedef _UIntType result_type;
1542
1543private:
1544 result_type __x_[__r];
1545 result_type __c_;
1546 size_t __i_;
1547
1548 static const result_type _Dt = numeric_limits<result_type>::digits;
1549 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1550 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1551 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1552 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1553public:
1554 static const result_type _Min = 0;
1555 static const result_type _Max = __w == _Dt ? result_type(~0) :
1556 (result_type(1) << __w) - result_type(1);
1557 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1558
1559 // engine characteristics
1560 static const/*expr*/ size_t word_size = __w;
1561 static const/*expr*/ size_t short_lag = __s;
1562 static const/*expr*/ size_t long_lag = __r;
1563 static const/*expr*/ result_type min() { return _Min; }
1564 static const/*expr*/ result_type max() { return _Max; }
1565 static const/*expr*/ result_type default_seed = 19780503u;
1566
1567 // constructors and seeding functions
1568 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1569 {seed(__sd);}
1570 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1571 {seed(__q);}
1572 void seed(result_type __sd = default_seed)
1573 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1574 template<class _Sseq>
1575 typename enable_if
1576 <
1577 !is_convertible<_Sseq, result_type>::value,
1578 void
1579 >::type
1580 seed(_Sseq& __q)
1581 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1582
1583 // generating functions
1584 result_type operator()();
1585 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1586
1587 template<class _UI, size_t _W, size_t _S, size_t _R>
1588 friend
1589 bool
1590 operator==(
1591 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1592 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1593
1594 template<class _UI, size_t _W, size_t _S, size_t _R>
1595 friend
1596 bool
1597 operator!=(
1598 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1599 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1600
1601 template <class _CharT, class _Traits,
1602 class _UI, size_t _W, size_t _S, size_t _R>
1603 friend
1604 basic_ostream<_CharT, _Traits>&
1605 operator<<(basic_ostream<_CharT, _Traits>& __os,
1606 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1607
1608 template <class _CharT, class _Traits,
1609 class _UI, size_t _W, size_t _S, size_t _R>
1610 friend
1611 basic_istream<_CharT, _Traits>&
1612 operator>>(basic_istream<_CharT, _Traits>& __is,
1613 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1614
1615private:
1616
1617 void seed(result_type __sd, integral_constant<unsigned, 1>);
1618 void seed(result_type __sd, integral_constant<unsigned, 2>);
1619 template<class _Sseq>
1620 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1621 template<class _Sseq>
1622 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1623};
1624
1625template<class _UIntType, size_t __w, size_t __s, size_t __r>
1626void
1627subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1628 integral_constant<unsigned, 1>)
1629{
1630 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1631 __e(__sd == 0u ? default_seed : __sd);
1632 for (size_t __i = 0; __i < __r; ++__i)
1633 __x_[__i] = static_cast<result_type>(__e() & _Max);
1634 __c_ = __x_[__r-1] == 0;
1635 __i_ = 0;
1636}
1637
1638template<class _UIntType, size_t __w, size_t __s, size_t __r>
1639void
1640subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1641 integral_constant<unsigned, 2>)
1642{
1643 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1644 __e(__sd == 0u ? default_seed : __sd);
1645 for (size_t __i = 0; __i < __r; ++__i)
1646 __x_[__i] = static_cast<result_type>(
1647 (__e() + ((uint64_t)__e() << 32)) & _Max);
1648 __c_ = __x_[__r-1] == 0;
1649 __i_ = 0;
1650}
1651
1652template<class _UIntType, size_t __w, size_t __s, size_t __r>
1653template<class _Sseq>
1654void
1655subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1656 integral_constant<unsigned, 1>)
1657{
1658 const unsigned __k = 1;
1659 uint32_t __ar[__r * __k];
1660 __q.generate(__ar, __ar + __r * __k);
1661 for (size_t __i = 0; __i < __r; ++__i)
1662 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1663 __c_ = __x_[__r-1] == 0;
1664 __i_ = 0;
1665}
1666
1667template<class _UIntType, size_t __w, size_t __s, size_t __r>
1668template<class _Sseq>
1669void
1670subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1671 integral_constant<unsigned, 2>)
1672{
1673 const unsigned __k = 2;
1674 uint32_t __ar[__r * __k];
1675 __q.generate(__ar, __ar + __r * __k);
1676 for (size_t __i = 0; __i < __r; ++__i)
1677 __x_[__i] = static_cast<result_type>(
1678 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1679 __c_ = __x_[__r-1] == 0;
1680 __i_ = 0;
1681}
1682
1683template<class _UIntType, size_t __w, size_t __s, size_t __r>
1684_UIntType
1685subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
1686{
1687 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
1688 result_type& __xr = __x_[__i_];
1689 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
1690 __xr = (__xs - __xr - __c_) & _Max;
1691 __c_ = __new_c;
1692 __i_ = (__i_ + 1) % __r;
1693 return __xr;
1694}
1695
1696template<class _UI, size_t _W, size_t _S, size_t _R>
1697bool
1698operator==(
1699 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1700 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1701{
1702 if (__x.__c_ != __y.__c_)
1703 return false;
1704 if (__x.__i_ == __y.__i_)
1705 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
1706 if (__x.__i_ == 0 || __y.__i_ == 0)
1707 {
1708 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
1709 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1710 __y.__x_ + __y.__i_))
1711 return false;
1712 if (__x.__i_ == 0)
1713 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
1714 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
1715 }
1716 if (__x.__i_ < __y.__i_)
1717 {
1718 size_t __j = _R - __y.__i_;
1719 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1720 __y.__x_ + __y.__i_))
1721 return false;
1722 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
1723 __y.__x_))
1724 return false;
1725 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1726 __y.__x_ + (_R - (__x.__i_ + __j)));
1727 }
1728 size_t __j = _R - __x.__i_;
1729 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1730 __x.__x_ + __x.__i_))
1731 return false;
1732 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
1733 __x.__x_))
1734 return false;
1735 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1736 __x.__x_ + (_R - (__y.__i_ + __j)));
1737}
1738
1739template<class _UI, size_t _W, size_t _S, size_t _R>
1740inline
1741bool
1742operator!=(
1743 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1744 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1745{
1746 return !(__x == __y);
1747}
1748
1749template <class _CharT, class _Traits,
1750 class _UI, size_t _W, size_t _S, size_t _R>
1751basic_ostream<_CharT, _Traits>&
1752operator<<(basic_ostream<_CharT, _Traits>& __os,
1753 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1754{
1755 __save_flags<_CharT, _Traits> _(__os);
1756 __os.flags(ios_base::dec | ios_base::left);
1757 _CharT __sp = __os.widen(' ');
1758 __os.fill(__sp);
1759 __os << __x.__x_[__x.__i_];
1760 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
1761 __os << __sp << __x.__x_[__j];
1762 for (size_t __j = 0; __j < __x.__i_; ++__j)
1763 __os << __sp << __x.__x_[__j];
1764 __os << __sp << __x.__c_;
1765 return __os;
1766}
1767
1768template <class _CharT, class _Traits,
1769 class _UI, size_t _W, size_t _S, size_t _R>
1770basic_istream<_CharT, _Traits>&
1771operator>>(basic_istream<_CharT, _Traits>& __is,
1772 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1773{
1774 __save_flags<_CharT, _Traits> _(__is);
1775 __is.flags(ios_base::dec | ios_base::skipws);
1776 _UI __t[_R+1];
1777 for (size_t __i = 0; __i < _R+1; ++__i)
1778 __is >> __t[__i];
1779 if (!__is.fail())
1780 {
1781 for (size_t __i = 0; __i < _R; ++__i)
1782 __x.__x_[__i] = __t[__i];
1783 __x.__c_ = __t[_R];
1784 __x.__i_ = 0;
1785 }
1786 return __is;
1787}
1788
1789typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
1790typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
1791
1792// discard_block_engine
1793
1794template<class _Engine, size_t __p, size_t __r>
1795class discard_block_engine
1796{
1797 _Engine __e_;
1798 int __n_;
1799
1800 static_assert( 0 < __r, "discard_block_engine invalid parameters");
1801 static_assert(__r <= __p, "discard_block_engine invalid parameters");
1802public:
1803 // types
1804 typedef typename _Engine::result_type result_type;
1805
1806 // engine characteristics
1807 static const/*expr*/ size_t block_size = __p;
1808 static const/*expr*/ size_t used_block = __r;
1809
1810 // Temporary work around for lack of constexpr
1811 static const result_type _Min = _Engine::_Min;
1812 static const result_type _Max = _Engine::_Max;
1813
1814 static const/*expr*/ result_type min() { return _Engine::min(); }
1815 static const/*expr*/ result_type max() { return _Engine::max(); }
1816
1817 // constructors and seeding functions
1818 discard_block_engine() : __n_(0) {}
1819// explicit discard_block_engine(const _Engine& __e);
1820// explicit discard_block_engine(_Engine&& __e);
1821 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
1822 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
1823 : __e_(__q), __n_(0) {}
1824 void seed() {__e_.seed(); __n_ = 0;}
1825 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
1826 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
1827
1828 // generating functions
1829 result_type operator()();
1830 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1831
1832 // property functions
1833 const _Engine& base() const {return __e_;}
1834
1835 template<class _Eng, size_t _P, size_t _R>
1836 friend
1837 bool
1838 operator==(
1839 const discard_block_engine<_Eng, _P, _R>& __x,
1840 const discard_block_engine<_Eng, _P, _R>& __y);
1841
1842 template<class _Eng, size_t _P, size_t _R>
1843 friend
1844 bool
1845 operator!=(
1846 const discard_block_engine<_Eng, _P, _R>& __x,
1847 const discard_block_engine<_Eng, _P, _R>& __y);
1848
1849 template <class _CharT, class _Traits,
1850 class _Eng, size_t _P, size_t _R>
1851 friend
1852 basic_ostream<_CharT, _Traits>&
1853 operator<<(basic_ostream<_CharT, _Traits>& __os,
1854 const discard_block_engine<_Eng, _P, _R>& __x);
1855
1856 template <class _CharT, class _Traits,
1857 class _Eng, size_t _P, size_t _R>
1858 friend
1859 basic_istream<_CharT, _Traits>&
1860 operator>>(basic_istream<_CharT, _Traits>& __is,
1861 discard_block_engine<_Eng, _P, _R>& __x);
1862};
1863
1864template<class _Engine, size_t __p, size_t __r>
1865typename discard_block_engine<_Engine, __p, __r>::result_type
1866discard_block_engine<_Engine, __p, __r>::operator()()
1867{
1868 if (__n_ >= __r)
1869 {
1870 __e_.discard(__p - __r);
1871 __n_ = 0;
1872 }
1873 ++__n_;
1874 return __e_();
1875}
1876
1877template<class _Eng, size_t _P, size_t _R>
1878inline
1879bool
1880operator==(const discard_block_engine<_Eng, _P, _R>& __x,
1881 const discard_block_engine<_Eng, _P, _R>& __y)
1882{
1883 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
1884}
1885
1886template<class _Eng, size_t _P, size_t _R>
1887inline
1888bool
1889operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
1890 const discard_block_engine<_Eng, _P, _R>& __y)
1891{
1892 return !(__x == __y);
1893}
1894
1895template <class _CharT, class _Traits,
1896 class _Eng, size_t _P, size_t _R>
1897basic_ostream<_CharT, _Traits>&
1898operator<<(basic_ostream<_CharT, _Traits>& __os,
1899 const discard_block_engine<_Eng, _P, _R>& __x)
1900{
1901 __save_flags<_CharT, _Traits> _(__os);
1902 __os.flags(ios_base::dec | ios_base::left);
1903 _CharT __sp = __os.widen(' ');
1904 __os.fill(__sp);
1905 return __os << __x.__e_ << __sp << __x.__n_;
1906}
1907
1908template <class _CharT, class _Traits,
1909 class _Eng, size_t _P, size_t _R>
1910basic_istream<_CharT, _Traits>&
1911operator>>(basic_istream<_CharT, _Traits>& __is,
1912 discard_block_engine<_Eng, _P, _R>& __x)
1913{
1914 __save_flags<_CharT, _Traits> _(__is);
1915 __is.flags(ios_base::dec | ios_base::skipws);
1916 _Eng __e;
1917 int __n;
1918 __is >> __e >> __n;
1919 if (!__is.fail())
1920 {
1921 __x.__e_ = __e;
1922 __x.__n_ = __n;
1923 }
1924 return __is;
1925}
1926
1927typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1928typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1929
1930// independent_bits_engine
1931
1932template <unsigned long long _X, size_t _R>
1933struct __log2_imp
1934{
1935 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
1936 : __log2_imp<_X, _R - 1>::value;
1937};
1938
1939template <unsigned long long _X>
1940struct __log2_imp<_X, 0>
1941{
1942 static const size_t value = 0;
1943};
1944
1945template <size_t _R>
1946struct __log2_imp<0, _R>
1947{
1948 static const size_t value = _R + 1;
1949};
1950
1951template <class _UI, _UI _X>
1952struct __log2
1953{
1954 static const size_t value = __log2_imp<_X,
1955 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
1956};
1957
1958template<class _Engine, size_t __w, class _UIntType>
1959class independent_bits_engine
1960{
1961 template <class _UI, _UI _R0, size_t _W, size_t _M>
1962 class __get_n
1963 {
1964 static const size_t _Dt = numeric_limits<_UI>::digits;
1965 static const size_t _N = _W / _M + (_W % _M != 0);
1966 static const size_t _W0 = _W / _N;
1967 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
1968 public:
1969 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
1970 };
1971public:
1972 // types
1973 typedef _UIntType result_type;
1974
1975private:
1976 _Engine __e_;
1977
1978 static const result_type _Dt = numeric_limits<result_type>::digits;
1979 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
1980 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
1981
1982 typedef typename _Engine::result_type _Engine_result_type;
1983 typedef typename conditional
1984 <
1985 sizeof(_Engine_result_type) <= sizeof(result_type),
1986 result_type,
1987 _Engine_result_type
1988 >::type _Working_result_type;
1989 // Temporary work around for lack of constexpr
1990 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
1991 + _Working_result_type(1);
1992 static const size_t __m = __log2<_Working_result_type, _R>::value;
1993 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
1994 static const size_t __w0 = __w / __n;
1995 static const size_t __n0 = __n - __w % __n;
1996 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
1997 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
1998 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
1999 (_R >> __w0) << __w0;
2000 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2001 (_R >> (__w0+1)) << (__w0+1);
2002 static const _Engine_result_type __mask0 = __w0 > 0 ?
2003 _Engine_result_type(~0) >> (_EDt - __w0) :
2004 _Engine_result_type(0);
2005 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2006 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2007 _Engine_result_type(~0);
2008public:
2009 static const result_type _Min = 0;
2010 static const result_type _Max = __w == _Dt ? result_type(~0) :
2011 (result_type(1) << __w) - result_type(1);
2012 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2013
2014 // engine characteristics
2015 static const/*expr*/ result_type min() { return _Min; }
2016 static const/*expr*/ result_type max() { return _Max; }
2017
2018 // constructors and seeding functions
2019 independent_bits_engine() {}
2020// explicit independent_bits_engine(const _Engine& __e);
2021// explicit independent_bits_engine(_Engine&& __e);
2022 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2023 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2024 : __e_(__q) {}
2025 void seed() {__e_.seed();}
2026 void seed(result_type __sd) {__e_.seed(__sd);}
2027 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2028
2029 // generating functions
2030 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2031 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2032
2033 // property functions
2034 const _Engine& base() const {return __e_;}
2035
2036 template<class _Eng, size_t _W, class _UI>
2037 friend
2038 bool
2039 operator==(
2040 const independent_bits_engine<_Eng, _W, _UI>& __x,
2041 const independent_bits_engine<_Eng, _W, _UI>& __y);
2042
2043 template<class _Eng, size_t _W, class _UI>
2044 friend
2045 bool
2046 operator!=(
2047 const independent_bits_engine<_Eng, _W, _UI>& __x,
2048 const independent_bits_engine<_Eng, _W, _UI>& __y);
2049
2050 template <class _CharT, class _Traits,
2051 class _Eng, size_t _W, class _UI>
2052 friend
2053 basic_ostream<_CharT, _Traits>&
2054 operator<<(basic_ostream<_CharT, _Traits>& __os,
2055 const independent_bits_engine<_Eng, _W, _UI>& __x);
2056
2057 template <class _CharT, class _Traits,
2058 class _Eng, size_t _W, class _UI>
2059 friend
2060 basic_istream<_CharT, _Traits>&
2061 operator>>(basic_istream<_CharT, _Traits>& __is,
2062 independent_bits_engine<_Eng, _W, _UI>& __x);
2063
2064private:
2065 result_type __eval(false_type);
2066 result_type __eval(true_type);
2067
2068 template <size_t __count>
2069 static
2070 typename enable_if
2071 <
2072 __count < _Dt,
2073 result_type
2074 >::type
2075 __lshift(result_type __x) {return __x << __count;}
2076
2077 template <size_t __count>
2078 static
2079 typename enable_if
2080 <
2081 (__count >= _Dt),
2082 result_type
2083 >::type
2084 __lshift(result_type __x) {return result_type(0);}
2085};
2086
2087template<class _Engine, size_t __w, class _UIntType>
2088inline
2089_UIntType
2090independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2091{
2092 return static_cast<result_type>(__e_() & __mask0);
2093}
2094
2095template<class _Engine, size_t __w, class _UIntType>
2096_UIntType
2097independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2098{
2099 result_type _S = 0;
2100 for (size_t __k = 0; __k < __n0; ++__k)
2101 {
2102 _Engine_result_type __u;
2103 do
2104 {
2105 __u = __e_() - _Engine::min();
2106 } while (__u >= __y0);
2107 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2108 }
2109 for (size_t __k = __n0; __k < __n; ++__k)
2110 {
2111 _Engine_result_type __u;
2112 do
2113 {
2114 __u = __e_() - _Engine::min();
2115 } while (__u >= __y1);
2116 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2117 }
2118 return _S;
2119}
2120
2121template<class _Eng, size_t _W, class _UI>
2122inline
2123bool
2124operator==(
2125 const independent_bits_engine<_Eng, _W, _UI>& __x,
2126 const independent_bits_engine<_Eng, _W, _UI>& __y)
2127{
2128 return __x.base() == __y.base();
2129}
2130
2131template<class _Eng, size_t _W, class _UI>
2132inline
2133bool
2134operator!=(
2135 const independent_bits_engine<_Eng, _W, _UI>& __x,
2136 const independent_bits_engine<_Eng, _W, _UI>& __y)
2137{
2138 return !(__x == __y);
2139}
2140
2141template <class _CharT, class _Traits,
2142 class _Eng, size_t _W, class _UI>
2143basic_ostream<_CharT, _Traits>&
2144operator<<(basic_ostream<_CharT, _Traits>& __os,
2145 const independent_bits_engine<_Eng, _W, _UI>& __x)
2146{
2147 return __os << __x.base();
2148}
2149
2150template <class _CharT, class _Traits,
2151 class _Eng, size_t _W, class _UI>
2152basic_istream<_CharT, _Traits>&
2153operator>>(basic_istream<_CharT, _Traits>& __is,
2154 independent_bits_engine<_Eng, _W, _UI>& __x)
2155{
2156 _Eng __e;
2157 __is >> __e;
2158 if (!__is.fail())
2159 __x.__e_ = __e;
2160 return __is;
2161}
2162
2163// shuffle_order_engine
2164
2165template <uint64_t _Xp, uint64_t _Yp>
2166struct __ugcd
2167{
2168 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2169};
2170
2171template <uint64_t _Xp>
2172struct __ugcd<_Xp, 0>
2173{
2174 static const uint64_t value = _Xp;
2175};
2176
2177template <uint64_t _N, uint64_t _D>
2178class __uratio
2179{
2180 static_assert(_D != 0, "__uratio divide by 0");
2181 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2182public:
2183 static const uint64_t num = _N / __gcd;
2184 static const uint64_t den = _D / __gcd;
2185
2186 typedef __uratio<num, den> type;
2187};
2188
2189template<class _Engine, size_t __k>
2190class shuffle_order_engine
2191{
2192 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2193public:
2194 // types
2195 typedef typename _Engine::result_type result_type;
2196
2197private:
2198 _Engine __e_;
2199 result_type _V_[__k];
2200 result_type _Y_;
2201
2202public:
2203 // engine characteristics
2204 static const/*expr*/ size_t table_size = __k;
2205
2206 static const result_type _Min = _Engine::_Min;
2207 static const result_type _Max = _Engine::_Max;
2208 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2209 static const/*expr*/ result_type min() { return _Min; }
2210 static const/*expr*/ result_type max() { return _Max; }
2211
2212 static const unsigned long long _R = _Max - _Min + 1ull;
2213
2214 // constructors and seeding functions
2215 shuffle_order_engine() {__init();}
2216// explicit shuffle_order_engine(const _Engine& __e);
2217// explicit shuffle_order_engine(_Engine&& e);
2218 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2219 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2220 : __e_(__q) {__init();}
2221 void seed() {__e_.seed(); __init();}
2222 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2223 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2224
2225 // generating functions
2226 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2227 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2228
2229 // property functions
2230 const _Engine& base() const {return __e_;}
2231
2232private:
2233 template<class _Eng, size_t _K>
2234 friend
2235 bool
2236 operator==(
2237 const shuffle_order_engine<_Eng, _K>& __x,
2238 const shuffle_order_engine<_Eng, _K>& __y);
2239
2240 template<class _Eng, size_t _K>
2241 friend
2242 bool
2243 operator!=(
2244 const shuffle_order_engine<_Eng, _K>& __x,
2245 const shuffle_order_engine<_Eng, _K>& __y);
2246
2247 template <class _CharT, class _Traits,
2248 class _Eng, size_t _K>
2249 friend
2250 basic_ostream<_CharT, _Traits>&
2251 operator<<(basic_ostream<_CharT, _Traits>& __os,
2252 const shuffle_order_engine<_Eng, _K>& __x);
2253
2254 template <class _CharT, class _Traits,
2255 class _Eng, size_t _K>
2256 friend
2257 basic_istream<_CharT, _Traits>&
2258 operator>>(basic_istream<_CharT, _Traits>& __is,
2259 shuffle_order_engine<_Eng, _K>& __x);
2260
2261 void __init()
2262 {
2263 for (size_t __i = 0; __i < __k; ++__i)
2264 _V_[__i] = __e_();
2265 _Y_ = __e_();
2266 }
2267
2268 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2269 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2270
2271 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2272 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2273
2274 template <uint64_t _N, uint64_t _D>
2275 typename enable_if
2276 <
2277 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2278 result_type
2279 >::type
2280 __eval(__uratio<_N, _D>)
2281 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2282
2283 template <uint64_t _N, uint64_t _D>
2284 typename enable_if
2285 <
2286 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2287 result_type
2288 >::type
2289 __eval(__uratio<_N, _D>)
2290 {
2291 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2292 / __uratio<_N, _D>::den);
2293 _Y_ = _V_[__j];
2294 _V_[__j] = __e_();
2295 return _Y_;
2296 }
2297
2298 template <uint64_t __n, uint64_t __d>
2299 result_type __evalf()
2300 {
2301 const double _F = __d == 0 ?
2302 __n / (2. * 0x8000000000000000ull) :
2303 __n / (double)__d;
2304 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2305 _Y_ = _V_[__j];
2306 _V_[__j] = __e_();
2307 return _Y_;
2308 }
2309};
2310
2311template<class _Eng, size_t _K>
2312bool
2313operator==(
2314 const shuffle_order_engine<_Eng, _K>& __x,
2315 const shuffle_order_engine<_Eng, _K>& __y)
2316{
2317 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2318 __x.__e_ == __y.__e_;
2319}
2320
2321template<class _Eng, size_t _K>
2322inline
2323bool
2324operator!=(
2325 const shuffle_order_engine<_Eng, _K>& __x,
2326 const shuffle_order_engine<_Eng, _K>& __y)
2327{
2328 return !(__x == __y);
2329}
2330
2331template <class _CharT, class _Traits,
2332 class _Eng, size_t _K>
2333basic_ostream<_CharT, _Traits>&
2334operator<<(basic_ostream<_CharT, _Traits>& __os,
2335 const shuffle_order_engine<_Eng, _K>& __x)
2336{
2337 __save_flags<_CharT, _Traits> _(__os);
2338 __os.flags(ios_base::dec | ios_base::left);
2339 _CharT __sp = __os.widen(' ');
2340 __os.fill(__sp);
2341 __os << __x.__e_ << __sp << __x._V_[0];
2342 for (size_t __i = 1; __i < _K; ++__i)
2343 __os << __sp << __x._V_[__i];
2344 return __os << __sp << __x._Y_;
2345}
2346
2347template <class _CharT, class _Traits,
2348 class _Eng, size_t _K>
2349basic_istream<_CharT, _Traits>&
2350operator>>(basic_istream<_CharT, _Traits>& __is,
2351 shuffle_order_engine<_Eng, _K>& __x)
2352{
2353 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2354 __save_flags<_CharT, _Traits> _(__is);
2355 __is.flags(ios_base::dec | ios_base::skipws);
2356 _Eng __e;
2357 result_type _V[_K+1];
2358 __is >> __e;
2359 for (size_t __i = 0; __i < _K+1; ++__i)
2360 __is >> _V[__i];
2361 if (!__is.fail())
2362 {
2363 __x.__e_ = __e;
2364 for (size_t __i = 0; __i < _K; ++__i)
2365 __x._V_[__i] = _V[__i];
2366 __x._Y_ = _V[_K];
2367 }
2368 return __is;
2369}
2370
2371typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2372
2373// random_device
2374
2375class random_device
2376{
2377 int __f_;
2378public:
2379 // types
2380 typedef unsigned result_type;
2381
2382 // generator characteristics
2383 static const result_type _Min = 0;
2384 static const result_type _Max = 0xFFFFFFFFu;
2385
2386 static const/*expr*/ result_type min() { return _Min;}
2387 static const/*expr*/ result_type max() { return _Max;}
2388
2389 // constructors
2390 explicit random_device(const string& __token = "/dev/urandom");
2391 ~random_device();
2392
2393 // generating functions
2394 result_type operator()();
2395
2396 // property functions
2397 double entropy() const;
2398
2399private:
2400 // no copy functions
2401 random_device(const random_device&); // = delete;
2402 random_device& operator=(const random_device&); // = delete;
2403};
2404
2405// seed_seq
2406
2407class seed_seq
2408{
2409public:
2410 // types
2411 typedef uint32_t result_type;
2412
2413private:
2414 vector<result_type> __v_;
2415
2416 template<class _InputIterator>
2417 void init(_InputIterator __first, _InputIterator __last);
2418public:
2419 // constructors
2420 seed_seq() {}
2421 template<class _Tp>
2422 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2423
2424 template<class _InputIterator>
2425 seed_seq(_InputIterator __first, _InputIterator __last)
2426 {init(__first, __last);}
2427
2428 // generating functions
2429 template<class _RandomAccessIterator>
2430 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2431
2432 // property functions
2433 size_t size() const {return __v_.size();}
2434 template<class _OutputIterator>
2435 void param(_OutputIterator __dest) const
2436 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2437
2438private:
2439 // no copy functions
2440 seed_seq(const seed_seq&); // = delete;
2441 void operator=(const seed_seq&); // = delete;
2442
2443 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2444};
2445
2446template<class _InputIterator>
2447void
2448seed_seq::init(_InputIterator __first, _InputIterator __last)
2449{
2450 for (_InputIterator __s = __first; __s != __last; ++__s)
2451 __v_.push_back(*__s & 0xFFFFFFFF);
2452}
2453
2454template<class _RandomAccessIterator>
2455void
2456seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2457{
2458 if (__first != __last)
2459 {
2460 _STD::fill(__first, __last, 0x8b8b8b8b);
2461 const size_t __n = static_cast<size_t>(__last - __first);
2462 const size_t __s = __v_.size();
2463 const size_t __t = (__n >= 623) ? 11
2464 : (__n >= 68) ? 7
2465 : (__n >= 39) ? 5
2466 : (__n >= 7) ? 3
2467 : (__n - 1) / 2;
2468 const size_t __p = (__n - __t) / 2;
2469 const size_t __q = __p + __t;
2470 const size_t __m = _STD::max(__s + 1, __n);
2471 // __k = 0;
2472 {
2473 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2474 ^ __first[__n - 1]);
2475 __first[__p] += __r;
2476 __r += __s;
2477 __first[__q] += __r;
2478 __first[0] = __r;
2479 }
2480 for (size_t __k = 1; __k <= __s; ++__k)
2481 {
2482 const size_t __kmodn = __k % __n;
2483 const size_t __kpmodn = (__k + __p) % __n;
2484 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2485 ^ __first[(__k - 1) % __n]);
2486 __first[__kpmodn] += __r;
2487 __r += __kmodn + __v_[__k-1];
2488 __first[(__k + __q) % __n] += __r;
2489 __first[__kmodn] = __r;
2490 }
2491 for (size_t __k = __s + 1; __k < __m; ++__k)
2492 {
2493 const size_t __kmodn = __k % __n;
2494 const size_t __kpmodn = (__k + __p) % __n;
2495 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2496 ^ __first[(__k - 1) % __n]);
2497 __first[__kpmodn] += __r;
2498 __r += __kmodn;
2499 __first[(__k + __q) % __n] += __r;
2500 __first[__kmodn] = __r;
2501 }
2502 for (size_t __k = __m; __k < __m + __n; ++__k)
2503 {
2504 const size_t __kmodn = __k % __n;
2505 const size_t __kpmodn = (__k + __p) % __n;
2506 result_type __r = 1566083941 * _T(__first[__kmodn] +
2507 __first[__kpmodn] +
2508 __first[(__k - 1) % __n]);
2509 __first[__kpmodn] ^= __r;
2510 __r -= __kmodn;
2511 __first[(__k + __q) % __n] ^= __r;
2512 __first[__kmodn] = __r;
2513 }
2514 }
2515}
2516
Howard Hinnant30a840f2010-05-12 17:08:57 +00002517// generate_canonical
2518
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519template<class _RealType, size_t __bits, class _URNG>
2520_RealType
2521generate_canonical(_URNG& __g)
2522{
2523 const size_t _Dt = numeric_limits<_RealType>::digits;
2524 const size_t __b = _Dt < __bits ? _Dt : __bits;
2525 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2526 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2527 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2528 _RealType __base = _R;
2529 _RealType _S = __g() - _URNG::_Min;
2530 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2531 _S += (__g() - _URNG::_Min) * __base;
2532 return _S / __base;
2533}
2534
2535// __independent_bits_engine
2536
2537template<class _Engine, class _UIntType>
2538class __independent_bits_engine
2539{
2540public:
2541 // types
2542 typedef _UIntType result_type;
2543
2544private:
2545 typedef typename _Engine::result_type _Engine_result_type;
2546 typedef typename conditional
2547 <
2548 sizeof(_Engine_result_type) <= sizeof(result_type),
2549 result_type,
2550 _Engine_result_type
2551 >::type _Working_result_type;
2552
2553 _Engine& __e_;
2554 size_t __w_;
2555 size_t __w0_;
2556 size_t __n_;
2557 size_t __n0_;
2558 _Working_result_type __y0_;
2559 _Working_result_type __y1_;
2560 _Engine_result_type __mask0_;
2561 _Engine_result_type __mask1_;
2562
2563 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2564 + _Working_result_type(1);
2565 static const size_t __m = __log2<_Working_result_type, _R>::value;
2566 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2567 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2568
2569public:
2570 // constructors and seeding functions
2571 __independent_bits_engine(_Engine& __e, size_t __w);
2572
2573 // generating functions
2574 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2575
2576private:
2577 result_type __eval(false_type);
2578 result_type __eval(true_type);
2579};
2580
2581template<class _Engine, class _UIntType>
2582__independent_bits_engine<_Engine, _UIntType>
2583 ::__independent_bits_engine(_Engine& __e, size_t __w)
2584 : __e_(__e),
2585 __w_(__w)
2586{
2587 __n_ = __w_ / __m + (__w_ % __m != 0);
2588 __w0_ = __w_ / __n_;
2589 if (_R == 0)
2590 __y0_ = _R;
2591 else if (__w0_ < _WDt)
2592 __y0_ = (_R >> __w0_) << __w0_;
2593 else
2594 __y0_ = 0;
2595 if (_R - __y0_ > __y0_ / __n_)
2596 {
2597 ++__n_;
2598 __w0_ = __w_ / __n_;
2599 if (__w0_ < _WDt)
2600 __y0_ = (_R >> __w0_) << __w0_;
2601 else
2602 __y0_ = 0;
2603 }
2604 __n0_ = __n_ - __w_ % __n_;
2605 if (__w0_ < _WDt - 1)
2606 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2607 else
2608 __y1_ = 0;
2609 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2610 _Engine_result_type(0);
2611 __mask1_ = __w0_ < _EDt - 1 ?
2612 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2613 _Engine_result_type(~0);
2614}
2615
2616template<class _Engine, class _UIntType>
2617inline
2618_UIntType
2619__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2620{
2621 return static_cast<result_type>(__e_() & __mask0_);
2622}
2623
2624template<class _Engine, class _UIntType>
2625_UIntType
2626__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2627{
2628 result_type _S = 0;
2629 for (size_t __k = 0; __k < __n0_; ++__k)
2630 {
2631 _Engine_result_type __u;
2632 do
2633 {
2634 __u = __e_() - _Engine::min();
2635 } while (__u >= __y0_);
2636 if (__w0_ < _EDt)
2637 _S <<= __w0_;
2638 else
2639 _S = 0;
2640 _S += __u & __mask0_;
2641 }
2642 for (size_t __k = __n0_; __k < __n_; ++__k)
2643 {
2644 _Engine_result_type __u;
2645 do
2646 {
2647 __u = __e_() - _Engine::min();
2648 } while (__u >= __y1_);
2649 if (__w0_ < _EDt - 1)
2650 _S <<= __w0_ + 1;
2651 else
2652 _S = 0;
2653 _S += __u & __mask1_;
2654 }
2655 return _S;
2656}
2657
Howard Hinnant30a840f2010-05-12 17:08:57 +00002658// uniform_int_distribution
2659
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660template<class _IntType = int>
2661class uniform_int_distribution
2662{
2663public:
2664 // types
2665 typedef _IntType result_type;
2666
2667 class param_type
2668 {
2669 result_type __a_;
2670 result_type __b_;
2671 public:
2672 typedef uniform_int_distribution distribution_type;
2673
2674 explicit param_type(result_type __a = 0,
2675 result_type __b = numeric_limits<result_type>::max())
2676 : __a_(__a), __b_(__b) {}
2677
2678 result_type a() const {return __a_;}
2679 result_type b() const {return __b_;}
2680
2681 friend bool operator==(const param_type& __x, const param_type& __y)
2682 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2683 friend bool operator!=(const param_type& __x, const param_type& __y)
2684 {return !(__x == __y);}
2685 };
2686
2687private:
2688 param_type __p_;
2689
2690public:
2691 // constructors and reset functions
2692 explicit uniform_int_distribution(result_type __a = 0,
2693 result_type __b = numeric_limits<result_type>::max())
2694 : __p_(param_type(__a, __b)) {}
2695 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2696 void reset() {}
2697
2698 // generating functions
2699 template<class _URNG> result_type operator()(_URNG& __g)
2700 {return (*this)(__g, __p_);}
2701 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2702
2703 // property functions
2704 result_type a() const {return __p_.a();}
2705 result_type b() const {return __p_.b();}
2706
2707 param_type param() const {return __p_;}
2708 void param(const param_type& __p) {__p_ = __p;}
2709
2710 result_type min() const {return a();}
2711 result_type max() const {return b();}
2712
2713 friend bool operator==(const uniform_int_distribution& __x,
2714 const uniform_int_distribution& __y)
2715 {return __x.__p_ == __y.__p_;}
2716 friend bool operator!=(const uniform_int_distribution& __x,
2717 const uniform_int_distribution& __y)
2718 {return !(__x == __y);}
2719};
2720
2721template<class _IntType>
2722template<class _URNG>
2723typename uniform_int_distribution<_IntType>::result_type
2724uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2725{
2726 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2727 uint32_t, uint64_t>::type _UIntType;
2728 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2729 if (_R == 1)
2730 return __p.a();
2731 const size_t _Dt = numeric_limits<_UIntType>::digits;
2732 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2733 if (_R == 0)
2734 return static_cast<result_type>(_Eng(__g, _Dt)());
2735 size_t __w = _Dt - __clz(_R) - 1;
2736 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2737 ++__w;
2738 _Eng __e(__g, __w);
2739 _UIntType __u;
2740 do
2741 {
2742 __u = __e();
2743 } while (__u >= _R);
2744 return static_cast<result_type>(__u + __p.a());
2745}
2746
2747template <class _CharT, class _Traits, class _IT>
2748basic_ostream<_CharT, _Traits>&
2749operator<<(basic_ostream<_CharT, _Traits>& __os,
2750 const uniform_int_distribution<_IT>& __x)
2751{
2752 __save_flags<_CharT, _Traits> _(__os);
2753 __os.flags(ios_base::dec | ios_base::left);
2754 _CharT __sp = __os.widen(' ');
2755 __os.fill(__sp);
2756 return __os << __x.a() << __sp << __x.b();
2757}
2758
2759template <class _CharT, class _Traits, class _IT>
2760basic_istream<_CharT, _Traits>&
2761operator>>(basic_istream<_CharT, _Traits>& __is,
2762 uniform_int_distribution<_IT>& __x)
2763{
2764 typedef uniform_int_distribution<_IT> _Eng;
2765 typedef typename _Eng::result_type result_type;
2766 typedef typename _Eng::param_type param_type;
2767 __save_flags<_CharT, _Traits> _(__is);
2768 __is.flags(ios_base::dec | ios_base::skipws);
2769 result_type __a;
2770 result_type __b;
2771 __is >> __a >> __b;
2772 if (!__is.fail())
2773 __x.param(param_type(__a, __b));
2774 return __is;
2775}
2776
Howard Hinnant30a840f2010-05-12 17:08:57 +00002777// uniform_real_distribution
2778
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779template<class _RealType = double>
2780class uniform_real_distribution
2781{
2782public:
2783 // types
2784 typedef _RealType result_type;
2785
2786 class param_type
2787 {
2788 result_type __a_;
2789 result_type __b_;
2790 public:
2791 typedef uniform_real_distribution distribution_type;
2792
2793 explicit param_type(result_type __a = 0,
2794 result_type __b = 1)
2795 : __a_(__a), __b_(__b) {}
2796
2797 result_type a() const {return __a_;}
2798 result_type b() const {return __b_;}
2799
2800 friend bool operator==(const param_type& __x, const param_type& __y)
2801 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2802 friend bool operator!=(const param_type& __x, const param_type& __y)
2803 {return !(__x == __y);}
2804 };
2805
2806private:
2807 param_type __p_;
2808
2809public:
2810 // constructors and reset functions
2811 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
2812 : __p_(param_type(__a, __b)) {}
2813 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
2814 void reset() {}
2815
2816 // generating functions
2817 template<class _URNG> result_type operator()(_URNG& __g)
2818 {return (*this)(__g, __p_);}
2819 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2820
2821 // property functions
2822 result_type a() const {return __p_.a();}
2823 result_type b() const {return __p_.b();}
2824
2825 param_type param() const {return __p_;}
2826 void param(const param_type& __p) {__p_ = __p;}
2827
2828 result_type min() const {return a();}
2829 result_type max() const {return b();}
2830
2831 friend bool operator==(const uniform_real_distribution& __x,
2832 const uniform_real_distribution& __y)
2833 {return __x.__p_ == __y.__p_;}
2834 friend bool operator!=(const uniform_real_distribution& __x,
2835 const uniform_real_distribution& __y)
2836 {return !(__x == __y);}
2837};
2838
2839template<class _RealType>
2840template<class _URNG>
2841inline
2842typename uniform_real_distribution<_RealType>::result_type
2843uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
2844{
2845 return (__p.b() - __p.a())
2846 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
2847 + __p.a();
2848}
2849
2850template <class _CharT, class _Traits, class _RT>
2851basic_ostream<_CharT, _Traits>&
2852operator<<(basic_ostream<_CharT, _Traits>& __os,
2853 const uniform_real_distribution<_RT>& __x)
2854{
2855 __save_flags<_CharT, _Traits> _(__os);
2856 __os.flags(ios_base::dec | ios_base::left);
2857 _CharT __sp = __os.widen(' ');
2858 __os.fill(__sp);
2859 return __os << __x.a() << __sp << __x.b();
2860}
2861
2862template <class _CharT, class _Traits, class _RT>
2863basic_istream<_CharT, _Traits>&
2864operator>>(basic_istream<_CharT, _Traits>& __is,
2865 uniform_real_distribution<_RT>& __x)
2866{
2867 typedef uniform_real_distribution<_RT> _Eng;
2868 typedef typename _Eng::result_type result_type;
2869 typedef typename _Eng::param_type param_type;
2870 __save_flags<_CharT, _Traits> _(__is);
2871 __is.flags(ios_base::dec | ios_base::skipws);
2872 result_type __a;
2873 result_type __b;
2874 __is >> __a >> __b;
2875 if (!__is.fail())
2876 __x.param(param_type(__a, __b));
2877 return __is;
2878}
2879
Howard Hinnant30a840f2010-05-12 17:08:57 +00002880// bernoulli_distribution
2881
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882class bernoulli_distribution
2883{
2884public:
2885 // types
2886 typedef bool result_type;
2887
2888 class param_type
2889 {
2890 double __p_;
2891 public:
2892 typedef bernoulli_distribution distribution_type;
2893
2894 explicit param_type(double __p = 0.5) : __p_(__p) {}
2895
2896 double p() const {return __p_;}
2897
2898 friend bool operator==(const param_type& __x, const param_type& __y)
2899 {return __x.__p_ == __y.__p_;}
2900 friend bool operator!=(const param_type& __x, const param_type& __y)
2901 {return !(__x == __y);}
2902 };
2903
2904private:
2905 param_type __p_;
2906
2907public:
2908 // constructors and reset functions
2909 explicit bernoulli_distribution(double __p = 0.5)
2910 : __p_(param_type(__p)) {}
2911 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
2912 void reset() {}
2913
2914 // generating functions
2915 template<class _URNG> result_type operator()(_URNG& __g)
2916 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00002917 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002918
2919 // property functions
2920 double p() const {return __p_.p();}
2921
2922 param_type param() const {return __p_;}
2923 void param(const param_type& __p) {__p_ = __p;}
2924
2925 result_type min() const {return false;}
2926 result_type max() const {return true;}
2927
2928 friend bool operator==(const bernoulli_distribution& __x,
2929 const bernoulli_distribution& __y)
2930 {return __x.__p_ == __y.__p_;}
2931 friend bool operator!=(const bernoulli_distribution& __x,
2932 const bernoulli_distribution& __y)
2933 {return !(__x == __y);}
2934};
2935
Howard Hinnant03aad812010-05-11 23:26:59 +00002936template<class _URNG>
2937inline
2938bernoulli_distribution::result_type
2939bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
2940{
2941 return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
2942}
2943
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944template <class _CharT, class _Traits>
2945basic_ostream<_CharT, _Traits>&
2946operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
2947{
2948 __save_flags<_CharT, _Traits> _(__os);
2949 __os.flags(ios_base::dec | ios_base::left);
2950 _CharT __sp = __os.widen(' ');
2951 __os.fill(__sp);
2952 return __os << __x.p();
2953}
2954
2955template <class _CharT, class _Traits>
2956basic_istream<_CharT, _Traits>&
2957operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
2958{
2959 typedef bernoulli_distribution _Eng;
2960 typedef typename _Eng::param_type param_type;
2961 __save_flags<_CharT, _Traits> _(__is);
2962 __is.flags(ios_base::dec | ios_base::skipws);
2963 double __p;
2964 __is >> __p;
2965 if (!__is.fail())
2966 __x.param(param_type(__p));
2967 return __is;
2968}
2969
Howard Hinnant30a840f2010-05-12 17:08:57 +00002970// binomial_distribution
2971
Howard Hinnant03aad812010-05-11 23:26:59 +00002972template<class _IntType = int>
2973class binomial_distribution
2974{
2975public:
2976 // types
2977 typedef _IntType result_type;
2978
2979 class param_type
2980 {
2981 result_type __t_;
2982 double __p_;
2983 public:
2984 typedef binomial_distribution distribution_type;
2985
2986 explicit param_type(result_type __t = 1, double __p = 0.5)
2987 : __t_(__t), __p_(__p) {}
2988
2989 result_type t() const {return __t_;}
2990 double p() const {return __p_;}
2991
2992 friend bool operator==(const param_type& __x, const param_type& __y)
2993 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
2994 friend bool operator!=(const param_type& __x, const param_type& __y)
2995 {return !(__x == __y);}
2996 };
2997
2998private:
2999 param_type __p_;
3000
3001public:
3002 // constructors and reset functions
3003 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3004 : __p_(param_type(__t, __p)) {}
3005 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3006 void reset() {}
3007
3008 // generating functions
3009 template<class _URNG> result_type operator()(_URNG& __g)
3010 {return (*this)(__g, __p_);}
3011 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3012
3013 // property functions
3014 result_type t() const {return __p_.t();}
3015 double p() const {return __p_.p();}
3016
3017 param_type param() const {return __p_;}
3018 void param(const param_type& __p) {__p_ = __p;}
3019
3020 result_type min() const {return 0;}
3021 result_type max() const {return t();}
3022
3023 friend bool operator==(const binomial_distribution& __x,
3024 const binomial_distribution& __y)
3025 {return __x.__p_ == __y.__p_;}
3026 friend bool operator!=(const binomial_distribution& __x,
3027 const binomial_distribution& __y)
3028 {return !(__x == __y);}
3029};
3030
3031template<class _IntType>
3032template<class _URNG>
3033_IntType
3034binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3035{
3036 bernoulli_distribution __bd(__p.p());
3037 _IntType __r = 0;
3038 _IntType __t = __p.t();
3039 for (_IntType __i = 0; __i < __t; ++__i)
3040 __r += __bd(__g);
3041 return __r;
3042}
3043
3044template <class _CharT, class _Traits, class _IntType>
3045basic_ostream<_CharT, _Traits>&
3046operator<<(basic_ostream<_CharT, _Traits>& __os,
3047 const binomial_distribution<_IntType>& __x)
3048{
3049 __save_flags<_CharT, _Traits> _(__os);
3050 __os.flags(ios_base::dec | ios_base::left);
3051 _CharT __sp = __os.widen(' ');
3052 __os.fill(__sp);
3053 return __os << __x.t() << __sp << __x.p();
3054}
3055
3056template <class _CharT, class _Traits, class _IntType>
3057basic_istream<_CharT, _Traits>&
3058operator>>(basic_istream<_CharT, _Traits>& __is,
3059 binomial_distribution<_IntType>& __x)
3060{
3061 typedef binomial_distribution<_IntType> _Eng;
3062 typedef typename _Eng::result_type result_type;
3063 typedef typename _Eng::param_type param_type;
3064 __save_flags<_CharT, _Traits> _(__is);
3065 __is.flags(ios_base::dec | ios_base::skipws);
3066 result_type __t;
3067 double __p;
3068 __is >> __t >> __p;
3069 if (!__is.fail())
3070 __x.param(param_type(__t, __p));
3071 return __is;
3072}
3073
Howard Hinnant30a840f2010-05-12 17:08:57 +00003074// exponential_distribution
3075
3076template<class _RealType = double>
3077class exponential_distribution
3078{
3079public:
3080 // types
3081 typedef _RealType result_type;
3082
3083 class param_type
3084 {
3085 result_type __lambda_;
3086 public:
3087 typedef exponential_distribution distribution_type;
3088
3089 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3090
3091 result_type lambda() const {return __lambda_;}
3092
3093 friend bool operator==(const param_type& __x, const param_type& __y)
3094 {return __x.__lambda_ == __y.__lambda_;}
3095 friend bool operator!=(const param_type& __x, const param_type& __y)
3096 {return !(__x == __y);}
3097 };
3098
3099private:
3100 param_type __p_;
3101
3102public:
3103 // constructors and reset functions
3104 explicit exponential_distribution(result_type __lambda = 1)
3105 : __p_(param_type(__lambda)) {}
3106 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3107 void reset() {}
3108
3109 // generating functions
3110 template<class _URNG> result_type operator()(_URNG& __g)
3111 {return (*this)(__g, __p_);}
3112 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3113
3114 // property functions
3115 result_type lambda() const {return __p_.lambda();}
3116
3117 param_type param() const {return __p_;}
3118 void param(const param_type& __p) {__p_ = __p;}
3119
3120 result_type min() const {return 0;}
3121 result_type max() const
3122 {return -std::log(1-std::nextafter(result_type(1), result_type(-1))) /
3123 __p_.lambda();}
3124
3125 friend bool operator==(const exponential_distribution& __x,
3126 const exponential_distribution& __y)
3127 {return __x.__p_ == __y.__p_;}
3128 friend bool operator!=(const exponential_distribution& __x,
3129 const exponential_distribution& __y)
3130 {return !(__x == __y);}
3131};
3132
3133template <class _RealType>
3134template<class _URNG>
3135_RealType
3136exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3137{
3138 return -_STD::log
3139 (
3140 result_type(1) -
3141 _STD::generate_canonical<result_type,
3142 numeric_limits<result_type>::digits>(__g)
3143 )
3144 / __p.lambda();
3145}
3146
3147template <class _CharT, class _Traits, class _RealType>
3148basic_ostream<_CharT, _Traits>&
3149operator<<(basic_ostream<_CharT, _Traits>& __os,
3150 const exponential_distribution<_RealType>& __x)
3151{
3152 __save_flags<_CharT, _Traits> _(__os);
3153 __os.flags(ios_base::dec | ios_base::left);
3154 return __os << __x.lambda();
3155}
3156
3157template <class _CharT, class _Traits, class _RealType>
3158basic_istream<_CharT, _Traits>&
3159operator>>(basic_istream<_CharT, _Traits>& __is,
3160 exponential_distribution<_RealType>& __x)
3161{
3162 typedef exponential_distribution<_RealType> _Eng;
3163 typedef typename _Eng::result_type result_type;
3164 typedef typename _Eng::param_type param_type;
3165 __save_flags<_CharT, _Traits> _(__is);
3166 __is.flags(ios_base::dec | ios_base::skipws);
3167 result_type __lambda;
3168 __is >> __lambda;
3169 if (!__is.fail())
3170 __x.param(param_type(__lambda));
3171 return __is;
3172}
3173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174_LIBCPP_END_NAMESPACE_STD
3175
3176#endif // _LIBCPP_RANDOM