blob: 305f8866d30ade69ac9328924c1e723733aa99f6 [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//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000220 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221};
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000272 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273};
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000326 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327};
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;
Howard Hinnantd6d11712010-05-20 15:11:46 +0000374typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000395 double entropy() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396
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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000488
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000547
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000602
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000660
Howard Hinnant03aad812010-05-11 23:26:59 +0000661 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>
Howard Hinnant34e8a572010-05-17 13:44:27 +0000669class geometric_distribution
670{
671public:
672 // types
673 typedef IntType result_type;
674
675 class param_type
676 {
677 public:
678 typedef geometric_distribution distribution_type;
679
680 explicit param_type(double p = 0.5);
681
682 double p() const;
683
684 friend bool operator==(const param_type& x, const param_type& y);
685 friend bool operator!=(const param_type& x, const param_type& y);
686 };
687
688 // constructors and reset functions
689 explicit geometric_distribution(double p = 0.5);
690 explicit geometric_distribution(const param_type& parm);
691 void reset();
692
693 // generating functions
694 template<class URNG> result_type operator()(URNG& g);
695 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
696
697 // property functions
698 double p() const;
699
700 param_type param() const;
701 void param(const param_type& parm);
702
703 result_type min() const;
704 result_type max() const;
705
706 friend bool operator==(const geometric_distribution& x,
707 const geometric_distribution& y);
708 friend bool operator!=(const geometric_distribution& x,
709 const geometric_distribution& y);
710
711 template <class charT, class traits>
712 friend
713 basic_ostream<charT, traits>&
714 operator<<(basic_ostream<charT, traits>& os,
715 const geometric_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000716
Howard Hinnant34e8a572010-05-17 13:44:27 +0000717 template <class charT, class traits>
718 friend
719 basic_istream<charT, traits>&
720 operator>>(basic_istream<charT, traits>& is,
721 geometric_distribution& x);
722};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000725class negative_binomial_distribution
726{
727public:
728 // types
729 typedef IntType result_type;
730
731 class param_type
732 {
733 public:
734 typedef negative_binomial_distribution distribution_type;
735
736 explicit param_type(result_type k = 1, double p = 0.5);
737
738 result_type k() const;
739 double p() const;
740
741 friend bool operator==(const param_type& x, const param_type& y);
742 friend bool operator!=(const param_type& x, const param_type& y);
743 };
744
745 // constructor and reset functions
746 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
747 explicit negative_binomial_distribution(const param_type& parm);
748 void reset();
749
750 // generating functions
751 template<class URNG> result_type operator()(URNG& g);
752 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
753
754 // property functions
755 result_type k() const;
756 double p() const;
757
758 param_type param() const;
759 void param(const param_type& parm);
760
761 result_type min() const;
762 result_type max() const;
763
764 friend bool operator==(const negative_binomial_distribution& x,
765 const negative_binomial_distribution& y);
766 friend bool operator!=(const negative_binomial_distribution& x,
767 const negative_binomial_distribution& y);
768
769 template <class charT, class traits>
770 friend
771 basic_ostream<charT, traits>&
772 operator<<(basic_ostream<charT, traits>& os,
773 const negative_binomial_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000774
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000775 template <class charT, class traits>
776 friend
777 basic_istream<charT, traits>&
778 operator>>(basic_istream<charT, traits>& is,
779 negative_binomial_distribution& x);
780};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781
782template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000783class poisson_distribution
784{
785public:
786 // types
787 typedef IntType result_type;
788
789 class param_type
790 {
791 public:
792 typedef poisson_distribution distribution_type;
793
794 explicit param_type(double mean = 1.0);
795
796 double mean() const;
797
798 friend bool operator==(const param_type& x, const param_type& y);
799 friend bool operator!=(const param_type& x, const param_type& y);
800 };
801
802 // constructors and reset functions
803 explicit poisson_distribution(double mean = 1.0);
804 explicit poisson_distribution(const param_type& parm);
805 void reset();
806
807 // generating functions
808 template<class URNG> result_type operator()(URNG& g);
809 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
810
811 // property functions
812 double mean() const;
813
814 param_type param() const;
815 void param(const param_type& parm);
816
817 result_type min() const;
818 result_type max() const;
819
820 friend bool operator==(const poisson_distribution& x,
821 const poisson_distribution& y);
822 friend bool operator!=(const poisson_distribution& x,
823 const poisson_distribution& y);
824
825 template <class charT, class traits>
826 friend
827 basic_ostream<charT, traits>&
828 operator<<(basic_ostream<charT, traits>& os,
829 const poisson_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000830
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000831 template <class charT, class traits>
832 friend
833 basic_istream<charT, traits>&
834 operator>>(basic_istream<charT, traits>& is,
835 poisson_distribution& x);
836};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
838template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000839class exponential_distribution
840{
841public:
842 // types
843 typedef RealType result_type;
844
845 class param_type
846 {
847 public:
848 typedef exponential_distribution distribution_type;
849
Howard Hinnanta64111c2010-05-12 21:02:31 +0000850 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000851
Howard Hinnanta64111c2010-05-12 21:02:31 +0000852 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000853
854 friend bool operator==(const param_type& x, const param_type& y);
855 friend bool operator!=(const param_type& x, const param_type& y);
856 };
857
858 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000859 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000860 explicit exponential_distribution(const param_type& parm);
861 void reset();
862
863 // generating functions
864 template<class URNG> result_type operator()(URNG& g);
865 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
866
867 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000868 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000869
870 param_type param() const;
871 void param(const param_type& parm);
872
873 result_type min() const;
874 result_type max() const;
875
876 friend bool operator==(const exponential_distribution& x,
877 const exponential_distribution& y);
878 friend bool operator!=(const exponential_distribution& x,
879 const exponential_distribution& y);
880
881 template <class charT, class traits>
882 friend
883 basic_ostream<charT, traits>&
884 operator<<(basic_ostream<charT, traits>& os,
885 const exponential_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000886
Howard Hinnant30a840f2010-05-12 17:08:57 +0000887 template <class charT, class traits>
888 friend
889 basic_istream<charT, traits>&
890 operator>>(basic_istream<charT, traits>& is,
891 exponential_distribution& x);
892};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
894template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000895class gamma_distribution
896{
897public:
898 // types
899 typedef RealType result_type;
900
901 class param_type
902 {
903 public:
904 typedef gamma_distribution distribution_type;
905
906 explicit param_type(result_type alpha = 1, result_type beta = 1);
907
908 result_type alpha() const;
909 result_type beta() const;
910
911 friend bool operator==(const param_type& x, const param_type& y);
912 friend bool operator!=(const param_type& x, const param_type& y);
913 };
914
915 // constructors and reset functions
916 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
917 explicit gamma_distribution(const param_type& parm);
918 void reset();
919
920 // generating functions
921 template<class URNG> result_type operator()(URNG& g);
922 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
923
924 // property functions
925 result_type alpha() const;
926 result_type beta() const;
927
928 param_type param() const;
929 void param(const param_type& parm);
930
931 result_type min() const;
932 result_type max() const;
933
934 friend bool operator==(const gamma_distribution& x,
935 const gamma_distribution& y);
936 friend bool operator!=(const gamma_distribution& x,
937 const gamma_distribution& y);
938
939 template <class charT, class traits>
940 friend
941 basic_ostream<charT, traits>&
942 operator<<(basic_ostream<charT, traits>& os,
943 const gamma_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000944
Howard Hinnantc7c49132010-05-13 17:58:28 +0000945 template <class charT, class traits>
946 friend
947 basic_istream<charT, traits>&
948 operator>>(basic_istream<charT, traits>& is,
949 gamma_distribution& x);
950};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
952template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02 +0000953class weibull_distribution
954{
955public:
956 // types
957 typedef RealType result_type;
958
959 class param_type
960 {
961 public:
962 typedef weibull_distribution distribution_type;
963
964 explicit param_type(result_type alpha = 1, result_type beta = 1);
965
966 result_type a() const;
967 result_type b() const;
968
969 friend bool operator==(const param_type& x, const param_type& y);
970 friend bool operator!=(const param_type& x, const param_type& y);
971 };
972
973 // constructor and reset functions
974 explicit weibull_distribution(result_type a = 1, result_type b = 1);
975 explicit weibull_distribution(const param_type& parm);
976 void reset();
977
978 // generating functions
979 template<class URNG> result_type operator()(URNG& g);
980 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
981
982 // property functions
983 result_type a() const;
984 result_type b() const;
985
986 param_type param() const;
987 void param(const param_type& parm);
988
989 result_type min() const;
990 result_type max() const;
991
Howard Hinnant9de6e302010-05-16 01:09:02 +0000992 friend bool operator==(const weibull_distribution& x,
993 const weibull_distribution& y);
994 friend bool operator!=(const weibull_distribution& x,
995 const weibull_distribution& y);
996
997 template <class charT, class traits>
998 friend
999 basic_ostream<charT, traits>&
1000 operator<<(basic_ostream<charT, traits>& os,
1001 const weibull_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001002
Howard Hinnant9de6e302010-05-16 01:09:02 +00001003 template <class charT, class traits>
1004 friend
1005 basic_istream<charT, traits>&
1006 operator>>(basic_istream<charT, traits>& is,
1007 weibull_distribution& x);
1008};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009
1010template<class RealType = double>
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001011class extreme_value_distribution
1012{
1013public:
1014 // types
1015 typedef RealType result_type;
1016
1017 class param_type
1018 {
1019 public:
1020 typedef extreme_value_distribution distribution_type;
1021
1022 explicit param_type(result_type a = 0, result_type b = 1);
1023
1024 result_type a() const;
1025 result_type b() const;
1026
1027 friend bool operator==(const param_type& x, const param_type& y);
1028 friend bool operator!=(const param_type& x, const param_type& y);
1029 };
1030
1031 // constructor and reset functions
1032 explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
1033 explicit extreme_value_distribution(const param_type& parm);
1034 void reset();
1035
1036 // generating functions
1037 template<class URNG> result_type operator()(URNG& g);
1038 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1039
1040 // property functions
1041 result_type a() const;
1042 result_type b() const;
1043
1044 param_type param() const;
1045 void param(const param_type& parm);
1046
1047 result_type min() const;
1048 result_type max() const;
1049
1050 friend bool operator==(const extreme_value_distribution& x,
1051 const extreme_value_distribution& y);
1052 friend bool operator!=(const extreme_value_distribution& x,
1053 const extreme_value_distribution& y);
1054
1055 template <class charT, class traits>
1056 friend
1057 basic_ostream<charT, traits>&
1058 operator<<(basic_ostream<charT, traits>& os,
1059 const extreme_value_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001060
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001061 template <class charT, class traits>
1062 friend
1063 basic_istream<charT, traits>&
1064 operator>>(basic_istream<charT, traits>& is,
1065 extreme_value_distribution& x);
1066};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
1068template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +00001069class normal_distribution
1070{
1071public:
1072 // types
1073 typedef RealType result_type;
1074
1075 class param_type
1076 {
1077 public:
1078 typedef normal_distribution distribution_type;
1079
1080 explicit param_type(result_type mean = 0, result_type stddev = 1);
1081
1082 result_type mean() const;
1083 result_type stddev() const;
1084
1085 friend bool operator==(const param_type& x, const param_type& y);
1086 friend bool operator!=(const param_type& x, const param_type& y);
1087 };
1088
1089 // constructors and reset functions
1090 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1091 explicit normal_distribution(const param_type& parm);
1092 void reset();
1093
1094 // generating functions
1095 template<class URNG> result_type operator()(URNG& g);
1096 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1097
1098 // property functions
1099 result_type mean() const;
1100 result_type stddev() const;
1101
1102 param_type param() const;
1103 void param(const param_type& parm);
1104
1105 result_type min() const;
1106 result_type max() const;
1107
1108 friend bool operator==(const normal_distribution& x,
1109 const normal_distribution& y);
1110 friend bool operator!=(const normal_distribution& x,
1111 const normal_distribution& y);
1112
1113 template <class charT, class traits>
1114 friend
1115 basic_ostream<charT, traits>&
1116 operator<<(basic_ostream<charT, traits>& os,
1117 const normal_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001118
Howard Hinnanta64111c2010-05-12 21:02:31 +00001119 template <class charT, class traits>
1120 friend
1121 basic_istream<charT, traits>&
1122 operator>>(basic_istream<charT, traits>& is,
1123 normal_distribution& x);
1124};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125
1126template<class RealType = double>
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001127class lognormal_distribution
1128{
1129public:
1130 // types
1131 typedef RealType result_type;
1132
1133 class param_type
1134 {
1135 public:
1136 typedef lognormal_distribution distribution_type;
1137
1138 explicit param_type(result_type m = 0, result_type s = 1);
1139
1140 result_type m() const;
1141 result_type s() const;
1142
1143 friend bool operator==(const param_type& x, const param_type& y);
1144 friend bool operator!=(const param_type& x, const param_type& y);
1145 };
1146
1147 // constructor and reset functions
1148 explicit lognormal_distribution(result_type m = 0, result_type s = 1);
1149 explicit lognormal_distribution(const param_type& parm);
1150 void reset();
1151
1152 // generating functions
1153 template<class URNG> result_type operator()(URNG& g);
1154 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1155
1156 // property functions
1157 result_type m() const;
1158 result_type s() const;
1159
1160 param_type param() const;
1161 void param(const param_type& parm);
1162
1163 result_type min() const;
1164 result_type max() const;
1165
1166 friend bool operator==(const lognormal_distribution& x,
1167 const lognormal_distribution& y);
1168 friend bool operator!=(const lognormal_distribution& x,
1169 const lognormal_distribution& y);
1170
1171 template <class charT, class traits>
1172 friend
1173 basic_ostream<charT, traits>&
1174 operator<<(basic_ostream<charT, traits>& os,
1175 const lognormal_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001176
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001177 template <class charT, class traits>
1178 friend
1179 basic_istream<charT, traits>&
1180 operator>>(basic_istream<charT, traits>& is,
1181 lognormal_distribution& x);
1182};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
1184template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001185class chi_squared_distribution
1186{
1187public:
1188 // types
1189 typedef RealType result_type;
1190
1191 class param_type
1192 {
1193 public:
1194 typedef chi_squared_distribution distribution_type;
1195
1196 explicit param_type(result_type n = 1);
1197
1198 result_type n() const;
1199
1200 friend bool operator==(const param_type& x, const param_type& y);
1201 friend bool operator!=(const param_type& x, const param_type& y);
1202 };
1203
1204 // constructor and reset functions
1205 explicit chi_squared_distribution(result_type n = 1);
1206 explicit chi_squared_distribution(const param_type& parm);
1207 void reset();
1208
1209 // generating functions
1210 template<class URNG> result_type operator()(URNG& g);
1211 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1212
1213 // property functions
1214 result_type n() const;
1215
1216 param_type param() const;
1217 void param(const param_type& parm);
1218
1219 result_type min() const;
1220 result_type max() const;
1221
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001222 friend bool operator==(const chi_squared_distribution& x,
1223 const chi_squared_distribution& y);
1224 friend bool operator!=(const chi_squared_distribution& x,
1225 const chi_squared_distribution& y);
1226
1227 template <class charT, class traits>
1228 friend
1229 basic_ostream<charT, traits>&
1230 operator<<(basic_ostream<charT, traits>& os,
1231 const chi_squared_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001232
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001233 template <class charT, class traits>
1234 friend
1235 basic_istream<charT, traits>&
1236 operator>>(basic_istream<charT, traits>& is,
1237 chi_squared_distribution& x);
1238};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240template<class RealType = double>
Howard Hinnantd7d01132010-05-17 21:55:46 +00001241class cauchy_distribution
1242{
1243public:
1244 // types
1245 typedef RealType result_type;
1246
1247 class param_type
1248 {
1249 public:
1250 typedef cauchy_distribution distribution_type;
1251
1252 explicit param_type(result_type a = 0, result_type b = 1);
1253
1254 result_type a() const;
1255 result_type b() const;
1256
1257 friend bool operator==(const param_type& x, const param_type& y);
1258 friend bool operator!=(const param_type& x, const param_type& y);
1259 };
1260
1261 // constructor and reset functions
1262 explicit cauchy_distribution(result_type a = 0, result_type b = 1);
1263 explicit cauchy_distribution(const param_type& parm);
1264 void reset();
1265
1266 // generating functions
1267 template<class URNG> result_type operator()(URNG& g);
1268 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1269
1270 // property functions
1271 result_type a() const;
1272 result_type b() const;
1273
1274 param_type param() const;
1275 void param(const param_type& parm);
1276
1277 result_type min() const;
1278 result_type max() const;
1279
1280 friend bool operator==(const cauchy_distribution& x,
1281 const cauchy_distribution& y);
1282 friend bool operator!=(const cauchy_distribution& x,
1283 const cauchy_distribution& y);
1284
1285 template <class charT, class traits>
1286 friend
1287 basic_ostream<charT, traits>&
1288 operator<<(basic_ostream<charT, traits>& os,
1289 const cauchy_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001290
Howard Hinnantd7d01132010-05-17 21:55:46 +00001291 template <class charT, class traits>
1292 friend
1293 basic_istream<charT, traits>&
1294 operator>>(basic_istream<charT, traits>& is,
1295 cauchy_distribution& x);
1296};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
1298template<class RealType = double>
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001299class fisher_f_distribution
1300{
1301public:
1302 // types
1303 typedef RealType result_type;
1304
1305 class param_type
1306 {
1307 public:
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001308 typedef fisher_f_distribution distribution_type;
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001309
1310 explicit param_type(result_type m = 1, result_type n = 1);
1311
1312 result_type m() const;
1313 result_type n() const;
1314
1315 friend bool operator==(const param_type& x, const param_type& y);
1316 friend bool operator!=(const param_type& x, const param_type& y);
1317 };
1318
1319 // constructor and reset functions
1320 explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
1321 explicit fisher_f_distribution(const param_type& parm);
1322 void reset();
1323
1324 // generating functions
1325 template<class URNG> result_type operator()(URNG& g);
1326 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1327
1328 // property functions
1329 result_type m() const;
1330 result_type n() const;
1331
1332 param_type param() const;
1333 void param(const param_type& parm);
1334
1335 result_type min() const;
1336 result_type max() const;
1337
1338 friend bool operator==(const fisher_f_distribution& x,
1339 const fisher_f_distribution& y);
1340 friend bool operator!=(const fisher_f_distribution& x,
1341 const fisher_f_distribution& y);
1342
1343 template <class charT, class traits>
1344 friend
1345 basic_ostream<charT, traits>&
1346 operator<<(basic_ostream<charT, traits>& os,
1347 const fisher_f_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001348
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001349 template <class charT, class traits>
1350 friend
1351 basic_istream<charT, traits>&
1352 operator>>(basic_istream<charT, traits>& is,
1353 fisher_f_distribution& x);
1354};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355
1356template<class RealType = double>
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001357class student_t_distribution
1358{
1359public:
1360 // types
1361 typedef RealType result_type;
1362
1363 class param_type
1364 {
1365 public:
1366 typedef student_t_distribution distribution_type;
1367
1368 explicit param_type(result_type n = 1);
1369
1370 result_type n() const;
1371
1372 friend bool operator==(const param_type& x, const param_type& y);
1373 friend bool operator!=(const param_type& x, const param_type& y);
1374 };
1375
1376 // constructor and reset functions
1377 explicit student_t_distribution(result_type n = 1);
1378 explicit student_t_distribution(const param_type& parm);
1379 void reset();
1380
1381 // generating functions
1382 template<class URNG> result_type operator()(URNG& g);
1383 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1384
1385 // property functions
1386 result_type n() const;
1387
1388 param_type param() const;
1389 void param(const param_type& parm);
1390
1391 result_type min() const;
1392 result_type max() const;
1393
1394 friend bool operator==(const student_t_distribution& x,
1395 const student_t_distribution& y);
1396 friend bool operator!=(const student_t_distribution& x,
1397 const student_t_distribution& y);
1398
1399 template <class charT, class traits>
1400 friend
1401 basic_ostream<charT, traits>&
1402 operator<<(basic_ostream<charT, traits>& os,
1403 const student_t_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001404
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001405 template <class charT, class traits>
1406 friend
1407 basic_istream<charT, traits>&
1408 operator>>(basic_istream<charT, traits>& is,
1409 student_t_distribution& x);
1410};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411
1412template<class IntType = int>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001413class discrete_distribution
1414{
1415public:
1416 // types
1417 typedef IntType result_type;
1418
1419 class param_type
1420 {
1421 public:
1422 typedef discrete_distribution distribution_type;
1423
1424 param_type();
1425 template<class InputIterator>
1426 param_type(InputIterator firstW, InputIterator lastW);
1427 param_type(initializer_list<double> wl);
1428 template<class UnaryOperation>
1429 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1430
1431 vector<double> probabilities() const;
1432
1433 friend bool operator==(const param_type& x, const param_type& y);
1434 friend bool operator!=(const param_type& x, const param_type& y);
1435 };
1436
1437 // constructor and reset functions
1438 discrete_distribution();
1439 template<class InputIterator>
1440 discrete_distribution(InputIterator firstW, InputIterator lastW);
1441 discrete_distribution(initializer_list<double> wl);
1442 template<class UnaryOperation>
1443 discrete_distribution(size_t nw, double xmin, double xmax,
1444 UnaryOperation fw);
1445 explicit discrete_distribution(const param_type& parm);
1446 void reset();
1447
1448 // generating functions
1449 template<class URNG> result_type operator()(URNG& g);
1450 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1451
1452 // property functions
1453 vector<double> probabilities() const;
1454
1455 param_type param() const;
1456 void param(const param_type& parm);
1457
1458 result_type min() const;
1459 result_type max() const;
1460
1461 friend bool operator==(const discrete_distribution& x,
1462 const discrete_distribution& y);
1463 friend bool operator!=(const discrete_distribution& x,
1464 const discrete_distribution& y);
1465
1466 template <class charT, class traits>
1467 friend
1468 basic_ostream<charT, traits>&
1469 operator<<(basic_ostream<charT, traits>& os,
1470 const discrete_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001471
Howard Hinnant551d8e42010-05-19 01:53:57 +00001472 template <class charT, class traits>
1473 friend
1474 basic_istream<charT, traits>&
1475 operator>>(basic_istream<charT, traits>& is,
1476 discrete_distribution& x);
1477};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template<class RealType = double>
Howard Hinnantd6d11712010-05-20 15:11:46 +00001480class piecewise_constant_distribution
1481{
1482 // types
1483 typedef RealType result_type;
1484
1485 class param_type
1486 {
1487 public:
1488 typedef piecewise_constant_distribution distribution_type;
1489
1490 param_type();
1491 template<class InputIteratorB, class InputIteratorW>
1492 param_type(InputIteratorB firstB, InputIteratorB lastB,
1493 InputIteratorW firstW);
1494 template<class UnaryOperation>
1495 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1496 template<class UnaryOperation>
1497 param_type(size_t nw, result_type xmin, result_type xmax,
1498 UnaryOperation fw);
1499
1500 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001501 vector<result_type> densities() const;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001502
1503 friend bool operator==(const param_type& x, const param_type& y);
1504 friend bool operator!=(const param_type& x, const param_type& y);
1505 };
1506
1507 // constructor and reset functions
1508 piecewise_constant_distribution();
1509 template<class InputIteratorB, class InputIteratorW>
1510 piecewise_constant_distribution(InputIteratorB firstB,
1511 InputIteratorB lastB,
1512 InputIteratorW firstW);
1513 template<class UnaryOperation>
1514 piecewise_constant_distribution(initializer_list<result_type> bl,
1515 UnaryOperation fw);
1516 template<class UnaryOperation>
1517 piecewise_constant_distribution(size_t nw, result_type xmin,
1518 result_type xmax, UnaryOperation fw);
1519 explicit piecewise_constant_distribution(const param_type& parm);
1520 void reset();
1521
1522 // generating functions
1523 template<class URNG> result_type operator()(URNG& g);
1524 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1525
1526 // property functions
1527 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001528 vector<result_type> densities() const;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001529
1530 param_type param() const;
1531 void param(const param_type& parm);
1532
1533 result_type min() const;
1534 result_type max() const;
1535
1536 friend bool operator==(const piecewise_constant_distribution& x,
1537 const piecewise_constant_distribution& y);
1538 friend bool operator!=(const piecewise_constant_distribution& x,
1539 const piecewise_constant_distribution& y);
1540
1541 template <class charT, class traits>
1542 friend
1543 basic_ostream<charT, traits>&
1544 operator<<(basic_ostream<charT, traits>& os,
1545 const piecewise_constant_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001546
Howard Hinnantd6d11712010-05-20 15:11:46 +00001547 template <class charT, class traits>
1548 friend
1549 basic_istream<charT, traits>&
1550 operator>>(basic_istream<charT, traits>& is,
1551 piecewise_constant_distribution& x);
1552};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553
1554template<class RealType = double>
Howard Hinnant54305402010-05-25 00:27:34 +00001555class piecewise_linear_distribution
1556{
1557 // types
1558 typedef RealType result_type;
1559
1560 class param_type
1561 {
1562 public:
1563 typedef piecewise_linear_distribution distribution_type;
1564
1565 param_type();
1566 template<class InputIteratorB, class InputIteratorW>
1567 param_type(InputIteratorB firstB, InputIteratorB lastB,
1568 InputIteratorW firstW);
1569 template<class UnaryOperation>
1570 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1571 template<class UnaryOperation>
1572 param_type(size_t nw, result_type xmin, result_type xmax,
1573 UnaryOperation fw);
1574
1575 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001576 vector<result_type> densities() const;
Howard Hinnant54305402010-05-25 00:27:34 +00001577
1578 friend bool operator==(const param_type& x, const param_type& y);
1579 friend bool operator!=(const param_type& x, const param_type& y);
1580 };
1581
1582 // constructor and reset functions
1583 piecewise_linear_distribution();
1584 template<class InputIteratorB, class InputIteratorW>
1585 piecewise_linear_distribution(InputIteratorB firstB,
1586 InputIteratorB lastB,
1587 InputIteratorW firstW);
Howard Hinnant324bb032010-08-22 00:02:43 +00001588
Howard Hinnant54305402010-05-25 00:27:34 +00001589 template<class UnaryOperation>
1590 piecewise_linear_distribution(initializer_list<result_type> bl,
1591 UnaryOperation fw);
1592
1593 template<class UnaryOperation>
1594 piecewise_linear_distribution(size_t nw, result_type xmin,
1595 result_type xmax, UnaryOperation fw);
1596
1597 explicit piecewise_linear_distribution(const param_type& parm);
1598 void reset();
1599
1600 // generating functions
1601 template<class URNG> result_type operator()(URNG& g);
1602 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1603
1604 // property functions
1605 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001606 vector<result_type> densities() const;
Howard Hinnant54305402010-05-25 00:27:34 +00001607
1608 param_type param() const;
1609 void param(const param_type& parm);
1610
1611 result_type min() const;
1612 result_type max() const;
1613
1614 friend bool operator==(const piecewise_linear_distribution& x,
1615 const piecewise_linear_distribution& y);
1616 friend bool operator!=(const piecewise_linear_distribution& x,
1617 const piecewise_linear_distribution& y);
1618
1619 template <class charT, class traits>
1620 friend
1621 basic_ostream<charT, traits>&
1622 operator<<(basic_ostream<charT, traits>& os,
1623 const piecewise_linear_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001624
Howard Hinnant54305402010-05-25 00:27:34 +00001625 template <class charT, class traits>
1626 friend
1627 basic_istream<charT, traits>&
1628 operator>>(basic_istream<charT, traits>& is,
1629 piecewise_linear_distribution& x);
1630};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631
1632} // std
1633*/
1634
1635#include <__config>
1636#include <cstddef>
1637#include <type_traits>
1638#include <initializer_list>
1639#include <cstdint>
1640#include <limits>
1641#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001642#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643#include <vector>
1644#include <string>
1645#include <istream>
1646#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001647#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648
Howard Hinnant66c6f972011-11-29 16:45:27 +00001649#include <__undef_min_max>
1650
Howard Hinnant08e17472011-10-17 20:05:10 +00001651#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +00001653#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654
1655_LIBCPP_BEGIN_NAMESPACE_STD
1656
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001657// __is_seed_sequence
1658
1659template <class _Sseq, class _Engine>
1660struct __is_seed_sequence
1661{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001662 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001663 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1664 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1665};
1666
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667// linear_congruential_engine
1668
1669template <unsigned long long __a, unsigned long long __c,
Howard Hinnant99968442011-11-29 18:15:50 +00001670 unsigned long long __m, unsigned long long _Mp,
1671 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672struct __lce_ta;
1673
1674// 64
1675
1676template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1677struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1678{
1679 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 static result_type next(result_type __x)
1682 {
1683 // Schrage's algorithm
1684 const result_type __q = __m / __a;
1685 const result_type __r = __m % __a;
1686 const result_type __t0 = __a * (__x % __q);
1687 const result_type __t1 = __r * (__x / __q);
1688 __x = __t0 + (__t0 < __t1) * __m - __t1;
1689 __x += __c - (__x >= __m - __c) * __m;
1690 return __x;
1691 }
1692};
1693
1694template <unsigned long long __a, unsigned long long __m>
1695struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1696{
1697 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 static result_type next(result_type __x)
1700 {
1701 // Schrage's algorithm
1702 const result_type __q = __m / __a;
1703 const result_type __r = __m % __a;
1704 const result_type __t0 = __a * (__x % __q);
1705 const result_type __t1 = __r * (__x / __q);
1706 __x = __t0 + (__t0 < __t1) * __m - __t1;
1707 return __x;
1708 }
1709};
1710
1711template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1712struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1713{
1714 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 static result_type next(result_type __x)
1717 {
1718 return (__a * __x + __c) % __m;
1719 }
1720};
1721
1722template <unsigned long long __a, unsigned long long __c>
1723struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1724{
1725 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 static result_type next(result_type __x)
1728 {
1729 return __a * __x + __c;
1730 }
1731};
1732
1733// 32
1734
Howard Hinnant99968442011-11-29 18:15:50 +00001735template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1736struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737{
1738 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 static result_type next(result_type __x)
1741 {
Howard Hinnant99968442011-11-29 18:15:50 +00001742 const result_type __a = static_cast<result_type>(_Ap);
1743 const result_type __c = static_cast<result_type>(_Cp);
1744 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 // Schrage's algorithm
1746 const result_type __q = __m / __a;
1747 const result_type __r = __m % __a;
1748 const result_type __t0 = __a * (__x % __q);
1749 const result_type __t1 = __r * (__x / __q);
1750 __x = __t0 + (__t0 < __t1) * __m - __t1;
1751 __x += __c - (__x >= __m - __c) * __m;
1752 return __x;
1753 }
1754};
1755
Howard Hinnant99968442011-11-29 18:15:50 +00001756template <unsigned long long _Ap, unsigned long long _Mp>
1757struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758{
1759 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 static result_type next(result_type __x)
1762 {
Howard Hinnant99968442011-11-29 18:15:50 +00001763 const result_type __a = static_cast<result_type>(_Ap);
1764 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 // Schrage's algorithm
1766 const result_type __q = __m / __a;
1767 const result_type __r = __m % __a;
1768 const result_type __t0 = __a * (__x % __q);
1769 const result_type __t1 = __r * (__x / __q);
1770 __x = __t0 + (__t0 < __t1) * __m - __t1;
1771 return __x;
1772 }
1773};
1774
Howard Hinnant99968442011-11-29 18:15:50 +00001775template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1776struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777{
1778 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780 static result_type next(result_type __x)
1781 {
Howard Hinnant99968442011-11-29 18:15:50 +00001782 const result_type __a = static_cast<result_type>(_Ap);
1783 const result_type __c = static_cast<result_type>(_Cp);
1784 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 return (__a * __x + __c) % __m;
1786 }
1787};
1788
Howard Hinnant99968442011-11-29 18:15:50 +00001789template <unsigned long long _Ap, unsigned long long _Cp>
1790struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791{
1792 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 static result_type next(result_type __x)
1795 {
Howard Hinnant99968442011-11-29 18:15:50 +00001796 const result_type __a = static_cast<result_type>(_Ap);
1797 const result_type __c = static_cast<result_type>(_Cp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 return __a * __x + __c;
1799 }
1800};
1801
1802// 16
1803
1804template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1805struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1806{
1807 typedef unsigned short result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 static result_type next(result_type __x)
1810 {
1811 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1812 }
1813};
1814
1815template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1816class linear_congruential_engine;
1817
1818template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001819 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820basic_ostream<_CharT, _Traits>&
1821operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001822 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823
1824template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001825 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826basic_istream<_CharT, _Traits>&
1827operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001828 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829
1830template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001831class _LIBCPP_VISIBLE linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832{
1833public:
1834 // types
1835 typedef _UIntType result_type;
1836
1837private:
1838 result_type __x_;
1839
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001840 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841
1842 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1843 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1844public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001845 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1846 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1848
1849 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001850 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1851 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1852 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001854 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001856 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1857 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858
1859 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 explicit linear_congruential_engine(result_type __s = default_seed)
1862 {seed(__s);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00001863 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001865 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001866 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 void seed(result_type __s = default_seed)
1870 {seed(integral_constant<bool, __m == 0>(),
1871 integral_constant<bool, __c == 0>(), __s);}
1872 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 typename enable_if
1875 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001876 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 void
1878 >::type
1879 seed(_Sseq& __q)
1880 {__seed(__q, integral_constant<unsigned,
1881 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1882 : (__m-1) / 0x100000000ull)>());}
1883
1884 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 result_type operator()()
Howard Hinnant99968442011-11-29 18:15:50 +00001887 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1890
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001891 friend _LIBCPP_INLINE_VISIBILITY
1892 bool operator==(const linear_congruential_engine& __x,
1893 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894 {return __x.__x_ == __y.__x_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001895 friend _LIBCPP_INLINE_VISIBILITY
1896 bool operator!=(const linear_congruential_engine& __x,
1897 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 {return !(__x == __y);}
1899
1900private:
1901
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1908 1 : __s % __m;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1911
1912 template<class _Sseq>
1913 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1914 template<class _Sseq>
1915 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1916
1917 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001918 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 friend
1920 basic_ostream<_CharT, _Traits>&
1921 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001922 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001923
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001925 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 friend
1927 basic_istream<_CharT, _Traits>&
1928 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001929 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930};
1931
1932template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1933template<class _Sseq>
1934void
1935linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1936 integral_constant<unsigned, 1>)
1937{
1938 const unsigned __k = 1;
1939 uint32_t __ar[__k+3];
1940 __q.generate(__ar, __ar + __k + 3);
1941 result_type __s = static_cast<result_type>(__ar[3] % __m);
1942 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1943}
1944
1945template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1946template<class _Sseq>
1947void
1948linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1949 integral_constant<unsigned, 2>)
1950{
1951 const unsigned __k = 2;
1952 uint32_t __ar[__k+3];
1953 __q.generate(__ar, __ar + __k + 3);
1954 result_type __s = static_cast<result_type>((__ar[3] +
1955 (uint64_t)__ar[4] << 32) % __m);
1956 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1957}
1958
1959template <class _CharT, class _Traits>
1960class __save_flags
1961{
1962 typedef basic_ios<_CharT, _Traits> __stream_type;
1963 typedef typename __stream_type::fmtflags fmtflags;
1964
1965 __stream_type& __stream_;
1966 fmtflags __fmtflags_;
1967 _CharT __fill_;
1968
1969 __save_flags(const __save_flags&);
1970 __save_flags& operator=(const __save_flags&);
1971public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973 explicit __save_flags(__stream_type& __stream)
1974 : __stream_(__stream),
1975 __fmtflags_(__stream.flags()),
1976 __fill_(__stream.fill())
1977 {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 ~__save_flags()
1980 {
1981 __stream_.flags(__fmtflags_);
1982 __stream_.fill(__fill_);
1983 }
1984};
1985
1986template <class _CharT, class _Traits,
1987 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001988inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989basic_ostream<_CharT, _Traits>&
1990operator<<(basic_ostream<_CharT, _Traits>& __os,
1991 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1992{
1993 __save_flags<_CharT, _Traits> _(__os);
1994 __os.flags(ios_base::dec | ios_base::left);
1995 __os.fill(__os.widen(' '));
1996 return __os << __x.__x_;
1997}
1998
1999template <class _CharT, class _Traits,
2000 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2001basic_istream<_CharT, _Traits>&
2002operator>>(basic_istream<_CharT, _Traits>& __is,
2003 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2004{
2005 __save_flags<_CharT, _Traits> _(__is);
2006 __is.flags(ios_base::dec | ios_base::skipws);
2007 _UIntType __t;
2008 __is >> __t;
2009 if (!__is.fail())
2010 __x.__x_ = __t;
2011 return __is;
2012}
2013
2014typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2015 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2017 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00002018typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019// mersenne_twister_engine
2020
2021template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2022 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2023 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2024class mersenne_twister_engine;
2025
Howard Hinnant99968442011-11-29 18:15:50 +00002026template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2027 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2028 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029bool
Howard Hinnant99968442011-11-29 18:15:50 +00002030operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2031 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2032 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2033 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034
Howard Hinnant99968442011-11-29 18:15:50 +00002035template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2036 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2037 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038bool
Howard Hinnant99968442011-11-29 18:15:50 +00002039operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2040 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2041 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2042 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043
2044template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002045 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2046 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2047 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048basic_ostream<_CharT, _Traits>&
2049operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002050 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2051 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052
2053template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002054 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2055 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2056 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057basic_istream<_CharT, _Traits>&
2058operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002059 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2060 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061
2062template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2063 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2064 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002065class _LIBCPP_VISIBLE mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066{
2067public:
2068 // types
2069 typedef _UIntType result_type;
2070
2071private:
2072 result_type __x_[__n];
2073 size_t __i_;
2074
2075 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2076 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002077 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2079 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2080 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2081 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2082 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2083 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2084 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2085public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002086 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2087 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2088 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2090 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2091 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2092 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2093 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2094 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2095
2096 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002097 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2098 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2099 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2100 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2101 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2102 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2103 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2104 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2105 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2106 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2107 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2108 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2109 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002111 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002113 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2114 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115
2116 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 explicit mersenne_twister_engine(result_type __sd = default_seed)
2119 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002120 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002122 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002123 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 {seed(__q);}
2125 void seed(result_type __sd = default_seed);
2126 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 typename enable_if
2129 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002130 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 void
2132 >::type
2133 seed(_Sseq& __q)
2134 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2135
2136 // generating functions
2137 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2140
Howard Hinnant99968442011-11-29 18:15:50 +00002141 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2142 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2143 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 friend
2145 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002146 operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2147 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2148 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2149 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002150
Howard Hinnant99968442011-11-29 18:15:50 +00002151 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2152 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2153 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 friend
2155 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002156 operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2157 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2158 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2159 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160
2161 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002162 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2163 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2164 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 friend
2166 basic_ostream<_CharT, _Traits>&
2167 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002168 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2169 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170
2171 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002172 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2173 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2174 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 friend
2176 basic_istream<_CharT, _Traits>&
2177 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002178 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2179 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180private:
2181
2182 template<class _Sseq>
2183 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2184 template<class _Sseq>
2185 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2186
2187 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189 static
2190 typename enable_if
2191 <
2192 __count < __w,
2193 result_type
2194 >::type
2195 __lshift(result_type __x) {return (__x << __count) & _Max;}
2196
2197 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 static
2200 typename enable_if
2201 <
2202 (__count >= __w),
2203 result_type
2204 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002205 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206
2207 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209 static
2210 typename enable_if
2211 <
2212 __count < _Dt,
2213 result_type
2214 >::type
2215 __rshift(result_type __x) {return __x >> __count;}
2216
2217 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 static
2220 typename enable_if
2221 <
2222 (__count >= _Dt),
2223 result_type
2224 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002225 __rshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226};
2227
2228template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2229 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2230 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2231void
2232mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2233 __t, __c, __l, __f>::seed(result_type __sd)
2234{ // __w >= 2
2235 __x_[0] = __sd & _Max;
2236 for (size_t __i = 1; __i < __n; ++__i)
2237 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2238 __i_ = 0;
2239}
2240
2241template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2242 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2243 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2244template<class _Sseq>
2245void
2246mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2247 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2248{
2249 const unsigned __k = 1;
2250 uint32_t __ar[__n * __k];
2251 __q.generate(__ar, __ar + __n * __k);
2252 for (size_t __i = 0; __i < __n; ++__i)
2253 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2254 const result_type __mask = __r == _Dt ? result_type(~0) :
2255 (result_type(1) << __r) - result_type(1);
2256 __i_ = 0;
2257 if ((__x_[0] & ~__mask) == 0)
2258 {
2259 for (size_t __i = 1; __i < __n; ++__i)
2260 if (__x_[__i] != 0)
2261 return;
2262 __x_[0] = _Max;
2263 }
2264}
2265
2266template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2267 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2268 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2269template<class _Sseq>
2270void
2271mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2272 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2273{
2274 const unsigned __k = 2;
2275 uint32_t __ar[__n * __k];
2276 __q.generate(__ar, __ar + __n * __k);
2277 for (size_t __i = 0; __i < __n; ++__i)
2278 __x_[__i] = static_cast<result_type>(
2279 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2280 const result_type __mask = __r == _Dt ? result_type(~0) :
2281 (result_type(1) << __r) - result_type(1);
2282 __i_ = 0;
2283 if ((__x_[0] & ~__mask) == 0)
2284 {
2285 for (size_t __i = 1; __i < __n; ++__i)
2286 if (__x_[__i] != 0)
2287 return;
2288 __x_[0] = _Max;
2289 }
2290}
2291
2292template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2294 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2295_UIntType
2296mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2297 __t, __c, __l, __f>::operator()()
2298{
2299 const size_t __j = (__i_ + 1) % __n;
2300 const result_type __mask = __r == _Dt ? result_type(~0) :
2301 (result_type(1) << __r) - result_type(1);
Howard Hinnant99968442011-11-29 18:15:50 +00002302 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 const size_t __k = (__i_ + __m) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00002304 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2306 __i_ = __j;
2307 __z ^= __lshift<__s>(__z) & __b;
2308 __z ^= __lshift<__t>(__z) & __c;
2309 return __z ^ __rshift<__l>(__z);
2310}
2311
Howard Hinnant99968442011-11-29 18:15:50 +00002312template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2313 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2314 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315bool
Howard Hinnant99968442011-11-29 18:15:50 +00002316operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2317 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2318 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2319 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320{
2321 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002322 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 if (__x.__i_ == 0 || __y.__i_ == 0)
2324 {
Howard Hinnant99968442011-11-29 18:15:50 +00002325 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002326 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327 __y.__x_ + __y.__i_))
2328 return false;
2329 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002330 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2331 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 }
2333 if (__x.__i_ < __y.__i_)
2334 {
Howard Hinnant99968442011-11-29 18:15:50 +00002335 size_t __j = _Np - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002336 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 __y.__x_ + __y.__i_))
2338 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002339 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340 __y.__x_))
2341 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002342 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002343 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 }
Howard Hinnant99968442011-11-29 18:15:50 +00002345 size_t __j = _Np - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002346 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 __x.__x_ + __x.__i_))
2348 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002349 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 __x.__x_))
2351 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002352 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002353 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354}
2355
Howard Hinnant99968442011-11-29 18:15:50 +00002356template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2357 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2358 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002359inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360bool
Howard Hinnant99968442011-11-29 18:15:50 +00002361operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2362 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2363 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2364 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365{
2366 return !(__x == __y);
2367}
2368
2369template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002370 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2371 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2372 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373basic_ostream<_CharT, _Traits>&
2374operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002375 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2376 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377{
2378 __save_flags<_CharT, _Traits> _(__os);
2379 __os.flags(ios_base::dec | ios_base::left);
2380 _CharT __sp = __os.widen(' ');
2381 __os.fill(__sp);
2382 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002383 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 __os << __sp << __x.__x_[__j];
2385 for (size_t __j = 0; __j < __x.__i_; ++__j)
2386 __os << __sp << __x.__x_[__j];
2387 return __os;
2388}
2389
2390template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002391 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2392 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2393 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394basic_istream<_CharT, _Traits>&
2395operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002396 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2397 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398{
2399 __save_flags<_CharT, _Traits> _(__is);
2400 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002401 _UI __t[_Np];
2402 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403 __is >> __t[__i];
2404 if (!__is.fail())
2405 {
Howard Hinnant99968442011-11-29 18:15:50 +00002406 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 __x.__x_[__i] = __t[__i];
2408 __x.__i_ = 0;
2409 }
2410 return __is;
2411}
2412
2413typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2414 0x9908b0df, 11, 0xffffffff,
2415 7, 0x9d2c5680,
2416 15, 0xefc60000,
2417 18, 1812433253> mt19937;
2418typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2419 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2420 17, 0x71d67fffeda60000ULL,
2421 37, 0xfff7eee000000000ULL,
2422 43, 6364136223846793005ULL> mt19937_64;
2423
2424// subtract_with_carry_engine
2425
2426template<class _UIntType, size_t __w, size_t __s, size_t __r>
2427class subtract_with_carry_engine;
2428
Howard Hinnant99968442011-11-29 18:15:50 +00002429template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430bool
2431operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002432 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2433 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434
Howard Hinnant99968442011-11-29 18:15:50 +00002435template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436bool
2437operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002438 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2439 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440
2441template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002442 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443basic_ostream<_CharT, _Traits>&
2444operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002445 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446
2447template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002448 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449basic_istream<_CharT, _Traits>&
2450operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002451 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452
2453template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002454class _LIBCPP_VISIBLE subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455{
2456public:
2457 // types
2458 typedef _UIntType result_type;
2459
2460private:
2461 result_type __x_[__r];
2462 result_type __c_;
2463 size_t __i_;
2464
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002465 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002466 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2467 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2468 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2469 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2470public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002471 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2472 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2473 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2475
2476 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002477 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2478 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2479 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002481 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002483 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2484 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002485
2486 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2489 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002490 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002492 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002493 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496 void seed(result_type __sd = default_seed)
2497 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2498 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002500 typename enable_if
2501 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002502 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 void
2504 >::type
2505 seed(_Sseq& __q)
2506 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2507
2508 // generating functions
2509 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2512
Howard Hinnant99968442011-11-29 18:15:50 +00002513 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514 friend
2515 bool
2516 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002517 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2518 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002519
Howard Hinnant99968442011-11-29 18:15:50 +00002520 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002521 friend
2522 bool
2523 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002524 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2525 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002526
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002528 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 friend
2530 basic_ostream<_CharT, _Traits>&
2531 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002532 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002535 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 friend
2537 basic_istream<_CharT, _Traits>&
2538 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002539 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540
2541private:
2542
2543 void seed(result_type __sd, integral_constant<unsigned, 1>);
2544 void seed(result_type __sd, integral_constant<unsigned, 2>);
2545 template<class _Sseq>
2546 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2547 template<class _Sseq>
2548 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2549};
2550
2551template<class _UIntType, size_t __w, size_t __s, size_t __r>
2552void
2553subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2554 integral_constant<unsigned, 1>)
2555{
2556 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2557 __e(__sd == 0u ? default_seed : __sd);
2558 for (size_t __i = 0; __i < __r; ++__i)
2559 __x_[__i] = static_cast<result_type>(__e() & _Max);
2560 __c_ = __x_[__r-1] == 0;
2561 __i_ = 0;
2562}
2563
2564template<class _UIntType, size_t __w, size_t __s, size_t __r>
2565void
2566subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2567 integral_constant<unsigned, 2>)
2568{
2569 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2570 __e(__sd == 0u ? default_seed : __sd);
2571 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002572 {
2573 result_type __e0 = __e();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 __x_[__i] = static_cast<result_type>(
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002575 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2576 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 __c_ = __x_[__r-1] == 0;
2578 __i_ = 0;
2579}
2580
2581template<class _UIntType, size_t __w, size_t __s, size_t __r>
2582template<class _Sseq>
2583void
2584subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2585 integral_constant<unsigned, 1>)
2586{
2587 const unsigned __k = 1;
2588 uint32_t __ar[__r * __k];
2589 __q.generate(__ar, __ar + __r * __k);
2590 for (size_t __i = 0; __i < __r; ++__i)
2591 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2592 __c_ = __x_[__r-1] == 0;
2593 __i_ = 0;
2594}
2595
2596template<class _UIntType, size_t __w, size_t __s, size_t __r>
2597template<class _Sseq>
2598void
2599subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2600 integral_constant<unsigned, 2>)
2601{
2602 const unsigned __k = 2;
2603 uint32_t __ar[__r * __k];
2604 __q.generate(__ar, __ar + __r * __k);
2605 for (size_t __i = 0; __i < __r; ++__i)
2606 __x_[__i] = static_cast<result_type>(
2607 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2608 __c_ = __x_[__r-1] == 0;
2609 __i_ = 0;
2610}
2611
2612template<class _UIntType, size_t __w, size_t __s, size_t __r>
2613_UIntType
2614subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2615{
2616 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2617 result_type& __xr = __x_[__i_];
2618 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2619 __xr = (__xs - __xr - __c_) & _Max;
2620 __c_ = __new_c;
2621 __i_ = (__i_ + 1) % __r;
2622 return __xr;
2623}
2624
Howard Hinnant99968442011-11-29 18:15:50 +00002625template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626bool
2627operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002628 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2629 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630{
2631 if (__x.__c_ != __y.__c_)
2632 return false;
2633 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002634 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635 if (__x.__i_ == 0 || __y.__i_ == 0)
2636 {
Howard Hinnant99968442011-11-29 18:15:50 +00002637 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002638 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 __y.__x_ + __y.__i_))
2640 return false;
2641 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002642 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2643 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 }
2645 if (__x.__i_ < __y.__i_)
2646 {
Howard Hinnant99968442011-11-29 18:15:50 +00002647 size_t __j = _Rp - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002648 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 __y.__x_ + __y.__i_))
2650 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002651 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 __y.__x_))
2653 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002654 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002655 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 }
Howard Hinnant99968442011-11-29 18:15:50 +00002657 size_t __j = _Rp - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002658 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 __x.__x_ + __x.__i_))
2660 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002661 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 __x.__x_))
2663 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002664 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002665 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666}
2667
Howard Hinnant99968442011-11-29 18:15:50 +00002668template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002669inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670bool
2671operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002672 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2673 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002674{
2675 return !(__x == __y);
2676}
2677
2678template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002679 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680basic_ostream<_CharT, _Traits>&
2681operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002682 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683{
2684 __save_flags<_CharT, _Traits> _(__os);
2685 __os.flags(ios_base::dec | ios_base::left);
2686 _CharT __sp = __os.widen(' ');
2687 __os.fill(__sp);
2688 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002689 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 __os << __sp << __x.__x_[__j];
2691 for (size_t __j = 0; __j < __x.__i_; ++__j)
2692 __os << __sp << __x.__x_[__j];
2693 __os << __sp << __x.__c_;
2694 return __os;
2695}
2696
2697template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002698 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699basic_istream<_CharT, _Traits>&
2700operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002701 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702{
2703 __save_flags<_CharT, _Traits> _(__is);
2704 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002705 _UI __t[_Rp+1];
2706 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 __is >> __t[__i];
2708 if (!__is.fail())
2709 {
Howard Hinnant99968442011-11-29 18:15:50 +00002710 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 __x.__x_[__i] = __t[__i];
Howard Hinnant99968442011-11-29 18:15:50 +00002712 __x.__c_ = __t[_Rp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 __x.__i_ = 0;
2714 }
2715 return __is;
2716}
2717
2718typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2719typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2720
2721// discard_block_engine
2722
2723template<class _Engine, size_t __p, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002724class _LIBCPP_VISIBLE discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725{
2726 _Engine __e_;
2727 int __n_;
2728
2729 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2730 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2731public:
2732 // types
2733 typedef typename _Engine::result_type result_type;
2734
2735 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002736 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2737 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002739#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 static const result_type _Min = _Engine::_Min;
2741 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002742#else
2743 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2744 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2745#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002748 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002750 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751
2752 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002756 explicit discard_block_engine(const _Engine& __e)
2757 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002760 explicit discard_block_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002761 : __e_(_VSTD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002762#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002765 template<class _Sseq>
2766 _LIBCPP_INLINE_VISIBILITY
2767 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002768 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002769 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002775 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002777 typename enable_if
2778 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002779 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002780 void
2781 >::type
2782 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783
2784 // generating functions
2785 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2788
2789 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00002791 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792
Howard Hinnant99968442011-11-29 18:15:50 +00002793 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002794 friend
2795 bool
2796 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002797 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2798 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002799
Howard Hinnant99968442011-11-29 18:15:50 +00002800 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801 friend
2802 bool
2803 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002804 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2805 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002806
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002808 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809 friend
2810 basic_ostream<_CharT, _Traits>&
2811 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002812 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002813
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002815 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816 friend
2817 basic_istream<_CharT, _Traits>&
2818 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002819 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820};
2821
2822template<class _Engine, size_t __p, size_t __r>
2823typename discard_block_engine<_Engine, __p, __r>::result_type
2824discard_block_engine<_Engine, __p, __r>::operator()()
2825{
2826 if (__n_ >= __r)
2827 {
2828 __e_.discard(__p - __r);
2829 __n_ = 0;
2830 }
2831 ++__n_;
2832 return __e_();
2833}
2834
Howard Hinnant99968442011-11-29 18:15:50 +00002835template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002836inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837bool
Howard Hinnant99968442011-11-29 18:15:50 +00002838operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2839 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840{
2841 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2842}
2843
Howard Hinnant99968442011-11-29 18:15:50 +00002844template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002845inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846bool
Howard Hinnant99968442011-11-29 18:15:50 +00002847operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2848 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002849{
2850 return !(__x == __y);
2851}
2852
2853template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002854 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855basic_ostream<_CharT, _Traits>&
2856operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002857 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858{
2859 __save_flags<_CharT, _Traits> _(__os);
2860 __os.flags(ios_base::dec | ios_base::left);
2861 _CharT __sp = __os.widen(' ');
2862 __os.fill(__sp);
2863 return __os << __x.__e_ << __sp << __x.__n_;
2864}
2865
2866template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002867 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868basic_istream<_CharT, _Traits>&
2869operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002870 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871{
2872 __save_flags<_CharT, _Traits> _(__is);
2873 __is.flags(ios_base::dec | ios_base::skipws);
2874 _Eng __e;
2875 int __n;
2876 __is >> __e >> __n;
2877 if (!__is.fail())
2878 {
2879 __x.__e_ = __e;
2880 __x.__n_ = __n;
2881 }
2882 return __is;
2883}
2884
2885typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2886typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2887
2888// independent_bits_engine
2889
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002891class _LIBCPP_VISIBLE independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892{
Howard Hinnant99968442011-11-29 18:15:50 +00002893 template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894 class __get_n
2895 {
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002896 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits;
2897 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
2898 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
2899 static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002900 public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002901 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902 };
2903public:
2904 // types
2905 typedef _UIntType result_type;
2906
2907private:
2908 _Engine __e_;
2909
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002910 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2912 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2913
2914 typedef typename _Engine::result_type _Engine_result_type;
2915 typedef typename conditional
2916 <
2917 sizeof(_Engine_result_type) <= sizeof(result_type),
2918 result_type,
2919 _Engine_result_type
2920 >::type _Working_result_type;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002921#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00002922 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002923 + _Working_result_type(1);
2924#else
2925 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2926 + _Working_result_type(1);
2927#endif
2928 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2929 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
2930 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
2931 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
2932 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2933 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2934 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2935 (_Rp >> __w0) << __w0;
2936 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2937 (_Rp >> (__w0+1)) << (__w0+1);
2938 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002939 _Engine_result_type(~0) >> (_EDt - __w0) :
2940 _Engine_result_type(0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002941 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2943 _Engine_result_type(~0);
2944public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002945 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2946 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2947 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2949
2950 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002952 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002954 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002955
2956 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002958 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002960 explicit independent_bits_engine(const _Engine& __e)
2961 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002962#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002964 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002965 : __e_(_VSTD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002966#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002969 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002971 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002972 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002973 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002979 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002981 typename enable_if
2982 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002983 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002984 void
2985 >::type
2986 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002987
2988 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002990 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2993
2994 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00002996 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002997
Howard Hinnant99968442011-11-29 18:15:50 +00002998 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002999 friend
3000 bool
3001 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003002 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3003 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003004
Howard Hinnant99968442011-11-29 18:15:50 +00003005 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003006 friend
3007 bool
3008 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003009 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3010 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003011
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003013 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014 friend
3015 basic_ostream<_CharT, _Traits>&
3016 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003017 const independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003018
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003019 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003020 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021 friend
3022 basic_istream<_CharT, _Traits>&
3023 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003024 independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003025
3026private:
3027 result_type __eval(false_type);
3028 result_type __eval(true_type);
3029
3030 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003032 static
3033 typename enable_if
3034 <
3035 __count < _Dt,
3036 result_type
3037 >::type
3038 __lshift(result_type __x) {return __x << __count;}
3039
3040 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003042 static
3043 typename enable_if
3044 <
3045 (__count >= _Dt),
3046 result_type
3047 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00003048 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003049};
3050
3051template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003053_UIntType
3054independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3055{
3056 return static_cast<result_type>(__e_() & __mask0);
3057}
3058
3059template<class _Engine, size_t __w, class _UIntType>
3060_UIntType
3061independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3062{
Howard Hinnant99968442011-11-29 18:15:50 +00003063 result_type _Sp = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064 for (size_t __k = 0; __k < __n0; ++__k)
3065 {
3066 _Engine_result_type __u;
3067 do
3068 {
3069 __u = __e_() - _Engine::min();
3070 } while (__u >= __y0);
Howard Hinnant99968442011-11-29 18:15:50 +00003071 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072 }
3073 for (size_t __k = __n0; __k < __n; ++__k)
3074 {
3075 _Engine_result_type __u;
3076 do
3077 {
3078 __u = __e_() - _Engine::min();
3079 } while (__u >= __y1);
Howard Hinnant99968442011-11-29 18:15:50 +00003080 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003081 }
Howard Hinnant99968442011-11-29 18:15:50 +00003082 return _Sp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003083}
3084
Howard Hinnant99968442011-11-29 18:15:50 +00003085template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003086inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003087bool
3088operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003089 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3090 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003091{
3092 return __x.base() == __y.base();
3093}
3094
Howard Hinnant99968442011-11-29 18:15:50 +00003095template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003097bool
3098operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003099 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3100 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101{
3102 return !(__x == __y);
3103}
3104
3105template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003106 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107basic_ostream<_CharT, _Traits>&
3108operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003109 const independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003110{
3111 return __os << __x.base();
3112}
3113
3114template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003115 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003116basic_istream<_CharT, _Traits>&
3117operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003118 independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119{
3120 _Eng __e;
3121 __is >> __e;
3122 if (!__is.fail())
3123 __x.__e_ = __e;
3124 return __is;
3125}
3126
3127// shuffle_order_engine
3128
3129template <uint64_t _Xp, uint64_t _Yp>
3130struct __ugcd
3131{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003132 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003133};
3134
3135template <uint64_t _Xp>
3136struct __ugcd<_Xp, 0>
3137{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003138 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139};
3140
Howard Hinnant99968442011-11-29 18:15:50 +00003141template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003142class __uratio
3143{
Howard Hinnant99968442011-11-29 18:15:50 +00003144 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003145 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003146public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003147 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3148 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149
3150 typedef __uratio<num, den> type;
3151};
3152
3153template<class _Engine, size_t __k>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003154class _LIBCPP_VISIBLE shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155{
3156 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3157public:
3158 // types
3159 typedef typename _Engine::result_type result_type;
3160
3161private:
3162 _Engine __e_;
3163 result_type _V_[__k];
3164 result_type _Y_;
3165
3166public:
3167 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003168 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003169
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003170#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171 static const result_type _Min = _Engine::_Min;
3172 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003173#else
3174 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3175 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3176#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003179 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003181 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003183 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184
3185 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003189 explicit shuffle_order_engine(const _Engine& __e)
3190 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003191#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003193 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003194 : __e_(_VSTD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003195#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003198 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003200 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003201 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003202 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003203 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003207 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003208 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003210 typename enable_if
3211 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003212 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003213 void
3214 >::type
3215 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003216
3217 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003219 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003221 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3222
3223 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00003225 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003226
3227private:
Howard Hinnant99968442011-11-29 18:15:50 +00003228 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229 friend
3230 bool
3231 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003232 const shuffle_order_engine<_Eng, _Kp>& __x,
3233 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003234
Howard Hinnant99968442011-11-29 18:15:50 +00003235 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003236 friend
3237 bool
3238 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003239 const shuffle_order_engine<_Eng, _Kp>& __x,
3240 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003241
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003242 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003243 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003244 friend
3245 basic_ostream<_CharT, _Traits>&
3246 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003247 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003248
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003250 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251 friend
3252 basic_istream<_CharT, _Traits>&
3253 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003254 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257 void __init()
3258 {
3259 for (size_t __i = 0; __i < __k; ++__i)
3260 _V_[__i] = __e_();
3261 _Y_ = __e_();
3262 }
3263
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003265 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003267 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003270 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003272 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3273
Howard Hinnant99968442011-11-29 18:15:50 +00003274 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003276 typename enable_if
3277 <
Howard Hinnant99968442011-11-29 18:15:50 +00003278 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279 result_type
3280 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003281 __eval(__uratio<_Np, _Dp>)
3282 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283
Howard Hinnant99968442011-11-29 18:15:50 +00003284 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003286 typename enable_if
3287 <
Howard Hinnant99968442011-11-29 18:15:50 +00003288 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289 result_type
3290 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003291 __eval(__uratio<_Np, _Dp>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292 {
Howard Hinnant99968442011-11-29 18:15:50 +00003293 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3294 / __uratio<_Np, _Dp>::den);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295 _Y_ = _V_[__j];
3296 _V_[__j] = __e_();
3297 return _Y_;
3298 }
3299
3300 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003302 result_type __evalf()
3303 {
Howard Hinnant99968442011-11-29 18:15:50 +00003304 const double _Fp = __d == 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003305 __n / (2. * 0x8000000000000000ull) :
3306 __n / (double)__d;
Howard Hinnant99968442011-11-29 18:15:50 +00003307 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308 _Y_ = _V_[__j];
3309 _V_[__j] = __e_();
3310 return _Y_;
3311 }
3312};
3313
Howard Hinnant99968442011-11-29 18:15:50 +00003314template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315bool
3316operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003317 const shuffle_order_engine<_Eng, _Kp>& __x,
3318 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003319{
Howard Hinnant99968442011-11-29 18:15:50 +00003320 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003321 __x.__e_ == __y.__e_;
3322}
3323
Howard Hinnant99968442011-11-29 18:15:50 +00003324template<class _Eng, size_t _Kp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003325inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003326bool
3327operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003328 const shuffle_order_engine<_Eng, _Kp>& __x,
3329 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003330{
3331 return !(__x == __y);
3332}
3333
3334template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003335 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003336basic_ostream<_CharT, _Traits>&
3337operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003338 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339{
3340 __save_flags<_CharT, _Traits> _(__os);
3341 __os.flags(ios_base::dec | ios_base::left);
3342 _CharT __sp = __os.widen(' ');
3343 __os.fill(__sp);
3344 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnant99968442011-11-29 18:15:50 +00003345 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003346 __os << __sp << __x._V_[__i];
3347 return __os << __sp << __x._Y_;
3348}
3349
3350template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003351 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352basic_istream<_CharT, _Traits>&
3353operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003354 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355{
Howard Hinnant99968442011-11-29 18:15:50 +00003356 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357 __save_flags<_CharT, _Traits> _(__is);
3358 __is.flags(ios_base::dec | ios_base::skipws);
3359 _Eng __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003360 result_type _Vp[_Kp+1];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003361 __is >> __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003362 for (size_t __i = 0; __i < _Kp+1; ++__i)
3363 __is >> _Vp[__i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364 if (!__is.fail())
3365 {
3366 __x.__e_ = __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003367 for (size_t __i = 0; __i < _Kp; ++__i)
3368 __x._V_[__i] = _Vp[__i];
3369 __x._Y_ = _Vp[_Kp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003370 }
3371 return __is;
3372}
3373
3374typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3375
3376// random_device
3377
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003378class _LIBCPP_VISIBLE random_device
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379{
3380 int __f_;
3381public:
3382 // types
3383 typedef unsigned result_type;
3384
3385 // generator characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003386 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3387 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003390 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003392 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003393
3394 // constructors
3395 explicit random_device(const string& __token = "/dev/urandom");
3396 ~random_device();
3397
3398 // generating functions
3399 result_type operator()();
3400
3401 // property functions
Howard Hinnantc83960a2012-07-20 21:44:27 +00003402 double entropy() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003403
3404private:
3405 // no copy functions
3406 random_device(const random_device&); // = delete;
3407 random_device& operator=(const random_device&); // = delete;
3408};
3409
3410// seed_seq
3411
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003412class _LIBCPP_VISIBLE seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413{
3414public:
3415 // types
3416 typedef uint32_t result_type;
3417
3418private:
3419 vector<result_type> __v_;
3420
3421 template<class _InputIterator>
3422 void init(_InputIterator __first, _InputIterator __last);
3423public:
3424 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003426 seed_seq() {}
Howard Hinnante3e32912011-08-12 21:56:02 +00003427#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003428 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003430 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00003431#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:43 +00003432
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003435 seed_seq(_InputIterator __first, _InputIterator __last)
3436 {init(__first, __last);}
3437
3438 // generating functions
3439 template<class _RandomAccessIterator>
3440 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3441
3442 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003444 size_t size() const {return __v_.size();}
3445 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003447 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:19 +00003448 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449
3450private:
3451 // no copy functions
3452 seed_seq(const seed_seq&); // = delete;
3453 void operator=(const seed_seq&); // = delete;
3454
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003456 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003457};
3458
3459template<class _InputIterator>
3460void
3461seed_seq::init(_InputIterator __first, _InputIterator __last)
3462{
3463 for (_InputIterator __s = __first; __s != __last; ++__s)
3464 __v_.push_back(*__s & 0xFFFFFFFF);
3465}
3466
3467template<class _RandomAccessIterator>
3468void
3469seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3470{
3471 if (__first != __last)
3472 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003473 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474 const size_t __n = static_cast<size_t>(__last - __first);
3475 const size_t __s = __v_.size();
3476 const size_t __t = (__n >= 623) ? 11
3477 : (__n >= 68) ? 7
3478 : (__n >= 39) ? 5
3479 : (__n >= 7) ? 3
3480 : (__n - 1) / 2;
3481 const size_t __p = (__n - __t) / 2;
3482 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003483 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484 // __k = 0;
3485 {
Howard Hinnant99968442011-11-29 18:15:50 +00003486 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003487 ^ __first[__n - 1]);
3488 __first[__p] += __r;
3489 __r += __s;
3490 __first[__q] += __r;
3491 __first[0] = __r;
3492 }
3493 for (size_t __k = 1; __k <= __s; ++__k)
3494 {
3495 const size_t __kmodn = __k % __n;
3496 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003497 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003498 ^ __first[(__k - 1) % __n]);
3499 __first[__kpmodn] += __r;
3500 __r += __kmodn + __v_[__k-1];
3501 __first[(__k + __q) % __n] += __r;
3502 __first[__kmodn] = __r;
3503 }
3504 for (size_t __k = __s + 1; __k < __m; ++__k)
3505 {
3506 const size_t __kmodn = __k % __n;
3507 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003508 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003509 ^ __first[(__k - 1) % __n]);
3510 __first[__kpmodn] += __r;
3511 __r += __kmodn;
3512 __first[(__k + __q) % __n] += __r;
3513 __first[__kmodn] = __r;
3514 }
3515 for (size_t __k = __m; __k < __m + __n; ++__k)
3516 {
3517 const size_t __kmodn = __k % __n;
3518 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003519 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520 __first[__kpmodn] +
3521 __first[(__k - 1) % __n]);
3522 __first[__kpmodn] ^= __r;
3523 __r -= __kmodn;
3524 __first[(__k + __q) % __n] ^= __r;
3525 __first[__kmodn] = __r;
3526 }
3527 }
3528}
3529
Howard Hinnant30a840f2010-05-12 17:08:57 +00003530// generate_canonical
3531
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532template<class _RealType, size_t __bits, class _URNG>
3533_RealType
3534generate_canonical(_URNG& __g)
3535{
3536 const size_t _Dt = numeric_limits<_RealType>::digits;
3537 const size_t __b = _Dt < __bits ? _Dt : __bits;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003538#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003540#else
3541 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3542#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003543 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003544 const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
Howard Hinnant99968442011-11-29 18:15:50 +00003545 _RealType __base = _Rp;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003546 _RealType _Sp = __g() - _URNG::min();
Howard Hinnant99968442011-11-29 18:15:50 +00003547 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003548 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnant99968442011-11-29 18:15:50 +00003549 return _Sp / __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550}
3551
Howard Hinnant30a840f2010-05-12 17:08:57 +00003552// uniform_int_distribution
3553
Howard Hinnantc3267212010-05-26 17:49:34 +00003554// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555
3556template <class _CharT, class _Traits, class _IT>
3557basic_ostream<_CharT, _Traits>&
3558operator<<(basic_ostream<_CharT, _Traits>& __os,
3559 const uniform_int_distribution<_IT>& __x)
3560{
3561 __save_flags<_CharT, _Traits> _(__os);
3562 __os.flags(ios_base::dec | ios_base::left);
3563 _CharT __sp = __os.widen(' ');
3564 __os.fill(__sp);
3565 return __os << __x.a() << __sp << __x.b();
3566}
3567
3568template <class _CharT, class _Traits, class _IT>
3569basic_istream<_CharT, _Traits>&
3570operator>>(basic_istream<_CharT, _Traits>& __is,
3571 uniform_int_distribution<_IT>& __x)
3572{
3573 typedef uniform_int_distribution<_IT> _Eng;
3574 typedef typename _Eng::result_type result_type;
3575 typedef typename _Eng::param_type param_type;
3576 __save_flags<_CharT, _Traits> _(__is);
3577 __is.flags(ios_base::dec | ios_base::skipws);
3578 result_type __a;
3579 result_type __b;
3580 __is >> __a >> __b;
3581 if (!__is.fail())
3582 __x.param(param_type(__a, __b));
3583 return __is;
3584}
3585
Howard Hinnant30a840f2010-05-12 17:08:57 +00003586// uniform_real_distribution
3587
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003588template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003589class _LIBCPP_VISIBLE uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590{
3591public:
3592 // types
3593 typedef _RealType result_type;
3594
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003595 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596 {
3597 result_type __a_;
3598 result_type __b_;
3599 public:
3600 typedef uniform_real_distribution distribution_type;
3601
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003603 explicit param_type(result_type __a = 0,
3604 result_type __b = 1)
3605 : __a_(__a), __b_(__b) {}
3606
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003610 result_type b() const {return __b_;}
3611
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003612 friend _LIBCPP_INLINE_VISIBILITY
3613 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003614 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003615 friend _LIBCPP_INLINE_VISIBILITY
3616 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617 {return !(__x == __y);}
3618 };
3619
3620private:
3621 param_type __p_;
3622
3623public:
3624 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003626 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3627 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003631 void reset() {}
3632
3633 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003634 template<class _URNG>
3635 _LIBCPP_INLINE_VISIBILITY
3636 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003637 {return (*this)(__g, __p_);}
3638 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3639
3640 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003642 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644 result_type b() const {return __p_.b();}
3645
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003649 void param(const param_type& __p) {__p_ = __p;}
3650
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654 result_type max() const {return b();}
3655
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003656 friend _LIBCPP_INLINE_VISIBILITY
3657 bool operator==(const uniform_real_distribution& __x,
3658 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003660 friend _LIBCPP_INLINE_VISIBILITY
3661 bool operator!=(const uniform_real_distribution& __x,
3662 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003663 {return !(__x == __y);}
3664};
3665
3666template<class _RealType>
3667template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669typename uniform_real_distribution<_RealType>::result_type
3670uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3671{
3672 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:19 +00003673 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674 + __p.a();
3675}
3676
3677template <class _CharT, class _Traits, class _RT>
3678basic_ostream<_CharT, _Traits>&
3679operator<<(basic_ostream<_CharT, _Traits>& __os,
3680 const uniform_real_distribution<_RT>& __x)
3681{
3682 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003683 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3684 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003685 _CharT __sp = __os.widen(' ');
3686 __os.fill(__sp);
3687 return __os << __x.a() << __sp << __x.b();
3688}
3689
3690template <class _CharT, class _Traits, class _RT>
3691basic_istream<_CharT, _Traits>&
3692operator>>(basic_istream<_CharT, _Traits>& __is,
3693 uniform_real_distribution<_RT>& __x)
3694{
3695 typedef uniform_real_distribution<_RT> _Eng;
3696 typedef typename _Eng::result_type result_type;
3697 typedef typename _Eng::param_type param_type;
3698 __save_flags<_CharT, _Traits> _(__is);
3699 __is.flags(ios_base::dec | ios_base::skipws);
3700 result_type __a;
3701 result_type __b;
3702 __is >> __a >> __b;
3703 if (!__is.fail())
3704 __x.param(param_type(__a, __b));
3705 return __is;
3706}
3707
Howard Hinnant30a840f2010-05-12 17:08:57 +00003708// bernoulli_distribution
3709
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003710class _LIBCPP_VISIBLE bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711{
3712public:
3713 // types
3714 typedef bool result_type;
3715
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003716 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003717 {
3718 double __p_;
3719 public:
3720 typedef bernoulli_distribution distribution_type;
3721
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723 explicit param_type(double __p = 0.5) : __p_(__p) {}
3724
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726 double p() const {return __p_;}
3727
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003728 friend _LIBCPP_INLINE_VISIBILITY
3729 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003731 friend _LIBCPP_INLINE_VISIBILITY
3732 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733 {return !(__x == __y);}
3734 };
3735
3736private:
3737 param_type __p_;
3738
3739public:
3740 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742 explicit bernoulli_distribution(double __p = 0.5)
3743 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747 void reset() {}
3748
3749 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003750 template<class _URNG>
3751 _LIBCPP_INLINE_VISIBILITY
3752 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003754 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755
3756 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758 double p() const {return __p_.p();}
3759
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763 void param(const param_type& __p) {__p_ = __p;}
3764
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003768 result_type max() const {return true;}
3769
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003770 friend _LIBCPP_INLINE_VISIBILITY
3771 bool operator==(const bernoulli_distribution& __x,
3772 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003774 friend _LIBCPP_INLINE_VISIBILITY
3775 bool operator!=(const bernoulli_distribution& __x,
3776 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777 {return !(__x == __y);}
3778};
3779
Howard Hinnant03aad812010-05-11 23:26:59 +00003780template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003781inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003782bernoulli_distribution::result_type
3783bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3784{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003785 uniform_real_distribution<double> __gen;
3786 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003787}
3788
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789template <class _CharT, class _Traits>
3790basic_ostream<_CharT, _Traits>&
3791operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3792{
3793 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003794 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3795 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796 _CharT __sp = __os.widen(' ');
3797 __os.fill(__sp);
3798 return __os << __x.p();
3799}
3800
3801template <class _CharT, class _Traits>
3802basic_istream<_CharT, _Traits>&
3803operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3804{
3805 typedef bernoulli_distribution _Eng;
3806 typedef typename _Eng::param_type param_type;
3807 __save_flags<_CharT, _Traits> _(__is);
3808 __is.flags(ios_base::dec | ios_base::skipws);
3809 double __p;
3810 __is >> __p;
3811 if (!__is.fail())
3812 __x.param(param_type(__p));
3813 return __is;
3814}
3815
Howard Hinnant30a840f2010-05-12 17:08:57 +00003816// binomial_distribution
3817
Howard Hinnant03aad812010-05-11 23:26:59 +00003818template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003819class _LIBCPP_VISIBLE binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003820{
3821public:
3822 // types
3823 typedef _IntType result_type;
3824
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003825 class _LIBCPP_VISIBLE param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003826 {
3827 result_type __t_;
3828 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003829 double __pr_;
3830 double __odds_ratio_;
3831 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003832 public:
3833 typedef binomial_distribution distribution_type;
3834
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003835 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003836
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003838 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003840 double p() const {return __p_;}
3841
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003842 friend _LIBCPP_INLINE_VISIBILITY
3843 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003844 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003845 friend _LIBCPP_INLINE_VISIBILITY
3846 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003847 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003848
3849 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003850 };
3851
3852private:
3853 param_type __p_;
3854
3855public:
3856 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003858 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3859 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003861 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003863 void reset() {}
3864
3865 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003866 template<class _URNG>
3867 _LIBCPP_INLINE_VISIBILITY
3868 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003869 {return (*this)(__g, __p_);}
3870 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3871
3872 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003874 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003876 double p() const {return __p_.p();}
3877
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003879 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003881 void param(const param_type& __p) {__p_ = __p;}
3882
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003884 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003886 result_type max() const {return t();}
3887
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003888 friend _LIBCPP_INLINE_VISIBILITY
3889 bool operator==(const binomial_distribution& __x,
3890 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003891 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003892 friend _LIBCPP_INLINE_VISIBILITY
3893 bool operator!=(const binomial_distribution& __x,
3894 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003895 {return !(__x == __y);}
3896};
3897
3898template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003899binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3900 : __t_(__t), __p_(__p)
3901{
3902 if (0 < __p_ && __p_ < 1)
3903 {
3904 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003905 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
3906 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
3907 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003908 __odds_ratio_ = __p_ / (1 - __p_);
3909 }
3910}
3911
3912template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003913template<class _URNG>
3914_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003915binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003916{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003917 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3918 return 0;
3919 if (__pr.__p_ == 1)
3920 return __pr.__t_;
3921 uniform_real_distribution<double> __gen;
3922 double __u = __gen(__g) - __pr.__pr_;
3923 if (__u < 0)
3924 return __pr.__r0_;
3925 double __pu = __pr.__pr_;
3926 double __pd = __pu;
3927 result_type __ru = __pr.__r0_;
3928 result_type __rd = __ru;
3929 while (true)
3930 {
3931 if (__rd >= 1)
3932 {
3933 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3934 __u -= __pd;
3935 if (__u < 0)
3936 return __rd - 1;
3937 }
3938 --__rd;
3939 ++__ru;
3940 if (__ru <= __pr.__t_)
3941 {
3942 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3943 __u -= __pu;
3944 if (__u < 0)
3945 return __ru;
3946 }
3947 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003948}
3949
3950template <class _CharT, class _Traits, class _IntType>
3951basic_ostream<_CharT, _Traits>&
3952operator<<(basic_ostream<_CharT, _Traits>& __os,
3953 const binomial_distribution<_IntType>& __x)
3954{
3955 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003956 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3957 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003958 _CharT __sp = __os.widen(' ');
3959 __os.fill(__sp);
3960 return __os << __x.t() << __sp << __x.p();
3961}
3962
3963template <class _CharT, class _Traits, class _IntType>
3964basic_istream<_CharT, _Traits>&
3965operator>>(basic_istream<_CharT, _Traits>& __is,
3966 binomial_distribution<_IntType>& __x)
3967{
3968 typedef binomial_distribution<_IntType> _Eng;
3969 typedef typename _Eng::result_type result_type;
3970 typedef typename _Eng::param_type param_type;
3971 __save_flags<_CharT, _Traits> _(__is);
3972 __is.flags(ios_base::dec | ios_base::skipws);
3973 result_type __t;
3974 double __p;
3975 __is >> __t >> __p;
3976 if (!__is.fail())
3977 __x.param(param_type(__t, __p));
3978 return __is;
3979}
3980
Howard Hinnant30a840f2010-05-12 17:08:57 +00003981// exponential_distribution
3982
3983template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003984class _LIBCPP_VISIBLE exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00003985{
3986public:
3987 // types
3988 typedef _RealType result_type;
3989
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003990 class _LIBCPP_VISIBLE param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00003991 {
3992 result_type __lambda_;
3993 public:
3994 typedef exponential_distribution distribution_type;
3995
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003997 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3998
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004000 result_type lambda() const {return __lambda_;}
4001
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004002 friend _LIBCPP_INLINE_VISIBILITY
4003 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004004 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004005 friend _LIBCPP_INLINE_VISIBILITY
4006 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004007 {return !(__x == __y);}
4008 };
4009
4010private:
4011 param_type __p_;
4012
4013public:
4014 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004016 explicit exponential_distribution(result_type __lambda = 1)
4017 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004019 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004021 void reset() {}
4022
4023 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004024 template<class _URNG>
4025 _LIBCPP_INLINE_VISIBILITY
4026 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004027 {return (*this)(__g, __p_);}
4028 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4029
4030 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004032 result_type lambda() const {return __p_.lambda();}
4033
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004035 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004037 void param(const param_type& __p) {__p_ = __p;}
4038
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004040 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004042 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004043
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004044 friend _LIBCPP_INLINE_VISIBILITY
4045 bool operator==(const exponential_distribution& __x,
4046 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004047 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004048 friend _LIBCPP_INLINE_VISIBILITY
4049 bool operator!=(const exponential_distribution& __x,
4050 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004051 {return !(__x == __y);}
4052};
4053
4054template <class _RealType>
4055template<class _URNG>
4056_RealType
4057exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4058{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004059 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:57 +00004060 (
4061 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:19 +00004062 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:57 +00004063 numeric_limits<result_type>::digits>(__g)
4064 )
4065 / __p.lambda();
4066}
4067
4068template <class _CharT, class _Traits, class _RealType>
4069basic_ostream<_CharT, _Traits>&
4070operator<<(basic_ostream<_CharT, _Traits>& __os,
4071 const exponential_distribution<_RealType>& __x)
4072{
4073 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004074 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4075 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004076 return __os << __x.lambda();
4077}
4078
4079template <class _CharT, class _Traits, class _RealType>
4080basic_istream<_CharT, _Traits>&
4081operator>>(basic_istream<_CharT, _Traits>& __is,
4082 exponential_distribution<_RealType>& __x)
4083{
4084 typedef exponential_distribution<_RealType> _Eng;
4085 typedef typename _Eng::result_type result_type;
4086 typedef typename _Eng::param_type param_type;
4087 __save_flags<_CharT, _Traits> _(__is);
4088 __is.flags(ios_base::dec | ios_base::skipws);
4089 result_type __lambda;
4090 __is >> __lambda;
4091 if (!__is.fail())
4092 __x.param(param_type(__lambda));
4093 return __is;
4094}
4095
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004096// normal_distribution
4097
4098template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004099class _LIBCPP_VISIBLE normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004100{
4101public:
4102 // types
4103 typedef _RealType result_type;
4104
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004105 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004106 {
4107 result_type __mean_;
4108 result_type __stddev_;
4109 public:
4110 typedef normal_distribution distribution_type;
4111
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004113 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4114 : __mean_(__mean), __stddev_(__stddev) {}
4115
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004117 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004119 result_type stddev() const {return __stddev_;}
4120
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004121 friend _LIBCPP_INLINE_VISIBILITY
4122 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004123 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004124 friend _LIBCPP_INLINE_VISIBILITY
4125 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004126 {return !(__x == __y);}
4127 };
4128
4129private:
4130 param_type __p_;
4131 result_type _V_;
4132 bool _V_hot_;
4133
4134public:
4135 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004137 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4138 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004140 explicit normal_distribution(const param_type& __p)
4141 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004143 void reset() {_V_hot_ = false;}
4144
4145 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004146 template<class _URNG>
4147 _LIBCPP_INLINE_VISIBILITY
4148 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004149 {return (*this)(__g, __p_);}
4150 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4151
4152 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004154 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004156 result_type stddev() const {return __p_.stddev();}
4157
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004159 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004161 void param(const param_type& __p) {__p_ = __p;}
4162
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004164 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004166 result_type max() const {return numeric_limits<result_type>::infinity();}
4167
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004168 friend _LIBCPP_INLINE_VISIBILITY
4169 bool operator==(const normal_distribution& __x,
4170 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004171 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4172 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004173 friend _LIBCPP_INLINE_VISIBILITY
4174 bool operator!=(const normal_distribution& __x,
4175 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004176 {return !(__x == __y);}
4177
4178 template <class _CharT, class _Traits, class _RT>
4179 friend
4180 basic_ostream<_CharT, _Traits>&
4181 operator<<(basic_ostream<_CharT, _Traits>& __os,
4182 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004183
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004184 template <class _CharT, class _Traits, class _RT>
4185 friend
4186 basic_istream<_CharT, _Traits>&
4187 operator>>(basic_istream<_CharT, _Traits>& __is,
4188 normal_distribution<_RT>& __x);
4189};
4190
4191template <class _RealType>
4192template<class _URNG>
4193_RealType
4194normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4195{
Howard Hinnant99968442011-11-29 18:15:50 +00004196 result_type _Up;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004197 if (_V_hot_)
4198 {
4199 _V_hot_ = false;
Howard Hinnant99968442011-11-29 18:15:50 +00004200 _Up = _V_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004201 }
4202 else
4203 {
4204 uniform_real_distribution<result_type> _Uni(-1, 1);
4205 result_type __u;
4206 result_type __v;
4207 result_type __s;
4208 do
4209 {
4210 __u = _Uni(__g);
4211 __v = _Uni(__g);
4212 __s = __u * __u + __v * __v;
4213 } while (__s > 1 || __s == 0);
Howard Hinnant99968442011-11-29 18:15:50 +00004214 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4215 _V_ = __v * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004216 _V_hot_ = true;
Howard Hinnant99968442011-11-29 18:15:50 +00004217 _Up = __u * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004218 }
Howard Hinnant99968442011-11-29 18:15:50 +00004219 return _Up * __p.stddev() + __p.mean();
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004220}
4221
4222template <class _CharT, class _Traits, class _RT>
4223basic_ostream<_CharT, _Traits>&
4224operator<<(basic_ostream<_CharT, _Traits>& __os,
4225 const normal_distribution<_RT>& __x)
4226{
4227 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004228 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4229 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004230 _CharT __sp = __os.widen(' ');
4231 __os.fill(__sp);
4232 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4233 if (__x._V_hot_)
4234 __os << __sp << __x._V_;
4235 return __os;
4236}
4237
4238template <class _CharT, class _Traits, class _RT>
4239basic_istream<_CharT, _Traits>&
4240operator>>(basic_istream<_CharT, _Traits>& __is,
4241 normal_distribution<_RT>& __x)
4242{
4243 typedef normal_distribution<_RT> _Eng;
4244 typedef typename _Eng::result_type result_type;
4245 typedef typename _Eng::param_type param_type;
4246 __save_flags<_CharT, _Traits> _(__is);
4247 __is.flags(ios_base::dec | ios_base::skipws);
4248 result_type __mean;
4249 result_type __stddev;
Howard Hinnant99968442011-11-29 18:15:50 +00004250 result_type _Vp = 0;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004251 bool _V_hot = false;
4252 __is >> __mean >> __stddev >> _V_hot;
4253 if (_V_hot)
Howard Hinnant99968442011-11-29 18:15:50 +00004254 __is >> _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004255 if (!__is.fail())
4256 {
4257 __x.param(param_type(__mean, __stddev));
4258 __x._V_hot_ = _V_hot;
Howard Hinnant99968442011-11-29 18:15:50 +00004259 __x._V_ = _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004260 }
4261 return __is;
4262}
4263
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004264// lognormal_distribution
4265
4266template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004267class _LIBCPP_VISIBLE lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004268{
4269public:
4270 // types
4271 typedef _RealType result_type;
4272
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004273 class _LIBCPP_VISIBLE param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004274 {
4275 normal_distribution<result_type> __nd_;
4276 public:
4277 typedef lognormal_distribution distribution_type;
4278
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004280 explicit param_type(result_type __m = 0, result_type __s = 1)
4281 : __nd_(__m, __s) {}
4282
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004284 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004286 result_type s() const {return __nd_.stddev();}
4287
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004288 friend _LIBCPP_INLINE_VISIBILITY
4289 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004290 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004291 friend _LIBCPP_INLINE_VISIBILITY
4292 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004293 {return !(__x == __y);}
4294 friend class lognormal_distribution;
4295
4296 template <class _CharT, class _Traits, class _RT>
4297 friend
4298 basic_ostream<_CharT, _Traits>&
4299 operator<<(basic_ostream<_CharT, _Traits>& __os,
4300 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004301
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004302 template <class _CharT, class _Traits, class _RT>
4303 friend
4304 basic_istream<_CharT, _Traits>&
4305 operator>>(basic_istream<_CharT, _Traits>& __is,
4306 lognormal_distribution<_RT>& __x);
4307 };
4308
4309private:
4310 param_type __p_;
4311
4312public:
4313 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004315 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4316 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004318 explicit lognormal_distribution(const param_type& __p)
4319 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004321 void reset() {__p_.__nd_.reset();}
4322
4323 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004324 template<class _URNG>
4325 _LIBCPP_INLINE_VISIBILITY
4326 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004327 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004328 template<class _URNG>
4329 _LIBCPP_INLINE_VISIBILITY
4330 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004331 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004332
4333 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004335 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004337 result_type s() const {return __p_.s();}
4338
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004340 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004342 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004343
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004345 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004347 result_type max() const {return numeric_limits<result_type>::infinity();}
4348
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004349 friend _LIBCPP_INLINE_VISIBILITY
4350 bool operator==(const lognormal_distribution& __x,
4351 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004352 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004353 friend _LIBCPP_INLINE_VISIBILITY
4354 bool operator!=(const lognormal_distribution& __x,
4355 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004356 {return !(__x == __y);}
4357
4358 template <class _CharT, class _Traits, class _RT>
4359 friend
4360 basic_ostream<_CharT, _Traits>&
4361 operator<<(basic_ostream<_CharT, _Traits>& __os,
4362 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004363
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004364 template <class _CharT, class _Traits, class _RT>
4365 friend
4366 basic_istream<_CharT, _Traits>&
4367 operator>>(basic_istream<_CharT, _Traits>& __is,
4368 lognormal_distribution<_RT>& __x);
4369};
4370
4371template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004372inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004373basic_ostream<_CharT, _Traits>&
4374operator<<(basic_ostream<_CharT, _Traits>& __os,
4375 const lognormal_distribution<_RT>& __x)
4376{
4377 return __os << __x.__p_.__nd_;
4378}
4379
4380template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004381inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004382basic_istream<_CharT, _Traits>&
4383operator>>(basic_istream<_CharT, _Traits>& __is,
4384 lognormal_distribution<_RT>& __x)
4385{
4386 return __is >> __x.__p_.__nd_;
4387}
4388
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004389// poisson_distribution
4390
4391template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004392class _LIBCPP_VISIBLE poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004393{
4394public:
4395 // types
4396 typedef _IntType result_type;
4397
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004398 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004399 {
4400 double __mean_;
4401 double __s_;
4402 double __d_;
4403 double __l_;
4404 double __omega_;
4405 double __c0_;
4406 double __c1_;
4407 double __c2_;
4408 double __c3_;
4409 double __c_;
4410
4411 public:
4412 typedef poisson_distribution distribution_type;
4413
4414 explicit param_type(double __mean = 1.0);
4415
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004417 double mean() const {return __mean_;}
4418
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004419 friend _LIBCPP_INLINE_VISIBILITY
4420 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004421 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004422 friend _LIBCPP_INLINE_VISIBILITY
4423 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004424 {return !(__x == __y);}
4425
4426 friend class poisson_distribution;
4427 };
4428
4429private:
4430 param_type __p_;
4431
4432public:
4433 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004435 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004437 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004439 void reset() {}
4440
4441 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004442 template<class _URNG>
4443 _LIBCPP_INLINE_VISIBILITY
4444 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004445 {return (*this)(__g, __p_);}
4446 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4447
4448 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004450 double mean() const {return __p_.mean();}
4451
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004453 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004455 void param(const param_type& __p) {__p_ = __p;}
4456
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004458 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004460 result_type max() const {return numeric_limits<result_type>::max();}
4461
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004462 friend _LIBCPP_INLINE_VISIBILITY
4463 bool operator==(const poisson_distribution& __x,
4464 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004465 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004466 friend _LIBCPP_INLINE_VISIBILITY
4467 bool operator!=(const poisson_distribution& __x,
4468 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004469 {return !(__x == __y);}
4470};
4471
4472template<class _IntType>
4473poisson_distribution<_IntType>::param_type::param_type(double __mean)
4474 : __mean_(__mean)
4475{
4476 if (__mean_ < 10)
4477 {
4478 __s_ = 0;
4479 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004480 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004481 __omega_ = 0;
4482 __c3_ = 0;
4483 __c2_ = 0;
4484 __c1_ = 0;
4485 __c0_ = 0;
4486 __c_ = 0;
4487 }
4488 else
4489 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004490 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004491 __d_ = 6 * __mean_ * __mean_;
4492 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4493 __omega_ = .3989423 / __s_;
4494 double __b1_ = .4166667E-1 / __mean_;
4495 double __b2_ = .3 * __b1_ * __b1_;
4496 __c3_ = .1428571 * __b1_ * __b2_;
4497 __c2_ = __b2_ - 15. * __c3_;
4498 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4499 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4500 __c_ = .1069 / __mean_;
4501 }
4502}
4503
4504template <class _IntType>
4505template<class _URNG>
4506_IntType
4507poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4508{
4509 result_type __x;
4510 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004511 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004512 {
4513 __x = 0;
4514 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4515 __p *= __urd(__urng);
4516 }
4517 else
4518 {
4519 double __difmuk;
4520 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4521 double __u;
4522 if (__g > 0)
4523 {
4524 __x = static_cast<result_type>(__g);
4525 if (__x >= __pr.__l_)
4526 return __x;
4527 __difmuk = __pr.__mean_ - __x;
4528 __u = __urd(__urng);
4529 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4530 return __x;
4531 }
4532 exponential_distribution<double> __edist;
4533 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4534 {
4535 double __e;
4536 if (__using_exp_dist || __g < 0)
4537 {
4538 double __t;
4539 do
4540 {
4541 __e = __edist(__urng);
4542 __u = __urd(__urng);
4543 __u += __u - 1;
4544 __t = 1.8 + (__u < 0 ? -__e : __e);
4545 } while (__t <= -.6744);
4546 __x = __pr.__mean_ + __pr.__s_ * __t;
4547 __difmuk = __pr.__mean_ - __x;
4548 __using_exp_dist = true;
4549 }
4550 double __px;
4551 double __py;
4552 if (__x < 10)
4553 {
4554 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4555 40320, 362880};
4556 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004557 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004558 }
4559 else
4560 {
4561 double __del = .8333333E-1 / __x;
4562 __del -= 4.8 * __del * __del * __del;
4563 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004564 if (_VSTD::abs(__v) > 0.25)
4565 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004566 else
4567 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4568 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4569 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004570 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004571 }
4572 double __r = (0.5 - __difmuk) / __pr.__s_;
4573 double __r2 = __r * __r;
4574 double __fx = -0.5 * __r2;
4575 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4576 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4577 if (__using_exp_dist)
4578 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004579 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4580 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004581 break;
4582 }
4583 else
4584 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004585 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004586 break;
4587 }
4588 }
4589 }
4590 return __x;
4591}
4592
4593template <class _CharT, class _Traits, class _IntType>
4594basic_ostream<_CharT, _Traits>&
4595operator<<(basic_ostream<_CharT, _Traits>& __os,
4596 const poisson_distribution<_IntType>& __x)
4597{
4598 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004599 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4600 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004601 return __os << __x.mean();
4602}
4603
4604template <class _CharT, class _Traits, class _IntType>
4605basic_istream<_CharT, _Traits>&
4606operator>>(basic_istream<_CharT, _Traits>& __is,
4607 poisson_distribution<_IntType>& __x)
4608{
4609 typedef poisson_distribution<_IntType> _Eng;
4610 typedef typename _Eng::param_type param_type;
4611 __save_flags<_CharT, _Traits> _(__is);
4612 __is.flags(ios_base::dec | ios_base::skipws);
4613 double __mean;
4614 __is >> __mean;
4615 if (!__is.fail())
4616 __x.param(param_type(__mean));
4617 return __is;
4618}
4619
Howard Hinnant9de6e302010-05-16 01:09:02 +00004620// weibull_distribution
4621
4622template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004623class _LIBCPP_VISIBLE weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004624{
4625public:
4626 // types
4627 typedef _RealType result_type;
4628
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004629 class _LIBCPP_VISIBLE param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004630 {
4631 result_type __a_;
4632 result_type __b_;
4633 public:
4634 typedef weibull_distribution distribution_type;
4635
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004637 explicit param_type(result_type __a = 1, result_type __b = 1)
4638 : __a_(__a), __b_(__b) {}
4639
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004641 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004643 result_type b() const {return __b_;}
4644
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004645 friend _LIBCPP_INLINE_VISIBILITY
4646 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004647 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004648 friend _LIBCPP_INLINE_VISIBILITY
4649 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004650 {return !(__x == __y);}
4651 };
4652
4653private:
4654 param_type __p_;
4655
4656public:
4657 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004659 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4660 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004662 explicit weibull_distribution(const param_type& __p)
4663 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004665 void reset() {}
4666
4667 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004668 template<class _URNG>
4669 _LIBCPP_INLINE_VISIBILITY
4670 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004671 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004672 template<class _URNG>
4673 _LIBCPP_INLINE_VISIBILITY
4674 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004675 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004676 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:02 +00004677
4678 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004680 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004682 result_type b() const {return __p_.b();}
4683
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004685 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004687 void param(const param_type& __p) {__p_ = __p;}
4688
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004690 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004692 result_type max() const {return numeric_limits<result_type>::infinity();}
4693
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004694 friend _LIBCPP_INLINE_VISIBILITY
4695 bool operator==(const weibull_distribution& __x,
4696 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004697 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004698 friend _LIBCPP_INLINE_VISIBILITY
4699 bool operator!=(const weibull_distribution& __x,
4700 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004701 {return !(__x == __y);}
4702};
4703
4704template <class _CharT, class _Traits, class _RT>
4705basic_ostream<_CharT, _Traits>&
4706operator<<(basic_ostream<_CharT, _Traits>& __os,
4707 const weibull_distribution<_RT>& __x)
4708{
4709 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004710 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4711 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004712 _CharT __sp = __os.widen(' ');
4713 __os.fill(__sp);
4714 __os << __x.a() << __sp << __x.b();
4715 return __os;
4716}
4717
4718template <class _CharT, class _Traits, class _RT>
4719basic_istream<_CharT, _Traits>&
4720operator>>(basic_istream<_CharT, _Traits>& __is,
4721 weibull_distribution<_RT>& __x)
4722{
4723 typedef weibull_distribution<_RT> _Eng;
4724 typedef typename _Eng::result_type result_type;
4725 typedef typename _Eng::param_type param_type;
4726 __save_flags<_CharT, _Traits> _(__is);
4727 __is.flags(ios_base::dec | ios_base::skipws);
4728 result_type __a;
4729 result_type __b;
4730 __is >> __a >> __b;
4731 if (!__is.fail())
4732 __x.param(param_type(__a, __b));
4733 return __is;
4734}
4735
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004736template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004737class _LIBCPP_VISIBLE extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004738{
4739public:
4740 // types
4741 typedef _RealType result_type;
4742
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004743 class _LIBCPP_VISIBLE param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004744 {
4745 result_type __a_;
4746 result_type __b_;
4747 public:
4748 typedef extreme_value_distribution distribution_type;
4749
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004751 explicit param_type(result_type __a = 0, result_type __b = 1)
4752 : __a_(__a), __b_(__b) {}
4753
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004755 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004757 result_type b() const {return __b_;}
4758
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004759 friend _LIBCPP_INLINE_VISIBILITY
4760 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004761 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004762 friend _LIBCPP_INLINE_VISIBILITY
4763 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004764 {return !(__x == __y);}
4765 };
4766
4767private:
4768 param_type __p_;
4769
4770public:
4771 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004773 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4774 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004776 explicit extreme_value_distribution(const param_type& __p)
4777 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004779 void reset() {}
4780
4781 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004782 template<class _URNG>
4783 _LIBCPP_INLINE_VISIBILITY
4784 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004785 {return (*this)(__g, __p_);}
4786 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4787
4788 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004790 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004792 result_type b() const {return __p_.b();}
4793
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004795 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004797 void param(const param_type& __p) {__p_ = __p;}
4798
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004800 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004802 result_type max() const {return numeric_limits<result_type>::infinity();}
4803
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004804 friend _LIBCPP_INLINE_VISIBILITY
4805 bool operator==(const extreme_value_distribution& __x,
4806 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004807 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004808 friend _LIBCPP_INLINE_VISIBILITY
4809 bool operator!=(const extreme_value_distribution& __x,
4810 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004811 {return !(__x == __y);}
4812};
4813
4814template<class _RealType>
4815template<class _URNG>
4816_RealType
4817extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4818{
4819 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004820 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004821}
4822
4823template <class _CharT, class _Traits, class _RT>
4824basic_ostream<_CharT, _Traits>&
4825operator<<(basic_ostream<_CharT, _Traits>& __os,
4826 const extreme_value_distribution<_RT>& __x)
4827{
4828 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004829 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4830 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004831 _CharT __sp = __os.widen(' ');
4832 __os.fill(__sp);
4833 __os << __x.a() << __sp << __x.b();
4834 return __os;
4835}
4836
4837template <class _CharT, class _Traits, class _RT>
4838basic_istream<_CharT, _Traits>&
4839operator>>(basic_istream<_CharT, _Traits>& __is,
4840 extreme_value_distribution<_RT>& __x)
4841{
4842 typedef extreme_value_distribution<_RT> _Eng;
4843 typedef typename _Eng::result_type result_type;
4844 typedef typename _Eng::param_type param_type;
4845 __save_flags<_CharT, _Traits> _(__is);
4846 __is.flags(ios_base::dec | ios_base::skipws);
4847 result_type __a;
4848 result_type __b;
4849 __is >> __a >> __b;
4850 if (!__is.fail())
4851 __x.param(param_type(__a, __b));
4852 return __is;
4853}
4854
Howard Hinnantc7c49132010-05-13 17:58:28 +00004855// gamma_distribution
4856
4857template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004858class _LIBCPP_VISIBLE gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004859{
4860public:
4861 // types
4862 typedef _RealType result_type;
4863
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004864 class _LIBCPP_VISIBLE param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004865 {
4866 result_type __alpha_;
4867 result_type __beta_;
4868 public:
4869 typedef gamma_distribution distribution_type;
4870
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004872 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4873 : __alpha_(__alpha), __beta_(__beta) {}
4874
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004876 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004878 result_type beta() const {return __beta_;}
4879
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004880 friend _LIBCPP_INLINE_VISIBILITY
4881 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004882 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004883 friend _LIBCPP_INLINE_VISIBILITY
4884 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004885 {return !(__x == __y);}
4886 };
4887
4888private:
4889 param_type __p_;
4890
4891public:
4892 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004894 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4895 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004897 explicit gamma_distribution(const param_type& __p)
4898 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004900 void reset() {}
4901
4902 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004903 template<class _URNG>
4904 _LIBCPP_INLINE_VISIBILITY
4905 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004906 {return (*this)(__g, __p_);}
4907 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4908
4909 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004911 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004913 result_type beta() const {return __p_.beta();}
4914
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004916 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004918 void param(const param_type& __p) {__p_ = __p;}
4919
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004921 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004923 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00004924
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004925 friend _LIBCPP_INLINE_VISIBILITY
4926 bool operator==(const gamma_distribution& __x,
4927 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004928 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004929 friend _LIBCPP_INLINE_VISIBILITY
4930 bool operator!=(const gamma_distribution& __x,
4931 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004932 {return !(__x == __y);}
4933};
4934
4935template <class _RealType>
4936template<class _URNG>
4937_RealType
4938gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4939{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004940 result_type __a = __p.alpha();
4941 uniform_real_distribution<result_type> __gen(0, 1);
4942 exponential_distribution<result_type> __egen;
4943 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004944 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004945 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004946 else if (__a > 1)
4947 {
4948 const result_type __b = __a - 1;
4949 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004950 while (true)
4951 {
4952 const result_type __u = __gen(__g);
4953 const result_type __v = __gen(__g);
4954 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004955 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004956 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004957 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:28 +00004958 (__u - result_type(0.5));
4959 __x = __b + __y;
4960 if (__x >= 0)
4961 {
4962 const result_type __z = 64 * __w * __w * __w * __v * __v;
4963 if (__z <= 1 - 2 * __y * __y / __x)
4964 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004965 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:28 +00004966 break;
4967 }
4968 }
4969 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004970 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004971 else // __a < 1
4972 {
4973 while (true)
4974 {
4975 const result_type __u = __gen(__g);
4976 const result_type __es = __egen(__g);
4977 if (__u <= 1 - __a)
4978 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004979 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004980 if (__x <= __es)
4981 break;
4982 }
4983 else
4984 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004985 const result_type __e = -_VSTD::log((1-__u)/__a);
4986 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004987 if (__x <= __e + __es)
4988 break;
4989 }
4990 }
4991 }
4992 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004993}
4994
4995template <class _CharT, class _Traits, class _RT>
4996basic_ostream<_CharT, _Traits>&
4997operator<<(basic_ostream<_CharT, _Traits>& __os,
4998 const gamma_distribution<_RT>& __x)
4999{
5000 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005001 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5002 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005003 _CharT __sp = __os.widen(' ');
5004 __os.fill(__sp);
5005 __os << __x.alpha() << __sp << __x.beta();
5006 return __os;
5007}
5008
5009template <class _CharT, class _Traits, class _RT>
5010basic_istream<_CharT, _Traits>&
5011operator>>(basic_istream<_CharT, _Traits>& __is,
5012 gamma_distribution<_RT>& __x)
5013{
5014 typedef gamma_distribution<_RT> _Eng;
5015 typedef typename _Eng::result_type result_type;
5016 typedef typename _Eng::param_type param_type;
5017 __save_flags<_CharT, _Traits> _(__is);
5018 __is.flags(ios_base::dec | ios_base::skipws);
5019 result_type __alpha;
5020 result_type __beta;
5021 __is >> __alpha >> __beta;
5022 if (!__is.fail())
5023 __x.param(param_type(__alpha, __beta));
5024 return __is;
5025}
Howard Hinnanta64111c2010-05-12 21:02:31 +00005026
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005027// negative_binomial_distribution
5028
5029template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005030class _LIBCPP_VISIBLE negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005031{
5032public:
5033 // types
5034 typedef _IntType result_type;
5035
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005036 class _LIBCPP_VISIBLE param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005037 {
5038 result_type __k_;
5039 double __p_;
5040 public:
5041 typedef negative_binomial_distribution distribution_type;
5042
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005044 explicit param_type(result_type __k = 1, double __p = 0.5)
5045 : __k_(__k), __p_(__p) {}
5046
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005048 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005050 double p() const {return __p_;}
5051
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005052 friend _LIBCPP_INLINE_VISIBILITY
5053 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005054 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005055 friend _LIBCPP_INLINE_VISIBILITY
5056 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005057 {return !(__x == __y);}
5058 };
5059
5060private:
5061 param_type __p_;
5062
5063public:
5064 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005066 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5067 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005069 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005071 void reset() {}
5072
5073 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005074 template<class _URNG>
5075 _LIBCPP_INLINE_VISIBILITY
5076 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005077 {return (*this)(__g, __p_);}
5078 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5079
5080 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005082 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005084 double p() const {return __p_.p();}
5085
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005087 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005089 void param(const param_type& __p) {__p_ = __p;}
5090
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005092 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005094 result_type max() const {return numeric_limits<result_type>::max();}
5095
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005096 friend _LIBCPP_INLINE_VISIBILITY
5097 bool operator==(const negative_binomial_distribution& __x,
5098 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005099 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005100 friend _LIBCPP_INLINE_VISIBILITY
5101 bool operator!=(const negative_binomial_distribution& __x,
5102 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005103 {return !(__x == __y);}
5104};
5105
5106template <class _IntType>
5107template<class _URNG>
5108_IntType
5109negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5110{
5111 result_type __k = __pr.k();
5112 double __p = __pr.p();
5113 if (__k <= 21 * __p)
5114 {
5115 bernoulli_distribution __gen(__p);
5116 result_type __f = 0;
5117 result_type __s = 0;
5118 while (__s < __k)
5119 {
5120 if (__gen(__urng))
5121 ++__s;
5122 else
5123 ++__f;
5124 }
5125 return __f;
5126 }
5127 return poisson_distribution<result_type>(gamma_distribution<double>
5128 (__k, (1-__p)/__p)(__urng))(__urng);
5129}
5130
5131template <class _CharT, class _Traits, class _IntType>
5132basic_ostream<_CharT, _Traits>&
5133operator<<(basic_ostream<_CharT, _Traits>& __os,
5134 const negative_binomial_distribution<_IntType>& __x)
5135{
5136 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005137 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5138 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005139 _CharT __sp = __os.widen(' ');
5140 __os.fill(__sp);
5141 return __os << __x.k() << __sp << __x.p();
5142}
5143
5144template <class _CharT, class _Traits, class _IntType>
5145basic_istream<_CharT, _Traits>&
5146operator>>(basic_istream<_CharT, _Traits>& __is,
5147 negative_binomial_distribution<_IntType>& __x)
5148{
5149 typedef negative_binomial_distribution<_IntType> _Eng;
5150 typedef typename _Eng::result_type result_type;
5151 typedef typename _Eng::param_type param_type;
5152 __save_flags<_CharT, _Traits> _(__is);
5153 __is.flags(ios_base::dec | ios_base::skipws);
5154 result_type __k;
5155 double __p;
5156 __is >> __k >> __p;
5157 if (!__is.fail())
5158 __x.param(param_type(__k, __p));
5159 return __is;
5160}
5161
Howard Hinnant34e8a572010-05-17 13:44:27 +00005162// geometric_distribution
5163
5164template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005165class _LIBCPP_VISIBLE geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005166{
5167public:
5168 // types
5169 typedef _IntType result_type;
5170
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005171 class _LIBCPP_VISIBLE param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005172 {
5173 double __p_;
5174 public:
5175 typedef geometric_distribution distribution_type;
5176
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005178 explicit param_type(double __p = 0.5) : __p_(__p) {}
5179
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005181 double p() const {return __p_;}
5182
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005183 friend _LIBCPP_INLINE_VISIBILITY
5184 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005185 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005186 friend _LIBCPP_INLINE_VISIBILITY
5187 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005188 {return !(__x == __y);}
5189 };
5190
5191private:
5192 param_type __p_;
5193
5194public:
5195 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005197 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005199 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005201 void reset() {}
5202
5203 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005204 template<class _URNG>
5205 _LIBCPP_INLINE_VISIBILITY
5206 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005207 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005208 template<class _URNG>
5209 _LIBCPP_INLINE_VISIBILITY
5210 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005211 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5212
5213 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005215 double p() const {return __p_.p();}
5216
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005218 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005220 void param(const param_type& __p) {__p_ = __p;}
5221
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005223 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005225 result_type max() const {return numeric_limits<result_type>::max();}
5226
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005227 friend _LIBCPP_INLINE_VISIBILITY
5228 bool operator==(const geometric_distribution& __x,
5229 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005230 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005231 friend _LIBCPP_INLINE_VISIBILITY
5232 bool operator!=(const geometric_distribution& __x,
5233 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005234 {return !(__x == __y);}
5235};
5236
5237template <class _CharT, class _Traits, class _IntType>
5238basic_ostream<_CharT, _Traits>&
5239operator<<(basic_ostream<_CharT, _Traits>& __os,
5240 const geometric_distribution<_IntType>& __x)
5241{
5242 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005243 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5244 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005245 return __os << __x.p();
5246}
5247
5248template <class _CharT, class _Traits, class _IntType>
5249basic_istream<_CharT, _Traits>&
5250operator>>(basic_istream<_CharT, _Traits>& __is,
5251 geometric_distribution<_IntType>& __x)
5252{
5253 typedef geometric_distribution<_IntType> _Eng;
5254 typedef typename _Eng::param_type param_type;
5255 __save_flags<_CharT, _Traits> _(__is);
5256 __is.flags(ios_base::dec | ios_base::skipws);
5257 double __p;
5258 __is >> __p;
5259 if (!__is.fail())
5260 __x.param(param_type(__p));
5261 return __is;
5262}
5263
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005264// chi_squared_distribution
5265
5266template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005267class _LIBCPP_VISIBLE chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005268{
5269public:
5270 // types
5271 typedef _RealType result_type;
5272
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005273 class _LIBCPP_VISIBLE param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005274 {
5275 result_type __n_;
5276 public:
5277 typedef chi_squared_distribution distribution_type;
5278
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005280 explicit param_type(result_type __n = 1) : __n_(__n) {}
5281
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005283 result_type n() const {return __n_;}
5284
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005285 friend _LIBCPP_INLINE_VISIBILITY
5286 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005287 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005288 friend _LIBCPP_INLINE_VISIBILITY
5289 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005290 {return !(__x == __y);}
5291 };
5292
5293private:
5294 param_type __p_;
5295
5296public:
5297 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005299 explicit chi_squared_distribution(result_type __n = 1)
5300 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005302 explicit chi_squared_distribution(const param_type& __p)
5303 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005305 void reset() {}
5306
5307 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005308 template<class _URNG>
5309 _LIBCPP_INLINE_VISIBILITY
5310 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005311 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005312 template<class _URNG>
5313 _LIBCPP_INLINE_VISIBILITY
5314 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005315 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5316
5317 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005319 result_type n() const {return __p_.n();}
5320
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005322 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005324 void param(const param_type& __p) {__p_ = __p;}
5325
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005327 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005329 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005330
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005331 friend _LIBCPP_INLINE_VISIBILITY
5332 bool operator==(const chi_squared_distribution& __x,
5333 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005334 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005335 friend _LIBCPP_INLINE_VISIBILITY
5336 bool operator!=(const chi_squared_distribution& __x,
5337 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005338 {return !(__x == __y);}
5339};
5340
5341template <class _CharT, class _Traits, class _RT>
5342basic_ostream<_CharT, _Traits>&
5343operator<<(basic_ostream<_CharT, _Traits>& __os,
5344 const chi_squared_distribution<_RT>& __x)
5345{
5346 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005347 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5348 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005349 __os << __x.n();
5350 return __os;
5351}
5352
5353template <class _CharT, class _Traits, class _RT>
5354basic_istream<_CharT, _Traits>&
5355operator>>(basic_istream<_CharT, _Traits>& __is,
5356 chi_squared_distribution<_RT>& __x)
5357{
5358 typedef chi_squared_distribution<_RT> _Eng;
5359 typedef typename _Eng::result_type result_type;
5360 typedef typename _Eng::param_type param_type;
5361 __save_flags<_CharT, _Traits> _(__is);
5362 __is.flags(ios_base::dec | ios_base::skipws);
5363 result_type __n;
5364 __is >> __n;
5365 if (!__is.fail())
5366 __x.param(param_type(__n));
5367 return __is;
5368}
5369
Howard Hinnantd7d01132010-05-17 21:55:46 +00005370// cauchy_distribution
5371
5372template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005373class _LIBCPP_VISIBLE cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005374{
5375public:
5376 // types
5377 typedef _RealType result_type;
5378
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005379 class _LIBCPP_VISIBLE param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005380 {
5381 result_type __a_;
5382 result_type __b_;
5383 public:
5384 typedef cauchy_distribution distribution_type;
5385
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005387 explicit param_type(result_type __a = 0, result_type __b = 1)
5388 : __a_(__a), __b_(__b) {}
5389
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005391 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005393 result_type b() const {return __b_;}
5394
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005395 friend _LIBCPP_INLINE_VISIBILITY
5396 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005397 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005398 friend _LIBCPP_INLINE_VISIBILITY
5399 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005400 {return !(__x == __y);}
5401 };
5402
5403private:
5404 param_type __p_;
5405
5406public:
5407 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005409 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5410 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005412 explicit cauchy_distribution(const param_type& __p)
5413 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005415 void reset() {}
5416
5417 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005418 template<class _URNG>
5419 _LIBCPP_INLINE_VISIBILITY
5420 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005421 {return (*this)(__g, __p_);}
5422 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5423
5424 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005426 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005428 result_type b() const {return __p_.b();}
5429
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005431 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005433 void param(const param_type& __p) {__p_ = __p;}
5434
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005436 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005438 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005439
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005440 friend _LIBCPP_INLINE_VISIBILITY
5441 bool operator==(const cauchy_distribution& __x,
5442 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005443 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005444 friend _LIBCPP_INLINE_VISIBILITY
5445 bool operator!=(const cauchy_distribution& __x,
5446 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005447 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005448};
5449
5450template <class _RealType>
5451template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005452inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005453_RealType
5454cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5455{
5456 uniform_real_distribution<result_type> __gen;
5457 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
Howard Hinnant0949eed2011-06-30 21:18:19 +00005458 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:46 +00005459}
5460
5461template <class _CharT, class _Traits, class _RT>
5462basic_ostream<_CharT, _Traits>&
5463operator<<(basic_ostream<_CharT, _Traits>& __os,
5464 const cauchy_distribution<_RT>& __x)
5465{
5466 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005467 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5468 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005469 _CharT __sp = __os.widen(' ');
5470 __os.fill(__sp);
5471 __os << __x.a() << __sp << __x.b();
5472 return __os;
5473}
5474
5475template <class _CharT, class _Traits, class _RT>
5476basic_istream<_CharT, _Traits>&
5477operator>>(basic_istream<_CharT, _Traits>& __is,
5478 cauchy_distribution<_RT>& __x)
5479{
5480 typedef cauchy_distribution<_RT> _Eng;
5481 typedef typename _Eng::result_type result_type;
5482 typedef typename _Eng::param_type param_type;
5483 __save_flags<_CharT, _Traits> _(__is);
5484 __is.flags(ios_base::dec | ios_base::skipws);
5485 result_type __a;
5486 result_type __b;
5487 __is >> __a >> __b;
5488 if (!__is.fail())
5489 __x.param(param_type(__a, __b));
5490 return __is;
5491}
5492
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005493// fisher_f_distribution
5494
5495template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005496class _LIBCPP_VISIBLE fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005497{
5498public:
5499 // types
5500 typedef _RealType result_type;
5501
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005502 class _LIBCPP_VISIBLE param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005503 {
5504 result_type __m_;
5505 result_type __n_;
5506 public:
5507 typedef fisher_f_distribution distribution_type;
5508
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005510 explicit param_type(result_type __m = 1, result_type __n = 1)
5511 : __m_(__m), __n_(__n) {}
5512
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005514 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005516 result_type n() const {return __n_;}
5517
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005518 friend _LIBCPP_INLINE_VISIBILITY
5519 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005520 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005521 friend _LIBCPP_INLINE_VISIBILITY
5522 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005523 {return !(__x == __y);}
5524 };
5525
5526private:
5527 param_type __p_;
5528
5529public:
5530 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005532 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5533 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005535 explicit fisher_f_distribution(const param_type& __p)
5536 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005538 void reset() {}
5539
5540 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005541 template<class _URNG>
5542 _LIBCPP_INLINE_VISIBILITY
5543 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005544 {return (*this)(__g, __p_);}
5545 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5546
5547 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005549 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005551 result_type n() const {return __p_.n();}
5552
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005554 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005556 void param(const param_type& __p) {__p_ = __p;}
5557
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005559 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005561 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005562
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005563 friend _LIBCPP_INLINE_VISIBILITY
5564 bool operator==(const fisher_f_distribution& __x,
5565 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005566 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005567 friend _LIBCPP_INLINE_VISIBILITY
5568 bool operator!=(const fisher_f_distribution& __x,
5569 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005570 {return !(__x == __y);}
5571};
5572
5573template <class _RealType>
5574template<class _URNG>
5575_RealType
5576fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5577{
5578 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5579 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5580 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5581}
5582
5583template <class _CharT, class _Traits, class _RT>
5584basic_ostream<_CharT, _Traits>&
5585operator<<(basic_ostream<_CharT, _Traits>& __os,
5586 const fisher_f_distribution<_RT>& __x)
5587{
5588 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005589 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5590 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005591 _CharT __sp = __os.widen(' ');
5592 __os.fill(__sp);
5593 __os << __x.m() << __sp << __x.n();
5594 return __os;
5595}
5596
5597template <class _CharT, class _Traits, class _RT>
5598basic_istream<_CharT, _Traits>&
5599operator>>(basic_istream<_CharT, _Traits>& __is,
5600 fisher_f_distribution<_RT>& __x)
5601{
5602 typedef fisher_f_distribution<_RT> _Eng;
5603 typedef typename _Eng::result_type result_type;
5604 typedef typename _Eng::param_type param_type;
5605 __save_flags<_CharT, _Traits> _(__is);
5606 __is.flags(ios_base::dec | ios_base::skipws);
5607 result_type __m;
5608 result_type __n;
5609 __is >> __m >> __n;
5610 if (!__is.fail())
5611 __x.param(param_type(__m, __n));
5612 return __is;
5613}
5614
Howard Hinnant551d8e42010-05-19 01:53:57 +00005615// student_t_distribution
5616
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005617template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005618class _LIBCPP_VISIBLE student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005619{
5620public:
5621 // types
5622 typedef _RealType result_type;
5623
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005624 class _LIBCPP_VISIBLE param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005625 {
5626 result_type __n_;
5627 public:
5628 typedef student_t_distribution distribution_type;
5629
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005631 explicit param_type(result_type __n = 1) : __n_(__n) {}
5632
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005634 result_type n() const {return __n_;}
5635
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005636 friend _LIBCPP_INLINE_VISIBILITY
5637 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005638 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005639 friend _LIBCPP_INLINE_VISIBILITY
5640 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005641 {return !(__x == __y);}
5642 };
5643
5644private:
5645 param_type __p_;
5646 normal_distribution<result_type> __nd_;
5647
5648public:
5649 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005651 explicit student_t_distribution(result_type __n = 1)
5652 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005654 explicit student_t_distribution(const param_type& __p)
5655 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005657 void reset() {__nd_.reset();}
5658
5659 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005660 template<class _URNG>
5661 _LIBCPP_INLINE_VISIBILITY
5662 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005663 {return (*this)(__g, __p_);}
5664 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5665
5666 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005668 result_type n() const {return __p_.n();}
5669
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005671 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005673 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005674
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005676 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005678 result_type max() const {return numeric_limits<result_type>::infinity();}
5679
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005680 friend _LIBCPP_INLINE_VISIBILITY
5681 bool operator==(const student_t_distribution& __x,
5682 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005683 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005684 friend _LIBCPP_INLINE_VISIBILITY
5685 bool operator!=(const student_t_distribution& __x,
5686 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005687 {return !(__x == __y);}
5688};
5689
5690template <class _RealType>
5691template<class _URNG>
5692_RealType
5693student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5694{
5695 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005696 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005697}
5698
5699template <class _CharT, class _Traits, class _RT>
5700basic_ostream<_CharT, _Traits>&
5701operator<<(basic_ostream<_CharT, _Traits>& __os,
5702 const student_t_distribution<_RT>& __x)
5703{
5704 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005705 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5706 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005707 __os << __x.n();
5708 return __os;
5709}
5710
5711template <class _CharT, class _Traits, class _RT>
5712basic_istream<_CharT, _Traits>&
5713operator>>(basic_istream<_CharT, _Traits>& __is,
5714 student_t_distribution<_RT>& __x)
5715{
5716 typedef student_t_distribution<_RT> _Eng;
5717 typedef typename _Eng::result_type result_type;
5718 typedef typename _Eng::param_type param_type;
5719 __save_flags<_CharT, _Traits> _(__is);
5720 __is.flags(ios_base::dec | ios_base::skipws);
5721 result_type __n;
5722 __is >> __n;
5723 if (!__is.fail())
5724 __x.param(param_type(__n));
5725 return __is;
5726}
5727
Howard Hinnant551d8e42010-05-19 01:53:57 +00005728// discrete_distribution
5729
5730template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005731class _LIBCPP_VISIBLE discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005732{
5733public:
5734 // types
5735 typedef _IntType result_type;
5736
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005737 class _LIBCPP_VISIBLE param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005738 {
5739 vector<double> __p_;
5740 public:
5741 typedef discrete_distribution distribution_type;
5742
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005744 param_type() {}
5745 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005747 param_type(_InputIterator __f, _InputIterator __l)
5748 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005749#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005751 param_type(initializer_list<double> __wl)
5752 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005753#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005754 template<class _UnaryOperation>
5755 param_type(size_t __nw, double __xmin, double __xmax,
5756 _UnaryOperation __fw);
5757
5758 vector<double> probabilities() const;
5759
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005760 friend _LIBCPP_INLINE_VISIBILITY
5761 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005762 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005763 friend _LIBCPP_INLINE_VISIBILITY
5764 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005765 {return !(__x == __y);}
5766
5767 private:
5768 void __init();
5769
5770 friend class discrete_distribution;
5771
5772 template <class _CharT, class _Traits, class _IT>
5773 friend
5774 basic_ostream<_CharT, _Traits>&
5775 operator<<(basic_ostream<_CharT, _Traits>& __os,
5776 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005777
Howard Hinnant551d8e42010-05-19 01:53:57 +00005778 template <class _CharT, class _Traits, class _IT>
5779 friend
5780 basic_istream<_CharT, _Traits>&
5781 operator>>(basic_istream<_CharT, _Traits>& __is,
5782 discrete_distribution<_IT>& __x);
5783 };
5784
5785private:
5786 param_type __p_;
5787
5788public:
5789 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005791 discrete_distribution() {}
5792 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005794 discrete_distribution(_InputIterator __f, _InputIterator __l)
5795 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005796#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005798 discrete_distribution(initializer_list<double> __wl)
5799 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005800#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005801 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005803 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5804 _UnaryOperation __fw)
5805 : __p_(__nw, __xmin, __xmax, __fw) {}
5806 explicit discrete_distribution(const param_type& __p)
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005808 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005810 void reset() {}
5811
5812 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005813 template<class _URNG>
5814 _LIBCPP_INLINE_VISIBILITY
5815 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005816 {return (*this)(__g, __p_);}
5817 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5818
5819 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005821 vector<double> probabilities() const {return __p_.probabilities();}
5822
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005824 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005826 void param(const param_type& __p) {__p_ = __p;}
5827
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005829 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005831 result_type max() const {return __p_.__p_.size();}
5832
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005833 friend _LIBCPP_INLINE_VISIBILITY
5834 bool operator==(const discrete_distribution& __x,
5835 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005836 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005837 friend _LIBCPP_INLINE_VISIBILITY
5838 bool operator!=(const discrete_distribution& __x,
5839 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005840 {return !(__x == __y);}
5841
5842 template <class _CharT, class _Traits, class _IT>
5843 friend
5844 basic_ostream<_CharT, _Traits>&
5845 operator<<(basic_ostream<_CharT, _Traits>& __os,
5846 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005847
Howard Hinnant551d8e42010-05-19 01:53:57 +00005848 template <class _CharT, class _Traits, class _IT>
5849 friend
5850 basic_istream<_CharT, _Traits>&
5851 operator>>(basic_istream<_CharT, _Traits>& __is,
5852 discrete_distribution<_IT>& __x);
5853};
5854
5855template<class _IntType>
5856template<class _UnaryOperation>
5857discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5858 double __xmin,
5859 double __xmax,
5860 _UnaryOperation __fw)
5861{
5862 if (__nw > 1)
5863 {
5864 __p_.reserve(__nw - 1);
5865 double __d = (__xmax - __xmin) / __nw;
5866 double __d2 = __d / 2;
5867 for (size_t __k = 0; __k < __nw; ++__k)
5868 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5869 __init();
5870 }
5871}
5872
5873template<class _IntType>
5874void
5875discrete_distribution<_IntType>::param_type::__init()
5876{
5877 if (!__p_.empty())
5878 {
5879 if (__p_.size() > 1)
5880 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005881 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5882 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:57 +00005883 __i < __e; ++__i)
5884 *__i /= __s;
5885 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005886 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005887 swap(__p_, __t);
5888 }
5889 else
5890 {
5891 __p_.clear();
5892 __p_.shrink_to_fit();
5893 }
5894 }
5895}
5896
5897template<class _IntType>
5898vector<double>
5899discrete_distribution<_IntType>::param_type::probabilities() const
5900{
5901 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00005902 _VSTD::vector<double> __p(__n+1);
5903 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005904 if (__n > 0)
5905 __p[__n] = 1 - __p_[__n-1];
5906 else
5907 __p[0] = 1;
5908 return __p;
5909}
5910
5911template<class _IntType>
5912template<class _URNG>
5913_IntType
5914discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5915{
5916 uniform_real_distribution<double> __gen;
5917 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:19 +00005918 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:57 +00005919 __p.__p_.begin());
5920}
5921
5922template <class _CharT, class _Traits, class _IT>
5923basic_ostream<_CharT, _Traits>&
5924operator<<(basic_ostream<_CharT, _Traits>& __os,
5925 const discrete_distribution<_IT>& __x)
5926{
5927 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005928 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5929 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005930 _CharT __sp = __os.widen(' ');
5931 __os.fill(__sp);
5932 size_t __n = __x.__p_.__p_.size();
5933 __os << __n;
5934 for (size_t __i = 0; __i < __n; ++__i)
5935 __os << __sp << __x.__p_.__p_[__i];
5936 return __os;
5937}
5938
5939template <class _CharT, class _Traits, class _IT>
5940basic_istream<_CharT, _Traits>&
5941operator>>(basic_istream<_CharT, _Traits>& __is,
5942 discrete_distribution<_IT>& __x)
5943{
5944 typedef discrete_distribution<_IT> _Eng;
5945 typedef typename _Eng::result_type result_type;
5946 typedef typename _Eng::param_type param_type;
5947 __save_flags<_CharT, _Traits> _(__is);
5948 __is.flags(ios_base::dec | ios_base::skipws);
5949 size_t __n;
5950 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005951 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005952 for (size_t __i = 0; __i < __n; ++__i)
5953 __is >> __p[__i];
5954 if (!__is.fail())
5955 swap(__x.__p_.__p_, __p);
5956 return __is;
5957}
5958
Howard Hinnantd6d11712010-05-20 15:11:46 +00005959// piecewise_constant_distribution
5960
5961template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005962class _LIBCPP_VISIBLE piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00005963{
5964public:
5965 // types
5966 typedef _RealType result_type;
5967
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005968 class _LIBCPP_VISIBLE param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00005969 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005970 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005971 vector<result_type> __densities_;
5972 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005973 public:
5974 typedef piecewise_constant_distribution distribution_type;
5975
5976 param_type();
5977 template<class _InputIteratorB, class _InputIteratorW>
5978 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5979 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00005980#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005981 template<class _UnaryOperation>
5982 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00005983#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005984 template<class _UnaryOperation>
5985 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5986 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00005987 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005988
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005990 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005992 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005993
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005994 friend _LIBCPP_INLINE_VISIBILITY
5995 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005996 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005997 friend _LIBCPP_INLINE_VISIBILITY
5998 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005999 {return !(__x == __y);}
6000
6001 private:
6002 void __init();
6003
6004 friend class piecewise_constant_distribution;
6005
6006 template <class _CharT, class _Traits, class _RT>
6007 friend
6008 basic_ostream<_CharT, _Traits>&
6009 operator<<(basic_ostream<_CharT, _Traits>& __os,
6010 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006011
Howard Hinnantd6d11712010-05-20 15:11:46 +00006012 template <class _CharT, class _Traits, class _RT>
6013 friend
6014 basic_istream<_CharT, _Traits>&
6015 operator>>(basic_istream<_CharT, _Traits>& __is,
6016 piecewise_constant_distribution<_RT>& __x);
6017 };
6018
6019private:
6020 param_type __p_;
6021
6022public:
6023 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006025 piecewise_constant_distribution() {}
6026 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006028 piecewise_constant_distribution(_InputIteratorB __fB,
6029 _InputIteratorB __lB,
6030 _InputIteratorW __fW)
6031 : __p_(__fB, __lB, __fW) {}
6032
Howard Hinnante3e32912011-08-12 21:56:02 +00006033#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006034 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006036 piecewise_constant_distribution(initializer_list<result_type> __bl,
6037 _UnaryOperation __fw)
6038 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006039#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006040
6041 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006043 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6044 result_type __xmax, _UnaryOperation __fw)
6045 : __p_(__nw, __xmin, __xmax, __fw) {}
6046
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006048 explicit piecewise_constant_distribution(const param_type& __p)
6049 : __p_(__p) {}
6050
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006052 void reset() {}
6053
6054 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006055 template<class _URNG>
6056 _LIBCPP_INLINE_VISIBILITY
6057 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006058 {return (*this)(__g, __p_);}
6059 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6060
6061 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006063 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006065 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006066
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006068 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006070 void param(const param_type& __p) {__p_ = __p;}
6071
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006073 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006075 result_type max() const {return __p_.__b_.back();}
6076
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006077 friend _LIBCPP_INLINE_VISIBILITY
6078 bool operator==(const piecewise_constant_distribution& __x,
6079 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006080 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006081 friend _LIBCPP_INLINE_VISIBILITY
6082 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006083 const piecewise_constant_distribution& __y)
6084 {return !(__x == __y);}
6085
6086 template <class _CharT, class _Traits, class _RT>
6087 friend
6088 basic_ostream<_CharT, _Traits>&
6089 operator<<(basic_ostream<_CharT, _Traits>& __os,
6090 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006091
Howard Hinnantd6d11712010-05-20 15:11:46 +00006092 template <class _CharT, class _Traits, class _RT>
6093 friend
6094 basic_istream<_CharT, _Traits>&
6095 operator>>(basic_istream<_CharT, _Traits>& __is,
6096 piecewise_constant_distribution<_RT>& __x);
6097};
6098
6099template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006100typename piecewise_constant_distribution<_RealType>::param_type &
6101piecewise_constant_distribution<_RealType>::param_type::operator=
6102 (const param_type& __rhs)
6103{
6104// These can throw
6105 __b_.reserve (__rhs.__b_.size ());
6106 __densities_.reserve(__rhs.__densities_.size());
6107 __areas_.reserve (__rhs.__areas_.size());
6108
6109// These can not throw
6110 __b_ = __rhs.__b_;
6111 __densities_ = __rhs.__densities_;
6112 __areas_ = __rhs.__areas_;
6113 return *this;
6114}
6115
6116template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006117void
6118piecewise_constant_distribution<_RealType>::param_type::__init()
6119{
Howard Hinnant2a592542010-05-24 00:35:40 +00006120 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:19 +00006121 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006122 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006123 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006124 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6125 __densities_[__i] /= __total_area;
6126 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006127 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:19 +00006128 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:40 +00006129 __areas_.begin() + 1);
6130 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6131 __densities_.back() = 1 - __areas_.back(); // correct round off error
6132 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6133 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6134 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006135}
6136
6137template<class _RealType>
6138piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006139 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006140 __densities_(1, 1.0),
6141 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006142{
6143 __b_[1] = 1;
6144}
6145
6146template<class _RealType>
6147template<class _InputIteratorB, class _InputIteratorW>
6148piecewise_constant_distribution<_RealType>::param_type::param_type(
6149 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6150 : __b_(__fB, __lB)
6151{
6152 if (__b_.size() < 2)
6153 {
6154 __b_.resize(2);
6155 __b_[0] = 0;
6156 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006157 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006158 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006159 }
6160 else
6161 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006162 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006163 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006164 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006165 __init();
6166 }
6167}
6168
Howard Hinnante3e32912011-08-12 21:56:02 +00006169#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6170
Howard Hinnantd6d11712010-05-20 15:11:46 +00006171template<class _RealType>
6172template<class _UnaryOperation>
6173piecewise_constant_distribution<_RealType>::param_type::param_type(
6174 initializer_list<result_type> __bl, _UnaryOperation __fw)
6175 : __b_(__bl.begin(), __bl.end())
6176{
6177 if (__b_.size() < 2)
6178 {
6179 __b_.resize(2);
6180 __b_[0] = 0;
6181 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006182 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006183 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006184 }
6185 else
6186 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006187 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006188 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006189 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006190 __init();
6191 }
6192}
6193
Howard Hinnante3e32912011-08-12 21:56:02 +00006194#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6195
Howard Hinnantd6d11712010-05-20 15:11:46 +00006196template<class _RealType>
6197template<class _UnaryOperation>
6198piecewise_constant_distribution<_RealType>::param_type::param_type(
6199 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6200 : __b_(__nw == 0 ? 2 : __nw + 1)
6201{
6202 size_t __n = __b_.size() - 1;
6203 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006204 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006205 for (size_t __i = 0; __i < __n; ++__i)
6206 {
6207 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006208 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006209 }
6210 __b_[__n] = __xmax;
6211 __init();
6212}
6213
6214template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006215template<class _URNG>
6216_RealType
6217piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6218{
6219 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006220 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006221 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006222 __u) - __p.__areas_.begin() - 1;
6223 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006224}
6225
6226template <class _CharT, class _Traits, class _RT>
6227basic_ostream<_CharT, _Traits>&
6228operator<<(basic_ostream<_CharT, _Traits>& __os,
6229 const piecewise_constant_distribution<_RT>& __x)
6230{
6231 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006232 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6233 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006234 _CharT __sp = __os.widen(' ');
6235 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006236 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006237 __os << __n;
6238 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006239 __os << __sp << __x.__p_.__b_[__i];
6240 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006241 __os << __sp << __n;
6242 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006243 __os << __sp << __x.__p_.__densities_[__i];
6244 __n = __x.__p_.__areas_.size();
6245 __os << __sp << __n;
6246 for (size_t __i = 0; __i < __n; ++__i)
6247 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006248 return __os;
6249}
6250
6251template <class _CharT, class _Traits, class _RT>
6252basic_istream<_CharT, _Traits>&
6253operator>>(basic_istream<_CharT, _Traits>& __is,
6254 piecewise_constant_distribution<_RT>& __x)
6255{
6256 typedef piecewise_constant_distribution<_RT> _Eng;
6257 typedef typename _Eng::result_type result_type;
6258 typedef typename _Eng::param_type param_type;
6259 __save_flags<_CharT, _Traits> _(__is);
6260 __is.flags(ios_base::dec | ios_base::skipws);
6261 size_t __n;
6262 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006263 vector<result_type> __b(__n);
6264 for (size_t __i = 0; __i < __n; ++__i)
6265 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006266 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006267 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006268 for (size_t __i = 0; __i < __n; ++__i)
6269 __is >> __densities[__i];
6270 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006271 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006272 for (size_t __i = 0; __i < __n; ++__i)
6273 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006274 if (!__is.fail())
6275 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006276 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006277 swap(__x.__p_.__densities_, __densities);
6278 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006279 }
6280 return __is;
6281}
6282
Howard Hinnant54305402010-05-25 00:27:34 +00006283// piecewise_linear_distribution
6284
6285template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006286class _LIBCPP_VISIBLE piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006287{
6288public:
6289 // types
6290 typedef _RealType result_type;
6291
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006292 class _LIBCPP_VISIBLE param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006293 {
Howard Hinnant54305402010-05-25 00:27:34 +00006294 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006295 vector<result_type> __densities_;
6296 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006297 public:
6298 typedef piecewise_linear_distribution distribution_type;
6299
6300 param_type();
6301 template<class _InputIteratorB, class _InputIteratorW>
6302 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6303 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006304#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006305 template<class _UnaryOperation>
6306 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006307#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006308 template<class _UnaryOperation>
6309 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6310 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006311 param_type & operator=(const param_type& __rhs);
6312
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006314 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006316 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006317
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006318 friend _LIBCPP_INLINE_VISIBILITY
6319 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006320 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006321 friend _LIBCPP_INLINE_VISIBILITY
6322 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006323 {return !(__x == __y);}
6324
6325 private:
6326 void __init();
6327
6328 friend class piecewise_linear_distribution;
6329
6330 template <class _CharT, class _Traits, class _RT>
6331 friend
6332 basic_ostream<_CharT, _Traits>&
6333 operator<<(basic_ostream<_CharT, _Traits>& __os,
6334 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006335
Howard Hinnant54305402010-05-25 00:27:34 +00006336 template <class _CharT, class _Traits, class _RT>
6337 friend
6338 basic_istream<_CharT, _Traits>&
6339 operator>>(basic_istream<_CharT, _Traits>& __is,
6340 piecewise_linear_distribution<_RT>& __x);
6341 };
6342
6343private:
6344 param_type __p_;
6345
6346public:
6347 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006349 piecewise_linear_distribution() {}
6350 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006352 piecewise_linear_distribution(_InputIteratorB __fB,
6353 _InputIteratorB __lB,
6354 _InputIteratorW __fW)
6355 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006356
Howard Hinnante3e32912011-08-12 21:56:02 +00006357#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006358 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006360 piecewise_linear_distribution(initializer_list<result_type> __bl,
6361 _UnaryOperation __fw)
6362 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006363#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006364
6365 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006367 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6368 result_type __xmax, _UnaryOperation __fw)
6369 : __p_(__nw, __xmin, __xmax, __fw) {}
6370
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006372 explicit piecewise_linear_distribution(const param_type& __p)
6373 : __p_(__p) {}
6374
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006376 void reset() {}
6377
6378 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006379 template<class _URNG>
6380 _LIBCPP_INLINE_VISIBILITY
6381 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006382 {return (*this)(__g, __p_);}
6383 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6384
6385 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006387 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006389 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006390
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006392 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006394 void param(const param_type& __p) {__p_ = __p;}
6395
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006397 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006399 result_type max() const {return __p_.__b_.back();}
6400
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006401 friend _LIBCPP_INLINE_VISIBILITY
6402 bool operator==(const piecewise_linear_distribution& __x,
6403 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006404 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006405 friend _LIBCPP_INLINE_VISIBILITY
6406 bool operator!=(const piecewise_linear_distribution& __x,
6407 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006408 {return !(__x == __y);}
6409
6410 template <class _CharT, class _Traits, class _RT>
6411 friend
6412 basic_ostream<_CharT, _Traits>&
6413 operator<<(basic_ostream<_CharT, _Traits>& __os,
6414 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006415
Howard Hinnant54305402010-05-25 00:27:34 +00006416 template <class _CharT, class _Traits, class _RT>
6417 friend
6418 basic_istream<_CharT, _Traits>&
6419 operator>>(basic_istream<_CharT, _Traits>& __is,
6420 piecewise_linear_distribution<_RT>& __x);
6421};
6422
6423template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006424typename piecewise_linear_distribution<_RealType>::param_type &
6425piecewise_linear_distribution<_RealType>::param_type::operator=
6426 (const param_type& __rhs)
6427{
6428// These can throw
6429 __b_.reserve (__rhs.__b_.size ());
6430 __densities_.reserve(__rhs.__densities_.size());
6431 __areas_.reserve (__rhs.__areas_.size());
6432
6433// These can not throw
6434 __b_ = __rhs.__b_;
6435 __densities_ = __rhs.__densities_;
6436 __areas_ = __rhs.__areas_;
6437 return *this;
6438}
6439
6440
6441template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006442void
6443piecewise_linear_distribution<_RealType>::param_type::__init()
6444{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006445 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnant99968442011-11-29 18:15:50 +00006446 result_type _Sp = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006447 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6448 {
6449 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6450 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnant99968442011-11-29 18:15:50 +00006451 _Sp += __areas_[__i];
Howard Hinnant54305402010-05-25 00:27:34 +00006452 }
6453 for (size_t __i = __areas_.size(); __i > 1;)
6454 {
6455 --__i;
Howard Hinnant99968442011-11-29 18:15:50 +00006456 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006457 }
6458 __areas_[0] = 0;
6459 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6460 __areas_[__i] += __areas_[__i-1];
6461 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnant99968442011-11-29 18:15:50 +00006462 __densities_[__i] /= _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006463}
6464
6465template<class _RealType>
6466piecewise_linear_distribution<_RealType>::param_type::param_type()
6467 : __b_(2),
6468 __densities_(2, 1.0),
6469 __areas_(1, 0.0)
6470{
6471 __b_[1] = 1;
6472}
6473
6474template<class _RealType>
6475template<class _InputIteratorB, class _InputIteratorW>
6476piecewise_linear_distribution<_RealType>::param_type::param_type(
6477 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6478 : __b_(__fB, __lB)
6479{
6480 if (__b_.size() < 2)
6481 {
6482 __b_.resize(2);
6483 __b_[0] = 0;
6484 __b_[1] = 1;
6485 __densities_.assign(2, 1.0);
6486 __areas_.assign(1, 0.0);
6487 }
6488 else
6489 {
6490 __densities_.reserve(__b_.size());
6491 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6492 __densities_.push_back(*__fW);
6493 __init();
6494 }
6495}
6496
Howard Hinnante3e32912011-08-12 21:56:02 +00006497#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6498
Howard Hinnant54305402010-05-25 00:27:34 +00006499template<class _RealType>
6500template<class _UnaryOperation>
6501piecewise_linear_distribution<_RealType>::param_type::param_type(
6502 initializer_list<result_type> __bl, _UnaryOperation __fw)
6503 : __b_(__bl.begin(), __bl.end())
6504{
6505 if (__b_.size() < 2)
6506 {
6507 __b_.resize(2);
6508 __b_[0] = 0;
6509 __b_[1] = 1;
6510 __densities_.assign(2, 1.0);
6511 __areas_.assign(1, 0.0);
6512 }
6513 else
6514 {
6515 __densities_.reserve(__b_.size());
6516 for (size_t __i = 0; __i < __b_.size(); ++__i)
6517 __densities_.push_back(__fw(__b_[__i]));
6518 __init();
6519 }
6520}
6521
Howard Hinnante3e32912011-08-12 21:56:02 +00006522#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6523
Howard Hinnant54305402010-05-25 00:27:34 +00006524template<class _RealType>
6525template<class _UnaryOperation>
6526piecewise_linear_distribution<_RealType>::param_type::param_type(
6527 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6528 : __b_(__nw == 0 ? 2 : __nw + 1)
6529{
6530 size_t __n = __b_.size() - 1;
6531 result_type __d = (__xmax - __xmin) / __n;
6532 __densities_.reserve(__b_.size());
6533 for (size_t __i = 0; __i < __n; ++__i)
6534 {
6535 __b_[__i] = __xmin + __i * __d;
6536 __densities_.push_back(__fw(__b_[__i]));
6537 }
6538 __b_[__n] = __xmax;
6539 __densities_.push_back(__fw(__b_[__n]));
6540 __init();
6541}
6542
6543template<class _RealType>
6544template<class _URNG>
6545_RealType
6546piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6547{
6548 typedef uniform_real_distribution<result_type> _Gen;
6549 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006550 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006551 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006552 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006553 const result_type __dk = __p.__densities_[__k];
6554 const result_type __dk1 = __p.__densities_[__k+1];
6555 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006556 const result_type __bk = __p.__b_[__k];
6557 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006558 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006559 const result_type __bk1 = __p.__b_[__k+1];
6560 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006561 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:19 +00006562 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006563 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006564}
6565
6566template <class _CharT, class _Traits, class _RT>
6567basic_ostream<_CharT, _Traits>&
6568operator<<(basic_ostream<_CharT, _Traits>& __os,
6569 const piecewise_linear_distribution<_RT>& __x)
6570{
6571 __save_flags<_CharT, _Traits> _(__os);
6572 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6573 ios_base::scientific);
6574 _CharT __sp = __os.widen(' ');
6575 __os.fill(__sp);
6576 size_t __n = __x.__p_.__b_.size();
6577 __os << __n;
6578 for (size_t __i = 0; __i < __n; ++__i)
6579 __os << __sp << __x.__p_.__b_[__i];
6580 __n = __x.__p_.__densities_.size();
6581 __os << __sp << __n;
6582 for (size_t __i = 0; __i < __n; ++__i)
6583 __os << __sp << __x.__p_.__densities_[__i];
6584 __n = __x.__p_.__areas_.size();
6585 __os << __sp << __n;
6586 for (size_t __i = 0; __i < __n; ++__i)
6587 __os << __sp << __x.__p_.__areas_[__i];
6588 return __os;
6589}
6590
6591template <class _CharT, class _Traits, class _RT>
6592basic_istream<_CharT, _Traits>&
6593operator>>(basic_istream<_CharT, _Traits>& __is,
6594 piecewise_linear_distribution<_RT>& __x)
6595{
6596 typedef piecewise_linear_distribution<_RT> _Eng;
6597 typedef typename _Eng::result_type result_type;
6598 typedef typename _Eng::param_type param_type;
Howard Hinnant54305402010-05-25 00:27:34 +00006599 __save_flags<_CharT, _Traits> _(__is);
6600 __is.flags(ios_base::dec | ios_base::skipws);
6601 size_t __n;
6602 __is >> __n;
6603 vector<result_type> __b(__n);
6604 for (size_t __i = 0; __i < __n; ++__i)
6605 __is >> __b[__i];
6606 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006607 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006608 for (size_t __i = 0; __i < __n; ++__i)
6609 __is >> __densities[__i];
6610 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006611 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006612 for (size_t __i = 0; __i < __n; ++__i)
6613 __is >> __areas[__i];
6614 if (!__is.fail())
6615 {
6616 swap(__x.__p_.__b_, __b);
6617 swap(__x.__p_.__densities_, __densities);
6618 swap(__x.__p_.__areas_, __areas);
6619 }
6620 return __is;
6621}
6622
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006623_LIBCPP_END_NAMESPACE_STD
6624
6625#endif // _LIBCPP_RANDOM