blob: 04d942bc87663903ee04ae3b73e0986c22d361ed [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>
Howard Hinnant33be35e2012-09-14 00:39:16 +00001816class _LIBCPP_VISIBLE linear_congruential_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817
1818template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001819 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnant33be35e2012-09-14 00:39:16 +00001820_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821basic_ostream<_CharT, _Traits>&
1822operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001823 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824
1825template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001826 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827basic_istream<_CharT, _Traits>&
1828operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001829 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830
1831template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001832class _LIBCPP_VISIBLE linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833{
1834public:
1835 // types
1836 typedef _UIntType result_type;
1837
1838private:
1839 result_type __x_;
1840
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001841 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842
1843 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1844 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1845public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001846 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1847 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1849
1850 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001851 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1852 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1853 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001855 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001857 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1858 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859
1860 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 explicit linear_congruential_engine(result_type __s = default_seed)
1863 {seed(__s);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00001864 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001866 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001867 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 void seed(result_type __s = default_seed)
1871 {seed(integral_constant<bool, __m == 0>(),
1872 integral_constant<bool, __c == 0>(), __s);}
1873 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 typename enable_if
1876 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001877 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 void
1879 >::type
1880 seed(_Sseq& __q)
1881 {__seed(__q, integral_constant<unsigned,
1882 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1883 : (__m-1) / 0x100000000ull)>());}
1884
1885 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 result_type operator()()
Howard Hinnant99968442011-11-29 18:15:50 +00001888 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1891
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001892 friend _LIBCPP_INLINE_VISIBILITY
1893 bool operator==(const linear_congruential_engine& __x,
1894 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001895 {return __x.__x_ == __y.__x_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001896 friend _LIBCPP_INLINE_VISIBILITY
1897 bool operator!=(const linear_congruential_engine& __x,
1898 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899 {return !(__x == __y);}
1900
1901private:
1902
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1909 1 : __s % __m;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1912
1913 template<class _Sseq>
1914 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1915 template<class _Sseq>
1916 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1917
1918 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001919 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 friend
1921 basic_ostream<_CharT, _Traits>&
1922 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001923 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001924
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001926 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 friend
1928 basic_istream<_CharT, _Traits>&
1929 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001930 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931};
1932
1933template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00001934 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1935 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
1936
1937template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1938 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1939 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
1940
1941template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1942 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1943 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
1944
1945template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1946 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1947 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
1948
1949template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950template<class _Sseq>
1951void
1952linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1953 integral_constant<unsigned, 1>)
1954{
1955 const unsigned __k = 1;
1956 uint32_t __ar[__k+3];
1957 __q.generate(__ar, __ar + __k + 3);
1958 result_type __s = static_cast<result_type>(__ar[3] % __m);
1959 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1960}
1961
1962template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1963template<class _Sseq>
1964void
1965linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1966 integral_constant<unsigned, 2>)
1967{
1968 const unsigned __k = 2;
1969 uint32_t __ar[__k+3];
1970 __q.generate(__ar, __ar + __k + 3);
1971 result_type __s = static_cast<result_type>((__ar[3] +
1972 (uint64_t)__ar[4] << 32) % __m);
1973 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1974}
1975
1976template <class _CharT, class _Traits>
1977class __save_flags
1978{
1979 typedef basic_ios<_CharT, _Traits> __stream_type;
1980 typedef typename __stream_type::fmtflags fmtflags;
1981
1982 __stream_type& __stream_;
1983 fmtflags __fmtflags_;
1984 _CharT __fill_;
1985
1986 __save_flags(const __save_flags&);
1987 __save_flags& operator=(const __save_flags&);
1988public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990 explicit __save_flags(__stream_type& __stream)
1991 : __stream_(__stream),
1992 __fmtflags_(__stream.flags()),
1993 __fill_(__stream.fill())
1994 {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 ~__save_flags()
1997 {
1998 __stream_.flags(__fmtflags_);
1999 __stream_.fill(__fill_);
2000 }
2001};
2002
2003template <class _CharT, class _Traits,
2004 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002005inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006basic_ostream<_CharT, _Traits>&
2007operator<<(basic_ostream<_CharT, _Traits>& __os,
2008 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2009{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002010 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011 __os.flags(ios_base::dec | ios_base::left);
2012 __os.fill(__os.widen(' '));
2013 return __os << __x.__x_;
2014}
2015
2016template <class _CharT, class _Traits,
2017 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2018basic_istream<_CharT, _Traits>&
2019operator>>(basic_istream<_CharT, _Traits>& __is,
2020 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2021{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002022 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 __is.flags(ios_base::dec | ios_base::skipws);
2024 _UIntType __t;
2025 __is >> __t;
2026 if (!__is.fail())
2027 __x.__x_ = __t;
2028 return __is;
2029}
2030
2031typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2032 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2034 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00002035typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036// mersenne_twister_engine
2037
2038template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2039 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2040 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002041class _LIBCPP_VISIBLE mersenne_twister_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042
Howard Hinnant99968442011-11-29 18:15:50 +00002043template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2044 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2045 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046bool
Howard Hinnant99968442011-11-29 18:15:50 +00002047operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2048 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2049 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2050 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051
Howard Hinnant99968442011-11-29 18:15:50 +00002052template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2053 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2054 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002055_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056bool
Howard Hinnant99968442011-11-29 18:15:50 +00002057operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2058 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2059 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2060 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061
2062template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002063 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2064 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2065 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066basic_ostream<_CharT, _Traits>&
2067operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002068 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2069 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070
2071template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002072 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2073 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2074 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075basic_istream<_CharT, _Traits>&
2076operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002077 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2078 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079
2080template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2081 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2082 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002083class _LIBCPP_VISIBLE mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084{
2085public:
2086 // types
2087 typedef _UIntType result_type;
2088
2089private:
2090 result_type __x_[__n];
2091 size_t __i_;
2092
2093 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2094 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002095 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2097 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2098 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2099 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2100 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2101 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2102 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2103public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002104 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2105 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2106 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2108 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2109 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2110 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2111 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2112 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2113
2114 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002115 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2116 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2117 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2118 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2119 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2120 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2121 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2122 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2123 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2124 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2125 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2126 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2127 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002129 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002131 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2132 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133
2134 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136 explicit mersenne_twister_engine(result_type __sd = default_seed)
2137 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002138 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002140 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002141 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 {seed(__q);}
2143 void seed(result_type __sd = default_seed);
2144 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146 typename enable_if
2147 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002148 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 void
2150 >::type
2151 seed(_Sseq& __q)
2152 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2153
2154 // generating functions
2155 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2158
Howard Hinnant99968442011-11-29 18:15:50 +00002159 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2160 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2161 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162 friend
2163 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002164 operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2165 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2166 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2167 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002168
Howard Hinnant99968442011-11-29 18:15:50 +00002169 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2170 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2171 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 friend
2173 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002174 operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2175 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2176 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2177 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178
2179 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002180 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2181 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2182 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183 friend
2184 basic_ostream<_CharT, _Traits>&
2185 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002186 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2187 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188
2189 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002190 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2191 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2192 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 friend
2194 basic_istream<_CharT, _Traits>&
2195 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002196 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2197 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198private:
2199
2200 template<class _Sseq>
2201 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2202 template<class _Sseq>
2203 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2204
2205 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002207 static
2208 typename enable_if
2209 <
2210 __count < __w,
2211 result_type
2212 >::type
2213 __lshift(result_type __x) {return (__x << __count) & _Max;}
2214
2215 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217 static
2218 typename enable_if
2219 <
2220 (__count >= __w),
2221 result_type
2222 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002223 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224
2225 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 static
2228 typename enable_if
2229 <
2230 __count < _Dt,
2231 result_type
2232 >::type
2233 __rshift(result_type __x) {return __x >> __count;}
2234
2235 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 static
2238 typename enable_if
2239 <
2240 (__count >= _Dt),
2241 result_type
2242 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002243 __rshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244};
2245
2246template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2247 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2248 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002249 _LIBCPP_CONSTEXPR const size_t
2250 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2251
2252template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2253 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2254 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2255 _LIBCPP_CONSTEXPR const size_t
2256 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2257
2258template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2259 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2260 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2261 _LIBCPP_CONSTEXPR const size_t
2262 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2263
2264template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2265 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2266 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2267 _LIBCPP_CONSTEXPR const size_t
2268 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
2269
2270template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2271 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2272 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2273 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2274 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2275
2276template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2277 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2278 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2279 _LIBCPP_CONSTEXPR const size_t
2280 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2281
2282template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2283 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2284 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2285 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2286 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2287
2288template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2289 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2290 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2291 _LIBCPP_CONSTEXPR const size_t
2292 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2293
2294template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2295 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2296 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2297 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2298 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2299
2300template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2301 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2302 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2303 _LIBCPP_CONSTEXPR const size_t
2304 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2305
2306template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2307 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2308 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2309 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2310 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2311
2312template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2313 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2314 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2315 _LIBCPP_CONSTEXPR const size_t
2316 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
2317
2318template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2319 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2320 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2321 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2322 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2323
2324template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2325 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2326 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2327 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2328 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2329
2330template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2331 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2332 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333void
2334mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2335 __t, __c, __l, __f>::seed(result_type __sd)
2336{ // __w >= 2
2337 __x_[0] = __sd & _Max;
2338 for (size_t __i = 1; __i < __n; ++__i)
2339 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2340 __i_ = 0;
2341}
2342
2343template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2344 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2345 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2346template<class _Sseq>
2347void
2348mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2349 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2350{
2351 const unsigned __k = 1;
2352 uint32_t __ar[__n * __k];
2353 __q.generate(__ar, __ar + __n * __k);
2354 for (size_t __i = 0; __i < __n; ++__i)
2355 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2356 const result_type __mask = __r == _Dt ? result_type(~0) :
2357 (result_type(1) << __r) - result_type(1);
2358 __i_ = 0;
2359 if ((__x_[0] & ~__mask) == 0)
2360 {
2361 for (size_t __i = 1; __i < __n; ++__i)
2362 if (__x_[__i] != 0)
2363 return;
2364 __x_[0] = _Max;
2365 }
2366}
2367
2368template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2369 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2370 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2371template<class _Sseq>
2372void
2373mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2374 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2375{
2376 const unsigned __k = 2;
2377 uint32_t __ar[__n * __k];
2378 __q.generate(__ar, __ar + __n * __k);
2379 for (size_t __i = 0; __i < __n; ++__i)
2380 __x_[__i] = static_cast<result_type>(
2381 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2382 const result_type __mask = __r == _Dt ? result_type(~0) :
2383 (result_type(1) << __r) - result_type(1);
2384 __i_ = 0;
2385 if ((__x_[0] & ~__mask) == 0)
2386 {
2387 for (size_t __i = 1; __i < __n; ++__i)
2388 if (__x_[__i] != 0)
2389 return;
2390 __x_[0] = _Max;
2391 }
2392}
2393
2394template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2395 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2396 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2397_UIntType
2398mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2399 __t, __c, __l, __f>::operator()()
2400{
2401 const size_t __j = (__i_ + 1) % __n;
2402 const result_type __mask = __r == _Dt ? result_type(~0) :
2403 (result_type(1) << __r) - result_type(1);
Howard Hinnant99968442011-11-29 18:15:50 +00002404 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 const size_t __k = (__i_ + __m) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00002406 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2408 __i_ = __j;
2409 __z ^= __lshift<__s>(__z) & __b;
2410 __z ^= __lshift<__t>(__z) & __c;
2411 return __z ^ __rshift<__l>(__z);
2412}
2413
Howard Hinnant99968442011-11-29 18:15:50 +00002414template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2415 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2416 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002417bool
Howard Hinnant99968442011-11-29 18:15:50 +00002418operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2419 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2420 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2421 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422{
2423 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002424 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 if (__x.__i_ == 0 || __y.__i_ == 0)
2426 {
Howard Hinnant99968442011-11-29 18:15:50 +00002427 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002428 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429 __y.__x_ + __y.__i_))
2430 return false;
2431 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002432 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2433 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434 }
2435 if (__x.__i_ < __y.__i_)
2436 {
Howard Hinnant99968442011-11-29 18:15:50 +00002437 size_t __j = _Np - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002438 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 __y.__x_ + __y.__i_))
2440 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002441 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002442 __y.__x_))
2443 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002444 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002445 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446 }
Howard Hinnant99968442011-11-29 18:15:50 +00002447 size_t __j = _Np - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002448 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 __x.__x_ + __x.__i_))
2450 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002451 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452 __x.__x_))
2453 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002454 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002455 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456}
2457
Howard Hinnant99968442011-11-29 18:15:50 +00002458template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2459 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2460 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462bool
Howard Hinnant99968442011-11-29 18:15:50 +00002463operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2464 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2465 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2466 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002467{
2468 return !(__x == __y);
2469}
2470
2471template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002472 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2473 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2474 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475basic_ostream<_CharT, _Traits>&
2476operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002477 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2478 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002480 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002481 __os.flags(ios_base::dec | ios_base::left);
2482 _CharT __sp = __os.widen(' ');
2483 __os.fill(__sp);
2484 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002485 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 __os << __sp << __x.__x_[__j];
2487 for (size_t __j = 0; __j < __x.__i_; ++__j)
2488 __os << __sp << __x.__x_[__j];
2489 return __os;
2490}
2491
2492template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002493 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2494 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2495 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496basic_istream<_CharT, _Traits>&
2497operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002498 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2499 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002500{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002501 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002503 _UI __t[_Np];
2504 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505 __is >> __t[__i];
2506 if (!__is.fail())
2507 {
Howard Hinnant99968442011-11-29 18:15:50 +00002508 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 __x.__x_[__i] = __t[__i];
2510 __x.__i_ = 0;
2511 }
2512 return __is;
2513}
2514
2515typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2516 0x9908b0df, 11, 0xffffffff,
2517 7, 0x9d2c5680,
2518 15, 0xefc60000,
2519 18, 1812433253> mt19937;
2520typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2521 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2522 17, 0x71d67fffeda60000ULL,
2523 37, 0xfff7eee000000000ULL,
2524 43, 6364136223846793005ULL> mt19937_64;
2525
2526// subtract_with_carry_engine
2527
2528template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002529class _LIBCPP_VISIBLE subtract_with_carry_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530
Howard Hinnant99968442011-11-29 18:15:50 +00002531template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532bool
2533operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002534 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2535 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536
Howard Hinnant99968442011-11-29 18:15:50 +00002537template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002538_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539bool
2540operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002541 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2542 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543
2544template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002545 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546basic_ostream<_CharT, _Traits>&
2547operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002548 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549
2550template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002551 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552basic_istream<_CharT, _Traits>&
2553operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002554 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555
2556template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002557class _LIBCPP_VISIBLE subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558{
2559public:
2560 // types
2561 typedef _UIntType result_type;
2562
2563private:
2564 result_type __x_[__r];
2565 result_type __c_;
2566 size_t __i_;
2567
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002568 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2570 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2571 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2572 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2573public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002574 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2575 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2576 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2578
2579 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002580 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2581 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2582 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002584 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002586 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2587 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588
2589 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2592 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002593 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002595 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002596 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002599 void seed(result_type __sd = default_seed)
2600 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2601 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603 typename enable_if
2604 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002605 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606 void
2607 >::type
2608 seed(_Sseq& __q)
2609 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2610
2611 // generating functions
2612 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2615
Howard Hinnant99968442011-11-29 18:15:50 +00002616 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617 friend
2618 bool
2619 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002620 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2621 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002622
Howard Hinnant99968442011-11-29 18:15:50 +00002623 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002624 friend
2625 bool
2626 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002627 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2628 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002629
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002631 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632 friend
2633 basic_ostream<_CharT, _Traits>&
2634 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002635 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002636
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002638 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 friend
2640 basic_istream<_CharT, _Traits>&
2641 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002642 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643
2644private:
2645
2646 void seed(result_type __sd, integral_constant<unsigned, 1>);
2647 void seed(result_type __sd, integral_constant<unsigned, 2>);
2648 template<class _Sseq>
2649 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2650 template<class _Sseq>
2651 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2652};
2653
2654template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002655 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2656
2657template<class _UIntType, size_t __w, size_t __s, size_t __r>
2658 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2659
2660template<class _UIntType, size_t __w, size_t __s, size_t __r>
2661 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2662
2663template<class _UIntType, size_t __w, size_t __s, size_t __r>
2664 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2665 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2666
2667template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668void
2669subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2670 integral_constant<unsigned, 1>)
2671{
2672 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2673 __e(__sd == 0u ? default_seed : __sd);
2674 for (size_t __i = 0; __i < __r; ++__i)
2675 __x_[__i] = static_cast<result_type>(__e() & _Max);
2676 __c_ = __x_[__r-1] == 0;
2677 __i_ = 0;
2678}
2679
2680template<class _UIntType, size_t __w, size_t __s, size_t __r>
2681void
2682subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2683 integral_constant<unsigned, 2>)
2684{
2685 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2686 __e(__sd == 0u ? default_seed : __sd);
2687 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002688 {
2689 result_type __e0 = __e();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 __x_[__i] = static_cast<result_type>(
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002691 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2692 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693 __c_ = __x_[__r-1] == 0;
2694 __i_ = 0;
2695}
2696
2697template<class _UIntType, size_t __w, size_t __s, size_t __r>
2698template<class _Sseq>
2699void
2700subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2701 integral_constant<unsigned, 1>)
2702{
2703 const unsigned __k = 1;
2704 uint32_t __ar[__r * __k];
2705 __q.generate(__ar, __ar + __r * __k);
2706 for (size_t __i = 0; __i < __r; ++__i)
2707 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2708 __c_ = __x_[__r-1] == 0;
2709 __i_ = 0;
2710}
2711
2712template<class _UIntType, size_t __w, size_t __s, size_t __r>
2713template<class _Sseq>
2714void
2715subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2716 integral_constant<unsigned, 2>)
2717{
2718 const unsigned __k = 2;
2719 uint32_t __ar[__r * __k];
2720 __q.generate(__ar, __ar + __r * __k);
2721 for (size_t __i = 0; __i < __r; ++__i)
2722 __x_[__i] = static_cast<result_type>(
2723 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2724 __c_ = __x_[__r-1] == 0;
2725 __i_ = 0;
2726}
2727
2728template<class _UIntType, size_t __w, size_t __s, size_t __r>
2729_UIntType
2730subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2731{
2732 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2733 result_type& __xr = __x_[__i_];
2734 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2735 __xr = (__xs - __xr - __c_) & _Max;
2736 __c_ = __new_c;
2737 __i_ = (__i_ + 1) % __r;
2738 return __xr;
2739}
2740
Howard Hinnant99968442011-11-29 18:15:50 +00002741template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742bool
2743operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002744 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2745 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746{
2747 if (__x.__c_ != __y.__c_)
2748 return false;
2749 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002750 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 if (__x.__i_ == 0 || __y.__i_ == 0)
2752 {
Howard Hinnant99968442011-11-29 18:15:50 +00002753 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002754 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 __y.__x_ + __y.__i_))
2756 return false;
2757 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002758 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2759 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760 }
2761 if (__x.__i_ < __y.__i_)
2762 {
Howard Hinnant99968442011-11-29 18:15:50 +00002763 size_t __j = _Rp - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002764 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765 __y.__x_ + __y.__i_))
2766 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002767 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768 __y.__x_))
2769 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002770 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002771 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772 }
Howard Hinnant99968442011-11-29 18:15:50 +00002773 size_t __j = _Rp - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002774 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775 __x.__x_ + __x.__i_))
2776 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002777 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778 __x.__x_))
2779 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002780 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002781 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782}
2783
Howard Hinnant99968442011-11-29 18:15:50 +00002784template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002785inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786bool
2787operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002788 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2789 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790{
2791 return !(__x == __y);
2792}
2793
2794template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002795 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002796basic_ostream<_CharT, _Traits>&
2797operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002798 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002800 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801 __os.flags(ios_base::dec | ios_base::left);
2802 _CharT __sp = __os.widen(' ');
2803 __os.fill(__sp);
2804 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002805 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 __os << __sp << __x.__x_[__j];
2807 for (size_t __j = 0; __j < __x.__i_; ++__j)
2808 __os << __sp << __x.__x_[__j];
2809 __os << __sp << __x.__c_;
2810 return __os;
2811}
2812
2813template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002814 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815basic_istream<_CharT, _Traits>&
2816operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002817 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002819 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002821 _UI __t[_Rp+1];
2822 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 __is >> __t[__i];
2824 if (!__is.fail())
2825 {
Howard Hinnant99968442011-11-29 18:15:50 +00002826 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827 __x.__x_[__i] = __t[__i];
Howard Hinnant99968442011-11-29 18:15:50 +00002828 __x.__c_ = __t[_Rp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829 __x.__i_ = 0;
2830 }
2831 return __is;
2832}
2833
2834typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2835typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2836
2837// discard_block_engine
2838
2839template<class _Engine, size_t __p, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002840class _LIBCPP_VISIBLE discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841{
2842 _Engine __e_;
2843 int __n_;
2844
2845 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2846 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2847public:
2848 // types
2849 typedef typename _Engine::result_type result_type;
2850
2851 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002852 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2853 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002855#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856 static const result_type _Min = _Engine::_Min;
2857 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002858#else
2859 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2860 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2861#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002864 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002866 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867
2868 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002872 explicit discard_block_engine(const _Engine& __e)
2873 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002876 explicit discard_block_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002877 : __e_(_VSTD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002878#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002881 template<class _Sseq>
2882 _LIBCPP_INLINE_VISIBILITY
2883 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002884 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002885 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002891 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002893 typename enable_if
2894 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002895 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002896 void
2897 >::type
2898 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002899
2900 // generating functions
2901 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002903 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2904
2905 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00002907 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908
Howard Hinnant99968442011-11-29 18:15:50 +00002909 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002910 friend
2911 bool
2912 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002913 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2914 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002915
Howard Hinnant99968442011-11-29 18:15:50 +00002916 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917 friend
2918 bool
2919 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002920 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2921 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002922
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002924 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925 friend
2926 basic_ostream<_CharT, _Traits>&
2927 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002928 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002929
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002931 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 friend
2933 basic_istream<_CharT, _Traits>&
2934 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002935 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936};
2937
2938template<class _Engine, size_t __p, size_t __r>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002939 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
2940
2941template<class _Engine, size_t __p, size_t __r>
2942 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
2943
2944template<class _Engine, size_t __p, size_t __r>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945typename discard_block_engine<_Engine, __p, __r>::result_type
2946discard_block_engine<_Engine, __p, __r>::operator()()
2947{
2948 if (__n_ >= __r)
2949 {
2950 __e_.discard(__p - __r);
2951 __n_ = 0;
2952 }
2953 ++__n_;
2954 return __e_();
2955}
2956
Howard Hinnant99968442011-11-29 18:15:50 +00002957template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959bool
Howard Hinnant99968442011-11-29 18:15:50 +00002960operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2961 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002962{
2963 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2964}
2965
Howard Hinnant99968442011-11-29 18:15:50 +00002966template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002967inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968bool
Howard Hinnant99968442011-11-29 18:15:50 +00002969operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2970 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002971{
2972 return !(__x == __y);
2973}
2974
2975template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002976 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002977basic_ostream<_CharT, _Traits>&
2978operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002979 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002980{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002981 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002982 __os.flags(ios_base::dec | ios_base::left);
2983 _CharT __sp = __os.widen(' ');
2984 __os.fill(__sp);
2985 return __os << __x.__e_ << __sp << __x.__n_;
2986}
2987
2988template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002989 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990basic_istream<_CharT, _Traits>&
2991operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002992 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002993{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002994 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002995 __is.flags(ios_base::dec | ios_base::skipws);
2996 _Eng __e;
2997 int __n;
2998 __is >> __e >> __n;
2999 if (!__is.fail())
3000 {
3001 __x.__e_ = __e;
3002 __x.__n_ = __n;
3003 }
3004 return __is;
3005}
3006
3007typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
3008typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
3009
3010// independent_bits_engine
3011
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003013class _LIBCPP_VISIBLE independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014{
Howard Hinnant99968442011-11-29 18:15:50 +00003015 template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003016 class __get_n
3017 {
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003018 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits;
3019 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
3020 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
3021 static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003022 public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003023 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003024 };
3025public:
3026 // types
3027 typedef _UIntType result_type;
3028
3029private:
3030 _Engine __e_;
3031
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003032 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003033 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3034 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3035
3036 typedef typename _Engine::result_type _Engine_result_type;
3037 typedef typename conditional
3038 <
3039 sizeof(_Engine_result_type) <= sizeof(result_type),
3040 result_type,
3041 _Engine_result_type
3042 >::type _Working_result_type;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003043#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00003044 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003045 + _Working_result_type(1);
3046#else
3047 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3048 + _Working_result_type(1);
3049#endif
3050 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3051 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3052 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3053 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3054 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3055 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3056 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3057 (_Rp >> __w0) << __w0;
3058 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3059 (_Rp >> (__w0+1)) << (__w0+1);
3060 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003061 _Engine_result_type(~0) >> (_EDt - __w0) :
3062 _Engine_result_type(0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003063 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3065 _Engine_result_type(~0);
3066public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003067 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3068 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3069 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003070 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3071
3072 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003074 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003076 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003077
3078 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003082 explicit independent_bits_engine(const _Engine& __e)
3083 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003084#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003086 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003087 : __e_(_VSTD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003088#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003091 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003093 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003094 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003095 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003098 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003100 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003101 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003103 typename enable_if
3104 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003105 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003106 void
3107 >::type
3108 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003109
3110 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003112 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3115
3116 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00003118 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119
Howard Hinnant99968442011-11-29 18:15:50 +00003120 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121 friend
3122 bool
3123 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003124 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3125 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003126
Howard Hinnant99968442011-11-29 18:15:50 +00003127 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128 friend
3129 bool
3130 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003131 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3132 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003133
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003135 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136 friend
3137 basic_ostream<_CharT, _Traits>&
3138 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003139 const independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003140
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003142 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003143 friend
3144 basic_istream<_CharT, _Traits>&
3145 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003146 independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147
3148private:
3149 result_type __eval(false_type);
3150 result_type __eval(true_type);
3151
3152 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003154 static
3155 typename enable_if
3156 <
3157 __count < _Dt,
3158 result_type
3159 >::type
3160 __lshift(result_type __x) {return __x << __count;}
3161
3162 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164 static
3165 typename enable_if
3166 <
3167 (__count >= _Dt),
3168 result_type
3169 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00003170 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171};
3172
3173template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003174inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003175_UIntType
3176independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3177{
3178 return static_cast<result_type>(__e_() & __mask0);
3179}
3180
3181template<class _Engine, size_t __w, class _UIntType>
3182_UIntType
3183independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3184{
Howard Hinnant99968442011-11-29 18:15:50 +00003185 result_type _Sp = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186 for (size_t __k = 0; __k < __n0; ++__k)
3187 {
3188 _Engine_result_type __u;
3189 do
3190 {
3191 __u = __e_() - _Engine::min();
3192 } while (__u >= __y0);
Howard Hinnant99968442011-11-29 18:15:50 +00003193 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194 }
3195 for (size_t __k = __n0; __k < __n; ++__k)
3196 {
3197 _Engine_result_type __u;
3198 do
3199 {
3200 __u = __e_() - _Engine::min();
3201 } while (__u >= __y1);
Howard Hinnant99968442011-11-29 18:15:50 +00003202 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003203 }
Howard Hinnant99968442011-11-29 18:15:50 +00003204 return _Sp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205}
3206
Howard Hinnant99968442011-11-29 18:15:50 +00003207template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003208inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003209bool
3210operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003211 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3212 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213{
3214 return __x.base() == __y.base();
3215}
3216
Howard Hinnant99968442011-11-29 18:15:50 +00003217template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003218inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003219bool
3220operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003221 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3222 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003223{
3224 return !(__x == __y);
3225}
3226
3227template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003228 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229basic_ostream<_CharT, _Traits>&
3230operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003231 const independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003232{
3233 return __os << __x.base();
3234}
3235
3236template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003237 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003238basic_istream<_CharT, _Traits>&
3239operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003240 independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003241{
3242 _Eng __e;
3243 __is >> __e;
3244 if (!__is.fail())
3245 __x.__e_ = __e;
3246 return __is;
3247}
3248
3249// shuffle_order_engine
3250
3251template <uint64_t _Xp, uint64_t _Yp>
3252struct __ugcd
3253{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003254 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255};
3256
3257template <uint64_t _Xp>
3258struct __ugcd<_Xp, 0>
3259{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003260 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003261};
3262
Howard Hinnant99968442011-11-29 18:15:50 +00003263template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003264class __uratio
3265{
Howard Hinnant99968442011-11-29 18:15:50 +00003266 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003267 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003269 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3270 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271
3272 typedef __uratio<num, den> type;
3273};
3274
3275template<class _Engine, size_t __k>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003276class _LIBCPP_VISIBLE shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003277{
3278 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3279public:
3280 // types
3281 typedef typename _Engine::result_type result_type;
3282
3283private:
3284 _Engine __e_;
3285 result_type _V_[__k];
3286 result_type _Y_;
3287
3288public:
3289 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003290 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003291
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003292#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003293 static const result_type _Min = _Engine::_Min;
3294 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003295#else
3296 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3297 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3298#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003299 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003301 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003303 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003304
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003305 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306
3307 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003309 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003311 explicit shuffle_order_engine(const _Engine& __e)
3312 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003313#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003315 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003316 : __e_(_VSTD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003317#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003319 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003320 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003322 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003323 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003324 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003325 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003327 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003330 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003332 typename enable_if
3333 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003334 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003335 void
3336 >::type
3337 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003338
3339 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003341 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003343 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3344
3345 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00003347 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348
3349private:
Howard Hinnant99968442011-11-29 18:15:50 +00003350 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003351 friend
3352 bool
3353 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003354 const shuffle_order_engine<_Eng, _Kp>& __x,
3355 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003356
Howard Hinnant99968442011-11-29 18:15:50 +00003357 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358 friend
3359 bool
3360 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003361 const shuffle_order_engine<_Eng, _Kp>& __x,
3362 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003363
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003365 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003366 friend
3367 basic_ostream<_CharT, _Traits>&
3368 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003369 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003370
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003371 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003372 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003373 friend
3374 basic_istream<_CharT, _Traits>&
3375 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003376 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379 void __init()
3380 {
3381 for (size_t __i = 0; __i < __k; ++__i)
3382 _V_[__i] = __e_();
3383 _Y_ = __e_();
3384 }
3385
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003389 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003390
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003394 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3395
Howard Hinnant99968442011-11-29 18:15:50 +00003396 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003398 typename enable_if
3399 <
Howard Hinnant99968442011-11-29 18:15:50 +00003400 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401 result_type
3402 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003403 __eval(__uratio<_Np, _Dp>)
3404 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003405
Howard Hinnant99968442011-11-29 18:15:50 +00003406 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003408 typename enable_if
3409 <
Howard Hinnant99968442011-11-29 18:15:50 +00003410 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003411 result_type
3412 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003413 __eval(__uratio<_Np, _Dp>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003414 {
Howard Hinnant99968442011-11-29 18:15:50 +00003415 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3416 / __uratio<_Np, _Dp>::den);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417 _Y_ = _V_[__j];
3418 _V_[__j] = __e_();
3419 return _Y_;
3420 }
3421
3422 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424 result_type __evalf()
3425 {
Howard Hinnant99968442011-11-29 18:15:50 +00003426 const double _Fp = __d == 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003427 __n / (2. * 0x8000000000000000ull) :
3428 __n / (double)__d;
Howard Hinnant99968442011-11-29 18:15:50 +00003429 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003430 _Y_ = _V_[__j];
3431 _V_[__j] = __e_();
3432 return _Y_;
3433 }
3434};
3435
Howard Hinnant0a69fa12012-12-12 21:14:28 +00003436template<class _Engine, size_t __k>
3437 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3438
Howard Hinnant99968442011-11-29 18:15:50 +00003439template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003440bool
3441operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003442 const shuffle_order_engine<_Eng, _Kp>& __x,
3443 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003444{
Howard Hinnant99968442011-11-29 18:15:50 +00003445 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446 __x.__e_ == __y.__e_;
3447}
3448
Howard Hinnant99968442011-11-29 18:15:50 +00003449template<class _Eng, size_t _Kp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003451bool
3452operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003453 const shuffle_order_engine<_Eng, _Kp>& __x,
3454 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455{
3456 return !(__x == __y);
3457}
3458
3459template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003460 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461basic_ostream<_CharT, _Traits>&
3462operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003463 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003464{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003465 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003466 __os.flags(ios_base::dec | ios_base::left);
3467 _CharT __sp = __os.widen(' ');
3468 __os.fill(__sp);
3469 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnant99968442011-11-29 18:15:50 +00003470 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003471 __os << __sp << __x._V_[__i];
3472 return __os << __sp << __x._Y_;
3473}
3474
3475template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003476 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003477basic_istream<_CharT, _Traits>&
3478operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003479 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003480{
Howard Hinnant99968442011-11-29 18:15:50 +00003481 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003482 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003483 __is.flags(ios_base::dec | ios_base::skipws);
3484 _Eng __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003485 result_type _Vp[_Kp+1];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003486 __is >> __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003487 for (size_t __i = 0; __i < _Kp+1; ++__i)
3488 __is >> _Vp[__i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489 if (!__is.fail())
3490 {
3491 __x.__e_ = __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003492 for (size_t __i = 0; __i < _Kp; ++__i)
3493 __x._V_[__i] = _Vp[__i];
3494 __x._Y_ = _Vp[_Kp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495 }
3496 return __is;
3497}
3498
3499typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3500
3501// random_device
3502
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003503class _LIBCPP_VISIBLE random_device
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003504{
3505 int __f_;
3506public:
3507 // types
3508 typedef unsigned result_type;
3509
3510 // generator characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003511 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3512 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003515 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003517 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003518
3519 // constructors
3520 explicit random_device(const string& __token = "/dev/urandom");
3521 ~random_device();
3522
3523 // generating functions
3524 result_type operator()();
3525
3526 // property functions
Howard Hinnantc83960a2012-07-20 21:44:27 +00003527 double entropy() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003528
3529private:
3530 // no copy functions
3531 random_device(const random_device&); // = delete;
3532 random_device& operator=(const random_device&); // = delete;
3533};
3534
3535// seed_seq
3536
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003537class _LIBCPP_VISIBLE seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003538{
3539public:
3540 // types
3541 typedef uint32_t result_type;
3542
3543private:
3544 vector<result_type> __v_;
3545
3546 template<class _InputIterator>
3547 void init(_InputIterator __first, _InputIterator __last);
3548public:
3549 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551 seed_seq() {}
Howard Hinnante3e32912011-08-12 21:56:02 +00003552#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00003556#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:43 +00003557
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003560 seed_seq(_InputIterator __first, _InputIterator __last)
3561 {init(__first, __last);}
3562
3563 // generating functions
3564 template<class _RandomAccessIterator>
3565 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3566
3567 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003569 size_t size() const {return __v_.size();}
3570 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:19 +00003573 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574
3575private:
3576 // no copy functions
3577 seed_seq(const seed_seq&); // = delete;
3578 void operator=(const seed_seq&); // = delete;
3579
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003581 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582};
3583
3584template<class _InputIterator>
3585void
3586seed_seq::init(_InputIterator __first, _InputIterator __last)
3587{
3588 for (_InputIterator __s = __first; __s != __last; ++__s)
3589 __v_.push_back(*__s & 0xFFFFFFFF);
3590}
3591
3592template<class _RandomAccessIterator>
3593void
3594seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3595{
3596 if (__first != __last)
3597 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003598 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599 const size_t __n = static_cast<size_t>(__last - __first);
3600 const size_t __s = __v_.size();
3601 const size_t __t = (__n >= 623) ? 11
3602 : (__n >= 68) ? 7
3603 : (__n >= 39) ? 5
3604 : (__n >= 7) ? 3
3605 : (__n - 1) / 2;
3606 const size_t __p = (__n - __t) / 2;
3607 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003608 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609 // __k = 0;
3610 {
Howard Hinnant99968442011-11-29 18:15:50 +00003611 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003612 ^ __first[__n - 1]);
3613 __first[__p] += __r;
3614 __r += __s;
3615 __first[__q] += __r;
3616 __first[0] = __r;
3617 }
3618 for (size_t __k = 1; __k <= __s; ++__k)
3619 {
3620 const size_t __kmodn = __k % __n;
3621 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003622 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623 ^ __first[(__k - 1) % __n]);
3624 __first[__kpmodn] += __r;
3625 __r += __kmodn + __v_[__k-1];
3626 __first[(__k + __q) % __n] += __r;
3627 __first[__kmodn] = __r;
3628 }
3629 for (size_t __k = __s + 1; __k < __m; ++__k)
3630 {
3631 const size_t __kmodn = __k % __n;
3632 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003633 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634 ^ __first[(__k - 1) % __n]);
3635 __first[__kpmodn] += __r;
3636 __r += __kmodn;
3637 __first[(__k + __q) % __n] += __r;
3638 __first[__kmodn] = __r;
3639 }
3640 for (size_t __k = __m; __k < __m + __n; ++__k)
3641 {
3642 const size_t __kmodn = __k % __n;
3643 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003644 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645 __first[__kpmodn] +
3646 __first[(__k - 1) % __n]);
3647 __first[__kpmodn] ^= __r;
3648 __r -= __kmodn;
3649 __first[(__k + __q) % __n] ^= __r;
3650 __first[__kmodn] = __r;
3651 }
3652 }
3653}
3654
Howard Hinnant30a840f2010-05-12 17:08:57 +00003655// generate_canonical
3656
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657template<class _RealType, size_t __bits, class _URNG>
3658_RealType
3659generate_canonical(_URNG& __g)
3660{
3661 const size_t _Dt = numeric_limits<_RealType>::digits;
3662 const size_t __b = _Dt < __bits ? _Dt : __bits;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003663#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003665#else
3666 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3667#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003669 const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
Howard Hinnant99968442011-11-29 18:15:50 +00003670 _RealType __base = _Rp;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003671 _RealType _Sp = __g() - _URNG::min();
Howard Hinnant99968442011-11-29 18:15:50 +00003672 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003673 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnant99968442011-11-29 18:15:50 +00003674 return _Sp / __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675}
3676
Howard Hinnant30a840f2010-05-12 17:08:57 +00003677// uniform_int_distribution
3678
Howard Hinnantc3267212010-05-26 17:49:34 +00003679// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680
3681template <class _CharT, class _Traits, class _IT>
3682basic_ostream<_CharT, _Traits>&
3683operator<<(basic_ostream<_CharT, _Traits>& __os,
3684 const uniform_int_distribution<_IT>& __x)
3685{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003686 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687 __os.flags(ios_base::dec | ios_base::left);
3688 _CharT __sp = __os.widen(' ');
3689 __os.fill(__sp);
3690 return __os << __x.a() << __sp << __x.b();
3691}
3692
3693template <class _CharT, class _Traits, class _IT>
3694basic_istream<_CharT, _Traits>&
3695operator>>(basic_istream<_CharT, _Traits>& __is,
3696 uniform_int_distribution<_IT>& __x)
3697{
3698 typedef uniform_int_distribution<_IT> _Eng;
3699 typedef typename _Eng::result_type result_type;
3700 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003701 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702 __is.flags(ios_base::dec | ios_base::skipws);
3703 result_type __a;
3704 result_type __b;
3705 __is >> __a >> __b;
3706 if (!__is.fail())
3707 __x.param(param_type(__a, __b));
3708 return __is;
3709}
3710
Howard Hinnant30a840f2010-05-12 17:08:57 +00003711// uniform_real_distribution
3712
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003714class _LIBCPP_VISIBLE uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003715{
3716public:
3717 // types
3718 typedef _RealType result_type;
3719
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003720 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003721 {
3722 result_type __a_;
3723 result_type __b_;
3724 public:
3725 typedef uniform_real_distribution distribution_type;
3726
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728 explicit param_type(result_type __a = 0,
3729 result_type __b = 1)
3730 : __a_(__a), __b_(__b) {}
3731
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735 result_type b() const {return __b_;}
3736
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003737 friend _LIBCPP_INLINE_VISIBILITY
3738 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003740 friend _LIBCPP_INLINE_VISIBILITY
3741 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742 {return !(__x == __y);}
3743 };
3744
3745private:
3746 param_type __p_;
3747
3748public:
3749 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3752 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756 void reset() {}
3757
3758 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003759 template<class _URNG>
3760 _LIBCPP_INLINE_VISIBILITY
3761 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003762 {return (*this)(__g, __p_);}
3763 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3764
3765 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003769 result_type b() const {return __p_.b();}
3770
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774 void param(const param_type& __p) {__p_ = __p;}
3775
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779 result_type max() const {return b();}
3780
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003781 friend _LIBCPP_INLINE_VISIBILITY
3782 bool operator==(const uniform_real_distribution& __x,
3783 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003784 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003785 friend _LIBCPP_INLINE_VISIBILITY
3786 bool operator!=(const uniform_real_distribution& __x,
3787 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788 {return !(__x == __y);}
3789};
3790
3791template<class _RealType>
3792template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003793inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794typename uniform_real_distribution<_RealType>::result_type
3795uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3796{
3797 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:19 +00003798 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799 + __p.a();
3800}
3801
3802template <class _CharT, class _Traits, class _RT>
3803basic_ostream<_CharT, _Traits>&
3804operator<<(basic_ostream<_CharT, _Traits>& __os,
3805 const uniform_real_distribution<_RT>& __x)
3806{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003807 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003808 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3809 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003810 _CharT __sp = __os.widen(' ');
3811 __os.fill(__sp);
3812 return __os << __x.a() << __sp << __x.b();
3813}
3814
3815template <class _CharT, class _Traits, class _RT>
3816basic_istream<_CharT, _Traits>&
3817operator>>(basic_istream<_CharT, _Traits>& __is,
3818 uniform_real_distribution<_RT>& __x)
3819{
3820 typedef uniform_real_distribution<_RT> _Eng;
3821 typedef typename _Eng::result_type result_type;
3822 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003823 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003824 __is.flags(ios_base::dec | ios_base::skipws);
3825 result_type __a;
3826 result_type __b;
3827 __is >> __a >> __b;
3828 if (!__is.fail())
3829 __x.param(param_type(__a, __b));
3830 return __is;
3831}
3832
Howard Hinnant30a840f2010-05-12 17:08:57 +00003833// bernoulli_distribution
3834
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003835class _LIBCPP_VISIBLE bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003836{
3837public:
3838 // types
3839 typedef bool result_type;
3840
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003841 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842 {
3843 double __p_;
3844 public:
3845 typedef bernoulli_distribution distribution_type;
3846
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003848 explicit param_type(double __p = 0.5) : __p_(__p) {}
3849
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851 double p() const {return __p_;}
3852
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003853 friend _LIBCPP_INLINE_VISIBILITY
3854 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003856 friend _LIBCPP_INLINE_VISIBILITY
3857 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858 {return !(__x == __y);}
3859 };
3860
3861private:
3862 param_type __p_;
3863
3864public:
3865 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003867 explicit bernoulli_distribution(double __p = 0.5)
3868 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003872 void reset() {}
3873
3874 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003875 template<class _URNG>
3876 _LIBCPP_INLINE_VISIBILITY
3877 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003878 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003879 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003880
3881 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003883 double p() const {return __p_.p();}
3884
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003886 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888 void param(const param_type& __p) {__p_ = __p;}
3889
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003891 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003893 result_type max() const {return true;}
3894
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003895 friend _LIBCPP_INLINE_VISIBILITY
3896 bool operator==(const bernoulli_distribution& __x,
3897 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003898 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003899 friend _LIBCPP_INLINE_VISIBILITY
3900 bool operator!=(const bernoulli_distribution& __x,
3901 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902 {return !(__x == __y);}
3903};
3904
Howard Hinnant03aad812010-05-11 23:26:59 +00003905template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003906inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003907bernoulli_distribution::result_type
3908bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3909{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003910 uniform_real_distribution<double> __gen;
3911 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003912}
3913
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003914template <class _CharT, class _Traits>
3915basic_ostream<_CharT, _Traits>&
3916operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3917{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003918 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003919 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3920 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003921 _CharT __sp = __os.widen(' ');
3922 __os.fill(__sp);
3923 return __os << __x.p();
3924}
3925
3926template <class _CharT, class _Traits>
3927basic_istream<_CharT, _Traits>&
3928operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3929{
3930 typedef bernoulli_distribution _Eng;
3931 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003932 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003933 __is.flags(ios_base::dec | ios_base::skipws);
3934 double __p;
3935 __is >> __p;
3936 if (!__is.fail())
3937 __x.param(param_type(__p));
3938 return __is;
3939}
3940
Howard Hinnant30a840f2010-05-12 17:08:57 +00003941// binomial_distribution
3942
Howard Hinnant03aad812010-05-11 23:26:59 +00003943template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003944class _LIBCPP_VISIBLE binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003945{
3946public:
3947 // types
3948 typedef _IntType result_type;
3949
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003950 class _LIBCPP_VISIBLE param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003951 {
3952 result_type __t_;
3953 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003954 double __pr_;
3955 double __odds_ratio_;
3956 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003957 public:
3958 typedef binomial_distribution distribution_type;
3959
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003960 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003961
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003963 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003965 double p() const {return __p_;}
3966
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003967 friend _LIBCPP_INLINE_VISIBILITY
3968 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003969 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003970 friend _LIBCPP_INLINE_VISIBILITY
3971 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003972 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003973
3974 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003975 };
3976
3977private:
3978 param_type __p_;
3979
3980public:
3981 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003983 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3984 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003986 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003988 void reset() {}
3989
3990 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003991 template<class _URNG>
3992 _LIBCPP_INLINE_VISIBILITY
3993 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003994 {return (*this)(__g, __p_);}
3995 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3996
3997 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003999 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00004001 double p() const {return __p_.p();}
4002
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00004004 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00004006 void param(const param_type& __p) {__p_ = __p;}
4007
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00004009 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00004011 result_type max() const {return t();}
4012
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004013 friend _LIBCPP_INLINE_VISIBILITY
4014 bool operator==(const binomial_distribution& __x,
4015 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00004016 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004017 friend _LIBCPP_INLINE_VISIBILITY
4018 bool operator!=(const binomial_distribution& __x,
4019 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00004020 {return !(__x == __y);}
4021};
4022
4023template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004024binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
4025 : __t_(__t), __p_(__p)
4026{
4027 if (0 < __p_ && __p_ < 1)
4028 {
4029 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004030 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
4031 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
4032 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004033 __odds_ratio_ = __p_ / (1 - __p_);
4034 }
4035}
4036
4037template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00004038template<class _URNG>
4039_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004040binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00004041{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004042 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4043 return 0;
4044 if (__pr.__p_ == 1)
4045 return __pr.__t_;
4046 uniform_real_distribution<double> __gen;
4047 double __u = __gen(__g) - __pr.__pr_;
4048 if (__u < 0)
4049 return __pr.__r0_;
4050 double __pu = __pr.__pr_;
4051 double __pd = __pu;
4052 result_type __ru = __pr.__r0_;
4053 result_type __rd = __ru;
4054 while (true)
4055 {
4056 if (__rd >= 1)
4057 {
4058 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4059 __u -= __pd;
4060 if (__u < 0)
4061 return __rd - 1;
4062 }
4063 --__rd;
4064 ++__ru;
4065 if (__ru <= __pr.__t_)
4066 {
4067 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4068 __u -= __pu;
4069 if (__u < 0)
4070 return __ru;
4071 }
4072 }
Howard Hinnant03aad812010-05-11 23:26:59 +00004073}
4074
4075template <class _CharT, class _Traits, class _IntType>
4076basic_ostream<_CharT, _Traits>&
4077operator<<(basic_ostream<_CharT, _Traits>& __os,
4078 const binomial_distribution<_IntType>& __x)
4079{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004080 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004081 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4082 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00004083 _CharT __sp = __os.widen(' ');
4084 __os.fill(__sp);
4085 return __os << __x.t() << __sp << __x.p();
4086}
4087
4088template <class _CharT, class _Traits, class _IntType>
4089basic_istream<_CharT, _Traits>&
4090operator>>(basic_istream<_CharT, _Traits>& __is,
4091 binomial_distribution<_IntType>& __x)
4092{
4093 typedef binomial_distribution<_IntType> _Eng;
4094 typedef typename _Eng::result_type result_type;
4095 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004096 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant03aad812010-05-11 23:26:59 +00004097 __is.flags(ios_base::dec | ios_base::skipws);
4098 result_type __t;
4099 double __p;
4100 __is >> __t >> __p;
4101 if (!__is.fail())
4102 __x.param(param_type(__t, __p));
4103 return __is;
4104}
4105
Howard Hinnant30a840f2010-05-12 17:08:57 +00004106// exponential_distribution
4107
4108template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004109class _LIBCPP_VISIBLE exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00004110{
4111public:
4112 // types
4113 typedef _RealType result_type;
4114
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004115 class _LIBCPP_VISIBLE param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00004116 {
4117 result_type __lambda_;
4118 public:
4119 typedef exponential_distribution distribution_type;
4120
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004122 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4123
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004125 result_type lambda() const {return __lambda_;}
4126
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004127 friend _LIBCPP_INLINE_VISIBILITY
4128 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004129 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004130 friend _LIBCPP_INLINE_VISIBILITY
4131 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004132 {return !(__x == __y);}
4133 };
4134
4135private:
4136 param_type __p_;
4137
4138public:
4139 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004141 explicit exponential_distribution(result_type __lambda = 1)
4142 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004144 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004146 void reset() {}
4147
4148 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004149 template<class _URNG>
4150 _LIBCPP_INLINE_VISIBILITY
4151 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004152 {return (*this)(__g, __p_);}
4153 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4154
4155 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004157 result_type lambda() const {return __p_.lambda();}
4158
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004160 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004162 void param(const param_type& __p) {__p_ = __p;}
4163
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004165 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004167 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004168
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004169 friend _LIBCPP_INLINE_VISIBILITY
4170 bool operator==(const exponential_distribution& __x,
4171 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004172 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004173 friend _LIBCPP_INLINE_VISIBILITY
4174 bool operator!=(const exponential_distribution& __x,
4175 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004176 {return !(__x == __y);}
4177};
4178
4179template <class _RealType>
4180template<class _URNG>
4181_RealType
4182exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4183{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004184 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:57 +00004185 (
4186 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:19 +00004187 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:57 +00004188 numeric_limits<result_type>::digits>(__g)
4189 )
4190 / __p.lambda();
4191}
4192
4193template <class _CharT, class _Traits, class _RealType>
4194basic_ostream<_CharT, _Traits>&
4195operator<<(basic_ostream<_CharT, _Traits>& __os,
4196 const exponential_distribution<_RealType>& __x)
4197{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004198 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004199 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4200 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004201 return __os << __x.lambda();
4202}
4203
4204template <class _CharT, class _Traits, class _RealType>
4205basic_istream<_CharT, _Traits>&
4206operator>>(basic_istream<_CharT, _Traits>& __is,
4207 exponential_distribution<_RealType>& __x)
4208{
4209 typedef exponential_distribution<_RealType> _Eng;
4210 typedef typename _Eng::result_type result_type;
4211 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004212 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004213 __is.flags(ios_base::dec | ios_base::skipws);
4214 result_type __lambda;
4215 __is >> __lambda;
4216 if (!__is.fail())
4217 __x.param(param_type(__lambda));
4218 return __is;
4219}
4220
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004221// normal_distribution
4222
4223template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004224class _LIBCPP_VISIBLE normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004225{
4226public:
4227 // types
4228 typedef _RealType result_type;
4229
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004230 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004231 {
4232 result_type __mean_;
4233 result_type __stddev_;
4234 public:
4235 typedef normal_distribution distribution_type;
4236
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004238 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4239 : __mean_(__mean), __stddev_(__stddev) {}
4240
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004242 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004244 result_type stddev() const {return __stddev_;}
4245
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004246 friend _LIBCPP_INLINE_VISIBILITY
4247 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004248 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004249 friend _LIBCPP_INLINE_VISIBILITY
4250 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004251 {return !(__x == __y);}
4252 };
4253
4254private:
4255 param_type __p_;
4256 result_type _V_;
4257 bool _V_hot_;
4258
4259public:
4260 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004262 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4263 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004265 explicit normal_distribution(const param_type& __p)
4266 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004268 void reset() {_V_hot_ = false;}
4269
4270 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004271 template<class _URNG>
4272 _LIBCPP_INLINE_VISIBILITY
4273 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004274 {return (*this)(__g, __p_);}
4275 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4276
4277 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004279 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004281 result_type stddev() const {return __p_.stddev();}
4282
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004284 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004286 void param(const param_type& __p) {__p_ = __p;}
4287
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004289 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004291 result_type max() const {return numeric_limits<result_type>::infinity();}
4292
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004293 friend _LIBCPP_INLINE_VISIBILITY
4294 bool operator==(const normal_distribution& __x,
4295 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004296 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4297 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004298 friend _LIBCPP_INLINE_VISIBILITY
4299 bool operator!=(const normal_distribution& __x,
4300 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004301 {return !(__x == __y);}
4302
4303 template <class _CharT, class _Traits, class _RT>
4304 friend
4305 basic_ostream<_CharT, _Traits>&
4306 operator<<(basic_ostream<_CharT, _Traits>& __os,
4307 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004308
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004309 template <class _CharT, class _Traits, class _RT>
4310 friend
4311 basic_istream<_CharT, _Traits>&
4312 operator>>(basic_istream<_CharT, _Traits>& __is,
4313 normal_distribution<_RT>& __x);
4314};
4315
4316template <class _RealType>
4317template<class _URNG>
4318_RealType
4319normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4320{
Howard Hinnant99968442011-11-29 18:15:50 +00004321 result_type _Up;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004322 if (_V_hot_)
4323 {
4324 _V_hot_ = false;
Howard Hinnant99968442011-11-29 18:15:50 +00004325 _Up = _V_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004326 }
4327 else
4328 {
4329 uniform_real_distribution<result_type> _Uni(-1, 1);
4330 result_type __u;
4331 result_type __v;
4332 result_type __s;
4333 do
4334 {
4335 __u = _Uni(__g);
4336 __v = _Uni(__g);
4337 __s = __u * __u + __v * __v;
4338 } while (__s > 1 || __s == 0);
Howard Hinnant99968442011-11-29 18:15:50 +00004339 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4340 _V_ = __v * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004341 _V_hot_ = true;
Howard Hinnant99968442011-11-29 18:15:50 +00004342 _Up = __u * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004343 }
Howard Hinnant99968442011-11-29 18:15:50 +00004344 return _Up * __p.stddev() + __p.mean();
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004345}
4346
4347template <class _CharT, class _Traits, class _RT>
4348basic_ostream<_CharT, _Traits>&
4349operator<<(basic_ostream<_CharT, _Traits>& __os,
4350 const normal_distribution<_RT>& __x)
4351{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004352 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004353 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4354 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004355 _CharT __sp = __os.widen(' ');
4356 __os.fill(__sp);
4357 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4358 if (__x._V_hot_)
4359 __os << __sp << __x._V_;
4360 return __os;
4361}
4362
4363template <class _CharT, class _Traits, class _RT>
4364basic_istream<_CharT, _Traits>&
4365operator>>(basic_istream<_CharT, _Traits>& __is,
4366 normal_distribution<_RT>& __x)
4367{
4368 typedef normal_distribution<_RT> _Eng;
4369 typedef typename _Eng::result_type result_type;
4370 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004371 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004372 __is.flags(ios_base::dec | ios_base::skipws);
4373 result_type __mean;
4374 result_type __stddev;
Howard Hinnant99968442011-11-29 18:15:50 +00004375 result_type _Vp = 0;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004376 bool _V_hot = false;
4377 __is >> __mean >> __stddev >> _V_hot;
4378 if (_V_hot)
Howard Hinnant99968442011-11-29 18:15:50 +00004379 __is >> _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004380 if (!__is.fail())
4381 {
4382 __x.param(param_type(__mean, __stddev));
4383 __x._V_hot_ = _V_hot;
Howard Hinnant99968442011-11-29 18:15:50 +00004384 __x._V_ = _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004385 }
4386 return __is;
4387}
4388
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004389// lognormal_distribution
4390
4391template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004392class _LIBCPP_VISIBLE lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004393{
4394public:
4395 // types
4396 typedef _RealType result_type;
4397
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004398 class _LIBCPP_VISIBLE param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004399 {
4400 normal_distribution<result_type> __nd_;
4401 public:
4402 typedef lognormal_distribution distribution_type;
4403
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004405 explicit param_type(result_type __m = 0, result_type __s = 1)
4406 : __nd_(__m, __s) {}
4407
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004409 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004411 result_type s() const {return __nd_.stddev();}
4412
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004413 friend _LIBCPP_INLINE_VISIBILITY
4414 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004415 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004416 friend _LIBCPP_INLINE_VISIBILITY
4417 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004418 {return !(__x == __y);}
4419 friend class lognormal_distribution;
4420
4421 template <class _CharT, class _Traits, class _RT>
4422 friend
4423 basic_ostream<_CharT, _Traits>&
4424 operator<<(basic_ostream<_CharT, _Traits>& __os,
4425 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004426
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004427 template <class _CharT, class _Traits, class _RT>
4428 friend
4429 basic_istream<_CharT, _Traits>&
4430 operator>>(basic_istream<_CharT, _Traits>& __is,
4431 lognormal_distribution<_RT>& __x);
4432 };
4433
4434private:
4435 param_type __p_;
4436
4437public:
4438 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004440 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4441 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004443 explicit lognormal_distribution(const param_type& __p)
4444 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004446 void reset() {__p_.__nd_.reset();}
4447
4448 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004449 template<class _URNG>
4450 _LIBCPP_INLINE_VISIBILITY
4451 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004452 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004453 template<class _URNG>
4454 _LIBCPP_INLINE_VISIBILITY
4455 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004456 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004457
4458 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004460 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004462 result_type s() const {return __p_.s();}
4463
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004465 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004467 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004468
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004470 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004472 result_type max() const {return numeric_limits<result_type>::infinity();}
4473
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004474 friend _LIBCPP_INLINE_VISIBILITY
4475 bool operator==(const lognormal_distribution& __x,
4476 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004477 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004478 friend _LIBCPP_INLINE_VISIBILITY
4479 bool operator!=(const lognormal_distribution& __x,
4480 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004481 {return !(__x == __y);}
4482
4483 template <class _CharT, class _Traits, class _RT>
4484 friend
4485 basic_ostream<_CharT, _Traits>&
4486 operator<<(basic_ostream<_CharT, _Traits>& __os,
4487 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004488
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004489 template <class _CharT, class _Traits, class _RT>
4490 friend
4491 basic_istream<_CharT, _Traits>&
4492 operator>>(basic_istream<_CharT, _Traits>& __is,
4493 lognormal_distribution<_RT>& __x);
4494};
4495
4496template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004497inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004498basic_ostream<_CharT, _Traits>&
4499operator<<(basic_ostream<_CharT, _Traits>& __os,
4500 const lognormal_distribution<_RT>& __x)
4501{
4502 return __os << __x.__p_.__nd_;
4503}
4504
4505template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004506inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004507basic_istream<_CharT, _Traits>&
4508operator>>(basic_istream<_CharT, _Traits>& __is,
4509 lognormal_distribution<_RT>& __x)
4510{
4511 return __is >> __x.__p_.__nd_;
4512}
4513
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004514// poisson_distribution
4515
4516template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004517class _LIBCPP_VISIBLE poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004518{
4519public:
4520 // types
4521 typedef _IntType result_type;
4522
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004523 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004524 {
4525 double __mean_;
4526 double __s_;
4527 double __d_;
4528 double __l_;
4529 double __omega_;
4530 double __c0_;
4531 double __c1_;
4532 double __c2_;
4533 double __c3_;
4534 double __c_;
4535
4536 public:
4537 typedef poisson_distribution distribution_type;
4538
4539 explicit param_type(double __mean = 1.0);
4540
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004542 double mean() const {return __mean_;}
4543
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004544 friend _LIBCPP_INLINE_VISIBILITY
4545 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004546 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004547 friend _LIBCPP_INLINE_VISIBILITY
4548 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004549 {return !(__x == __y);}
4550
4551 friend class poisson_distribution;
4552 };
4553
4554private:
4555 param_type __p_;
4556
4557public:
4558 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004560 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004562 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004564 void reset() {}
4565
4566 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004567 template<class _URNG>
4568 _LIBCPP_INLINE_VISIBILITY
4569 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004570 {return (*this)(__g, __p_);}
4571 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4572
4573 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004575 double mean() const {return __p_.mean();}
4576
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004578 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004580 void param(const param_type& __p) {__p_ = __p;}
4581
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004583 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004585 result_type max() const {return numeric_limits<result_type>::max();}
4586
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004587 friend _LIBCPP_INLINE_VISIBILITY
4588 bool operator==(const poisson_distribution& __x,
4589 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004590 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004591 friend _LIBCPP_INLINE_VISIBILITY
4592 bool operator!=(const poisson_distribution& __x,
4593 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004594 {return !(__x == __y);}
4595};
4596
4597template<class _IntType>
4598poisson_distribution<_IntType>::param_type::param_type(double __mean)
4599 : __mean_(__mean)
4600{
4601 if (__mean_ < 10)
4602 {
4603 __s_ = 0;
4604 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004605 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004606 __omega_ = 0;
4607 __c3_ = 0;
4608 __c2_ = 0;
4609 __c1_ = 0;
4610 __c0_ = 0;
4611 __c_ = 0;
4612 }
4613 else
4614 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004615 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004616 __d_ = 6 * __mean_ * __mean_;
4617 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4618 __omega_ = .3989423 / __s_;
4619 double __b1_ = .4166667E-1 / __mean_;
4620 double __b2_ = .3 * __b1_ * __b1_;
4621 __c3_ = .1428571 * __b1_ * __b2_;
4622 __c2_ = __b2_ - 15. * __c3_;
4623 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4624 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4625 __c_ = .1069 / __mean_;
4626 }
4627}
4628
4629template <class _IntType>
4630template<class _URNG>
4631_IntType
4632poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4633{
4634 result_type __x;
4635 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004636 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004637 {
4638 __x = 0;
4639 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4640 __p *= __urd(__urng);
4641 }
4642 else
4643 {
4644 double __difmuk;
4645 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4646 double __u;
4647 if (__g > 0)
4648 {
4649 __x = static_cast<result_type>(__g);
4650 if (__x >= __pr.__l_)
4651 return __x;
4652 __difmuk = __pr.__mean_ - __x;
4653 __u = __urd(__urng);
4654 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4655 return __x;
4656 }
4657 exponential_distribution<double> __edist;
4658 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4659 {
4660 double __e;
4661 if (__using_exp_dist || __g < 0)
4662 {
4663 double __t;
4664 do
4665 {
4666 __e = __edist(__urng);
4667 __u = __urd(__urng);
4668 __u += __u - 1;
4669 __t = 1.8 + (__u < 0 ? -__e : __e);
4670 } while (__t <= -.6744);
4671 __x = __pr.__mean_ + __pr.__s_ * __t;
4672 __difmuk = __pr.__mean_ - __x;
4673 __using_exp_dist = true;
4674 }
4675 double __px;
4676 double __py;
4677 if (__x < 10)
4678 {
4679 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4680 40320, 362880};
4681 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004682 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004683 }
4684 else
4685 {
4686 double __del = .8333333E-1 / __x;
4687 __del -= 4.8 * __del * __del * __del;
4688 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004689 if (_VSTD::abs(__v) > 0.25)
4690 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004691 else
4692 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4693 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4694 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004695 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004696 }
4697 double __r = (0.5 - __difmuk) / __pr.__s_;
4698 double __r2 = __r * __r;
4699 double __fx = -0.5 * __r2;
4700 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4701 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4702 if (__using_exp_dist)
4703 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004704 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4705 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004706 break;
4707 }
4708 else
4709 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004710 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004711 break;
4712 }
4713 }
4714 }
4715 return __x;
4716}
4717
4718template <class _CharT, class _Traits, class _IntType>
4719basic_ostream<_CharT, _Traits>&
4720operator<<(basic_ostream<_CharT, _Traits>& __os,
4721 const poisson_distribution<_IntType>& __x)
4722{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004723 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004724 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4725 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004726 return __os << __x.mean();
4727}
4728
4729template <class _CharT, class _Traits, class _IntType>
4730basic_istream<_CharT, _Traits>&
4731operator>>(basic_istream<_CharT, _Traits>& __is,
4732 poisson_distribution<_IntType>& __x)
4733{
4734 typedef poisson_distribution<_IntType> _Eng;
4735 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004736 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004737 __is.flags(ios_base::dec | ios_base::skipws);
4738 double __mean;
4739 __is >> __mean;
4740 if (!__is.fail())
4741 __x.param(param_type(__mean));
4742 return __is;
4743}
4744
Howard Hinnant9de6e302010-05-16 01:09:02 +00004745// weibull_distribution
4746
4747template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004748class _LIBCPP_VISIBLE weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004749{
4750public:
4751 // types
4752 typedef _RealType result_type;
4753
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004754 class _LIBCPP_VISIBLE param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004755 {
4756 result_type __a_;
4757 result_type __b_;
4758 public:
4759 typedef weibull_distribution distribution_type;
4760
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004762 explicit param_type(result_type __a = 1, result_type __b = 1)
4763 : __a_(__a), __b_(__b) {}
4764
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004766 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004768 result_type b() const {return __b_;}
4769
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004770 friend _LIBCPP_INLINE_VISIBILITY
4771 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004772 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004773 friend _LIBCPP_INLINE_VISIBILITY
4774 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004775 {return !(__x == __y);}
4776 };
4777
4778private:
4779 param_type __p_;
4780
4781public:
4782 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004784 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4785 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004787 explicit weibull_distribution(const param_type& __p)
4788 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004790 void reset() {}
4791
4792 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004793 template<class _URNG>
4794 _LIBCPP_INLINE_VISIBILITY
4795 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004796 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004797 template<class _URNG>
4798 _LIBCPP_INLINE_VISIBILITY
4799 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004800 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004801 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:02 +00004802
4803 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004805 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004807 result_type b() const {return __p_.b();}
4808
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004810 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004812 void param(const param_type& __p) {__p_ = __p;}
4813
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004815 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004817 result_type max() const {return numeric_limits<result_type>::infinity();}
4818
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004819 friend _LIBCPP_INLINE_VISIBILITY
4820 bool operator==(const weibull_distribution& __x,
4821 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004822 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004823 friend _LIBCPP_INLINE_VISIBILITY
4824 bool operator!=(const weibull_distribution& __x,
4825 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004826 {return !(__x == __y);}
4827};
4828
4829template <class _CharT, class _Traits, class _RT>
4830basic_ostream<_CharT, _Traits>&
4831operator<<(basic_ostream<_CharT, _Traits>& __os,
4832 const weibull_distribution<_RT>& __x)
4833{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004834 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004835 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4836 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004837 _CharT __sp = __os.widen(' ');
4838 __os.fill(__sp);
4839 __os << __x.a() << __sp << __x.b();
4840 return __os;
4841}
4842
4843template <class _CharT, class _Traits, class _RT>
4844basic_istream<_CharT, _Traits>&
4845operator>>(basic_istream<_CharT, _Traits>& __is,
4846 weibull_distribution<_RT>& __x)
4847{
4848 typedef weibull_distribution<_RT> _Eng;
4849 typedef typename _Eng::result_type result_type;
4850 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004851 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004852 __is.flags(ios_base::dec | ios_base::skipws);
4853 result_type __a;
4854 result_type __b;
4855 __is >> __a >> __b;
4856 if (!__is.fail())
4857 __x.param(param_type(__a, __b));
4858 return __is;
4859}
4860
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004861template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004862class _LIBCPP_VISIBLE extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004863{
4864public:
4865 // types
4866 typedef _RealType result_type;
4867
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004868 class _LIBCPP_VISIBLE param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004869 {
4870 result_type __a_;
4871 result_type __b_;
4872 public:
4873 typedef extreme_value_distribution distribution_type;
4874
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004876 explicit param_type(result_type __a = 0, result_type __b = 1)
4877 : __a_(__a), __b_(__b) {}
4878
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004880 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004882 result_type b() const {return __b_;}
4883
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004884 friend _LIBCPP_INLINE_VISIBILITY
4885 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004886 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004887 friend _LIBCPP_INLINE_VISIBILITY
4888 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004889 {return !(__x == __y);}
4890 };
4891
4892private:
4893 param_type __p_;
4894
4895public:
4896 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004898 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4899 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004901 explicit extreme_value_distribution(const param_type& __p)
4902 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004904 void reset() {}
4905
4906 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004907 template<class _URNG>
4908 _LIBCPP_INLINE_VISIBILITY
4909 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004910 {return (*this)(__g, __p_);}
4911 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4912
4913 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004915 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004917 result_type b() const {return __p_.b();}
4918
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004920 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004922 void param(const param_type& __p) {__p_ = __p;}
4923
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004925 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004927 result_type max() const {return numeric_limits<result_type>::infinity();}
4928
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004929 friend _LIBCPP_INLINE_VISIBILITY
4930 bool operator==(const extreme_value_distribution& __x,
4931 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004932 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004933 friend _LIBCPP_INLINE_VISIBILITY
4934 bool operator!=(const extreme_value_distribution& __x,
4935 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004936 {return !(__x == __y);}
4937};
4938
4939template<class _RealType>
4940template<class _URNG>
4941_RealType
4942extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4943{
4944 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004945 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004946}
4947
4948template <class _CharT, class _Traits, class _RT>
4949basic_ostream<_CharT, _Traits>&
4950operator<<(basic_ostream<_CharT, _Traits>& __os,
4951 const extreme_value_distribution<_RT>& __x)
4952{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004953 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004954 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4955 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004956 _CharT __sp = __os.widen(' ');
4957 __os.fill(__sp);
4958 __os << __x.a() << __sp << __x.b();
4959 return __os;
4960}
4961
4962template <class _CharT, class _Traits, class _RT>
4963basic_istream<_CharT, _Traits>&
4964operator>>(basic_istream<_CharT, _Traits>& __is,
4965 extreme_value_distribution<_RT>& __x)
4966{
4967 typedef extreme_value_distribution<_RT> _Eng;
4968 typedef typename _Eng::result_type result_type;
4969 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004970 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004971 __is.flags(ios_base::dec | ios_base::skipws);
4972 result_type __a;
4973 result_type __b;
4974 __is >> __a >> __b;
4975 if (!__is.fail())
4976 __x.param(param_type(__a, __b));
4977 return __is;
4978}
4979
Howard Hinnantc7c49132010-05-13 17:58:28 +00004980// gamma_distribution
4981
4982template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004983class _LIBCPP_VISIBLE gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004984{
4985public:
4986 // types
4987 typedef _RealType result_type;
4988
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004989 class _LIBCPP_VISIBLE param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004990 {
4991 result_type __alpha_;
4992 result_type __beta_;
4993 public:
4994 typedef gamma_distribution distribution_type;
4995
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004997 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4998 : __alpha_(__alpha), __beta_(__beta) {}
4999
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005001 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005003 result_type beta() const {return __beta_;}
5004
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005005 friend _LIBCPP_INLINE_VISIBILITY
5006 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005007 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005008 friend _LIBCPP_INLINE_VISIBILITY
5009 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005010 {return !(__x == __y);}
5011 };
5012
5013private:
5014 param_type __p_;
5015
5016public:
5017 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005019 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
5020 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005022 explicit gamma_distribution(const param_type& __p)
5023 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005025 void reset() {}
5026
5027 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005028 template<class _URNG>
5029 _LIBCPP_INLINE_VISIBILITY
5030 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005031 {return (*this)(__g, __p_);}
5032 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5033
5034 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005036 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005038 result_type beta() const {return __p_.beta();}
5039
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005041 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005043 void param(const param_type& __p) {__p_ = __p;}
5044
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005046 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005048 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00005049
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005050 friend _LIBCPP_INLINE_VISIBILITY
5051 bool operator==(const gamma_distribution& __x,
5052 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005053 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005054 friend _LIBCPP_INLINE_VISIBILITY
5055 bool operator!=(const gamma_distribution& __x,
5056 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005057 {return !(__x == __y);}
5058};
5059
5060template <class _RealType>
5061template<class _URNG>
5062_RealType
5063gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5064{
Howard Hinnantf417abe2010-05-14 18:43:10 +00005065 result_type __a = __p.alpha();
5066 uniform_real_distribution<result_type> __gen(0, 1);
5067 exponential_distribution<result_type> __egen;
5068 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00005069 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00005070 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005071 else if (__a > 1)
5072 {
5073 const result_type __b = __a - 1;
5074 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005075 while (true)
5076 {
5077 const result_type __u = __gen(__g);
5078 const result_type __v = __gen(__g);
5079 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00005080 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005081 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005082 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:28 +00005083 (__u - result_type(0.5));
5084 __x = __b + __y;
5085 if (__x >= 0)
5086 {
5087 const result_type __z = 64 * __w * __w * __w * __v * __v;
5088 if (__z <= 1 - 2 * __y * __y / __x)
5089 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005090 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:28 +00005091 break;
5092 }
5093 }
5094 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00005095 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00005096 else // __a < 1
5097 {
5098 while (true)
5099 {
5100 const result_type __u = __gen(__g);
5101 const result_type __es = __egen(__g);
5102 if (__u <= 1 - __a)
5103 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005104 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00005105 if (__x <= __es)
5106 break;
5107 }
5108 else
5109 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005110 const result_type __e = -_VSTD::log((1-__u)/__a);
5111 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00005112 if (__x <= __e + __es)
5113 break;
5114 }
5115 }
5116 }
5117 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00005118}
5119
5120template <class _CharT, class _Traits, class _RT>
5121basic_ostream<_CharT, _Traits>&
5122operator<<(basic_ostream<_CharT, _Traits>& __os,
5123 const gamma_distribution<_RT>& __x)
5124{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005125 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005126 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5127 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005128 _CharT __sp = __os.widen(' ');
5129 __os.fill(__sp);
5130 __os << __x.alpha() << __sp << __x.beta();
5131 return __os;
5132}
5133
5134template <class _CharT, class _Traits, class _RT>
5135basic_istream<_CharT, _Traits>&
5136operator>>(basic_istream<_CharT, _Traits>& __is,
5137 gamma_distribution<_RT>& __x)
5138{
5139 typedef gamma_distribution<_RT> _Eng;
5140 typedef typename _Eng::result_type result_type;
5141 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005142 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005143 __is.flags(ios_base::dec | ios_base::skipws);
5144 result_type __alpha;
5145 result_type __beta;
5146 __is >> __alpha >> __beta;
5147 if (!__is.fail())
5148 __x.param(param_type(__alpha, __beta));
5149 return __is;
5150}
Howard Hinnanta64111c2010-05-12 21:02:31 +00005151
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005152// negative_binomial_distribution
5153
5154template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005155class _LIBCPP_VISIBLE negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005156{
5157public:
5158 // types
5159 typedef _IntType result_type;
5160
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005161 class _LIBCPP_VISIBLE param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005162 {
5163 result_type __k_;
5164 double __p_;
5165 public:
5166 typedef negative_binomial_distribution distribution_type;
5167
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005169 explicit param_type(result_type __k = 1, double __p = 0.5)
5170 : __k_(__k), __p_(__p) {}
5171
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005173 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005175 double p() const {return __p_;}
5176
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005177 friend _LIBCPP_INLINE_VISIBILITY
5178 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005179 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005180 friend _LIBCPP_INLINE_VISIBILITY
5181 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005182 {return !(__x == __y);}
5183 };
5184
5185private:
5186 param_type __p_;
5187
5188public:
5189 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005191 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5192 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005194 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005196 void reset() {}
5197
5198 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005199 template<class _URNG>
5200 _LIBCPP_INLINE_VISIBILITY
5201 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005202 {return (*this)(__g, __p_);}
5203 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5204
5205 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005207 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005209 double p() const {return __p_.p();}
5210
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005212 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005214 void param(const param_type& __p) {__p_ = __p;}
5215
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005217 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005219 result_type max() const {return numeric_limits<result_type>::max();}
5220
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005221 friend _LIBCPP_INLINE_VISIBILITY
5222 bool operator==(const negative_binomial_distribution& __x,
5223 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005224 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005225 friend _LIBCPP_INLINE_VISIBILITY
5226 bool operator!=(const negative_binomial_distribution& __x,
5227 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005228 {return !(__x == __y);}
5229};
5230
5231template <class _IntType>
5232template<class _URNG>
5233_IntType
5234negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5235{
5236 result_type __k = __pr.k();
5237 double __p = __pr.p();
5238 if (__k <= 21 * __p)
5239 {
5240 bernoulli_distribution __gen(__p);
5241 result_type __f = 0;
5242 result_type __s = 0;
5243 while (__s < __k)
5244 {
5245 if (__gen(__urng))
5246 ++__s;
5247 else
5248 ++__f;
5249 }
5250 return __f;
5251 }
5252 return poisson_distribution<result_type>(gamma_distribution<double>
5253 (__k, (1-__p)/__p)(__urng))(__urng);
5254}
5255
5256template <class _CharT, class _Traits, class _IntType>
5257basic_ostream<_CharT, _Traits>&
5258operator<<(basic_ostream<_CharT, _Traits>& __os,
5259 const negative_binomial_distribution<_IntType>& __x)
5260{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005261 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005262 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5263 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005264 _CharT __sp = __os.widen(' ');
5265 __os.fill(__sp);
5266 return __os << __x.k() << __sp << __x.p();
5267}
5268
5269template <class _CharT, class _Traits, class _IntType>
5270basic_istream<_CharT, _Traits>&
5271operator>>(basic_istream<_CharT, _Traits>& __is,
5272 negative_binomial_distribution<_IntType>& __x)
5273{
5274 typedef negative_binomial_distribution<_IntType> _Eng;
5275 typedef typename _Eng::result_type result_type;
5276 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005277 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005278 __is.flags(ios_base::dec | ios_base::skipws);
5279 result_type __k;
5280 double __p;
5281 __is >> __k >> __p;
5282 if (!__is.fail())
5283 __x.param(param_type(__k, __p));
5284 return __is;
5285}
5286
Howard Hinnant34e8a572010-05-17 13:44:27 +00005287// geometric_distribution
5288
5289template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005290class _LIBCPP_VISIBLE geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005291{
5292public:
5293 // types
5294 typedef _IntType result_type;
5295
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005296 class _LIBCPP_VISIBLE param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005297 {
5298 double __p_;
5299 public:
5300 typedef geometric_distribution distribution_type;
5301
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005303 explicit param_type(double __p = 0.5) : __p_(__p) {}
5304
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005306 double p() const {return __p_;}
5307
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005308 friend _LIBCPP_INLINE_VISIBILITY
5309 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005310 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005311 friend _LIBCPP_INLINE_VISIBILITY
5312 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005313 {return !(__x == __y);}
5314 };
5315
5316private:
5317 param_type __p_;
5318
5319public:
5320 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005322 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005324 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005326 void reset() {}
5327
5328 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005329 template<class _URNG>
5330 _LIBCPP_INLINE_VISIBILITY
5331 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005332 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005333 template<class _URNG>
5334 _LIBCPP_INLINE_VISIBILITY
5335 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005336 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5337
5338 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005340 double p() const {return __p_.p();}
5341
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005343 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005345 void param(const param_type& __p) {__p_ = __p;}
5346
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005348 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005350 result_type max() const {return numeric_limits<result_type>::max();}
5351
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005352 friend _LIBCPP_INLINE_VISIBILITY
5353 bool operator==(const geometric_distribution& __x,
5354 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005355 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005356 friend _LIBCPP_INLINE_VISIBILITY
5357 bool operator!=(const geometric_distribution& __x,
5358 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005359 {return !(__x == __y);}
5360};
5361
5362template <class _CharT, class _Traits, class _IntType>
5363basic_ostream<_CharT, _Traits>&
5364operator<<(basic_ostream<_CharT, _Traits>& __os,
5365 const geometric_distribution<_IntType>& __x)
5366{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005367 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005368 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5369 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005370 return __os << __x.p();
5371}
5372
5373template <class _CharT, class _Traits, class _IntType>
5374basic_istream<_CharT, _Traits>&
5375operator>>(basic_istream<_CharT, _Traits>& __is,
5376 geometric_distribution<_IntType>& __x)
5377{
5378 typedef geometric_distribution<_IntType> _Eng;
5379 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005380 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005381 __is.flags(ios_base::dec | ios_base::skipws);
5382 double __p;
5383 __is >> __p;
5384 if (!__is.fail())
5385 __x.param(param_type(__p));
5386 return __is;
5387}
5388
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005389// chi_squared_distribution
5390
5391template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005392class _LIBCPP_VISIBLE chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005393{
5394public:
5395 // types
5396 typedef _RealType result_type;
5397
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005398 class _LIBCPP_VISIBLE param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005399 {
5400 result_type __n_;
5401 public:
5402 typedef chi_squared_distribution distribution_type;
5403
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005405 explicit param_type(result_type __n = 1) : __n_(__n) {}
5406
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005408 result_type n() const {return __n_;}
5409
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005410 friend _LIBCPP_INLINE_VISIBILITY
5411 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005412 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005413 friend _LIBCPP_INLINE_VISIBILITY
5414 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005415 {return !(__x == __y);}
5416 };
5417
5418private:
5419 param_type __p_;
5420
5421public:
5422 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005424 explicit chi_squared_distribution(result_type __n = 1)
5425 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005427 explicit chi_squared_distribution(const param_type& __p)
5428 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005430 void reset() {}
5431
5432 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005433 template<class _URNG>
5434 _LIBCPP_INLINE_VISIBILITY
5435 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005436 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005437 template<class _URNG>
5438 _LIBCPP_INLINE_VISIBILITY
5439 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005440 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5441
5442 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005444 result_type n() const {return __p_.n();}
5445
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005447 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005449 void param(const param_type& __p) {__p_ = __p;}
5450
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005452 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005454 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005455
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005456 friend _LIBCPP_INLINE_VISIBILITY
5457 bool operator==(const chi_squared_distribution& __x,
5458 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005459 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005460 friend _LIBCPP_INLINE_VISIBILITY
5461 bool operator!=(const chi_squared_distribution& __x,
5462 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005463 {return !(__x == __y);}
5464};
5465
5466template <class _CharT, class _Traits, class _RT>
5467basic_ostream<_CharT, _Traits>&
5468operator<<(basic_ostream<_CharT, _Traits>& __os,
5469 const chi_squared_distribution<_RT>& __x)
5470{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005471 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005472 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5473 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005474 __os << __x.n();
5475 return __os;
5476}
5477
5478template <class _CharT, class _Traits, class _RT>
5479basic_istream<_CharT, _Traits>&
5480operator>>(basic_istream<_CharT, _Traits>& __is,
5481 chi_squared_distribution<_RT>& __x)
5482{
5483 typedef chi_squared_distribution<_RT> _Eng;
5484 typedef typename _Eng::result_type result_type;
5485 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005486 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005487 __is.flags(ios_base::dec | ios_base::skipws);
5488 result_type __n;
5489 __is >> __n;
5490 if (!__is.fail())
5491 __x.param(param_type(__n));
5492 return __is;
5493}
5494
Howard Hinnantd7d01132010-05-17 21:55:46 +00005495// cauchy_distribution
5496
5497template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005498class _LIBCPP_VISIBLE cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005499{
5500public:
5501 // types
5502 typedef _RealType result_type;
5503
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005504 class _LIBCPP_VISIBLE param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005505 {
5506 result_type __a_;
5507 result_type __b_;
5508 public:
5509 typedef cauchy_distribution distribution_type;
5510
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005512 explicit param_type(result_type __a = 0, result_type __b = 1)
5513 : __a_(__a), __b_(__b) {}
5514
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005516 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005518 result_type b() const {return __b_;}
5519
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005520 friend _LIBCPP_INLINE_VISIBILITY
5521 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005522 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005523 friend _LIBCPP_INLINE_VISIBILITY
5524 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005525 {return !(__x == __y);}
5526 };
5527
5528private:
5529 param_type __p_;
5530
5531public:
5532 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005534 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5535 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005537 explicit cauchy_distribution(const param_type& __p)
5538 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005540 void reset() {}
5541
5542 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005543 template<class _URNG>
5544 _LIBCPP_INLINE_VISIBILITY
5545 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005546 {return (*this)(__g, __p_);}
5547 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5548
5549 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005551 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005553 result_type b() const {return __p_.b();}
5554
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005556 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005558 void param(const param_type& __p) {__p_ = __p;}
5559
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005561 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005563 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005564
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005565 friend _LIBCPP_INLINE_VISIBILITY
5566 bool operator==(const cauchy_distribution& __x,
5567 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005568 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005569 friend _LIBCPP_INLINE_VISIBILITY
5570 bool operator!=(const cauchy_distribution& __x,
5571 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005572 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005573};
5574
5575template <class _RealType>
5576template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005577inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005578_RealType
5579cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5580{
5581 uniform_real_distribution<result_type> __gen;
5582 // 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 +00005583 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:46 +00005584}
5585
5586template <class _CharT, class _Traits, class _RT>
5587basic_ostream<_CharT, _Traits>&
5588operator<<(basic_ostream<_CharT, _Traits>& __os,
5589 const cauchy_distribution<_RT>& __x)
5590{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005591 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005592 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5593 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005594 _CharT __sp = __os.widen(' ');
5595 __os.fill(__sp);
5596 __os << __x.a() << __sp << __x.b();
5597 return __os;
5598}
5599
5600template <class _CharT, class _Traits, class _RT>
5601basic_istream<_CharT, _Traits>&
5602operator>>(basic_istream<_CharT, _Traits>& __is,
5603 cauchy_distribution<_RT>& __x)
5604{
5605 typedef cauchy_distribution<_RT> _Eng;
5606 typedef typename _Eng::result_type result_type;
5607 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005608 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005609 __is.flags(ios_base::dec | ios_base::skipws);
5610 result_type __a;
5611 result_type __b;
5612 __is >> __a >> __b;
5613 if (!__is.fail())
5614 __x.param(param_type(__a, __b));
5615 return __is;
5616}
5617
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005618// fisher_f_distribution
5619
5620template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005621class _LIBCPP_VISIBLE fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005622{
5623public:
5624 // types
5625 typedef _RealType result_type;
5626
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005627 class _LIBCPP_VISIBLE param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005628 {
5629 result_type __m_;
5630 result_type __n_;
5631 public:
5632 typedef fisher_f_distribution distribution_type;
5633
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005635 explicit param_type(result_type __m = 1, result_type __n = 1)
5636 : __m_(__m), __n_(__n) {}
5637
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005639 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005641 result_type n() const {return __n_;}
5642
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005643 friend _LIBCPP_INLINE_VISIBILITY
5644 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005645 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005646 friend _LIBCPP_INLINE_VISIBILITY
5647 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005648 {return !(__x == __y);}
5649 };
5650
5651private:
5652 param_type __p_;
5653
5654public:
5655 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005657 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5658 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005660 explicit fisher_f_distribution(const param_type& __p)
5661 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005663 void reset() {}
5664
5665 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005666 template<class _URNG>
5667 _LIBCPP_INLINE_VISIBILITY
5668 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005669 {return (*this)(__g, __p_);}
5670 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5671
5672 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005674 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005676 result_type n() const {return __p_.n();}
5677
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005679 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005681 void param(const param_type& __p) {__p_ = __p;}
5682
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005684 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005686 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005687
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005688 friend _LIBCPP_INLINE_VISIBILITY
5689 bool operator==(const fisher_f_distribution& __x,
5690 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005691 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005692 friend _LIBCPP_INLINE_VISIBILITY
5693 bool operator!=(const fisher_f_distribution& __x,
5694 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005695 {return !(__x == __y);}
5696};
5697
5698template <class _RealType>
5699template<class _URNG>
5700_RealType
5701fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5702{
5703 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5704 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5705 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5706}
5707
5708template <class _CharT, class _Traits, class _RT>
5709basic_ostream<_CharT, _Traits>&
5710operator<<(basic_ostream<_CharT, _Traits>& __os,
5711 const fisher_f_distribution<_RT>& __x)
5712{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005713 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005714 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5715 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005716 _CharT __sp = __os.widen(' ');
5717 __os.fill(__sp);
5718 __os << __x.m() << __sp << __x.n();
5719 return __os;
5720}
5721
5722template <class _CharT, class _Traits, class _RT>
5723basic_istream<_CharT, _Traits>&
5724operator>>(basic_istream<_CharT, _Traits>& __is,
5725 fisher_f_distribution<_RT>& __x)
5726{
5727 typedef fisher_f_distribution<_RT> _Eng;
5728 typedef typename _Eng::result_type result_type;
5729 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005730 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005731 __is.flags(ios_base::dec | ios_base::skipws);
5732 result_type __m;
5733 result_type __n;
5734 __is >> __m >> __n;
5735 if (!__is.fail())
5736 __x.param(param_type(__m, __n));
5737 return __is;
5738}
5739
Howard Hinnant551d8e42010-05-19 01:53:57 +00005740// student_t_distribution
5741
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005742template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005743class _LIBCPP_VISIBLE student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005744{
5745public:
5746 // types
5747 typedef _RealType result_type;
5748
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005749 class _LIBCPP_VISIBLE param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005750 {
5751 result_type __n_;
5752 public:
5753 typedef student_t_distribution distribution_type;
5754
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005756 explicit param_type(result_type __n = 1) : __n_(__n) {}
5757
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005759 result_type n() const {return __n_;}
5760
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005761 friend _LIBCPP_INLINE_VISIBILITY
5762 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005763 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005764 friend _LIBCPP_INLINE_VISIBILITY
5765 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005766 {return !(__x == __y);}
5767 };
5768
5769private:
5770 param_type __p_;
5771 normal_distribution<result_type> __nd_;
5772
5773public:
5774 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005776 explicit student_t_distribution(result_type __n = 1)
5777 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005779 explicit student_t_distribution(const param_type& __p)
5780 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005782 void reset() {__nd_.reset();}
5783
5784 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005785 template<class _URNG>
5786 _LIBCPP_INLINE_VISIBILITY
5787 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005788 {return (*this)(__g, __p_);}
5789 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5790
5791 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005793 result_type n() const {return __p_.n();}
5794
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005796 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005798 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005799
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005801 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005803 result_type max() const {return numeric_limits<result_type>::infinity();}
5804
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005805 friend _LIBCPP_INLINE_VISIBILITY
5806 bool operator==(const student_t_distribution& __x,
5807 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005808 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005809 friend _LIBCPP_INLINE_VISIBILITY
5810 bool operator!=(const student_t_distribution& __x,
5811 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005812 {return !(__x == __y);}
5813};
5814
5815template <class _RealType>
5816template<class _URNG>
5817_RealType
5818student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5819{
5820 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005821 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005822}
5823
5824template <class _CharT, class _Traits, class _RT>
5825basic_ostream<_CharT, _Traits>&
5826operator<<(basic_ostream<_CharT, _Traits>& __os,
5827 const student_t_distribution<_RT>& __x)
5828{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005829 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005830 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5831 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005832 __os << __x.n();
5833 return __os;
5834}
5835
5836template <class _CharT, class _Traits, class _RT>
5837basic_istream<_CharT, _Traits>&
5838operator>>(basic_istream<_CharT, _Traits>& __is,
5839 student_t_distribution<_RT>& __x)
5840{
5841 typedef student_t_distribution<_RT> _Eng;
5842 typedef typename _Eng::result_type result_type;
5843 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005844 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005845 __is.flags(ios_base::dec | ios_base::skipws);
5846 result_type __n;
5847 __is >> __n;
5848 if (!__is.fail())
5849 __x.param(param_type(__n));
5850 return __is;
5851}
5852
Howard Hinnant551d8e42010-05-19 01:53:57 +00005853// discrete_distribution
5854
5855template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005856class _LIBCPP_VISIBLE discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005857{
5858public:
5859 // types
5860 typedef _IntType result_type;
5861
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005862 class _LIBCPP_VISIBLE param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005863 {
5864 vector<double> __p_;
5865 public:
5866 typedef discrete_distribution distribution_type;
5867
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005869 param_type() {}
5870 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005872 param_type(_InputIterator __f, _InputIterator __l)
5873 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005874#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005876 param_type(initializer_list<double> __wl)
5877 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005878#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005879 template<class _UnaryOperation>
5880 param_type(size_t __nw, double __xmin, double __xmax,
5881 _UnaryOperation __fw);
5882
5883 vector<double> probabilities() const;
5884
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005885 friend _LIBCPP_INLINE_VISIBILITY
5886 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005887 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005888 friend _LIBCPP_INLINE_VISIBILITY
5889 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005890 {return !(__x == __y);}
5891
5892 private:
5893 void __init();
5894
5895 friend class discrete_distribution;
5896
5897 template <class _CharT, class _Traits, class _IT>
5898 friend
5899 basic_ostream<_CharT, _Traits>&
5900 operator<<(basic_ostream<_CharT, _Traits>& __os,
5901 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005902
Howard Hinnant551d8e42010-05-19 01:53:57 +00005903 template <class _CharT, class _Traits, class _IT>
5904 friend
5905 basic_istream<_CharT, _Traits>&
5906 operator>>(basic_istream<_CharT, _Traits>& __is,
5907 discrete_distribution<_IT>& __x);
5908 };
5909
5910private:
5911 param_type __p_;
5912
5913public:
5914 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005916 discrete_distribution() {}
5917 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005919 discrete_distribution(_InputIterator __f, _InputIterator __l)
5920 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005921#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005923 discrete_distribution(initializer_list<double> __wl)
5924 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005925#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005926 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005928 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5929 _UnaryOperation __fw)
5930 : __p_(__nw, __xmin, __xmax, __fw) {}
5931 explicit discrete_distribution(const param_type& __p)
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005933 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005935 void reset() {}
5936
5937 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005938 template<class _URNG>
5939 _LIBCPP_INLINE_VISIBILITY
5940 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005941 {return (*this)(__g, __p_);}
5942 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5943
5944 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005946 vector<double> probabilities() const {return __p_.probabilities();}
5947
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005949 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005951 void param(const param_type& __p) {__p_ = __p;}
5952
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005954 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005956 result_type max() const {return __p_.__p_.size();}
5957
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005958 friend _LIBCPP_INLINE_VISIBILITY
5959 bool operator==(const discrete_distribution& __x,
5960 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005961 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005962 friend _LIBCPP_INLINE_VISIBILITY
5963 bool operator!=(const discrete_distribution& __x,
5964 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005965 {return !(__x == __y);}
5966
5967 template <class _CharT, class _Traits, class _IT>
5968 friend
5969 basic_ostream<_CharT, _Traits>&
5970 operator<<(basic_ostream<_CharT, _Traits>& __os,
5971 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005972
Howard Hinnant551d8e42010-05-19 01:53:57 +00005973 template <class _CharT, class _Traits, class _IT>
5974 friend
5975 basic_istream<_CharT, _Traits>&
5976 operator>>(basic_istream<_CharT, _Traits>& __is,
5977 discrete_distribution<_IT>& __x);
5978};
5979
5980template<class _IntType>
5981template<class _UnaryOperation>
5982discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5983 double __xmin,
5984 double __xmax,
5985 _UnaryOperation __fw)
5986{
5987 if (__nw > 1)
5988 {
5989 __p_.reserve(__nw - 1);
5990 double __d = (__xmax - __xmin) / __nw;
5991 double __d2 = __d / 2;
5992 for (size_t __k = 0; __k < __nw; ++__k)
5993 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5994 __init();
5995 }
5996}
5997
5998template<class _IntType>
5999void
6000discrete_distribution<_IntType>::param_type::__init()
6001{
6002 if (!__p_.empty())
6003 {
6004 if (__p_.size() > 1)
6005 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00006006 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
6007 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:57 +00006008 __i < __e; ++__i)
6009 *__i /= __s;
6010 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006011 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00006012 swap(__p_, __t);
6013 }
6014 else
6015 {
6016 __p_.clear();
6017 __p_.shrink_to_fit();
6018 }
6019 }
6020}
6021
6022template<class _IntType>
6023vector<double>
6024discrete_distribution<_IntType>::param_type::probabilities() const
6025{
6026 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00006027 _VSTD::vector<double> __p(__n+1);
6028 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00006029 if (__n > 0)
6030 __p[__n] = 1 - __p_[__n-1];
6031 else
6032 __p[0] = 1;
6033 return __p;
6034}
6035
6036template<class _IntType>
6037template<class _URNG>
6038_IntType
6039discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6040{
6041 uniform_real_distribution<double> __gen;
6042 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:19 +00006043 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:57 +00006044 __p.__p_.begin());
6045}
6046
6047template <class _CharT, class _Traits, class _IT>
6048basic_ostream<_CharT, _Traits>&
6049operator<<(basic_ostream<_CharT, _Traits>& __os,
6050 const discrete_distribution<_IT>& __x)
6051{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006052 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006053 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6054 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00006055 _CharT __sp = __os.widen(' ');
6056 __os.fill(__sp);
6057 size_t __n = __x.__p_.__p_.size();
6058 __os << __n;
6059 for (size_t __i = 0; __i < __n; ++__i)
6060 __os << __sp << __x.__p_.__p_[__i];
6061 return __os;
6062}
6063
6064template <class _CharT, class _Traits, class _IT>
6065basic_istream<_CharT, _Traits>&
6066operator>>(basic_istream<_CharT, _Traits>& __is,
6067 discrete_distribution<_IT>& __x)
6068{
6069 typedef discrete_distribution<_IT> _Eng;
6070 typedef typename _Eng::result_type result_type;
6071 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006072 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant551d8e42010-05-19 01:53:57 +00006073 __is.flags(ios_base::dec | ios_base::skipws);
6074 size_t __n;
6075 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006076 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00006077 for (size_t __i = 0; __i < __n; ++__i)
6078 __is >> __p[__i];
6079 if (!__is.fail())
6080 swap(__x.__p_.__p_, __p);
6081 return __is;
6082}
6083
Howard Hinnantd6d11712010-05-20 15:11:46 +00006084// piecewise_constant_distribution
6085
6086template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006087class _LIBCPP_VISIBLE piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00006088{
6089public:
6090 // types
6091 typedef _RealType result_type;
6092
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006093 class _LIBCPP_VISIBLE param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00006094 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006095 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006096 vector<result_type> __densities_;
6097 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006098 public:
6099 typedef piecewise_constant_distribution distribution_type;
6100
6101 param_type();
6102 template<class _InputIteratorB, class _InputIteratorW>
6103 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6104 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006105#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006106 template<class _UnaryOperation>
6107 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006108#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006109 template<class _UnaryOperation>
6110 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6111 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006112 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006113
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006115 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006117 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006118
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006119 friend _LIBCPP_INLINE_VISIBILITY
6120 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00006121 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006122 friend _LIBCPP_INLINE_VISIBILITY
6123 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006124 {return !(__x == __y);}
6125
6126 private:
6127 void __init();
6128
6129 friend class piecewise_constant_distribution;
6130
6131 template <class _CharT, class _Traits, class _RT>
6132 friend
6133 basic_ostream<_CharT, _Traits>&
6134 operator<<(basic_ostream<_CharT, _Traits>& __os,
6135 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006136
Howard Hinnantd6d11712010-05-20 15:11:46 +00006137 template <class _CharT, class _Traits, class _RT>
6138 friend
6139 basic_istream<_CharT, _Traits>&
6140 operator>>(basic_istream<_CharT, _Traits>& __is,
6141 piecewise_constant_distribution<_RT>& __x);
6142 };
6143
6144private:
6145 param_type __p_;
6146
6147public:
6148 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006150 piecewise_constant_distribution() {}
6151 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006153 piecewise_constant_distribution(_InputIteratorB __fB,
6154 _InputIteratorB __lB,
6155 _InputIteratorW __fW)
6156 : __p_(__fB, __lB, __fW) {}
6157
Howard Hinnante3e32912011-08-12 21:56:02 +00006158#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006159 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006161 piecewise_constant_distribution(initializer_list<result_type> __bl,
6162 _UnaryOperation __fw)
6163 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006164#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006165
6166 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006168 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6169 result_type __xmax, _UnaryOperation __fw)
6170 : __p_(__nw, __xmin, __xmax, __fw) {}
6171
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006173 explicit piecewise_constant_distribution(const param_type& __p)
6174 : __p_(__p) {}
6175
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006177 void reset() {}
6178
6179 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006180 template<class _URNG>
6181 _LIBCPP_INLINE_VISIBILITY
6182 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006183 {return (*this)(__g, __p_);}
6184 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6185
6186 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006188 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006190 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006191
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006193 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006195 void param(const param_type& __p) {__p_ = __p;}
6196
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006198 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006200 result_type max() const {return __p_.__b_.back();}
6201
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006202 friend _LIBCPP_INLINE_VISIBILITY
6203 bool operator==(const piecewise_constant_distribution& __x,
6204 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006205 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006206 friend _LIBCPP_INLINE_VISIBILITY
6207 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006208 const piecewise_constant_distribution& __y)
6209 {return !(__x == __y);}
6210
6211 template <class _CharT, class _Traits, class _RT>
6212 friend
6213 basic_ostream<_CharT, _Traits>&
6214 operator<<(basic_ostream<_CharT, _Traits>& __os,
6215 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006216
Howard Hinnantd6d11712010-05-20 15:11:46 +00006217 template <class _CharT, class _Traits, class _RT>
6218 friend
6219 basic_istream<_CharT, _Traits>&
6220 operator>>(basic_istream<_CharT, _Traits>& __is,
6221 piecewise_constant_distribution<_RT>& __x);
6222};
6223
6224template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006225typename piecewise_constant_distribution<_RealType>::param_type &
6226piecewise_constant_distribution<_RealType>::param_type::operator=
6227 (const param_type& __rhs)
6228{
6229// These can throw
6230 __b_.reserve (__rhs.__b_.size ());
6231 __densities_.reserve(__rhs.__densities_.size());
6232 __areas_.reserve (__rhs.__areas_.size());
6233
6234// These can not throw
6235 __b_ = __rhs.__b_;
6236 __densities_ = __rhs.__densities_;
6237 __areas_ = __rhs.__areas_;
6238 return *this;
6239}
6240
6241template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006242void
6243piecewise_constant_distribution<_RealType>::param_type::__init()
6244{
Howard Hinnant2a592542010-05-24 00:35:40 +00006245 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:19 +00006246 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006247 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006248 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006249 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6250 __densities_[__i] /= __total_area;
6251 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006252 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:19 +00006253 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:40 +00006254 __areas_.begin() + 1);
6255 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6256 __densities_.back() = 1 - __areas_.back(); // correct round off error
6257 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6258 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6259 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006260}
6261
6262template<class _RealType>
6263piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006264 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006265 __densities_(1, 1.0),
6266 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006267{
6268 __b_[1] = 1;
6269}
6270
6271template<class _RealType>
6272template<class _InputIteratorB, class _InputIteratorW>
6273piecewise_constant_distribution<_RealType>::param_type::param_type(
6274 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6275 : __b_(__fB, __lB)
6276{
6277 if (__b_.size() < 2)
6278 {
6279 __b_.resize(2);
6280 __b_[0] = 0;
6281 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006282 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006283 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006284 }
6285 else
6286 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006287 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006288 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006289 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006290 __init();
6291 }
6292}
6293
Howard Hinnante3e32912011-08-12 21:56:02 +00006294#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6295
Howard Hinnantd6d11712010-05-20 15:11:46 +00006296template<class _RealType>
6297template<class _UnaryOperation>
6298piecewise_constant_distribution<_RealType>::param_type::param_type(
6299 initializer_list<result_type> __bl, _UnaryOperation __fw)
6300 : __b_(__bl.begin(), __bl.end())
6301{
6302 if (__b_.size() < 2)
6303 {
6304 __b_.resize(2);
6305 __b_[0] = 0;
6306 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006307 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006308 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006309 }
6310 else
6311 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006312 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006313 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006314 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006315 __init();
6316 }
6317}
6318
Howard Hinnante3e32912011-08-12 21:56:02 +00006319#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6320
Howard Hinnantd6d11712010-05-20 15:11:46 +00006321template<class _RealType>
6322template<class _UnaryOperation>
6323piecewise_constant_distribution<_RealType>::param_type::param_type(
6324 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6325 : __b_(__nw == 0 ? 2 : __nw + 1)
6326{
6327 size_t __n = __b_.size() - 1;
6328 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006329 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006330 for (size_t __i = 0; __i < __n; ++__i)
6331 {
6332 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006333 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006334 }
6335 __b_[__n] = __xmax;
6336 __init();
6337}
6338
6339template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006340template<class _URNG>
6341_RealType
6342piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6343{
6344 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006345 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006346 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006347 __u) - __p.__areas_.begin() - 1;
6348 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006349}
6350
6351template <class _CharT, class _Traits, class _RT>
6352basic_ostream<_CharT, _Traits>&
6353operator<<(basic_ostream<_CharT, _Traits>& __os,
6354 const piecewise_constant_distribution<_RT>& __x)
6355{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006356 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006357 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6358 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006359 _CharT __sp = __os.widen(' ');
6360 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006361 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006362 __os << __n;
6363 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006364 __os << __sp << __x.__p_.__b_[__i];
6365 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006366 __os << __sp << __n;
6367 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006368 __os << __sp << __x.__p_.__densities_[__i];
6369 __n = __x.__p_.__areas_.size();
6370 __os << __sp << __n;
6371 for (size_t __i = 0; __i < __n; ++__i)
6372 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006373 return __os;
6374}
6375
6376template <class _CharT, class _Traits, class _RT>
6377basic_istream<_CharT, _Traits>&
6378operator>>(basic_istream<_CharT, _Traits>& __is,
6379 piecewise_constant_distribution<_RT>& __x)
6380{
6381 typedef piecewise_constant_distribution<_RT> _Eng;
6382 typedef typename _Eng::result_type result_type;
6383 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006384 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006385 __is.flags(ios_base::dec | ios_base::skipws);
6386 size_t __n;
6387 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006388 vector<result_type> __b(__n);
6389 for (size_t __i = 0; __i < __n; ++__i)
6390 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006391 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006392 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006393 for (size_t __i = 0; __i < __n; ++__i)
6394 __is >> __densities[__i];
6395 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006396 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006397 for (size_t __i = 0; __i < __n; ++__i)
6398 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006399 if (!__is.fail())
6400 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006401 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006402 swap(__x.__p_.__densities_, __densities);
6403 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006404 }
6405 return __is;
6406}
6407
Howard Hinnant54305402010-05-25 00:27:34 +00006408// piecewise_linear_distribution
6409
6410template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006411class _LIBCPP_VISIBLE piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006412{
6413public:
6414 // types
6415 typedef _RealType result_type;
6416
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006417 class _LIBCPP_VISIBLE param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006418 {
Howard Hinnant54305402010-05-25 00:27:34 +00006419 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006420 vector<result_type> __densities_;
6421 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006422 public:
6423 typedef piecewise_linear_distribution distribution_type;
6424
6425 param_type();
6426 template<class _InputIteratorB, class _InputIteratorW>
6427 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6428 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006429#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006430 template<class _UnaryOperation>
6431 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006432#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006433 template<class _UnaryOperation>
6434 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6435 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006436 param_type & operator=(const param_type& __rhs);
6437
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006439 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006441 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006442
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006443 friend _LIBCPP_INLINE_VISIBILITY
6444 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006445 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006446 friend _LIBCPP_INLINE_VISIBILITY
6447 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006448 {return !(__x == __y);}
6449
6450 private:
6451 void __init();
6452
6453 friend class piecewise_linear_distribution;
6454
6455 template <class _CharT, class _Traits, class _RT>
6456 friend
6457 basic_ostream<_CharT, _Traits>&
6458 operator<<(basic_ostream<_CharT, _Traits>& __os,
6459 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006460
Howard Hinnant54305402010-05-25 00:27:34 +00006461 template <class _CharT, class _Traits, class _RT>
6462 friend
6463 basic_istream<_CharT, _Traits>&
6464 operator>>(basic_istream<_CharT, _Traits>& __is,
6465 piecewise_linear_distribution<_RT>& __x);
6466 };
6467
6468private:
6469 param_type __p_;
6470
6471public:
6472 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006474 piecewise_linear_distribution() {}
6475 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006477 piecewise_linear_distribution(_InputIteratorB __fB,
6478 _InputIteratorB __lB,
6479 _InputIteratorW __fW)
6480 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006481
Howard Hinnante3e32912011-08-12 21:56:02 +00006482#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006483 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006485 piecewise_linear_distribution(initializer_list<result_type> __bl,
6486 _UnaryOperation __fw)
6487 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006488#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006489
6490 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006492 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6493 result_type __xmax, _UnaryOperation __fw)
6494 : __p_(__nw, __xmin, __xmax, __fw) {}
6495
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006497 explicit piecewise_linear_distribution(const param_type& __p)
6498 : __p_(__p) {}
6499
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006501 void reset() {}
6502
6503 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006504 template<class _URNG>
6505 _LIBCPP_INLINE_VISIBILITY
6506 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006507 {return (*this)(__g, __p_);}
6508 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6509
6510 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006512 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006514 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006515
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006517 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006519 void param(const param_type& __p) {__p_ = __p;}
6520
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006522 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006524 result_type max() const {return __p_.__b_.back();}
6525
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006526 friend _LIBCPP_INLINE_VISIBILITY
6527 bool operator==(const piecewise_linear_distribution& __x,
6528 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006529 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006530 friend _LIBCPP_INLINE_VISIBILITY
6531 bool operator!=(const piecewise_linear_distribution& __x,
6532 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006533 {return !(__x == __y);}
6534
6535 template <class _CharT, class _Traits, class _RT>
6536 friend
6537 basic_ostream<_CharT, _Traits>&
6538 operator<<(basic_ostream<_CharT, _Traits>& __os,
6539 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006540
Howard Hinnant54305402010-05-25 00:27:34 +00006541 template <class _CharT, class _Traits, class _RT>
6542 friend
6543 basic_istream<_CharT, _Traits>&
6544 operator>>(basic_istream<_CharT, _Traits>& __is,
6545 piecewise_linear_distribution<_RT>& __x);
6546};
6547
6548template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006549typename piecewise_linear_distribution<_RealType>::param_type &
6550piecewise_linear_distribution<_RealType>::param_type::operator=
6551 (const param_type& __rhs)
6552{
6553// These can throw
6554 __b_.reserve (__rhs.__b_.size ());
6555 __densities_.reserve(__rhs.__densities_.size());
6556 __areas_.reserve (__rhs.__areas_.size());
6557
6558// These can not throw
6559 __b_ = __rhs.__b_;
6560 __densities_ = __rhs.__densities_;
6561 __areas_ = __rhs.__areas_;
6562 return *this;
6563}
6564
6565
6566template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006567void
6568piecewise_linear_distribution<_RealType>::param_type::__init()
6569{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006570 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnant99968442011-11-29 18:15:50 +00006571 result_type _Sp = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006572 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6573 {
6574 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6575 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnant99968442011-11-29 18:15:50 +00006576 _Sp += __areas_[__i];
Howard Hinnant54305402010-05-25 00:27:34 +00006577 }
6578 for (size_t __i = __areas_.size(); __i > 1;)
6579 {
6580 --__i;
Howard Hinnant99968442011-11-29 18:15:50 +00006581 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006582 }
6583 __areas_[0] = 0;
6584 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6585 __areas_[__i] += __areas_[__i-1];
6586 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnant99968442011-11-29 18:15:50 +00006587 __densities_[__i] /= _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006588}
6589
6590template<class _RealType>
6591piecewise_linear_distribution<_RealType>::param_type::param_type()
6592 : __b_(2),
6593 __densities_(2, 1.0),
6594 __areas_(1, 0.0)
6595{
6596 __b_[1] = 1;
6597}
6598
6599template<class _RealType>
6600template<class _InputIteratorB, class _InputIteratorW>
6601piecewise_linear_distribution<_RealType>::param_type::param_type(
6602 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6603 : __b_(__fB, __lB)
6604{
6605 if (__b_.size() < 2)
6606 {
6607 __b_.resize(2);
6608 __b_[0] = 0;
6609 __b_[1] = 1;
6610 __densities_.assign(2, 1.0);
6611 __areas_.assign(1, 0.0);
6612 }
6613 else
6614 {
6615 __densities_.reserve(__b_.size());
6616 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6617 __densities_.push_back(*__fW);
6618 __init();
6619 }
6620}
6621
Howard Hinnante3e32912011-08-12 21:56:02 +00006622#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6623
Howard Hinnant54305402010-05-25 00:27:34 +00006624template<class _RealType>
6625template<class _UnaryOperation>
6626piecewise_linear_distribution<_RealType>::param_type::param_type(
6627 initializer_list<result_type> __bl, _UnaryOperation __fw)
6628 : __b_(__bl.begin(), __bl.end())
6629{
6630 if (__b_.size() < 2)
6631 {
6632 __b_.resize(2);
6633 __b_[0] = 0;
6634 __b_[1] = 1;
6635 __densities_.assign(2, 1.0);
6636 __areas_.assign(1, 0.0);
6637 }
6638 else
6639 {
6640 __densities_.reserve(__b_.size());
6641 for (size_t __i = 0; __i < __b_.size(); ++__i)
6642 __densities_.push_back(__fw(__b_[__i]));
6643 __init();
6644 }
6645}
6646
Howard Hinnante3e32912011-08-12 21:56:02 +00006647#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6648
Howard Hinnant54305402010-05-25 00:27:34 +00006649template<class _RealType>
6650template<class _UnaryOperation>
6651piecewise_linear_distribution<_RealType>::param_type::param_type(
6652 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6653 : __b_(__nw == 0 ? 2 : __nw + 1)
6654{
6655 size_t __n = __b_.size() - 1;
6656 result_type __d = (__xmax - __xmin) / __n;
6657 __densities_.reserve(__b_.size());
6658 for (size_t __i = 0; __i < __n; ++__i)
6659 {
6660 __b_[__i] = __xmin + __i * __d;
6661 __densities_.push_back(__fw(__b_[__i]));
6662 }
6663 __b_[__n] = __xmax;
6664 __densities_.push_back(__fw(__b_[__n]));
6665 __init();
6666}
6667
6668template<class _RealType>
6669template<class _URNG>
6670_RealType
6671piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6672{
6673 typedef uniform_real_distribution<result_type> _Gen;
6674 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006675 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006676 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006677 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006678 const result_type __dk = __p.__densities_[__k];
6679 const result_type __dk1 = __p.__densities_[__k+1];
6680 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006681 const result_type __bk = __p.__b_[__k];
6682 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006683 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006684 const result_type __bk1 = __p.__b_[__k+1];
6685 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006686 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:19 +00006687 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006688 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006689}
6690
6691template <class _CharT, class _Traits, class _RT>
6692basic_ostream<_CharT, _Traits>&
6693operator<<(basic_ostream<_CharT, _Traits>& __os,
6694 const piecewise_linear_distribution<_RT>& __x)
6695{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006696 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant54305402010-05-25 00:27:34 +00006697 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6698 ios_base::scientific);
6699 _CharT __sp = __os.widen(' ');
6700 __os.fill(__sp);
6701 size_t __n = __x.__p_.__b_.size();
6702 __os << __n;
6703 for (size_t __i = 0; __i < __n; ++__i)
6704 __os << __sp << __x.__p_.__b_[__i];
6705 __n = __x.__p_.__densities_.size();
6706 __os << __sp << __n;
6707 for (size_t __i = 0; __i < __n; ++__i)
6708 __os << __sp << __x.__p_.__densities_[__i];
6709 __n = __x.__p_.__areas_.size();
6710 __os << __sp << __n;
6711 for (size_t __i = 0; __i < __n; ++__i)
6712 __os << __sp << __x.__p_.__areas_[__i];
6713 return __os;
6714}
6715
6716template <class _CharT, class _Traits, class _RT>
6717basic_istream<_CharT, _Traits>&
6718operator>>(basic_istream<_CharT, _Traits>& __is,
6719 piecewise_linear_distribution<_RT>& __x)
6720{
6721 typedef piecewise_linear_distribution<_RT> _Eng;
6722 typedef typename _Eng::result_type result_type;
6723 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006724 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant54305402010-05-25 00:27:34 +00006725 __is.flags(ios_base::dec | ios_base::skipws);
6726 size_t __n;
6727 __is >> __n;
6728 vector<result_type> __b(__n);
6729 for (size_t __i = 0; __i < __n; ++__i)
6730 __is >> __b[__i];
6731 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006732 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006733 for (size_t __i = 0; __i < __n; ++__i)
6734 __is >> __densities[__i];
6735 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006736 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006737 for (size_t __i = 0; __i < __n; ++__i)
6738 __is >> __areas[__i];
6739 if (!__is.fail())
6740 {
6741 swap(__x.__p_.__b_, __b);
6742 swap(__x.__p_.__densities_, __densities);
6743 swap(__x.__p_.__areas_, __areas);
6744 }
6745 return __is;
6746}
6747
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006748_LIBCPP_END_NAMESPACE_STD
6749
6750#endif // _LIBCPP_RANDOM