blob: bccf92a31f39b66cef146410b1cda143fe3e2909 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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:27220 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16221};
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:27272 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16273};
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:27326 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16327};
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:46374typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16375
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:27395 double entropy() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16396
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:43488
Howard Hinnantbc8d3f92010-05-11 19:42:16489 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:43547
Howard Hinnantbc8d3f92010-05-11 19:42:16548 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:43602
Howard Hinnantbc8d3f92010-05-11 19:42:16603 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:59611class 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:43660
Howard Hinnant03aad812010-05-11 23:26:59661 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:16667
668template<class IntType = int>
Howard Hinnant34e8a572010-05-17 13:44:27669class 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:43716
Howard Hinnant34e8a572010-05-17 13:44:27717 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:16723
724template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38725class 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:43774
Howard Hinnantf2fe5d52010-05-17 00:09:38775 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:16781
782template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54783class 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:43830
Howard Hinnant4ff556c2010-05-14 21:38:54831 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:16837
838template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57839class 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:31850 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57851
Howard Hinnanta64111c2010-05-12 21:02:31852 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57853
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:31859 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57860 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:31868 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57869
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:43886
Howard Hinnant30a840f2010-05-12 17:08:57887 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:16893
894template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28895class 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:43944
Howard Hinnantc7c49132010-05-13 17:58:28945 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:16951
952template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02953class 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:02992 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:431002
Howard Hinnant9de6e302010-05-16 01:09:021003 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:161009
1010template<class RealType = double>
Howard Hinnantc2b0dc72010-05-17 16:21:561011class 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:431060
Howard Hinnantc2b0dc72010-05-17 16:21:561061 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:161067
1068template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:311069class 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:431118
Howard Hinnanta64111c2010-05-12 21:02:311119 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:161125
1126template<class RealType = double>
Howard Hinnant2bc36fc2010-05-17 18:31:531127class 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:431176
Howard Hinnant2bc36fc2010-05-17 18:31:531177 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:161183
1184template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:001185class 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:001222 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:431232
Howard Hinnant97dc2f32010-05-15 23:36:001233 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:161239
1240template<class RealType = double>
Howard Hinnantd7d01132010-05-17 21:55:461241class 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:431290
Howard Hinnantd7d01132010-05-17 21:55:461291 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:161297
1298template<class RealType = double>
Howard Hinnantd8bc09b2010-05-18 17:32:301299class 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:041308 typedef fisher_f_distribution distribution_type;
Howard Hinnantd8bc09b2010-05-18 17:32:301309
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:431348
Howard Hinnantd8bc09b2010-05-18 17:32:301349 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:161355
1356template<class RealType = double>
Howard Hinnant321b4bb2010-05-18 20:08:041357class 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:431404
Howard Hinnant321b4bb2010-05-18 20:08:041405 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:161411
1412template<class IntType = int>
Howard Hinnant551d8e42010-05-19 01:53:571413class 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:431471
Howard Hinnant551d8e42010-05-19 01:53:571472 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:161478
1479template<class RealType = double>
Howard Hinnantd6d11712010-05-20 15:11:461480class 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:481501 vector<result_type> densities() const;
Howard Hinnantd6d11712010-05-20 15:11:461502
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:481528 vector<result_type> densities() const;
Howard Hinnantd6d11712010-05-20 15:11:461529
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:431546
Howard Hinnantd6d11712010-05-20 15:11:461547 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:161553
1554template<class RealType = double>
Howard Hinnant54305402010-05-25 00:27:341555class 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:481576 vector<result_type> densities() const;
Howard Hinnant54305402010-05-25 00:27:341577
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:431588
Howard Hinnant54305402010-05-25 00:27:341589 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:481606 vector<result_type> densities() const;
Howard Hinnant54305402010-05-25 00:27:341607
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:431624
Howard Hinnant54305402010-05-25 00:27:341625 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:161631
1632} // std
1633*/
1634
1635#include <__config>
1636#include <cstddef>
Eric Fiselierf51d6762015-01-23 22:22:361637#include <cstdint>
1638#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:161639#include <type_traits>
1640#include <initializer_list>
Howard Hinnantbc8d3f92010-05-11 19:42:161641#include <limits>
1642#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:571643#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:161644#include <vector>
1645#include <string>
1646#include <istream>
1647#include <ostream>
Howard Hinnantbc8d3f92010-05-11 19:42:161648
Howard Hinnant66c6f972011-11-29 16:45:271649#include <__undef_min_max>
1650
Howard Hinnant08e17472011-10-17 20:05:101651#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:161652#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:101653#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161654
1655_LIBCPP_BEGIN_NAMESPACE_STD
1656
Howard Hinnantef3b2e22011-04-11 18:22:121657// __is_seed_sequence
1658
1659template <class _Sseq, class _Engine>
1660struct __is_seed_sequence
1661{
Howard Hinnant8efd3da2012-04-02 21:00:451662 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantef3b2e22011-04-11 18:22:121663 !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:161667// linear_congruential_engine
1668
1669template <unsigned long long __a, unsigned long long __c,
Howard Hinnant99968442011-11-29 18:15:501670 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:161672struct __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:381680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161681 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:381698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161699 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:381715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161716 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:381726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161727 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:501735template <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:161737{
1738 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:381739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161740 static result_type next(result_type __x)
1741 {
Howard Hinnant99968442011-11-29 18:15:501742 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:161745 // 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:501756template <unsigned long long _Ap, unsigned long long _Mp>
1757struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
Howard Hinnantbc8d3f92010-05-11 19:42:161758{
1759 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:381760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161761 static result_type next(result_type __x)
1762 {
Howard Hinnant99968442011-11-29 18:15:501763 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:161765 // 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:501775template <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:161777{
1778 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:381779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161780 static result_type next(result_type __x)
1781 {
Howard Hinnant99968442011-11-29 18:15:501782 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:161785 return (__a * __x + __c) % __m;
1786 }
1787};
1788
Howard Hinnant99968442011-11-29 18:15:501789template <unsigned long long _Ap, unsigned long long _Cp>
1790struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantbc8d3f92010-05-11 19:42:161791{
1792 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:381793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161794 static result_type next(result_type __x)
1795 {
Howard Hinnant99968442011-11-29 18:15:501796 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:161798 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:381808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161809 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 Hinnant0f678bd2013-08-12 18:38:341816class _LIBCPP_TYPE_VIS_ONLY linear_congruential_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:161817
1818template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:501819 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnant33be35e2012-09-14 00:39:161820_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161821basic_ostream<_CharT, _Traits>&
1822operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:501823 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantbc8d3f92010-05-11 19:42:161824
1825template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:501826 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:161827basic_istream<_CharT, _Traits>&
1828operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:501829 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161830
1831template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant0f678bd2013-08-12 18:38:341832class _LIBCPP_TYPE_VIS_ONLY linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:161833{
1834public:
1835 // types
1836 typedef _UIntType result_type;
1837
Howard Hinnantddb4e4c2013-05-21 21:19:351838private:
Howard Hinnantbc8d3f92010-05-11 19:42:161839 result_type __x_;
1840
Howard Hinnant8efd3da2012-04-02 21:00:451841 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:161842
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:451846 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:161848 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1849
1850 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:451851 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:381854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:451855 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:381856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:451857 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1858 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:161859
1860 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:381861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161862 explicit linear_congruential_engine(result_type __s = default_seed)
1863 {seed(__s);}
Howard Hinnantec3773c2011-12-01 20:21:041864 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:381865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:041866 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:121867 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:161868 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:381869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161870 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:381874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161875 typename enable_if
1876 <
Howard Hinnantef3b2e22011-04-11 18:22:121877 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161878 void
1879 >::type
1880 seed(_Sseq& __q)
1881 {__seed(__q, integral_constant<unsigned,
1882 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
Howard Hinnant8f72d5c2013-05-21 21:05:121883 : (__m > 0x100000000ull))>());}
Howard Hinnantbc8d3f92010-05-11 19:42:161884
1885 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:381886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161887 result_type operator()()
Howard Hinnant99968442011-11-29 18:15:501888 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantb9af2ea2010-09-22 18:02:381889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161890 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1891
Howard Hinnantb9af2ea2010-09-22 18:02:381892 friend _LIBCPP_INLINE_VISIBILITY
1893 bool operator==(const linear_congruential_engine& __x,
1894 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:161895 {return __x.__x_ == __y.__x_;}
Howard Hinnantb9af2ea2010-09-22 18:02:381896 friend _LIBCPP_INLINE_VISIBILITY
1897 bool operator!=(const linear_congruential_engine& __x,
1898 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:161899 {return !(__x == __y);}
1900
1901private:
1902
Howard Hinnantb9af2ea2010-09-22 18:02:381903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161904 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:381905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161906 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:381907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161908 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1909 1 : __s % __m;}
Howard Hinnantb9af2ea2010-09-22 18:02:381910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161911 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:501919 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:161920 friend
1921 basic_ostream<_CharT, _Traits>&
1922 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:501923 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant324bb032010-08-22 00:02:431924
Howard Hinnantbc8d3f92010-05-11 19:42:161925 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:501926 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:161927 friend
1928 basic_istream<_CharT, _Traits>&
1929 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:501930 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161931};
1932
1933template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant0a69fa12012-12-12 21:14:281934 _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:161950template<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] +
Howard Hinnant8f72d5c2013-05-21 21:05:121972 ((uint64_t)__ar[4] << 32)) % __m);
Howard Hinnantbc8d3f92010-05-11 19:42:161973 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1974}
1975
Howard Hinnantbc8d3f92010-05-11 19:42:161976template <class _CharT, class _Traits,
1977 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:381978inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161979basic_ostream<_CharT, _Traits>&
1980operator<<(basic_ostream<_CharT, _Traits>& __os,
1981 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1982{
Howard Hinnant9c0df142012-10-30 19:06:591983 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:161984 __os.flags(ios_base::dec | ios_base::left);
1985 __os.fill(__os.widen(' '));
1986 return __os << __x.__x_;
1987}
1988
1989template <class _CharT, class _Traits,
1990 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1991basic_istream<_CharT, _Traits>&
1992operator>>(basic_istream<_CharT, _Traits>& __is,
1993 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1994{
Howard Hinnant9c0df142012-10-30 19:06:591995 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:161996 __is.flags(ios_base::dec | ios_base::skipws);
1997 _UIntType __t;
1998 __is >> __t;
1999 if (!__is.fail())
2000 __x.__x_ = __t;
2001 return __is;
2002}
2003
2004typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2005 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:162006typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2007 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:462008typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:162009// mersenne_twister_engine
2010
2011template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2012 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2013 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0f678bd2013-08-12 18:38:342014class _LIBCPP_TYPE_VIS_ONLY mersenne_twister_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:162015
Howard Hinnant99968442011-11-29 18:15:502016template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2017 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2018 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162019bool
Howard Hinnant99968442011-11-29 18:15:502020operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2021 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2022 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2023 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:162024
Howard Hinnant99968442011-11-29 18:15:502025template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2026 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2027 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnant33be35e2012-09-14 00:39:162028_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162029bool
Howard Hinnant99968442011-11-29 18:15:502030operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2031 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2032 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2033 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:162034
2035template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502036 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2037 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2038 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162039basic_ostream<_CharT, _Traits>&
2040operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502041 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2042 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162043
2044template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502045 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2046 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2047 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162048basic_istream<_CharT, _Traits>&
2049operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502050 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2051 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162052
2053template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2054 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2055 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0f678bd2013-08-12 18:38:342056class _LIBCPP_TYPE_VIS_ONLY mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:162057{
2058public:
2059 // types
2060 typedef _UIntType result_type;
2061
2062private:
2063 result_type __x_[__n];
2064 size_t __i_;
2065
2066 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2067 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant8efd3da2012-04-02 21:00:452068 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:162069 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2070 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2071 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2072 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2073 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2074 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2075 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2076public:
Howard Hinnant8efd3da2012-04-02 21:00:452077 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2078 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2079 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:162080 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2081 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2082 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2083 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2084 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2085 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2086
2087 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:452088 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2089 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2090 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2091 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2092 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2093 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2094 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2095 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2096 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2097 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2098 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2099 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2100 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:382101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:452102 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:382103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:452104 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2105 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantbc8d3f92010-05-11 19:42:162106
2107 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:382108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162109 explicit mersenne_twister_engine(result_type __sd = default_seed)
2110 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:042111 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:382112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042113 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:122114 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:162115 {seed(__q);}
2116 void seed(result_type __sd = default_seed);
2117 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:382118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162119 typename enable_if
2120 <
Howard Hinnantef3b2e22011-04-11 18:22:122121 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:162122 void
2123 >::type
2124 seed(_Sseq& __q)
2125 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2126
2127 // generating functions
2128 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:382129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162130 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2131
Howard Hinnant99968442011-11-29 18:15:502132 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2133 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2134 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162135 friend
2136 bool
Howard Hinnant99968442011-11-29 18:15:502137 operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2138 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2139 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2140 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant324bb032010-08-22 00:02:432141
Howard Hinnant99968442011-11-29 18:15:502142 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2143 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2144 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162145 friend
2146 bool
Howard Hinnant99968442011-11-29 18:15:502147 operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2148 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2149 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2150 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:162151
2152 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502153 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2154 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2155 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162156 friend
2157 basic_ostream<_CharT, _Traits>&
2158 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502159 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2160 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162161
2162 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502163 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2164 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2165 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162166 friend
2167 basic_istream<_CharT, _Traits>&
2168 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502169 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2170 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162171private:
2172
2173 template<class _Sseq>
2174 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2175 template<class _Sseq>
2176 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2177
2178 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:382179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162180 static
2181 typename enable_if
2182 <
2183 __count < __w,
2184 result_type
2185 >::type
2186 __lshift(result_type __x) {return (__x << __count) & _Max;}
2187
2188 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:382189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162190 static
2191 typename enable_if
2192 <
2193 (__count >= __w),
2194 result_type
2195 >::type
Howard Hinnantec3773c2011-12-01 20:21:042196 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:162197
2198 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:382199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162200 static
2201 typename enable_if
2202 <
2203 __count < _Dt,
2204 result_type
2205 >::type
2206 __rshift(result_type __x) {return __x >> __count;}
2207
2208 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:382209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162210 static
2211 typename enable_if
2212 <
2213 (__count >= _Dt),
2214 result_type
2215 >::type
Howard Hinnantec3773c2011-12-01 20:21:042216 __rshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:162217};
2218
2219template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2220 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2221 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0a69fa12012-12-12 21:14:282222 _LIBCPP_CONSTEXPR const size_t
2223 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2224
2225template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2226 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2227 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2228 _LIBCPP_CONSTEXPR const size_t
2229 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2230
2231template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2232 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2233 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2234 _LIBCPP_CONSTEXPR const size_t
2235 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2236
2237template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2238 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2239 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2240 _LIBCPP_CONSTEXPR const size_t
2241 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
2242
2243template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2244 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2245 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2246 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2247 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2248
2249template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2250 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2251 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2252 _LIBCPP_CONSTEXPR const size_t
2253 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2254
2255template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2256 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2257 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2258 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2259 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2260
2261template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2262 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2263 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2264 _LIBCPP_CONSTEXPR const size_t
2265 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2266
2267template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2268 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2269 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2270 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2271 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2272
2273template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2274 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2275 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2276 _LIBCPP_CONSTEXPR const size_t
2277 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2278
2279template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2280 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2281 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2282 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2283 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2284
2285template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2286 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2287 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2288 _LIBCPP_CONSTEXPR const size_t
2289 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
2290
2291template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2292 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2293 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2294 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2295 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2296
2297template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2298 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2299 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2300 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2301 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2302
2303template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2304 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2305 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantbc8d3f92010-05-11 19:42:162306void
2307mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2308 __t, __c, __l, __f>::seed(result_type __sd)
2309{ // __w >= 2
2310 __x_[0] = __sd & _Max;
2311 for (size_t __i = 1; __i < __n; ++__i)
2312 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2313 __i_ = 0;
2314}
2315
2316template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2317 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2318 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2319template<class _Sseq>
2320void
2321mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2322 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2323{
2324 const unsigned __k = 1;
2325 uint32_t __ar[__n * __k];
2326 __q.generate(__ar, __ar + __n * __k);
2327 for (size_t __i = 0; __i < __n; ++__i)
2328 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2329 const result_type __mask = __r == _Dt ? result_type(~0) :
2330 (result_type(1) << __r) - result_type(1);
2331 __i_ = 0;
2332 if ((__x_[0] & ~__mask) == 0)
2333 {
2334 for (size_t __i = 1; __i < __n; ++__i)
2335 if (__x_[__i] != 0)
2336 return;
2337 __x_[0] = _Max;
2338 }
2339}
2340
2341template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2342 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2343 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2344template<class _Sseq>
2345void
2346mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2347 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2348{
2349 const unsigned __k = 2;
2350 uint32_t __ar[__n * __k];
2351 __q.generate(__ar, __ar + __n * __k);
2352 for (size_t __i = 0; __i < __n; ++__i)
2353 __x_[__i] = static_cast<result_type>(
2354 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2355 const result_type __mask = __r == _Dt ? result_type(~0) :
2356 (result_type(1) << __r) - result_type(1);
2357 __i_ = 0;
2358 if ((__x_[0] & ~__mask) == 0)
2359 {
2360 for (size_t __i = 1; __i < __n; ++__i)
2361 if (__x_[__i] != 0)
2362 return;
2363 __x_[0] = _Max;
2364 }
2365}
2366
2367template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2368 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2369 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2370_UIntType
2371mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2372 __t, __c, __l, __f>::operator()()
2373{
2374 const size_t __j = (__i_ + 1) % __n;
2375 const result_type __mask = __r == _Dt ? result_type(~0) :
2376 (result_type(1) << __r) - result_type(1);
Howard Hinnant99968442011-11-29 18:15:502377 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantbc8d3f92010-05-11 19:42:162378 const size_t __k = (__i_ + __m) % __n;
Howard Hinnant99968442011-11-29 18:15:502379 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantbc8d3f92010-05-11 19:42:162380 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2381 __i_ = __j;
2382 __z ^= __lshift<__s>(__z) & __b;
2383 __z ^= __lshift<__t>(__z) & __c;
2384 return __z ^ __rshift<__l>(__z);
2385}
2386
Howard Hinnant99968442011-11-29 18:15:502387template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2388 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2389 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162390bool
Howard Hinnant99968442011-11-29 18:15:502391operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2392 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2393 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2394 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:162395{
2396 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:502397 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:162398 if (__x.__i_ == 0 || __y.__i_ == 0)
2399 {
Howard Hinnant99968442011-11-29 18:15:502400 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:192401 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:162402 __y.__x_ + __y.__i_))
2403 return false;
2404 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:502405 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2406 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:162407 }
2408 if (__x.__i_ < __y.__i_)
2409 {
Howard Hinnant99968442011-11-29 18:15:502410 size_t __j = _Np - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:192411 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:162412 __y.__x_ + __y.__i_))
2413 return false;
Howard Hinnant99968442011-11-29 18:15:502414 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:162415 __y.__x_))
2416 return false;
Howard Hinnant0949eed2011-06-30 21:18:192417 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:502418 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:162419 }
Howard Hinnant99968442011-11-29 18:15:502420 size_t __j = _Np - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:192421 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:162422 __x.__x_ + __x.__i_))
2423 return false;
Howard Hinnant99968442011-11-29 18:15:502424 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:162425 __x.__x_))
2426 return false;
Howard Hinnant0949eed2011-06-30 21:18:192427 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:502428 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:162429}
2430
Howard Hinnant99968442011-11-29 18:15:502431template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2432 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2433 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantb9af2ea2010-09-22 18:02:382434inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162435bool
Howard Hinnant99968442011-11-29 18:15:502436operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2437 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2438 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2439 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:162440{
2441 return !(__x == __y);
2442}
2443
2444template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502445 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2446 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2447 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162448basic_ostream<_CharT, _Traits>&
2449operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502450 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2451 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162452{
Howard Hinnant9c0df142012-10-30 19:06:592453 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:162454 __os.flags(ios_base::dec | ios_base::left);
2455 _CharT __sp = __os.widen(' ');
2456 __os.fill(__sp);
2457 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:502458 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:162459 __os << __sp << __x.__x_[__j];
2460 for (size_t __j = 0; __j < __x.__i_; ++__j)
2461 __os << __sp << __x.__x_[__j];
2462 return __os;
2463}
2464
2465template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502466 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2467 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2468 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:162469basic_istream<_CharT, _Traits>&
2470operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502471 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2472 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162473{
Howard Hinnant9c0df142012-10-30 19:06:592474 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:162475 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:502476 _UI __t[_Np];
2477 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:162478 __is >> __t[__i];
2479 if (!__is.fail())
2480 {
Howard Hinnant99968442011-11-29 18:15:502481 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:162482 __x.__x_[__i] = __t[__i];
2483 __x.__i_ = 0;
2484 }
2485 return __is;
2486}
2487
2488typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2489 0x9908b0df, 11, 0xffffffff,
2490 7, 0x9d2c5680,
2491 15, 0xefc60000,
2492 18, 1812433253> mt19937;
2493typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2494 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2495 17, 0x71d67fffeda60000ULL,
2496 37, 0xfff7eee000000000ULL,
2497 43, 6364136223846793005ULL> mt19937_64;
2498
2499// subtract_with_carry_engine
2500
2501template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0f678bd2013-08-12 18:38:342502class _LIBCPP_TYPE_VIS_ONLY subtract_with_carry_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:162503
Howard Hinnant99968442011-11-29 18:15:502504template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162505bool
2506operator==(
Howard Hinnant99968442011-11-29 18:15:502507 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2508 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:162509
Howard Hinnant99968442011-11-29 18:15:502510template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnant33be35e2012-09-14 00:39:162511_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162512bool
2513operator!=(
Howard Hinnant99968442011-11-29 18:15:502514 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2515 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:162516
2517template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502518 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162519basic_ostream<_CharT, _Traits>&
2520operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502521 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162522
2523template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502524 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162525basic_istream<_CharT, _Traits>&
2526operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502527 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162528
2529template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0f678bd2013-08-12 18:38:342530class _LIBCPP_TYPE_VIS_ONLY subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:162531{
2532public:
2533 // types
2534 typedef _UIntType result_type;
2535
2536private:
2537 result_type __x_[__r];
2538 result_type __c_;
2539 size_t __i_;
2540
Howard Hinnant8efd3da2012-04-02 21:00:452541 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:162542 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2543 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2544 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2545 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2546public:
Howard Hinnant8efd3da2012-04-02 21:00:452547 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2548 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2549 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:162550 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2551
2552 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:452553 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2554 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2555 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:382556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:452557 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:382558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:452559 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2560 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantbc8d3f92010-05-11 19:42:162561
2562 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:382563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162564 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2565 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:042566 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:382567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042568 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:122569 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:162570 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:382571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162572 void seed(result_type __sd = default_seed)
2573 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2574 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:382575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162576 typename enable_if
2577 <
Howard Hinnantef3b2e22011-04-11 18:22:122578 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:162579 void
2580 >::type
2581 seed(_Sseq& __q)
2582 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2583
2584 // generating functions
2585 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:382586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162587 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2588
Howard Hinnant99968442011-11-29 18:15:502589 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162590 friend
2591 bool
2592 operator==(
Howard Hinnant99968442011-11-29 18:15:502593 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2594 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:432595
Howard Hinnant99968442011-11-29 18:15:502596 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162597 friend
2598 bool
2599 operator!=(
Howard Hinnant99968442011-11-29 18:15:502600 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2601 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:432602
Howard Hinnantbc8d3f92010-05-11 19:42:162603 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502604 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162605 friend
2606 basic_ostream<_CharT, _Traits>&
2607 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502608 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:432609
Howard Hinnantbc8d3f92010-05-11 19:42:162610 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502611 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162612 friend
2613 basic_istream<_CharT, _Traits>&
2614 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502615 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162616
2617private:
2618
2619 void seed(result_type __sd, integral_constant<unsigned, 1>);
2620 void seed(result_type __sd, integral_constant<unsigned, 2>);
2621 template<class _Sseq>
2622 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2623 template<class _Sseq>
2624 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2625};
2626
2627template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0a69fa12012-12-12 21:14:282628 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2629
2630template<class _UIntType, size_t __w, size_t __s, size_t __r>
2631 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2632
2633template<class _UIntType, size_t __w, size_t __s, size_t __r>
2634 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2635
2636template<class _UIntType, size_t __w, size_t __s, size_t __r>
2637 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2638 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2639
2640template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantbc8d3f92010-05-11 19:42:162641void
2642subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2643 integral_constant<unsigned, 1>)
2644{
2645 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2646 __e(__sd == 0u ? default_seed : __sd);
2647 for (size_t __i = 0; __i < __r; ++__i)
2648 __x_[__i] = static_cast<result_type>(__e() & _Max);
2649 __c_ = __x_[__r-1] == 0;
2650 __i_ = 0;
2651}
2652
2653template<class _UIntType, size_t __w, size_t __s, size_t __r>
2654void
2655subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2656 integral_constant<unsigned, 2>)
2657{
2658 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2659 __e(__sd == 0u ? default_seed : __sd);
2660 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant43edf2d2011-08-15 17:22:222661 {
2662 result_type __e0 = __e();
Howard Hinnantbc8d3f92010-05-11 19:42:162663 __x_[__i] = static_cast<result_type>(
Howard Hinnant43edf2d2011-08-15 17:22:222664 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2665 }
Howard Hinnantbc8d3f92010-05-11 19:42:162666 __c_ = __x_[__r-1] == 0;
2667 __i_ = 0;
2668}
2669
2670template<class _UIntType, size_t __w, size_t __s, size_t __r>
2671template<class _Sseq>
2672void
2673subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2674 integral_constant<unsigned, 1>)
2675{
2676 const unsigned __k = 1;
2677 uint32_t __ar[__r * __k];
2678 __q.generate(__ar, __ar + __r * __k);
2679 for (size_t __i = 0; __i < __r; ++__i)
2680 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2681 __c_ = __x_[__r-1] == 0;
2682 __i_ = 0;
2683}
2684
2685template<class _UIntType, size_t __w, size_t __s, size_t __r>
2686template<class _Sseq>
2687void
2688subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2689 integral_constant<unsigned, 2>)
2690{
2691 const unsigned __k = 2;
2692 uint32_t __ar[__r * __k];
2693 __q.generate(__ar, __ar + __r * __k);
2694 for (size_t __i = 0; __i < __r; ++__i)
2695 __x_[__i] = static_cast<result_type>(
2696 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2697 __c_ = __x_[__r-1] == 0;
2698 __i_ = 0;
2699}
2700
2701template<class _UIntType, size_t __w, size_t __s, size_t __r>
2702_UIntType
2703subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2704{
2705 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2706 result_type& __xr = __x_[__i_];
2707 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2708 __xr = (__xs - __xr - __c_) & _Max;
2709 __c_ = __new_c;
2710 __i_ = (__i_ + 1) % __r;
2711 return __xr;
2712}
2713
Howard Hinnant99968442011-11-29 18:15:502714template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162715bool
2716operator==(
Howard Hinnant99968442011-11-29 18:15:502717 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2718 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:162719{
2720 if (__x.__c_ != __y.__c_)
2721 return false;
2722 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:502723 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:162724 if (__x.__i_ == 0 || __y.__i_ == 0)
2725 {
Howard Hinnant99968442011-11-29 18:15:502726 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:192727 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:162728 __y.__x_ + __y.__i_))
2729 return false;
2730 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:502731 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2732 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:162733 }
2734 if (__x.__i_ < __y.__i_)
2735 {
Howard Hinnant99968442011-11-29 18:15:502736 size_t __j = _Rp - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:192737 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:162738 __y.__x_ + __y.__i_))
2739 return false;
Howard Hinnant99968442011-11-29 18:15:502740 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:162741 __y.__x_))
2742 return false;
Howard Hinnant0949eed2011-06-30 21:18:192743 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:502744 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:162745 }
Howard Hinnant99968442011-11-29 18:15:502746 size_t __j = _Rp - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:192747 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:162748 __x.__x_ + __x.__i_))
2749 return false;
Howard Hinnant99968442011-11-29 18:15:502750 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:162751 __x.__x_))
2752 return false;
Howard Hinnant0949eed2011-06-30 21:18:192753 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:502754 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:162755}
2756
Howard Hinnant99968442011-11-29 18:15:502757template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:382758inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162759bool
2760operator!=(
Howard Hinnant99968442011-11-29 18:15:502761 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2762 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:162763{
2764 return !(__x == __y);
2765}
2766
2767template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502768 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162769basic_ostream<_CharT, _Traits>&
2770operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502771 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162772{
Howard Hinnant9c0df142012-10-30 19:06:592773 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:162774 __os.flags(ios_base::dec | ios_base::left);
2775 _CharT __sp = __os.widen(' ');
2776 __os.fill(__sp);
2777 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:502778 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:162779 __os << __sp << __x.__x_[__j];
2780 for (size_t __j = 0; __j < __x.__i_; ++__j)
2781 __os << __sp << __x.__x_[__j];
2782 __os << __sp << __x.__c_;
2783 return __os;
2784}
2785
2786template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502787 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162788basic_istream<_CharT, _Traits>&
2789operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502790 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162791{
Howard Hinnant9c0df142012-10-30 19:06:592792 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:162793 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:502794 _UI __t[_Rp+1];
2795 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:162796 __is >> __t[__i];
2797 if (!__is.fail())
2798 {
Howard Hinnant99968442011-11-29 18:15:502799 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:162800 __x.__x_[__i] = __t[__i];
Howard Hinnant99968442011-11-29 18:15:502801 __x.__c_ = __t[_Rp];
Howard Hinnantbc8d3f92010-05-11 19:42:162802 __x.__i_ = 0;
2803 }
2804 return __is;
2805}
2806
2807typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2808typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2809
2810// discard_block_engine
2811
2812template<class _Engine, size_t __p, size_t __r>
Howard Hinnant0f678bd2013-08-12 18:38:342813class _LIBCPP_TYPE_VIS_ONLY discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:162814{
2815 _Engine __e_;
2816 int __n_;
2817
2818 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2819 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2820public:
2821 // types
2822 typedef typename _Engine::result_type result_type;
2823
2824 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:452825 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2826 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantbc8d3f92010-05-11 19:42:162827
Howard Hinnant8efd3da2012-04-02 21:00:452828#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:162829 static const result_type _Min = _Engine::_Min;
2830 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:452831#else
2832 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2833 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2834#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162835
Howard Hinnantb9af2ea2010-09-22 18:02:382836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:452837 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:382838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:452839 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantbc8d3f92010-05-11 19:42:162840
2841 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:382842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162843 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:382844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:542845 explicit discard_block_engine(const _Engine& __e)
2846 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:192847#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:382848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:542849 explicit discard_block_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:192850 : __e_(_VSTD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:192851#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:382852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162853 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:382854 template<class _Sseq>
2855 _LIBCPP_INLINE_VISIBILITY
2856 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:122857 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:542858 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:162859 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:382860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162861 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:382862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162863 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:542864 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:382865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:542866 typename enable_if
2867 <
Howard Hinnantef3b2e22011-04-11 18:22:122868 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:542869 void
2870 >::type
2871 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162872
2873 // generating functions
2874 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:382875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162876 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2877
2878 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:382879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:272880 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:162881
Howard Hinnant99968442011-11-29 18:15:502882 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162883 friend
2884 bool
2885 operator==(
Howard Hinnant99968442011-11-29 18:15:502886 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2887 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:432888
Howard Hinnant99968442011-11-29 18:15:502889 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162890 friend
2891 bool
2892 operator!=(
Howard Hinnant99968442011-11-29 18:15:502893 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2894 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:432895
Howard Hinnantbc8d3f92010-05-11 19:42:162896 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502897 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162898 friend
2899 basic_ostream<_CharT, _Traits>&
2900 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502901 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:432902
Howard Hinnantbc8d3f92010-05-11 19:42:162903 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502904 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162905 friend
2906 basic_istream<_CharT, _Traits>&
2907 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502908 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162909};
2910
2911template<class _Engine, size_t __p, size_t __r>
Howard Hinnant0a69fa12012-12-12 21:14:282912 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
2913
2914template<class _Engine, size_t __p, size_t __r>
2915 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
2916
2917template<class _Engine, size_t __p, size_t __r>
Howard Hinnantbc8d3f92010-05-11 19:42:162918typename discard_block_engine<_Engine, __p, __r>::result_type
2919discard_block_engine<_Engine, __p, __r>::operator()()
2920{
2921 if (__n_ >= __r)
2922 {
2923 __e_.discard(__p - __r);
2924 __n_ = 0;
2925 }
2926 ++__n_;
2927 return __e_();
2928}
2929
Howard Hinnant99968442011-11-29 18:15:502930template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:382931inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162932bool
Howard Hinnant99968442011-11-29 18:15:502933operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2934 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:162935{
2936 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2937}
2938
Howard Hinnant99968442011-11-29 18:15:502939template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:382940inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162941bool
Howard Hinnant99968442011-11-29 18:15:502942operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2943 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:162944{
2945 return !(__x == __y);
2946}
2947
2948template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502949 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162950basic_ostream<_CharT, _Traits>&
2951operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:502952 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162953{
Howard Hinnant9c0df142012-10-30 19:06:592954 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:162955 __os.flags(ios_base::dec | ios_base::left);
2956 _CharT __sp = __os.widen(' ');
2957 __os.fill(__sp);
2958 return __os << __x.__e_ << __sp << __x.__n_;
2959}
2960
2961template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:502962 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:162963basic_istream<_CharT, _Traits>&
2964operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:502965 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162966{
Howard Hinnant9c0df142012-10-30 19:06:592967 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:162968 __is.flags(ios_base::dec | ios_base::skipws);
2969 _Eng __e;
2970 int __n;
2971 __is >> __e >> __n;
2972 if (!__is.fail())
2973 {
2974 __x.__e_ = __e;
2975 __x.__n_ = __n;
2976 }
2977 return __is;
2978}
2979
2980typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2981typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2982
2983// independent_bits_engine
2984
Howard Hinnantbc8d3f92010-05-11 19:42:162985template<class _Engine, size_t __w, class _UIntType>
Howard Hinnant0f678bd2013-08-12 18:38:342986class _LIBCPP_TYPE_VIS_ONLY independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:162987{
Howard Hinnant99968442011-11-29 18:15:502988 template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
Howard Hinnantbc8d3f92010-05-11 19:42:162989 class __get_n
2990 {
Howard Hinnant8efd3da2012-04-02 21:00:452991 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits;
2992 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
2993 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
2994 static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantbc8d3f92010-05-11 19:42:162995 public:
Howard Hinnant8efd3da2012-04-02 21:00:452996 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:162997 };
2998public:
2999 // types
3000 typedef _UIntType result_type;
3001
3002private:
3003 _Engine __e_;
3004
Howard Hinnant8efd3da2012-04-02 21:00:453005 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:163006 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3007 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3008
3009 typedef typename _Engine::result_type _Engine_result_type;
3010 typedef typename conditional
3011 <
3012 sizeof(_Engine_result_type) <= sizeof(result_type),
3013 result_type,
3014 _Engine_result_type
3015 >::type _Working_result_type;
Howard Hinnant8efd3da2012-04-02 21:00:453016#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:503017 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:453018 + _Working_result_type(1);
3019#else
3020 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3021 + _Working_result_type(1);
3022#endif
3023 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3024 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3025 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3026 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3027 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3028 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3029 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3030 (_Rp >> __w0) << __w0;
3031 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3032 (_Rp >> (__w0+1)) << (__w0+1);
3033 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:163034 _Engine_result_type(~0) >> (_EDt - __w0) :
3035 _Engine_result_type(0);
Howard Hinnant8efd3da2012-04-02 21:00:453036 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantbc8d3f92010-05-11 19:42:163037 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3038 _Engine_result_type(~0);
3039public:
Howard Hinnant8efd3da2012-04-02 21:00:453040 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3041 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3042 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:163043 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3044
3045 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:383046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:453047 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:383048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:453049 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:163050
3051 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:383052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163053 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:383054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:543055 explicit independent_bits_engine(const _Engine& __e)
3056 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:193057#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:383058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:543059 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:193060 : __e_(_VSTD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:193061#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:383062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163063 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnantec3773c2011-12-01 20:21:043064 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:383065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:043066 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:123067 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:543068 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:163069 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163071 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:383072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163073 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:543074 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:383075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:543076 typename enable_if
3077 <
Howard Hinnantef3b2e22011-04-11 18:22:123078 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:543079 void
3080 >::type
3081 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:163082
3083 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:383084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:503085 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:383086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163087 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3088
3089 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:383090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:273091 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:163092
Howard Hinnant99968442011-11-29 18:15:503093 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:163094 friend
3095 bool
3096 operator==(
Howard Hinnant99968442011-11-29 18:15:503097 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3098 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:433099
Howard Hinnant99968442011-11-29 18:15:503100 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:163101 friend
3102 bool
3103 operator!=(
Howard Hinnant99968442011-11-29 18:15:503104 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3105 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:433106
Howard Hinnantbc8d3f92010-05-11 19:42:163107 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503108 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:163109 friend
3110 basic_ostream<_CharT, _Traits>&
3111 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:503112 const independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:433113
Howard Hinnantbc8d3f92010-05-11 19:42:163114 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503115 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:163116 friend
3117 basic_istream<_CharT, _Traits>&
3118 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:503119 independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163120
3121private:
3122 result_type __eval(false_type);
3123 result_type __eval(true_type);
3124
3125 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:383126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163127 static
3128 typename enable_if
3129 <
3130 __count < _Dt,
3131 result_type
3132 >::type
3133 __lshift(result_type __x) {return __x << __count;}
3134
3135 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:383136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163137 static
3138 typename enable_if
3139 <
3140 (__count >= _Dt),
3141 result_type
3142 >::type
Howard Hinnantec3773c2011-12-01 20:21:043143 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:163144};
3145
3146template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:383147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163148_UIntType
3149independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3150{
3151 return static_cast<result_type>(__e_() & __mask0);
3152}
3153
3154template<class _Engine, size_t __w, class _UIntType>
3155_UIntType
3156independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3157{
Howard Hinnant99968442011-11-29 18:15:503158 result_type _Sp = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:163159 for (size_t __k = 0; __k < __n0; ++__k)
3160 {
3161 _Engine_result_type __u;
3162 do
3163 {
3164 __u = __e_() - _Engine::min();
3165 } while (__u >= __y0);
Howard Hinnant99968442011-11-29 18:15:503166 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantbc8d3f92010-05-11 19:42:163167 }
3168 for (size_t __k = __n0; __k < __n; ++__k)
3169 {
3170 _Engine_result_type __u;
3171 do
3172 {
3173 __u = __e_() - _Engine::min();
3174 } while (__u >= __y1);
Howard Hinnant99968442011-11-29 18:15:503175 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantbc8d3f92010-05-11 19:42:163176 }
Howard Hinnant99968442011-11-29 18:15:503177 return _Sp;
Howard Hinnantbc8d3f92010-05-11 19:42:163178}
3179
Howard Hinnant99968442011-11-29 18:15:503180template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:383181inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163182bool
3183operator==(
Howard Hinnant99968442011-11-29 18:15:503184 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3185 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163186{
3187 return __x.base() == __y.base();
3188}
3189
Howard Hinnant99968442011-11-29 18:15:503190template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:383191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163192bool
3193operator!=(
Howard Hinnant99968442011-11-29 18:15:503194 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3195 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163196{
3197 return !(__x == __y);
3198}
3199
3200template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503201 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:163202basic_ostream<_CharT, _Traits>&
3203operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:503204 const independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:163205{
3206 return __os << __x.base();
3207}
3208
3209template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503210 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:163211basic_istream<_CharT, _Traits>&
3212operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:503213 independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:163214{
3215 _Eng __e;
3216 __is >> __e;
3217 if (!__is.fail())
3218 __x.__e_ = __e;
3219 return __is;
3220}
3221
3222// shuffle_order_engine
3223
3224template <uint64_t _Xp, uint64_t _Yp>
3225struct __ugcd
3226{
Howard Hinnant8efd3da2012-04-02 21:00:453227 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:163228};
3229
3230template <uint64_t _Xp>
3231struct __ugcd<_Xp, 0>
3232{
Howard Hinnant8efd3da2012-04-02 21:00:453233 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantbc8d3f92010-05-11 19:42:163234};
3235
Howard Hinnant99968442011-11-29 18:15:503236template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:163237class __uratio
3238{
Howard Hinnant99968442011-11-29 18:15:503239 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant8efd3da2012-04-02 21:00:453240 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:163241public:
Howard Hinnant8efd3da2012-04-02 21:00:453242 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3243 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantbc8d3f92010-05-11 19:42:163244
3245 typedef __uratio<num, den> type;
3246};
3247
3248template<class _Engine, size_t __k>
Howard Hinnant0f678bd2013-08-12 18:38:343249class _LIBCPP_TYPE_VIS_ONLY shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:163250{
3251 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3252public:
3253 // types
3254 typedef typename _Engine::result_type result_type;
3255
3256private:
3257 _Engine __e_;
3258 result_type _V_[__k];
3259 result_type _Y_;
3260
3261public:
3262 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:453263 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:433264
Howard Hinnant8efd3da2012-04-02 21:00:453265#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:163266 static const result_type _Min = _Engine::_Min;
3267 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:453268#else
3269 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3270 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3271#endif
Howard Hinnantbc8d3f92010-05-11 19:42:163272 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:383273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:453274 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:383275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:453276 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:163277
Howard Hinnant8efd3da2012-04-02 21:00:453278 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantbc8d3f92010-05-11 19:42:163279
3280 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:383281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163282 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:383283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:543284 explicit shuffle_order_engine(const _Engine& __e)
3285 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:193286#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:383287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:543288 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:193289 : __e_(_VSTD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:193290#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:383291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163292 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnantec3773c2011-12-01 20:21:043293 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:383294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:043295 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:123296 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:543297 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:163298 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:383299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163300 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:383301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163302 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:543303 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:383304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:543305 typename enable_if
3306 <
Howard Hinnantef3b2e22011-04-11 18:22:123307 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:543308 void
3309 >::type
3310 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:163311
3312 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:383313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:503314 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:383315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163316 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3317
3318 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:383319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:273320 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:163321
3322private:
Howard Hinnant99968442011-11-29 18:15:503323 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163324 friend
3325 bool
3326 operator==(
Howard Hinnant99968442011-11-29 18:15:503327 const shuffle_order_engine<_Eng, _Kp>& __x,
3328 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:433329
Howard Hinnant99968442011-11-29 18:15:503330 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163331 friend
3332 bool
3333 operator!=(
Howard Hinnant99968442011-11-29 18:15:503334 const shuffle_order_engine<_Eng, _Kp>& __x,
3335 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:433336
Howard Hinnantbc8d3f92010-05-11 19:42:163337 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503338 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163339 friend
3340 basic_ostream<_CharT, _Traits>&
3341 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:503342 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant324bb032010-08-22 00:02:433343
Howard Hinnantbc8d3f92010-05-11 19:42:163344 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503345 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163346 friend
3347 basic_istream<_CharT, _Traits>&
3348 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:503349 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163350
Howard Hinnantb9af2ea2010-09-22 18:02:383351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163352 void __init()
3353 {
3354 for (size_t __i = 0; __i < __k; ++__i)
3355 _V_[__i] = __e_();
3356 _Y_ = __e_();
3357 }
3358
Howard Hinnantb9af2ea2010-09-22 18:02:383359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163360 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:383361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:503362 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantbc8d3f92010-05-11 19:42:163363
Howard Hinnantb9af2ea2010-09-22 18:02:383364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163365 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:383366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163367 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3368
Howard Hinnant99968442011-11-29 18:15:503369 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:383370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163371 typename enable_if
3372 <
Howard Hinnant99968442011-11-29 18:15:503373 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantbc8d3f92010-05-11 19:42:163374 result_type
3375 >::type
Howard Hinnant99968442011-11-29 18:15:503376 __eval(__uratio<_Np, _Dp>)
3377 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantbc8d3f92010-05-11 19:42:163378
Howard Hinnant99968442011-11-29 18:15:503379 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:383380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163381 typename enable_if
3382 <
Howard Hinnant99968442011-11-29 18:15:503383 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantbc8d3f92010-05-11 19:42:163384 result_type
3385 >::type
Howard Hinnant99968442011-11-29 18:15:503386 __eval(__uratio<_Np, _Dp>)
Howard Hinnantbc8d3f92010-05-11 19:42:163387 {
Howard Hinnant99968442011-11-29 18:15:503388 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3389 / __uratio<_Np, _Dp>::den);
Howard Hinnantbc8d3f92010-05-11 19:42:163390 _Y_ = _V_[__j];
3391 _V_[__j] = __e_();
3392 return _Y_;
3393 }
3394
3395 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:383396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163397 result_type __evalf()
3398 {
Howard Hinnant99968442011-11-29 18:15:503399 const double _Fp = __d == 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:163400 __n / (2. * 0x8000000000000000ull) :
3401 __n / (double)__d;
Howard Hinnant99968442011-11-29 18:15:503402 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantbc8d3f92010-05-11 19:42:163403 _Y_ = _V_[__j];
3404 _V_[__j] = __e_();
3405 return _Y_;
3406 }
3407};
3408
Howard Hinnant0a69fa12012-12-12 21:14:283409template<class _Engine, size_t __k>
3410 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3411
Howard Hinnant99968442011-11-29 18:15:503412template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163413bool
3414operator==(
Howard Hinnant99968442011-11-29 18:15:503415 const shuffle_order_engine<_Eng, _Kp>& __x,
3416 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163417{
Howard Hinnant99968442011-11-29 18:15:503418 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:163419 __x.__e_ == __y.__e_;
3420}
3421
Howard Hinnant99968442011-11-29 18:15:503422template<class _Eng, size_t _Kp>
Howard Hinnantb9af2ea2010-09-22 18:02:383423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163424bool
3425operator!=(
Howard Hinnant99968442011-11-29 18:15:503426 const shuffle_order_engine<_Eng, _Kp>& __x,
3427 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163428{
3429 return !(__x == __y);
3430}
3431
3432template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503433 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163434basic_ostream<_CharT, _Traits>&
3435operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:503436 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:163437{
Howard Hinnant9c0df142012-10-30 19:06:593438 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:163439 __os.flags(ios_base::dec | ios_base::left);
3440 _CharT __sp = __os.widen(' ');
3441 __os.fill(__sp);
3442 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnant99968442011-11-29 18:15:503443 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:163444 __os << __sp << __x._V_[__i];
3445 return __os << __sp << __x._Y_;
3446}
3447
3448template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:503449 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:163450basic_istream<_CharT, _Traits>&
3451operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:503452 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:163453{
Howard Hinnant99968442011-11-29 18:15:503454 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant9c0df142012-10-30 19:06:593455 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:163456 __is.flags(ios_base::dec | ios_base::skipws);
3457 _Eng __e;
Howard Hinnant99968442011-11-29 18:15:503458 result_type _Vp[_Kp+1];
Howard Hinnantbc8d3f92010-05-11 19:42:163459 __is >> __e;
Howard Hinnant99968442011-11-29 18:15:503460 for (size_t __i = 0; __i < _Kp+1; ++__i)
3461 __is >> _Vp[__i];
Howard Hinnantbc8d3f92010-05-11 19:42:163462 if (!__is.fail())
3463 {
3464 __x.__e_ = __e;
Howard Hinnant99968442011-11-29 18:15:503465 for (size_t __i = 0; __i < _Kp; ++__i)
3466 __x._V_[__i] = _Vp[__i];
3467 __x._Y_ = _Vp[_Kp];
Howard Hinnantbc8d3f92010-05-11 19:42:163468 }
3469 return __is;
3470}
3471
3472typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3473
3474// random_device
3475
Howard Hinnant83eade62013-03-06 23:30:193476class _LIBCPP_TYPE_VIS random_device
Howard Hinnantbc8d3f92010-05-11 19:42:163477{
JF Bastien2bd5ffd2014-12-01 19:19:553478#if !(defined(_WIN32) || defined(_LIBCPP_USING_NACL_RANDOM))
Howard Hinnantbc8d3f92010-05-11 19:42:163479 int __f_;
JF Bastien2bd5ffd2014-12-01 19:19:553480#endif // !(defined(_WIN32) || defined(_LIBCPP_USING_NACL_RANDOM))
Howard Hinnantbc8d3f92010-05-11 19:42:163481public:
3482 // types
3483 typedef unsigned result_type;
3484
3485 // generator characteristics
Howard Hinnant8efd3da2012-04-02 21:00:453486 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3487 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantbc8d3f92010-05-11 19:42:163488
Howard Hinnantb9af2ea2010-09-22 18:02:383489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:413490 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:383491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:413492 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantbc8d3f92010-05-11 19:42:163493
3494 // constructors
3495 explicit random_device(const string& __token = "/dev/urandom");
3496 ~random_device();
3497
3498 // generating functions
3499 result_type operator()();
3500
3501 // property functions
Howard Hinnantc83960a2012-07-20 21:44:273502 double entropy() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:163503
3504private:
3505 // no copy functions
3506 random_device(const random_device&); // = delete;
3507 random_device& operator=(const random_device&); // = delete;
3508};
3509
3510// seed_seq
3511
Howard Hinnant0f678bd2013-08-12 18:38:343512class _LIBCPP_TYPE_VIS_ONLY seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:163513{
3514public:
3515 // types
3516 typedef uint32_t result_type;
3517
3518private:
3519 vector<result_type> __v_;
3520
3521 template<class _InputIterator>
3522 void init(_InputIterator __first, _InputIterator __last);
3523public:
3524 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:383525 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65ccddb2013-10-23 05:56:473526 seed_seq() _NOEXCEPT {}
Howard Hinnante3e32912011-08-12 21:56:023527#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:163528 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:383529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163530 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:023531#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:433532
Howard Hinnantbc8d3f92010-05-11 19:42:163533 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:383534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163535 seed_seq(_InputIterator __first, _InputIterator __last)
3536 {init(__first, __last);}
3537
3538 // generating functions
3539 template<class _RandomAccessIterator>
3540 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3541
3542 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:383543 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65ccddb2013-10-23 05:56:473544 size_t size() const _NOEXCEPT {return __v_.size();}
Howard Hinnantbc8d3f92010-05-11 19:42:163545 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:383546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163547 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:193548 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:163549
3550private:
3551 // no copy functions
3552 seed_seq(const seed_seq&); // = delete;
3553 void operator=(const seed_seq&); // = delete;
3554
Howard Hinnantb9af2ea2010-09-22 18:02:383555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:503556 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantbc8d3f92010-05-11 19:42:163557};
3558
3559template<class _InputIterator>
3560void
3561seed_seq::init(_InputIterator __first, _InputIterator __last)
3562{
3563 for (_InputIterator __s = __first; __s != __last; ++__s)
3564 __v_.push_back(*__s & 0xFFFFFFFF);
3565}
3566
3567template<class _RandomAccessIterator>
3568void
3569seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3570{
3571 if (__first != __last)
3572 {
Howard Hinnant0949eed2011-06-30 21:18:193573 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:163574 const size_t __n = static_cast<size_t>(__last - __first);
3575 const size_t __s = __v_.size();
3576 const size_t __t = (__n >= 623) ? 11
3577 : (__n >= 68) ? 7
3578 : (__n >= 39) ? 5
3579 : (__n >= 7) ? 3
3580 : (__n - 1) / 2;
3581 const size_t __p = (__n - __t) / 2;
3582 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:193583 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:163584 // __k = 0;
3585 {
Howard Hinnant99968442011-11-29 18:15:503586 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantbc8d3f92010-05-11 19:42:163587 ^ __first[__n - 1]);
3588 __first[__p] += __r;
3589 __r += __s;
3590 __first[__q] += __r;
3591 __first[0] = __r;
3592 }
3593 for (size_t __k = 1; __k <= __s; ++__k)
3594 {
3595 const size_t __kmodn = __k % __n;
3596 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:503597 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:163598 ^ __first[(__k - 1) % __n]);
3599 __first[__kpmodn] += __r;
3600 __r += __kmodn + __v_[__k-1];
3601 __first[(__k + __q) % __n] += __r;
3602 __first[__kmodn] = __r;
3603 }
3604 for (size_t __k = __s + 1; __k < __m; ++__k)
3605 {
3606 const size_t __kmodn = __k % __n;
3607 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:503608 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:163609 ^ __first[(__k - 1) % __n]);
3610 __first[__kpmodn] += __r;
3611 __r += __kmodn;
3612 __first[(__k + __q) % __n] += __r;
3613 __first[__kmodn] = __r;
3614 }
3615 for (size_t __k = __m; __k < __m + __n; ++__k)
3616 {
3617 const size_t __kmodn = __k % __n;
3618 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:503619 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantbc8d3f92010-05-11 19:42:163620 __first[__kpmodn] +
3621 __first[(__k - 1) % __n]);
3622 __first[__kpmodn] ^= __r;
3623 __r -= __kmodn;
3624 __first[(__k + __q) % __n] ^= __r;
3625 __first[__kmodn] = __r;
3626 }
3627 }
3628}
3629
Howard Hinnant30a840f2010-05-12 17:08:573630// generate_canonical
3631
Howard Hinnantbc8d3f92010-05-11 19:42:163632template<class _RealType, size_t __bits, class _URNG>
3633_RealType
3634generate_canonical(_URNG& __g)
3635{
3636 const size_t _Dt = numeric_limits<_RealType>::digits;
3637 const size_t __b = _Dt < __bits ? _Dt : __bits;
Howard Hinnant8efd3da2012-04-02 21:00:453638#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:163639 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant8efd3da2012-04-02 21:00:453640#else
3641 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3642#endif
Howard Hinnantbc8d3f92010-05-11 19:42:163643 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant8efd3da2012-04-02 21:00:453644 const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
Howard Hinnant99968442011-11-29 18:15:503645 _RealType __base = _Rp;
Howard Hinnant8efd3da2012-04-02 21:00:453646 _RealType _Sp = __g() - _URNG::min();
Howard Hinnant99968442011-11-29 18:15:503647 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant8efd3da2012-04-02 21:00:453648 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnant99968442011-11-29 18:15:503649 return _Sp / __base;
Howard Hinnantbc8d3f92010-05-11 19:42:163650}
3651
Howard Hinnant30a840f2010-05-12 17:08:573652// uniform_int_distribution
3653
Howard Hinnantc3267212010-05-26 17:49:343654// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:163655
3656template <class _CharT, class _Traits, class _IT>
3657basic_ostream<_CharT, _Traits>&
3658operator<<(basic_ostream<_CharT, _Traits>& __os,
3659 const uniform_int_distribution<_IT>& __x)
3660{
Howard Hinnant9c0df142012-10-30 19:06:593661 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:163662 __os.flags(ios_base::dec | ios_base::left);
3663 _CharT __sp = __os.widen(' ');
3664 __os.fill(__sp);
3665 return __os << __x.a() << __sp << __x.b();
3666}
3667
3668template <class _CharT, class _Traits, class _IT>
3669basic_istream<_CharT, _Traits>&
3670operator>>(basic_istream<_CharT, _Traits>& __is,
3671 uniform_int_distribution<_IT>& __x)
3672{
3673 typedef uniform_int_distribution<_IT> _Eng;
3674 typedef typename _Eng::result_type result_type;
3675 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:593676 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:163677 __is.flags(ios_base::dec | ios_base::skipws);
3678 result_type __a;
3679 result_type __b;
3680 __is >> __a >> __b;
3681 if (!__is.fail())
3682 __x.param(param_type(__a, __b));
3683 return __is;
3684}
3685
Howard Hinnant30a840f2010-05-12 17:08:573686// uniform_real_distribution
3687
Howard Hinnantbc8d3f92010-05-11 19:42:163688template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:343689class _LIBCPP_TYPE_VIS_ONLY uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:163690{
3691public:
3692 // types
3693 typedef _RealType result_type;
3694
Howard Hinnant0f678bd2013-08-12 18:38:343695 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantbc8d3f92010-05-11 19:42:163696 {
3697 result_type __a_;
3698 result_type __b_;
3699 public:
3700 typedef uniform_real_distribution distribution_type;
3701
Howard Hinnantb9af2ea2010-09-22 18:02:383702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163703 explicit param_type(result_type __a = 0,
3704 result_type __b = 1)
3705 : __a_(__a), __b_(__b) {}
3706
Howard Hinnantb9af2ea2010-09-22 18:02:383707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163708 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163710 result_type b() const {return __b_;}
3711
Howard Hinnantb9af2ea2010-09-22 18:02:383712 friend _LIBCPP_INLINE_VISIBILITY
3713 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163714 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383715 friend _LIBCPP_INLINE_VISIBILITY
3716 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163717 {return !(__x == __y);}
3718 };
3719
3720private:
3721 param_type __p_;
3722
3723public:
3724 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:383725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163726 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3727 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163729 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163731 void reset() {}
3732
3733 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:383734 template<class _URNG>
3735 _LIBCPP_INLINE_VISIBILITY
3736 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:163737 {return (*this)(__g, __p_);}
3738 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3739
3740 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:383741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163742 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:383743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163744 result_type b() const {return __p_.b();}
3745
Howard Hinnantb9af2ea2010-09-22 18:02:383746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163747 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163749 void param(const param_type& __p) {__p_ = __p;}
3750
Howard Hinnantb9af2ea2010-09-22 18:02:383751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163752 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:383753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163754 result_type max() const {return b();}
3755
Howard Hinnantb9af2ea2010-09-22 18:02:383756 friend _LIBCPP_INLINE_VISIBILITY
3757 bool operator==(const uniform_real_distribution& __x,
3758 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163759 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383760 friend _LIBCPP_INLINE_VISIBILITY
3761 bool operator!=(const uniform_real_distribution& __x,
3762 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163763 {return !(__x == __y);}
3764};
3765
3766template<class _RealType>
3767template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:383768inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163769typename uniform_real_distribution<_RealType>::result_type
3770uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3771{
3772 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:193773 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:163774 + __p.a();
3775}
3776
3777template <class _CharT, class _Traits, class _RT>
3778basic_ostream<_CharT, _Traits>&
3779operator<<(basic_ostream<_CharT, _Traits>& __os,
3780 const uniform_real_distribution<_RT>& __x)
3781{
Howard Hinnant9c0df142012-10-30 19:06:593782 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:403783 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3784 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:163785 _CharT __sp = __os.widen(' ');
3786 __os.fill(__sp);
3787 return __os << __x.a() << __sp << __x.b();
3788}
3789
3790template <class _CharT, class _Traits, class _RT>
3791basic_istream<_CharT, _Traits>&
3792operator>>(basic_istream<_CharT, _Traits>& __is,
3793 uniform_real_distribution<_RT>& __x)
3794{
3795 typedef uniform_real_distribution<_RT> _Eng;
3796 typedef typename _Eng::result_type result_type;
3797 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:593798 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:163799 __is.flags(ios_base::dec | ios_base::skipws);
3800 result_type __a;
3801 result_type __b;
3802 __is >> __a >> __b;
3803 if (!__is.fail())
3804 __x.param(param_type(__a, __b));
3805 return __is;
3806}
3807
Howard Hinnant30a840f2010-05-12 17:08:573808// bernoulli_distribution
3809
Howard Hinnant0f678bd2013-08-12 18:38:343810class _LIBCPP_TYPE_VIS_ONLY bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:163811{
3812public:
3813 // types
3814 typedef bool result_type;
3815
Howard Hinnant0f678bd2013-08-12 18:38:343816 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantbc8d3f92010-05-11 19:42:163817 {
3818 double __p_;
3819 public:
3820 typedef bernoulli_distribution distribution_type;
3821
Howard Hinnantb9af2ea2010-09-22 18:02:383822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163823 explicit param_type(double __p = 0.5) : __p_(__p) {}
3824
Howard Hinnantb9af2ea2010-09-22 18:02:383825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163826 double p() const {return __p_;}
3827
Howard Hinnantb9af2ea2010-09-22 18:02:383828 friend _LIBCPP_INLINE_VISIBILITY
3829 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163830 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383831 friend _LIBCPP_INLINE_VISIBILITY
3832 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163833 {return !(__x == __y);}
3834 };
3835
3836private:
3837 param_type __p_;
3838
3839public:
3840 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:383841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163842 explicit bernoulli_distribution(double __p = 0.5)
3843 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163845 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163847 void reset() {}
3848
3849 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:383850 template<class _URNG>
3851 _LIBCPP_INLINE_VISIBILITY
3852 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:163853 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:593854 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:163855
3856 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:383857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163858 double p() const {return __p_.p();}
3859
Howard Hinnantb9af2ea2010-09-22 18:02:383860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163861 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163863 void param(const param_type& __p) {__p_ = __p;}
3864
Howard Hinnantb9af2ea2010-09-22 18:02:383865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163866 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:383867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163868 result_type max() const {return true;}
3869
Howard Hinnantb9af2ea2010-09-22 18:02:383870 friend _LIBCPP_INLINE_VISIBILITY
3871 bool operator==(const bernoulli_distribution& __x,
3872 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163873 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383874 friend _LIBCPP_INLINE_VISIBILITY
3875 bool operator!=(const bernoulli_distribution& __x,
3876 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:163877 {return !(__x == __y);}
3878};
3879
Howard Hinnant03aad812010-05-11 23:26:593880template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:383881inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593882bernoulli_distribution::result_type
3883bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3884{
Howard Hinnantd6d11712010-05-20 15:11:463885 uniform_real_distribution<double> __gen;
3886 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:593887}
3888
Howard Hinnantbc8d3f92010-05-11 19:42:163889template <class _CharT, class _Traits>
3890basic_ostream<_CharT, _Traits>&
3891operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3892{
Howard Hinnant9c0df142012-10-30 19:06:593893 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:403894 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3895 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:163896 _CharT __sp = __os.widen(' ');
3897 __os.fill(__sp);
3898 return __os << __x.p();
3899}
3900
3901template <class _CharT, class _Traits>
3902basic_istream<_CharT, _Traits>&
3903operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3904{
3905 typedef bernoulli_distribution _Eng;
3906 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:593907 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:163908 __is.flags(ios_base::dec | ios_base::skipws);
3909 double __p;
3910 __is >> __p;
3911 if (!__is.fail())
3912 __x.param(param_type(__p));
3913 return __is;
3914}
3915
Howard Hinnant30a840f2010-05-12 17:08:573916// binomial_distribution
3917
Howard Hinnant03aad812010-05-11 23:26:593918template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:343919class _LIBCPP_TYPE_VIS_ONLY binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:593920{
3921public:
3922 // types
3923 typedef _IntType result_type;
3924
Howard Hinnant0f678bd2013-08-12 18:38:343925 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant03aad812010-05-11 23:26:593926 {
3927 result_type __t_;
3928 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:233929 double __pr_;
3930 double __odds_ratio_;
3931 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:593932 public:
3933 typedef binomial_distribution distribution_type;
3934
Howard Hinnant6add8dd2010-05-15 21:36:233935 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:593936
Howard Hinnantb9af2ea2010-09-22 18:02:383937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593938 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593940 double p() const {return __p_;}
3941
Howard Hinnantb9af2ea2010-09-22 18:02:383942 friend _LIBCPP_INLINE_VISIBILITY
3943 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:593944 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383945 friend _LIBCPP_INLINE_VISIBILITY
3946 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:593947 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:233948
3949 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:593950 };
3951
3952private:
3953 param_type __p_;
3954
3955public:
3956 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:383957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593958 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3959 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593961 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:383962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593963 void reset() {}
3964
3965 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:383966 template<class _URNG>
3967 _LIBCPP_INLINE_VISIBILITY
3968 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:593969 {return (*this)(__g, __p_);}
3970 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3971
3972 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:383973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593974 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:383975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593976 double p() const {return __p_.p();}
3977
Howard Hinnantb9af2ea2010-09-22 18:02:383978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593979 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593981 void param(const param_type& __p) {__p_ = __p;}
3982
Howard Hinnantb9af2ea2010-09-22 18:02:383983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593984 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:383985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:593986 result_type max() const {return t();}
3987
Howard Hinnantb9af2ea2010-09-22 18:02:383988 friend _LIBCPP_INLINE_VISIBILITY
3989 bool operator==(const binomial_distribution& __x,
3990 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:593991 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:383992 friend _LIBCPP_INLINE_VISIBILITY
3993 bool operator!=(const binomial_distribution& __x,
3994 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:593995 {return !(__x == __y);}
3996};
3997
3998template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:233999binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
4000 : __t_(__t), __p_(__p)
4001{
4002 if (0 < __p_ && __p_ < 1)
4003 {
4004 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:194005 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
4006 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
4007 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:234008 __odds_ratio_ = __p_ / (1 - __p_);
4009 }
4010}
4011
Marshall Clow5ffb8d02014-09-17 18:33:584012// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
4013// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
Howard Hinnant6add8dd2010-05-15 21:36:234014template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:594015template<class _URNG>
4016_IntType
Howard Hinnant6add8dd2010-05-15 21:36:234017binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:594018{
Howard Hinnant6add8dd2010-05-15 21:36:234019 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4020 return 0;
4021 if (__pr.__p_ == 1)
4022 return __pr.__t_;
4023 uniform_real_distribution<double> __gen;
4024 double __u = __gen(__g) - __pr.__pr_;
4025 if (__u < 0)
4026 return __pr.__r0_;
4027 double __pu = __pr.__pr_;
4028 double __pd = __pu;
4029 result_type __ru = __pr.__r0_;
4030 result_type __rd = __ru;
4031 while (true)
4032 {
4033 if (__rd >= 1)
4034 {
4035 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4036 __u -= __pd;
4037 if (__u < 0)
4038 return __rd - 1;
4039 }
Marshall Clow5ffb8d02014-09-17 18:33:584040 if ( __rd != 0 )
4041 --__rd;
Howard Hinnant6add8dd2010-05-15 21:36:234042 ++__ru;
4043 if (__ru <= __pr.__t_)
4044 {
4045 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4046 __u -= __pu;
4047 if (__u < 0)
4048 return __ru;
4049 }
4050 }
Howard Hinnant03aad812010-05-11 23:26:594051}
4052
4053template <class _CharT, class _Traits, class _IntType>
4054basic_ostream<_CharT, _Traits>&
4055operator<<(basic_ostream<_CharT, _Traits>& __os,
4056 const binomial_distribution<_IntType>& __x)
4057{
Howard Hinnant9c0df142012-10-30 19:06:594058 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:404059 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4060 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:594061 _CharT __sp = __os.widen(' ');
4062 __os.fill(__sp);
4063 return __os << __x.t() << __sp << __x.p();
4064}
4065
4066template <class _CharT, class _Traits, class _IntType>
4067basic_istream<_CharT, _Traits>&
4068operator>>(basic_istream<_CharT, _Traits>& __is,
4069 binomial_distribution<_IntType>& __x)
4070{
4071 typedef binomial_distribution<_IntType> _Eng;
4072 typedef typename _Eng::result_type result_type;
4073 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:594074 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant03aad812010-05-11 23:26:594075 __is.flags(ios_base::dec | ios_base::skipws);
4076 result_type __t;
4077 double __p;
4078 __is >> __t >> __p;
4079 if (!__is.fail())
4080 __x.param(param_type(__t, __p));
4081 return __is;
4082}
4083
Howard Hinnant30a840f2010-05-12 17:08:574084// exponential_distribution
4085
4086template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:344087class _LIBCPP_TYPE_VIS_ONLY exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:574088{
4089public:
4090 // types
4091 typedef _RealType result_type;
4092
Howard Hinnant0f678bd2013-08-12 18:38:344093 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant30a840f2010-05-12 17:08:574094 {
4095 result_type __lambda_;
4096 public:
4097 typedef exponential_distribution distribution_type;
4098
Howard Hinnantb9af2ea2010-09-22 18:02:384099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574100 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4101
Howard Hinnantb9af2ea2010-09-22 18:02:384102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574103 result_type lambda() const {return __lambda_;}
4104
Howard Hinnantb9af2ea2010-09-22 18:02:384105 friend _LIBCPP_INLINE_VISIBILITY
4106 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:574107 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384108 friend _LIBCPP_INLINE_VISIBILITY
4109 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:574110 {return !(__x == __y);}
4111 };
4112
4113private:
4114 param_type __p_;
4115
4116public:
4117 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574119 explicit exponential_distribution(result_type __lambda = 1)
4120 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574122 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574124 void reset() {}
4125
4126 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:384127 template<class _URNG>
4128 _LIBCPP_INLINE_VISIBILITY
4129 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:574130 {return (*this)(__g, __p_);}
4131 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4132
4133 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:384134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574135 result_type lambda() const {return __p_.lambda();}
4136
Howard Hinnantb9af2ea2010-09-22 18:02:384137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574138 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574140 void param(const param_type& __p) {__p_ = __p;}
4141
Howard Hinnantb9af2ea2010-09-22 18:02:384142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:574143 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:384144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:204145 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:574146
Howard Hinnantb9af2ea2010-09-22 18:02:384147 friend _LIBCPP_INLINE_VISIBILITY
4148 bool operator==(const exponential_distribution& __x,
4149 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:574150 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384151 friend _LIBCPP_INLINE_VISIBILITY
4152 bool operator!=(const exponential_distribution& __x,
4153 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:574154 {return !(__x == __y);}
4155};
4156
4157template <class _RealType>
4158template<class _URNG>
4159_RealType
4160exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4161{
Howard Hinnant0949eed2011-06-30 21:18:194162 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:574163 (
4164 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:194165 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:574166 numeric_limits<result_type>::digits>(__g)
4167 )
4168 / __p.lambda();
4169}
4170
4171template <class _CharT, class _Traits, class _RealType>
4172basic_ostream<_CharT, _Traits>&
4173operator<<(basic_ostream<_CharT, _Traits>& __os,
4174 const exponential_distribution<_RealType>& __x)
4175{
Howard Hinnant9c0df142012-10-30 19:06:594176 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:404177 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4178 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:574179 return __os << __x.lambda();
4180}
4181
4182template <class _CharT, class _Traits, class _RealType>
4183basic_istream<_CharT, _Traits>&
4184operator>>(basic_istream<_CharT, _Traits>& __is,
4185 exponential_distribution<_RealType>& __x)
4186{
4187 typedef exponential_distribution<_RealType> _Eng;
4188 typedef typename _Eng::result_type result_type;
4189 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:594190 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant30a840f2010-05-12 17:08:574191 __is.flags(ios_base::dec | ios_base::skipws);
4192 result_type __lambda;
4193 __is >> __lambda;
4194 if (!__is.fail())
4195 __x.param(param_type(__lambda));
4196 return __is;
4197}
4198
Howard Hinnant6add8dd2010-05-15 21:36:234199// normal_distribution
4200
4201template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:344202class _LIBCPP_TYPE_VIS_ONLY normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:234203{
4204public:
4205 // types
4206 typedef _RealType result_type;
4207
Howard Hinnant0f678bd2013-08-12 18:38:344208 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant6add8dd2010-05-15 21:36:234209 {
4210 result_type __mean_;
4211 result_type __stddev_;
4212 public:
4213 typedef normal_distribution distribution_type;
4214
Howard Hinnantb9af2ea2010-09-22 18:02:384215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234216 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4217 : __mean_(__mean), __stddev_(__stddev) {}
4218
Howard Hinnantb9af2ea2010-09-22 18:02:384219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234220 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234222 result_type stddev() const {return __stddev_;}
4223
Howard Hinnantb9af2ea2010-09-22 18:02:384224 friend _LIBCPP_INLINE_VISIBILITY
4225 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234226 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384227 friend _LIBCPP_INLINE_VISIBILITY
4228 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234229 {return !(__x == __y);}
4230 };
4231
4232private:
4233 param_type __p_;
4234 result_type _V_;
4235 bool _V_hot_;
4236
4237public:
4238 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234240 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4241 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234243 explicit normal_distribution(const param_type& __p)
4244 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234246 void reset() {_V_hot_ = false;}
4247
4248 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:384249 template<class _URNG>
4250 _LIBCPP_INLINE_VISIBILITY
4251 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:234252 {return (*this)(__g, __p_);}
4253 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4254
4255 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:384256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234257 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:384258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234259 result_type stddev() const {return __p_.stddev();}
4260
Howard Hinnantb9af2ea2010-09-22 18:02:384261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234262 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234264 void param(const param_type& __p) {__p_ = __p;}
4265
Howard Hinnantb9af2ea2010-09-22 18:02:384266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234267 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:384268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234269 result_type max() const {return numeric_limits<result_type>::infinity();}
4270
Howard Hinnantb9af2ea2010-09-22 18:02:384271 friend _LIBCPP_INLINE_VISIBILITY
4272 bool operator==(const normal_distribution& __x,
4273 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234274 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4275 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:384276 friend _LIBCPP_INLINE_VISIBILITY
4277 bool operator!=(const normal_distribution& __x,
4278 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234279 {return !(__x == __y);}
4280
4281 template <class _CharT, class _Traits, class _RT>
4282 friend
4283 basic_ostream<_CharT, _Traits>&
4284 operator<<(basic_ostream<_CharT, _Traits>& __os,
4285 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:434286
Howard Hinnant6add8dd2010-05-15 21:36:234287 template <class _CharT, class _Traits, class _RT>
4288 friend
4289 basic_istream<_CharT, _Traits>&
4290 operator>>(basic_istream<_CharT, _Traits>& __is,
4291 normal_distribution<_RT>& __x);
4292};
4293
4294template <class _RealType>
4295template<class _URNG>
4296_RealType
4297normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4298{
Howard Hinnant99968442011-11-29 18:15:504299 result_type _Up;
Howard Hinnant6add8dd2010-05-15 21:36:234300 if (_V_hot_)
4301 {
4302 _V_hot_ = false;
Howard Hinnant99968442011-11-29 18:15:504303 _Up = _V_;
Howard Hinnant6add8dd2010-05-15 21:36:234304 }
4305 else
4306 {
4307 uniform_real_distribution<result_type> _Uni(-1, 1);
4308 result_type __u;
4309 result_type __v;
4310 result_type __s;
4311 do
4312 {
4313 __u = _Uni(__g);
4314 __v = _Uni(__g);
4315 __s = __u * __u + __v * __v;
4316 } while (__s > 1 || __s == 0);
Howard Hinnant99968442011-11-29 18:15:504317 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4318 _V_ = __v * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:234319 _V_hot_ = true;
Howard Hinnant99968442011-11-29 18:15:504320 _Up = __u * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:234321 }
Howard Hinnant99968442011-11-29 18:15:504322 return _Up * __p.stddev() + __p.mean();
Howard Hinnant6add8dd2010-05-15 21:36:234323}
4324
4325template <class _CharT, class _Traits, class _RT>
4326basic_ostream<_CharT, _Traits>&
4327operator<<(basic_ostream<_CharT, _Traits>& __os,
4328 const normal_distribution<_RT>& __x)
4329{
Howard Hinnant9c0df142012-10-30 19:06:594330 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:404331 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4332 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:234333 _CharT __sp = __os.widen(' ');
4334 __os.fill(__sp);
4335 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4336 if (__x._V_hot_)
4337 __os << __sp << __x._V_;
4338 return __os;
4339}
4340
4341template <class _CharT, class _Traits, class _RT>
4342basic_istream<_CharT, _Traits>&
4343operator>>(basic_istream<_CharT, _Traits>& __is,
4344 normal_distribution<_RT>& __x)
4345{
4346 typedef normal_distribution<_RT> _Eng;
4347 typedef typename _Eng::result_type result_type;
4348 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:594349 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:234350 __is.flags(ios_base::dec | ios_base::skipws);
4351 result_type __mean;
4352 result_type __stddev;
Howard Hinnant99968442011-11-29 18:15:504353 result_type _Vp = 0;
Howard Hinnant6add8dd2010-05-15 21:36:234354 bool _V_hot = false;
4355 __is >> __mean >> __stddev >> _V_hot;
4356 if (_V_hot)
Howard Hinnant99968442011-11-29 18:15:504357 __is >> _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:234358 if (!__is.fail())
4359 {
4360 __x.param(param_type(__mean, __stddev));
4361 __x._V_hot_ = _V_hot;
Howard Hinnant99968442011-11-29 18:15:504362 __x._V_ = _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:234363 }
4364 return __is;
4365}
4366
Howard Hinnant2bc36fc2010-05-17 18:31:534367// lognormal_distribution
4368
4369template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:344370class _LIBCPP_TYPE_VIS_ONLY lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:534371{
4372public:
4373 // types
4374 typedef _RealType result_type;
4375
Howard Hinnant0f678bd2013-08-12 18:38:344376 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant2bc36fc2010-05-17 18:31:534377 {
4378 normal_distribution<result_type> __nd_;
4379 public:
4380 typedef lognormal_distribution distribution_type;
4381
Howard Hinnantb9af2ea2010-09-22 18:02:384382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534383 explicit param_type(result_type __m = 0, result_type __s = 1)
4384 : __nd_(__m, __s) {}
4385
Howard Hinnantb9af2ea2010-09-22 18:02:384386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534387 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:384388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534389 result_type s() const {return __nd_.stddev();}
4390
Howard Hinnantb9af2ea2010-09-22 18:02:384391 friend _LIBCPP_INLINE_VISIBILITY
4392 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:534393 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384394 friend _LIBCPP_INLINE_VISIBILITY
4395 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:534396 {return !(__x == __y);}
4397 friend class lognormal_distribution;
4398
4399 template <class _CharT, class _Traits, class _RT>
4400 friend
4401 basic_ostream<_CharT, _Traits>&
4402 operator<<(basic_ostream<_CharT, _Traits>& __os,
4403 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:434404
Howard Hinnant2bc36fc2010-05-17 18:31:534405 template <class _CharT, class _Traits, class _RT>
4406 friend
4407 basic_istream<_CharT, _Traits>&
4408 operator>>(basic_istream<_CharT, _Traits>& __is,
4409 lognormal_distribution<_RT>& __x);
4410 };
4411
4412private:
4413 param_type __p_;
4414
4415public:
4416 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534418 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4419 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534421 explicit lognormal_distribution(const param_type& __p)
4422 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534424 void reset() {__p_.__nd_.reset();}
4425
4426 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:384427 template<class _URNG>
4428 _LIBCPP_INLINE_VISIBILITY
4429 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:534430 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:384431 template<class _URNG>
4432 _LIBCPP_INLINE_VISIBILITY
4433 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:194434 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:534435
4436 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:384437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534438 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:384439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534440 result_type s() const {return __p_.s();}
4441
Howard Hinnantb9af2ea2010-09-22 18:02:384442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534443 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:574445 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:534446
Howard Hinnantb9af2ea2010-09-22 18:02:384447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534448 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:384449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534450 result_type max() const {return numeric_limits<result_type>::infinity();}
4451
Howard Hinnantb9af2ea2010-09-22 18:02:384452 friend _LIBCPP_INLINE_VISIBILITY
4453 bool operator==(const lognormal_distribution& __x,
4454 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:534455 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384456 friend _LIBCPP_INLINE_VISIBILITY
4457 bool operator!=(const lognormal_distribution& __x,
4458 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:534459 {return !(__x == __y);}
4460
4461 template <class _CharT, class _Traits, class _RT>
4462 friend
4463 basic_ostream<_CharT, _Traits>&
4464 operator<<(basic_ostream<_CharT, _Traits>& __os,
4465 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:434466
Howard Hinnant2bc36fc2010-05-17 18:31:534467 template <class _CharT, class _Traits, class _RT>
4468 friend
4469 basic_istream<_CharT, _Traits>&
4470 operator>>(basic_istream<_CharT, _Traits>& __is,
4471 lognormal_distribution<_RT>& __x);
4472};
4473
4474template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:384475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534476basic_ostream<_CharT, _Traits>&
4477operator<<(basic_ostream<_CharT, _Traits>& __os,
4478 const lognormal_distribution<_RT>& __x)
4479{
4480 return __os << __x.__p_.__nd_;
4481}
4482
4483template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:384484inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:534485basic_istream<_CharT, _Traits>&
4486operator>>(basic_istream<_CharT, _Traits>& __is,
4487 lognormal_distribution<_RT>& __x)
4488{
4489 return __is >> __x.__p_.__nd_;
4490}
4491
Howard Hinnant6add8dd2010-05-15 21:36:234492// poisson_distribution
4493
4494template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:344495class _LIBCPP_TYPE_VIS_ONLY poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:234496{
4497public:
4498 // types
4499 typedef _IntType result_type;
4500
Howard Hinnant0f678bd2013-08-12 18:38:344501 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant6add8dd2010-05-15 21:36:234502 {
4503 double __mean_;
4504 double __s_;
4505 double __d_;
4506 double __l_;
4507 double __omega_;
4508 double __c0_;
4509 double __c1_;
4510 double __c2_;
4511 double __c3_;
4512 double __c_;
4513
4514 public:
4515 typedef poisson_distribution distribution_type;
4516
4517 explicit param_type(double __mean = 1.0);
4518
Howard Hinnantb9af2ea2010-09-22 18:02:384519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234520 double mean() const {return __mean_;}
4521
Howard Hinnantb9af2ea2010-09-22 18:02:384522 friend _LIBCPP_INLINE_VISIBILITY
4523 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234524 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384525 friend _LIBCPP_INLINE_VISIBILITY
4526 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234527 {return !(__x == __y);}
4528
4529 friend class poisson_distribution;
4530 };
4531
4532private:
4533 param_type __p_;
4534
4535public:
4536 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234538 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234540 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234542 void reset() {}
4543
4544 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:384545 template<class _URNG>
4546 _LIBCPP_INLINE_VISIBILITY
4547 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:234548 {return (*this)(__g, __p_);}
4549 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4550
4551 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:384552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234553 double mean() const {return __p_.mean();}
4554
Howard Hinnantb9af2ea2010-09-22 18:02:384555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234556 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234558 void param(const param_type& __p) {__p_ = __p;}
4559
Howard Hinnantb9af2ea2010-09-22 18:02:384560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234561 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:384562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:234563 result_type max() const {return numeric_limits<result_type>::max();}
4564
Howard Hinnantb9af2ea2010-09-22 18:02:384565 friend _LIBCPP_INLINE_VISIBILITY
4566 bool operator==(const poisson_distribution& __x,
4567 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234568 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384569 friend _LIBCPP_INLINE_VISIBILITY
4570 bool operator!=(const poisson_distribution& __x,
4571 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:234572 {return !(__x == __y);}
4573};
4574
4575template<class _IntType>
4576poisson_distribution<_IntType>::param_type::param_type(double __mean)
4577 : __mean_(__mean)
4578{
4579 if (__mean_ < 10)
4580 {
4581 __s_ = 0;
4582 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:194583 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:234584 __omega_ = 0;
4585 __c3_ = 0;
4586 __c2_ = 0;
4587 __c1_ = 0;
4588 __c0_ = 0;
4589 __c_ = 0;
4590 }
4591 else
4592 {
Howard Hinnant0949eed2011-06-30 21:18:194593 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:234594 __d_ = 6 * __mean_ * __mean_;
4595 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4596 __omega_ = .3989423 / __s_;
4597 double __b1_ = .4166667E-1 / __mean_;
4598 double __b2_ = .3 * __b1_ * __b1_;
4599 __c3_ = .1428571 * __b1_ * __b2_;
4600 __c2_ = __b2_ - 15. * __c3_;
4601 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4602 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4603 __c_ = .1069 / __mean_;
4604 }
4605}
4606
4607template <class _IntType>
4608template<class _URNG>
4609_IntType
4610poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4611{
4612 result_type __x;
4613 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:224614 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:234615 {
4616 __x = 0;
4617 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4618 __p *= __urd(__urng);
4619 }
4620 else
4621 {
4622 double __difmuk;
4623 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4624 double __u;
4625 if (__g > 0)
4626 {
4627 __x = static_cast<result_type>(__g);
4628 if (__x >= __pr.__l_)
4629 return __x;
4630 __difmuk = __pr.__mean_ - __x;
4631 __u = __urd(__urng);
4632 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4633 return __x;
4634 }
4635 exponential_distribution<double> __edist;
4636 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4637 {
4638 double __e;
4639 if (__using_exp_dist || __g < 0)
4640 {
4641 double __t;
4642 do
4643 {
4644 __e = __edist(__urng);
4645 __u = __urd(__urng);
4646 __u += __u - 1;
4647 __t = 1.8 + (__u < 0 ? -__e : __e);
4648 } while (__t <= -.6744);
4649 __x = __pr.__mean_ + __pr.__s_ * __t;
4650 __difmuk = __pr.__mean_ - __x;
4651 __using_exp_dist = true;
4652 }
4653 double __px;
4654 double __py;
4655 if (__x < 10)
4656 {
4657 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4658 40320, 362880};
4659 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:194660 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:234661 }
4662 else
4663 {
4664 double __del = .8333333E-1 / __x;
4665 __del -= 4.8 * __del * __del * __del;
4666 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:194667 if (_VSTD::abs(__v) > 0.25)
4668 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:234669 else
4670 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4671 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4672 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:194673 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:234674 }
4675 double __r = (0.5 - __difmuk) / __pr.__s_;
4676 double __r2 = __r * __r;
4677 double __fx = -0.5 * __r2;
4678 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4679 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4680 if (__using_exp_dist)
4681 {
Howard Hinnant0949eed2011-06-30 21:18:194682 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4683 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:234684 break;
4685 }
4686 else
4687 {
Howard Hinnant0949eed2011-06-30 21:18:194688 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:234689 break;
4690 }
4691 }
4692 }
4693 return __x;
4694}
4695
4696template <class _CharT, class _Traits, class _IntType>
4697basic_ostream<_CharT, _Traits>&
4698operator<<(basic_ostream<_CharT, _Traits>& __os,
4699 const poisson_distribution<_IntType>& __x)
4700{
Howard Hinnant9c0df142012-10-30 19:06:594701 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:404702 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4703 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:234704 return __os << __x.mean();
4705}
4706
4707template <class _CharT, class _Traits, class _IntType>
4708basic_istream<_CharT, _Traits>&
4709operator>>(basic_istream<_CharT, _Traits>& __is,
4710 poisson_distribution<_IntType>& __x)
4711{
4712 typedef poisson_distribution<_IntType> _Eng;
4713 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:594714 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:234715 __is.flags(ios_base::dec | ios_base::skipws);
4716 double __mean;
4717 __is >> __mean;
4718 if (!__is.fail())
4719 __x.param(param_type(__mean));
4720 return __is;
4721}
4722
Howard Hinnant9de6e302010-05-16 01:09:024723// weibull_distribution
4724
4725template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:344726class _LIBCPP_TYPE_VIS_ONLY weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:024727{
4728public:
4729 // types
4730 typedef _RealType result_type;
4731
Howard Hinnant0f678bd2013-08-12 18:38:344732 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant9de6e302010-05-16 01:09:024733 {
4734 result_type __a_;
4735 result_type __b_;
4736 public:
4737 typedef weibull_distribution distribution_type;
4738
Howard Hinnantb9af2ea2010-09-22 18:02:384739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024740 explicit param_type(result_type __a = 1, result_type __b = 1)
4741 : __a_(__a), __b_(__b) {}
4742
Howard Hinnantb9af2ea2010-09-22 18:02:384743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024744 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024746 result_type b() const {return __b_;}
4747
Howard Hinnantb9af2ea2010-09-22 18:02:384748 friend _LIBCPP_INLINE_VISIBILITY
4749 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:024750 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384751 friend _LIBCPP_INLINE_VISIBILITY
4752 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:024753 {return !(__x == __y);}
4754 };
4755
4756private:
4757 param_type __p_;
4758
4759public:
4760 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024762 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4763 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024765 explicit weibull_distribution(const param_type& __p)
4766 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024768 void reset() {}
4769
4770 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:384771 template<class _URNG>
4772 _LIBCPP_INLINE_VISIBILITY
4773 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:024774 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:384775 template<class _URNG>
4776 _LIBCPP_INLINE_VISIBILITY
4777 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:024778 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:194779 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:024780
4781 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:384782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024783 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:384784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024785 result_type b() const {return __p_.b();}
4786
Howard Hinnantb9af2ea2010-09-22 18:02:384787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024788 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024790 void param(const param_type& __p) {__p_ = __p;}
4791
Howard Hinnantb9af2ea2010-09-22 18:02:384792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024793 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:384794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:024795 result_type max() const {return numeric_limits<result_type>::infinity();}
4796
Howard Hinnantb9af2ea2010-09-22 18:02:384797 friend _LIBCPP_INLINE_VISIBILITY
4798 bool operator==(const weibull_distribution& __x,
4799 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:024800 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384801 friend _LIBCPP_INLINE_VISIBILITY
4802 bool operator!=(const weibull_distribution& __x,
4803 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:024804 {return !(__x == __y);}
4805};
4806
4807template <class _CharT, class _Traits, class _RT>
4808basic_ostream<_CharT, _Traits>&
4809operator<<(basic_ostream<_CharT, _Traits>& __os,
4810 const weibull_distribution<_RT>& __x)
4811{
Howard Hinnant9c0df142012-10-30 19:06:594812 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:404813 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4814 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:024815 _CharT __sp = __os.widen(' ');
4816 __os.fill(__sp);
4817 __os << __x.a() << __sp << __x.b();
4818 return __os;
4819}
4820
4821template <class _CharT, class _Traits, class _RT>
4822basic_istream<_CharT, _Traits>&
4823operator>>(basic_istream<_CharT, _Traits>& __is,
4824 weibull_distribution<_RT>& __x)
4825{
4826 typedef weibull_distribution<_RT> _Eng;
4827 typedef typename _Eng::result_type result_type;
4828 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:594829 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant9de6e302010-05-16 01:09:024830 __is.flags(ios_base::dec | ios_base::skipws);
4831 result_type __a;
4832 result_type __b;
4833 __is >> __a >> __b;
4834 if (!__is.fail())
4835 __x.param(param_type(__a, __b));
4836 return __is;
4837}
4838
Howard Hinnantc2b0dc72010-05-17 16:21:564839template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:344840class _LIBCPP_TYPE_VIS_ONLY extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:564841{
4842public:
4843 // types
4844 typedef _RealType result_type;
4845
Howard Hinnant0f678bd2013-08-12 18:38:344846 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantc2b0dc72010-05-17 16:21:564847 {
4848 result_type __a_;
4849 result_type __b_;
4850 public:
4851 typedef extreme_value_distribution distribution_type;
4852
Howard Hinnantb9af2ea2010-09-22 18:02:384853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564854 explicit param_type(result_type __a = 0, result_type __b = 1)
4855 : __a_(__a), __b_(__b) {}
4856
Howard Hinnantb9af2ea2010-09-22 18:02:384857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564858 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564860 result_type b() const {return __b_;}
4861
Howard Hinnantb9af2ea2010-09-22 18:02:384862 friend _LIBCPP_INLINE_VISIBILITY
4863 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:564864 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384865 friend _LIBCPP_INLINE_VISIBILITY
4866 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:564867 {return !(__x == __y);}
4868 };
4869
4870private:
4871 param_type __p_;
4872
4873public:
4874 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564876 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4877 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564879 explicit extreme_value_distribution(const param_type& __p)
4880 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564882 void reset() {}
4883
4884 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:384885 template<class _URNG>
4886 _LIBCPP_INLINE_VISIBILITY
4887 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:564888 {return (*this)(__g, __p_);}
4889 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4890
4891 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:384892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564893 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:384894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564895 result_type b() const {return __p_.b();}
4896
Howard Hinnantb9af2ea2010-09-22 18:02:384897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564898 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564900 void param(const param_type& __p) {__p_ = __p;}
4901
Howard Hinnantb9af2ea2010-09-22 18:02:384902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564903 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:384904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:564905 result_type max() const {return numeric_limits<result_type>::infinity();}
4906
Howard Hinnantb9af2ea2010-09-22 18:02:384907 friend _LIBCPP_INLINE_VISIBILITY
4908 bool operator==(const extreme_value_distribution& __x,
4909 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:564910 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384911 friend _LIBCPP_INLINE_VISIBILITY
4912 bool operator!=(const extreme_value_distribution& __x,
4913 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:564914 {return !(__x == __y);}
4915};
4916
4917template<class _RealType>
4918template<class _URNG>
4919_RealType
4920extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4921{
4922 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:194923 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:564924}
4925
4926template <class _CharT, class _Traits, class _RT>
4927basic_ostream<_CharT, _Traits>&
4928operator<<(basic_ostream<_CharT, _Traits>& __os,
4929 const extreme_value_distribution<_RT>& __x)
4930{
Howard Hinnant9c0df142012-10-30 19:06:594931 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:404932 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4933 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:564934 _CharT __sp = __os.widen(' ');
4935 __os.fill(__sp);
4936 __os << __x.a() << __sp << __x.b();
4937 return __os;
4938}
4939
4940template <class _CharT, class _Traits, class _RT>
4941basic_istream<_CharT, _Traits>&
4942operator>>(basic_istream<_CharT, _Traits>& __is,
4943 extreme_value_distribution<_RT>& __x)
4944{
4945 typedef extreme_value_distribution<_RT> _Eng;
4946 typedef typename _Eng::result_type result_type;
4947 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:594948 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc2b0dc72010-05-17 16:21:564949 __is.flags(ios_base::dec | ios_base::skipws);
4950 result_type __a;
4951 result_type __b;
4952 __is >> __a >> __b;
4953 if (!__is.fail())
4954 __x.param(param_type(__a, __b));
4955 return __is;
4956}
4957
Howard Hinnantc7c49132010-05-13 17:58:284958// gamma_distribution
4959
4960template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:344961class _LIBCPP_TYPE_VIS_ONLY gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:284962{
4963public:
4964 // types
4965 typedef _RealType result_type;
4966
Howard Hinnant0f678bd2013-08-12 18:38:344967 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantc7c49132010-05-13 17:58:284968 {
4969 result_type __alpha_;
4970 result_type __beta_;
4971 public:
4972 typedef gamma_distribution distribution_type;
4973
Howard Hinnantb9af2ea2010-09-22 18:02:384974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:284975 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4976 : __alpha_(__alpha), __beta_(__beta) {}
4977
Howard Hinnantb9af2ea2010-09-22 18:02:384978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:284979 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:284981 result_type beta() const {return __beta_;}
4982
Howard Hinnantb9af2ea2010-09-22 18:02:384983 friend _LIBCPP_INLINE_VISIBILITY
4984 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:284985 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:384986 friend _LIBCPP_INLINE_VISIBILITY
4987 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:284988 {return !(__x == __y);}
4989 };
4990
4991private:
4992 param_type __p_;
4993
4994public:
4995 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:384996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:284997 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4998 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:384999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285000 explicit gamma_distribution(const param_type& __p)
5001 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285003 void reset() {}
5004
5005 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385006 template<class _URNG>
5007 _LIBCPP_INLINE_VISIBILITY
5008 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:285009 {return (*this)(__g, __p_);}
5010 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5011
5012 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285014 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:385015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285016 result_type beta() const {return __p_.beta();}
5017
Howard Hinnantb9af2ea2010-09-22 18:02:385018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285019 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285021 void param(const param_type& __p) {__p_ = __p;}
5022
Howard Hinnantb9af2ea2010-09-22 18:02:385023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:285024 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:385025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:435026 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:285027
Howard Hinnantb9af2ea2010-09-22 18:02:385028 friend _LIBCPP_INLINE_VISIBILITY
5029 bool operator==(const gamma_distribution& __x,
5030 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:285031 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385032 friend _LIBCPP_INLINE_VISIBILITY
5033 bool operator!=(const gamma_distribution& __x,
5034 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:285035 {return !(__x == __y);}
5036};
5037
5038template <class _RealType>
5039template<class _URNG>
5040_RealType
5041gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5042{
Howard Hinnantf417abe2010-05-14 18:43:105043 result_type __a = __p.alpha();
5044 uniform_real_distribution<result_type> __gen(0, 1);
5045 exponential_distribution<result_type> __egen;
5046 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:285047 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:105048 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:285049 else if (__a > 1)
5050 {
5051 const result_type __b = __a - 1;
5052 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:285053 while (true)
5054 {
5055 const result_type __u = __gen(__g);
5056 const result_type __v = __gen(__g);
5057 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:105058 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:285059 {
Howard Hinnant0949eed2011-06-30 21:18:195060 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:285061 (__u - result_type(0.5));
5062 __x = __b + __y;
5063 if (__x >= 0)
5064 {
5065 const result_type __z = 64 * __w * __w * __w * __v * __v;
5066 if (__z <= 1 - 2 * __y * __y / __x)
5067 break;
Howard Hinnant0949eed2011-06-30 21:18:195068 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:285069 break;
5070 }
5071 }
5072 }
Howard Hinnantc7c49132010-05-13 17:58:285073 }
Howard Hinnantf417abe2010-05-14 18:43:105074 else // __a < 1
5075 {
5076 while (true)
5077 {
5078 const result_type __u = __gen(__g);
5079 const result_type __es = __egen(__g);
5080 if (__u <= 1 - __a)
5081 {
Howard Hinnant0949eed2011-06-30 21:18:195082 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:105083 if (__x <= __es)
5084 break;
5085 }
5086 else
5087 {
Howard Hinnant0949eed2011-06-30 21:18:195088 const result_type __e = -_VSTD::log((1-__u)/__a);
5089 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:105090 if (__x <= __e + __es)
5091 break;
5092 }
5093 }
5094 }
5095 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:285096}
5097
5098template <class _CharT, class _Traits, class _RT>
5099basic_ostream<_CharT, _Traits>&
5100operator<<(basic_ostream<_CharT, _Traits>& __os,
5101 const gamma_distribution<_RT>& __x)
5102{
Howard Hinnant9c0df142012-10-30 19:06:595103 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405104 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5105 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:285106 _CharT __sp = __os.widen(' ');
5107 __os.fill(__sp);
5108 __os << __x.alpha() << __sp << __x.beta();
5109 return __os;
5110}
5111
5112template <class _CharT, class _Traits, class _RT>
5113basic_istream<_CharT, _Traits>&
5114operator>>(basic_istream<_CharT, _Traits>& __is,
5115 gamma_distribution<_RT>& __x)
5116{
5117 typedef gamma_distribution<_RT> _Eng;
5118 typedef typename _Eng::result_type result_type;
5119 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595120 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc7c49132010-05-13 17:58:285121 __is.flags(ios_base::dec | ios_base::skipws);
5122 result_type __alpha;
5123 result_type __beta;
5124 __is >> __alpha >> __beta;
5125 if (!__is.fail())
5126 __x.param(param_type(__alpha, __beta));
5127 return __is;
5128}
Howard Hinnanta64111c2010-05-12 21:02:315129
Howard Hinnantf2fe5d52010-05-17 00:09:385130// negative_binomial_distribution
5131
5132template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:345133class _LIBCPP_TYPE_VIS_ONLY negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:385134{
5135public:
5136 // types
5137 typedef _IntType result_type;
5138
Howard Hinnant0f678bd2013-08-12 18:38:345139 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantf2fe5d52010-05-17 00:09:385140 {
5141 result_type __k_;
5142 double __p_;
5143 public:
5144 typedef negative_binomial_distribution distribution_type;
5145
Howard Hinnantb9af2ea2010-09-22 18:02:385146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385147 explicit param_type(result_type __k = 1, double __p = 0.5)
5148 : __k_(__k), __p_(__p) {}
5149
Howard Hinnantb9af2ea2010-09-22 18:02:385150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385151 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385153 double p() const {return __p_;}
5154
Howard Hinnantb9af2ea2010-09-22 18:02:385155 friend _LIBCPP_INLINE_VISIBILITY
5156 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:385157 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385158 friend _LIBCPP_INLINE_VISIBILITY
5159 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:385160 {return !(__x == __y);}
5161 };
5162
5163private:
5164 param_type __p_;
5165
5166public:
5167 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385169 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5170 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385172 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385174 void reset() {}
5175
5176 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385177 template<class _URNG>
5178 _LIBCPP_INLINE_VISIBILITY
5179 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:385180 {return (*this)(__g, __p_);}
5181 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5182
5183 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385185 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:385186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385187 double p() const {return __p_.p();}
5188
Howard Hinnantb9af2ea2010-09-22 18:02:385189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385190 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385192 void param(const param_type& __p) {__p_ = __p;}
5193
Howard Hinnantb9af2ea2010-09-22 18:02:385194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385195 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:385196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:385197 result_type max() const {return numeric_limits<result_type>::max();}
5198
Howard Hinnantb9af2ea2010-09-22 18:02:385199 friend _LIBCPP_INLINE_VISIBILITY
5200 bool operator==(const negative_binomial_distribution& __x,
5201 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:385202 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385203 friend _LIBCPP_INLINE_VISIBILITY
5204 bool operator!=(const negative_binomial_distribution& __x,
5205 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:385206 {return !(__x == __y);}
5207};
5208
5209template <class _IntType>
5210template<class _URNG>
5211_IntType
5212negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5213{
5214 result_type __k = __pr.k();
5215 double __p = __pr.p();
5216 if (__k <= 21 * __p)
5217 {
5218 bernoulli_distribution __gen(__p);
5219 result_type __f = 0;
5220 result_type __s = 0;
5221 while (__s < __k)
5222 {
5223 if (__gen(__urng))
5224 ++__s;
5225 else
5226 ++__f;
5227 }
5228 return __f;
5229 }
5230 return poisson_distribution<result_type>(gamma_distribution<double>
5231 (__k, (1-__p)/__p)(__urng))(__urng);
5232}
5233
5234template <class _CharT, class _Traits, class _IntType>
5235basic_ostream<_CharT, _Traits>&
5236operator<<(basic_ostream<_CharT, _Traits>& __os,
5237 const negative_binomial_distribution<_IntType>& __x)
5238{
Howard Hinnant9c0df142012-10-30 19:06:595239 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405240 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5241 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:385242 _CharT __sp = __os.widen(' ');
5243 __os.fill(__sp);
5244 return __os << __x.k() << __sp << __x.p();
5245}
5246
5247template <class _CharT, class _Traits, class _IntType>
5248basic_istream<_CharT, _Traits>&
5249operator>>(basic_istream<_CharT, _Traits>& __is,
5250 negative_binomial_distribution<_IntType>& __x)
5251{
5252 typedef negative_binomial_distribution<_IntType> _Eng;
5253 typedef typename _Eng::result_type result_type;
5254 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595255 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantf2fe5d52010-05-17 00:09:385256 __is.flags(ios_base::dec | ios_base::skipws);
5257 result_type __k;
5258 double __p;
5259 __is >> __k >> __p;
5260 if (!__is.fail())
5261 __x.param(param_type(__k, __p));
5262 return __is;
5263}
5264
Howard Hinnant34e8a572010-05-17 13:44:275265// geometric_distribution
5266
5267template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:345268class _LIBCPP_TYPE_VIS_ONLY geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:275269{
5270public:
5271 // types
5272 typedef _IntType result_type;
5273
Howard Hinnant0f678bd2013-08-12 18:38:345274 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant34e8a572010-05-17 13:44:275275 {
5276 double __p_;
5277 public:
5278 typedef geometric_distribution distribution_type;
5279
Howard Hinnantb9af2ea2010-09-22 18:02:385280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275281 explicit param_type(double __p = 0.5) : __p_(__p) {}
5282
Howard Hinnantb9af2ea2010-09-22 18:02:385283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275284 double p() const {return __p_;}
5285
Howard Hinnantb9af2ea2010-09-22 18:02:385286 friend _LIBCPP_INLINE_VISIBILITY
5287 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:275288 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385289 friend _LIBCPP_INLINE_VISIBILITY
5290 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:275291 {return !(__x == __y);}
5292 };
5293
5294private:
5295 param_type __p_;
5296
5297public:
5298 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275300 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275302 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275304 void reset() {}
5305
5306 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385307 template<class _URNG>
5308 _LIBCPP_INLINE_VISIBILITY
5309 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:275310 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:385311 template<class _URNG>
5312 _LIBCPP_INLINE_VISIBILITY
5313 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:275314 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5315
5316 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275318 double p() const {return __p_.p();}
5319
Howard Hinnantb9af2ea2010-09-22 18:02:385320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275321 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275323 void param(const param_type& __p) {__p_ = __p;}
5324
Howard Hinnantb9af2ea2010-09-22 18:02:385325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275326 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:385327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:275328 result_type max() const {return numeric_limits<result_type>::max();}
5329
Howard Hinnantb9af2ea2010-09-22 18:02:385330 friend _LIBCPP_INLINE_VISIBILITY
5331 bool operator==(const geometric_distribution& __x,
5332 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:275333 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385334 friend _LIBCPP_INLINE_VISIBILITY
5335 bool operator!=(const geometric_distribution& __x,
5336 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:275337 {return !(__x == __y);}
5338};
5339
5340template <class _CharT, class _Traits, class _IntType>
5341basic_ostream<_CharT, _Traits>&
5342operator<<(basic_ostream<_CharT, _Traits>& __os,
5343 const geometric_distribution<_IntType>& __x)
5344{
Howard Hinnant9c0df142012-10-30 19:06:595345 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405346 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5347 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:275348 return __os << __x.p();
5349}
5350
5351template <class _CharT, class _Traits, class _IntType>
5352basic_istream<_CharT, _Traits>&
5353operator>>(basic_istream<_CharT, _Traits>& __is,
5354 geometric_distribution<_IntType>& __x)
5355{
5356 typedef geometric_distribution<_IntType> _Eng;
5357 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595358 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant34e8a572010-05-17 13:44:275359 __is.flags(ios_base::dec | ios_base::skipws);
5360 double __p;
5361 __is >> __p;
5362 if (!__is.fail())
5363 __x.param(param_type(__p));
5364 return __is;
5365}
5366
Howard Hinnant97dc2f32010-05-15 23:36:005367// chi_squared_distribution
5368
5369template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:345370class _LIBCPP_TYPE_VIS_ONLY chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:005371{
5372public:
5373 // types
5374 typedef _RealType result_type;
5375
Howard Hinnant0f678bd2013-08-12 18:38:345376 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant97dc2f32010-05-15 23:36:005377 {
5378 result_type __n_;
5379 public:
5380 typedef chi_squared_distribution distribution_type;
5381
Howard Hinnantb9af2ea2010-09-22 18:02:385382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005383 explicit param_type(result_type __n = 1) : __n_(__n) {}
5384
Howard Hinnantb9af2ea2010-09-22 18:02:385385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005386 result_type n() const {return __n_;}
5387
Howard Hinnantb9af2ea2010-09-22 18:02:385388 friend _LIBCPP_INLINE_VISIBILITY
5389 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:005390 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385391 friend _LIBCPP_INLINE_VISIBILITY
5392 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:005393 {return !(__x == __y);}
5394 };
5395
5396private:
5397 param_type __p_;
5398
5399public:
5400 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005402 explicit chi_squared_distribution(result_type __n = 1)
5403 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005405 explicit chi_squared_distribution(const param_type& __p)
5406 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005408 void reset() {}
5409
5410 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385411 template<class _URNG>
5412 _LIBCPP_INLINE_VISIBILITY
5413 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:005414 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:385415 template<class _URNG>
5416 _LIBCPP_INLINE_VISIBILITY
5417 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:005418 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5419
5420 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005422 result_type n() const {return __p_.n();}
5423
Howard Hinnantb9af2ea2010-09-22 18:02:385424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005425 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005427 void param(const param_type& __p) {__p_ = __p;}
5428
Howard Hinnantb9af2ea2010-09-22 18:02:385429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:005430 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:385431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:435432 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:005433
Howard Hinnantb9af2ea2010-09-22 18:02:385434 friend _LIBCPP_INLINE_VISIBILITY
5435 bool operator==(const chi_squared_distribution& __x,
5436 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:005437 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385438 friend _LIBCPP_INLINE_VISIBILITY
5439 bool operator!=(const chi_squared_distribution& __x,
5440 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:005441 {return !(__x == __y);}
5442};
5443
5444template <class _CharT, class _Traits, class _RT>
5445basic_ostream<_CharT, _Traits>&
5446operator<<(basic_ostream<_CharT, _Traits>& __os,
5447 const chi_squared_distribution<_RT>& __x)
5448{
Howard Hinnant9c0df142012-10-30 19:06:595449 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405450 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5451 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:005452 __os << __x.n();
5453 return __os;
5454}
5455
5456template <class _CharT, class _Traits, class _RT>
5457basic_istream<_CharT, _Traits>&
5458operator>>(basic_istream<_CharT, _Traits>& __is,
5459 chi_squared_distribution<_RT>& __x)
5460{
5461 typedef chi_squared_distribution<_RT> _Eng;
5462 typedef typename _Eng::result_type result_type;
5463 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595464 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant97dc2f32010-05-15 23:36:005465 __is.flags(ios_base::dec | ios_base::skipws);
5466 result_type __n;
5467 __is >> __n;
5468 if (!__is.fail())
5469 __x.param(param_type(__n));
5470 return __is;
5471}
5472
Howard Hinnantd7d01132010-05-17 21:55:465473// cauchy_distribution
5474
5475template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:345476class _LIBCPP_TYPE_VIS_ONLY cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:465477{
5478public:
5479 // types
5480 typedef _RealType result_type;
5481
Howard Hinnant0f678bd2013-08-12 18:38:345482 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantd7d01132010-05-17 21:55:465483 {
5484 result_type __a_;
5485 result_type __b_;
5486 public:
5487 typedef cauchy_distribution distribution_type;
5488
Howard Hinnantb9af2ea2010-09-22 18:02:385489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465490 explicit param_type(result_type __a = 0, result_type __b = 1)
5491 : __a_(__a), __b_(__b) {}
5492
Howard Hinnantb9af2ea2010-09-22 18:02:385493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465494 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465496 result_type b() const {return __b_;}
5497
Howard Hinnantb9af2ea2010-09-22 18:02:385498 friend _LIBCPP_INLINE_VISIBILITY
5499 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:465500 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385501 friend _LIBCPP_INLINE_VISIBILITY
5502 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:465503 {return !(__x == __y);}
5504 };
5505
5506private:
5507 param_type __p_;
5508
5509public:
5510 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465512 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5513 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465515 explicit cauchy_distribution(const param_type& __p)
5516 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465518 void reset() {}
5519
5520 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385521 template<class _URNG>
5522 _LIBCPP_INLINE_VISIBILITY
5523 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:465524 {return (*this)(__g, __p_);}
5525 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5526
5527 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465529 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:385530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465531 result_type b() const {return __p_.b();}
5532
Howard Hinnantb9af2ea2010-09-22 18:02:385533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465534 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465536 void param(const param_type& __p) {__p_ = __p;}
5537
Howard Hinnantb9af2ea2010-09-22 18:02:385538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465539 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:385540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:435541 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:465542
Howard Hinnantb9af2ea2010-09-22 18:02:385543 friend _LIBCPP_INLINE_VISIBILITY
5544 bool operator==(const cauchy_distribution& __x,
5545 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:465546 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385547 friend _LIBCPP_INLINE_VISIBILITY
5548 bool operator!=(const cauchy_distribution& __x,
5549 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:465550 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:465551};
5552
5553template <class _RealType>
5554template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:385555inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:465556_RealType
5557cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5558{
5559 uniform_real_distribution<result_type> __gen;
5560 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
Howard Hinnant0949eed2011-06-30 21:18:195561 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:465562}
5563
5564template <class _CharT, class _Traits, class _RT>
5565basic_ostream<_CharT, _Traits>&
5566operator<<(basic_ostream<_CharT, _Traits>& __os,
5567 const cauchy_distribution<_RT>& __x)
5568{
Howard Hinnant9c0df142012-10-30 19:06:595569 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405570 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5571 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:465572 _CharT __sp = __os.widen(' ');
5573 __os.fill(__sp);
5574 __os << __x.a() << __sp << __x.b();
5575 return __os;
5576}
5577
5578template <class _CharT, class _Traits, class _RT>
5579basic_istream<_CharT, _Traits>&
5580operator>>(basic_istream<_CharT, _Traits>& __is,
5581 cauchy_distribution<_RT>& __x)
5582{
5583 typedef cauchy_distribution<_RT> _Eng;
5584 typedef typename _Eng::result_type result_type;
5585 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595586 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd7d01132010-05-17 21:55:465587 __is.flags(ios_base::dec | ios_base::skipws);
5588 result_type __a;
5589 result_type __b;
5590 __is >> __a >> __b;
5591 if (!__is.fail())
5592 __x.param(param_type(__a, __b));
5593 return __is;
5594}
5595
Howard Hinnantd8bc09b2010-05-18 17:32:305596// fisher_f_distribution
5597
5598template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:345599class _LIBCPP_TYPE_VIS_ONLY fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:305600{
5601public:
5602 // types
5603 typedef _RealType result_type;
5604
Howard Hinnant0f678bd2013-08-12 18:38:345605 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantd8bc09b2010-05-18 17:32:305606 {
5607 result_type __m_;
5608 result_type __n_;
5609 public:
5610 typedef fisher_f_distribution distribution_type;
5611
Howard Hinnantb9af2ea2010-09-22 18:02:385612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305613 explicit param_type(result_type __m = 1, result_type __n = 1)
5614 : __m_(__m), __n_(__n) {}
5615
Howard Hinnantb9af2ea2010-09-22 18:02:385616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305617 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305619 result_type n() const {return __n_;}
5620
Howard Hinnantb9af2ea2010-09-22 18:02:385621 friend _LIBCPP_INLINE_VISIBILITY
5622 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:305623 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385624 friend _LIBCPP_INLINE_VISIBILITY
5625 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:305626 {return !(__x == __y);}
5627 };
5628
5629private:
5630 param_type __p_;
5631
5632public:
5633 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305635 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5636 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305638 explicit fisher_f_distribution(const param_type& __p)
5639 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305641 void reset() {}
5642
5643 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385644 template<class _URNG>
5645 _LIBCPP_INLINE_VISIBILITY
5646 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:305647 {return (*this)(__g, __p_);}
5648 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5649
5650 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305652 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:385653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305654 result_type n() const {return __p_.n();}
5655
Howard Hinnantb9af2ea2010-09-22 18:02:385656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305657 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305659 void param(const param_type& __p) {__p_ = __p;}
5660
Howard Hinnantb9af2ea2010-09-22 18:02:385661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:305662 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:385663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:435664 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:305665
Howard Hinnantb9af2ea2010-09-22 18:02:385666 friend _LIBCPP_INLINE_VISIBILITY
5667 bool operator==(const fisher_f_distribution& __x,
5668 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:305669 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385670 friend _LIBCPP_INLINE_VISIBILITY
5671 bool operator!=(const fisher_f_distribution& __x,
5672 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:305673 {return !(__x == __y);}
5674};
5675
5676template <class _RealType>
5677template<class _URNG>
5678_RealType
5679fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5680{
5681 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5682 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5683 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5684}
5685
5686template <class _CharT, class _Traits, class _RT>
5687basic_ostream<_CharT, _Traits>&
5688operator<<(basic_ostream<_CharT, _Traits>& __os,
5689 const fisher_f_distribution<_RT>& __x)
5690{
Howard Hinnant9c0df142012-10-30 19:06:595691 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405692 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5693 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:305694 _CharT __sp = __os.widen(' ');
5695 __os.fill(__sp);
5696 __os << __x.m() << __sp << __x.n();
5697 return __os;
5698}
5699
5700template <class _CharT, class _Traits, class _RT>
5701basic_istream<_CharT, _Traits>&
5702operator>>(basic_istream<_CharT, _Traits>& __is,
5703 fisher_f_distribution<_RT>& __x)
5704{
5705 typedef fisher_f_distribution<_RT> _Eng;
5706 typedef typename _Eng::result_type result_type;
5707 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595708 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd8bc09b2010-05-18 17:32:305709 __is.flags(ios_base::dec | ios_base::skipws);
5710 result_type __m;
5711 result_type __n;
5712 __is >> __m >> __n;
5713 if (!__is.fail())
5714 __x.param(param_type(__m, __n));
5715 return __is;
5716}
5717
Howard Hinnant551d8e42010-05-19 01:53:575718// student_t_distribution
5719
Howard Hinnant321b4bb2010-05-18 20:08:045720template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:345721class _LIBCPP_TYPE_VIS_ONLY student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:045722{
5723public:
5724 // types
5725 typedef _RealType result_type;
5726
Howard Hinnant0f678bd2013-08-12 18:38:345727 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant321b4bb2010-05-18 20:08:045728 {
5729 result_type __n_;
5730 public:
5731 typedef student_t_distribution distribution_type;
5732
Howard Hinnantb9af2ea2010-09-22 18:02:385733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045734 explicit param_type(result_type __n = 1) : __n_(__n) {}
5735
Howard Hinnantb9af2ea2010-09-22 18:02:385736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045737 result_type n() const {return __n_;}
5738
Howard Hinnantb9af2ea2010-09-22 18:02:385739 friend _LIBCPP_INLINE_VISIBILITY
5740 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:045741 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385742 friend _LIBCPP_INLINE_VISIBILITY
5743 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:045744 {return !(__x == __y);}
5745 };
5746
5747private:
5748 param_type __p_;
5749 normal_distribution<result_type> __nd_;
5750
5751public:
5752 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045754 explicit student_t_distribution(result_type __n = 1)
5755 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045757 explicit student_t_distribution(const param_type& __p)
5758 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045760 void reset() {__nd_.reset();}
5761
5762 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385763 template<class _URNG>
5764 _LIBCPP_INLINE_VISIBILITY
5765 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:045766 {return (*this)(__g, __p_);}
5767 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5768
5769 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045771 result_type n() const {return __p_.n();}
5772
Howard Hinnantb9af2ea2010-09-22 18:02:385773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045774 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575776 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:045777
Howard Hinnantb9af2ea2010-09-22 18:02:385778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045779 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:385780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:045781 result_type max() const {return numeric_limits<result_type>::infinity();}
5782
Howard Hinnantb9af2ea2010-09-22 18:02:385783 friend _LIBCPP_INLINE_VISIBILITY
5784 bool operator==(const student_t_distribution& __x,
5785 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:045786 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385787 friend _LIBCPP_INLINE_VISIBILITY
5788 bool operator!=(const student_t_distribution& __x,
5789 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:045790 {return !(__x == __y);}
5791};
5792
5793template <class _RealType>
5794template<class _URNG>
5795_RealType
5796student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5797{
5798 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:195799 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:045800}
5801
5802template <class _CharT, class _Traits, class _RT>
5803basic_ostream<_CharT, _Traits>&
5804operator<<(basic_ostream<_CharT, _Traits>& __os,
5805 const student_t_distribution<_RT>& __x)
5806{
Howard Hinnant9c0df142012-10-30 19:06:595807 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:405808 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5809 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:045810 __os << __x.n();
5811 return __os;
5812}
5813
5814template <class _CharT, class _Traits, class _RT>
5815basic_istream<_CharT, _Traits>&
5816operator>>(basic_istream<_CharT, _Traits>& __is,
5817 student_t_distribution<_RT>& __x)
5818{
5819 typedef student_t_distribution<_RT> _Eng;
5820 typedef typename _Eng::result_type result_type;
5821 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:595822 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant321b4bb2010-05-18 20:08:045823 __is.flags(ios_base::dec | ios_base::skipws);
5824 result_type __n;
5825 __is >> __n;
5826 if (!__is.fail())
5827 __x.param(param_type(__n));
5828 return __is;
5829}
5830
Howard Hinnant551d8e42010-05-19 01:53:575831// discrete_distribution
5832
5833template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:345834class _LIBCPP_TYPE_VIS_ONLY discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:575835{
5836public:
5837 // types
5838 typedef _IntType result_type;
5839
Howard Hinnant0f678bd2013-08-12 18:38:345840 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant551d8e42010-05-19 01:53:575841 {
5842 vector<double> __p_;
5843 public:
5844 typedef discrete_distribution distribution_type;
5845
Howard Hinnantb9af2ea2010-09-22 18:02:385846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575847 param_type() {}
5848 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:385849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575850 param_type(_InputIterator __f, _InputIterator __l)
5851 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:025852#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:385853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575854 param_type(initializer_list<double> __wl)
5855 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:025856#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:575857 template<class _UnaryOperation>
5858 param_type(size_t __nw, double __xmin, double __xmax,
5859 _UnaryOperation __fw);
5860
5861 vector<double> probabilities() const;
5862
Howard Hinnantb9af2ea2010-09-22 18:02:385863 friend _LIBCPP_INLINE_VISIBILITY
5864 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:575865 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385866 friend _LIBCPP_INLINE_VISIBILITY
5867 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:575868 {return !(__x == __y);}
5869
5870 private:
5871 void __init();
5872
5873 friend class discrete_distribution;
5874
5875 template <class _CharT, class _Traits, class _IT>
5876 friend
5877 basic_ostream<_CharT, _Traits>&
5878 operator<<(basic_ostream<_CharT, _Traits>& __os,
5879 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:435880
Howard Hinnant551d8e42010-05-19 01:53:575881 template <class _CharT, class _Traits, class _IT>
5882 friend
5883 basic_istream<_CharT, _Traits>&
5884 operator>>(basic_istream<_CharT, _Traits>& __is,
5885 discrete_distribution<_IT>& __x);
5886 };
5887
5888private:
5889 param_type __p_;
5890
5891public:
5892 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:385893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575894 discrete_distribution() {}
5895 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:385896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575897 discrete_distribution(_InputIterator __f, _InputIterator __l)
5898 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:025899#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:385900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575901 discrete_distribution(initializer_list<double> __wl)
5902 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:025903#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:575904 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:385905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575906 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5907 _UnaryOperation __fw)
5908 : __p_(__nw, __xmin, __xmax, __fw) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:205910 explicit discrete_distribution(const param_type& __p)
Howard Hinnant551d8e42010-05-19 01:53:575911 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:385912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575913 void reset() {}
5914
5915 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:385916 template<class _URNG>
5917 _LIBCPP_INLINE_VISIBILITY
5918 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:575919 {return (*this)(__g, __p_);}
5920 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5921
5922 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:385923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575924 vector<double> probabilities() const {return __p_.probabilities();}
5925
Howard Hinnantb9af2ea2010-09-22 18:02:385926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575927 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575929 void param(const param_type& __p) {__p_ = __p;}
5930
Howard Hinnantb9af2ea2010-09-22 18:02:385931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575932 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:385933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:575934 result_type max() const {return __p_.__p_.size();}
5935
Howard Hinnantb9af2ea2010-09-22 18:02:385936 friend _LIBCPP_INLINE_VISIBILITY
5937 bool operator==(const discrete_distribution& __x,
5938 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:575939 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:385940 friend _LIBCPP_INLINE_VISIBILITY
5941 bool operator!=(const discrete_distribution& __x,
5942 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:575943 {return !(__x == __y);}
5944
5945 template <class _CharT, class _Traits, class _IT>
5946 friend
5947 basic_ostream<_CharT, _Traits>&
5948 operator<<(basic_ostream<_CharT, _Traits>& __os,
5949 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:435950
Howard Hinnant551d8e42010-05-19 01:53:575951 template <class _CharT, class _Traits, class _IT>
5952 friend
5953 basic_istream<_CharT, _Traits>&
5954 operator>>(basic_istream<_CharT, _Traits>& __is,
5955 discrete_distribution<_IT>& __x);
5956};
5957
5958template<class _IntType>
5959template<class _UnaryOperation>
5960discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5961 double __xmin,
5962 double __xmax,
5963 _UnaryOperation __fw)
5964{
5965 if (__nw > 1)
5966 {
5967 __p_.reserve(__nw - 1);
5968 double __d = (__xmax - __xmin) / __nw;
5969 double __d2 = __d / 2;
5970 for (size_t __k = 0; __k < __nw; ++__k)
5971 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5972 __init();
5973 }
5974}
5975
5976template<class _IntType>
5977void
5978discrete_distribution<_IntType>::param_type::__init()
5979{
5980 if (!__p_.empty())
5981 {
5982 if (__p_.size() > 1)
5983 {
Howard Hinnant0949eed2011-06-30 21:18:195984 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5985 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:575986 __i < __e; ++__i)
5987 *__i /= __s;
5988 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:195989 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:575990 swap(__p_, __t);
5991 }
5992 else
5993 {
5994 __p_.clear();
5995 __p_.shrink_to_fit();
5996 }
5997 }
5998}
5999
6000template<class _IntType>
6001vector<double>
6002discrete_distribution<_IntType>::param_type::probabilities() const
6003{
6004 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:196005 _VSTD::vector<double> __p(__n+1);
6006 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:576007 if (__n > 0)
6008 __p[__n] = 1 - __p_[__n-1];
6009 else
6010 __p[0] = 1;
6011 return __p;
6012}
6013
6014template<class _IntType>
6015template<class _URNG>
6016_IntType
6017discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6018{
6019 uniform_real_distribution<double> __gen;
6020 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:196021 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:576022 __p.__p_.begin());
6023}
6024
6025template <class _CharT, class _Traits, class _IT>
6026basic_ostream<_CharT, _Traits>&
6027operator<<(basic_ostream<_CharT, _Traits>& __os,
6028 const discrete_distribution<_IT>& __x)
6029{
Howard Hinnant9c0df142012-10-30 19:06:596030 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:406031 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6032 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:576033 _CharT __sp = __os.widen(' ');
6034 __os.fill(__sp);
6035 size_t __n = __x.__p_.__p_.size();
6036 __os << __n;
6037 for (size_t __i = 0; __i < __n; ++__i)
6038 __os << __sp << __x.__p_.__p_[__i];
6039 return __os;
6040}
6041
6042template <class _CharT, class _Traits, class _IT>
6043basic_istream<_CharT, _Traits>&
6044operator>>(basic_istream<_CharT, _Traits>& __is,
6045 discrete_distribution<_IT>& __x)
6046{
6047 typedef discrete_distribution<_IT> _Eng;
6048 typedef typename _Eng::result_type result_type;
6049 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:596050 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant551d8e42010-05-19 01:53:576051 __is.flags(ios_base::dec | ios_base::skipws);
6052 size_t __n;
6053 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:466054 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:576055 for (size_t __i = 0; __i < __n; ++__i)
6056 __is >> __p[__i];
6057 if (!__is.fail())
6058 swap(__x.__p_.__p_, __p);
6059 return __is;
6060}
6061
Howard Hinnantd6d11712010-05-20 15:11:466062// piecewise_constant_distribution
6063
6064template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:346065class _LIBCPP_TYPE_VIS_ONLY piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:466066{
6067public:
6068 // types
6069 typedef _RealType result_type;
6070
Howard Hinnant0f678bd2013-08-12 18:38:346071 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantd6d11712010-05-20 15:11:466072 {
Howard Hinnantd6d11712010-05-20 15:11:466073 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:366074 vector<result_type> __densities_;
6075 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:466076 public:
6077 typedef piecewise_constant_distribution distribution_type;
6078
6079 param_type();
6080 template<class _InputIteratorB, class _InputIteratorW>
6081 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6082 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:026083#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:466084 template<class _UnaryOperation>
6085 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:026086#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:466087 template<class _UnaryOperation>
6088 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6089 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:096090 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:466091
Howard Hinnantb9af2ea2010-09-22 18:02:386092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466093 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:366095 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:466096
Howard Hinnantb9af2ea2010-09-22 18:02:386097 friend _LIBCPP_INLINE_VISIBILITY
6098 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:406099 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386100 friend _LIBCPP_INLINE_VISIBILITY
6101 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:466102 {return !(__x == __y);}
6103
6104 private:
6105 void __init();
6106
6107 friend class piecewise_constant_distribution;
6108
6109 template <class _CharT, class _Traits, class _RT>
6110 friend
6111 basic_ostream<_CharT, _Traits>&
6112 operator<<(basic_ostream<_CharT, _Traits>& __os,
6113 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:436114
Howard Hinnantd6d11712010-05-20 15:11:466115 template <class _CharT, class _Traits, class _RT>
6116 friend
6117 basic_istream<_CharT, _Traits>&
6118 operator>>(basic_istream<_CharT, _Traits>& __is,
6119 piecewise_constant_distribution<_RT>& __x);
6120 };
6121
6122private:
6123 param_type __p_;
6124
6125public:
6126 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:386127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466128 piecewise_constant_distribution() {}
6129 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:386130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466131 piecewise_constant_distribution(_InputIteratorB __fB,
6132 _InputIteratorB __lB,
6133 _InputIteratorW __fW)
6134 : __p_(__fB, __lB, __fW) {}
6135
Howard Hinnante3e32912011-08-12 21:56:026136#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:466137 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:386138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466139 piecewise_constant_distribution(initializer_list<result_type> __bl,
6140 _UnaryOperation __fw)
6141 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:026142#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:466143
6144 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:386145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466146 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6147 result_type __xmax, _UnaryOperation __fw)
6148 : __p_(__nw, __xmin, __xmax, __fw) {}
6149
Howard Hinnantb9af2ea2010-09-22 18:02:386150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466151 explicit piecewise_constant_distribution(const param_type& __p)
6152 : __p_(__p) {}
6153
Howard Hinnantb9af2ea2010-09-22 18:02:386154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466155 void reset() {}
6156
6157 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:386158 template<class _URNG>
6159 _LIBCPP_INLINE_VISIBILITY
6160 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:466161 {return (*this)(__g, __p_);}
6162 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6163
6164 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:386165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466166 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:386167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:366168 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:466169
Howard Hinnantb9af2ea2010-09-22 18:02:386170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466171 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466173 void param(const param_type& __p) {__p_ = __p;}
6174
Howard Hinnantb9af2ea2010-09-22 18:02:386175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466176 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:386177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:466178 result_type max() const {return __p_.__b_.back();}
6179
Howard Hinnantb9af2ea2010-09-22 18:02:386180 friend _LIBCPP_INLINE_VISIBILITY
6181 bool operator==(const piecewise_constant_distribution& __x,
6182 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:466183 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386184 friend _LIBCPP_INLINE_VISIBILITY
6185 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:466186 const piecewise_constant_distribution& __y)
6187 {return !(__x == __y);}
6188
6189 template <class _CharT, class _Traits, class _RT>
6190 friend
6191 basic_ostream<_CharT, _Traits>&
6192 operator<<(basic_ostream<_CharT, _Traits>& __os,
6193 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:436194
Howard Hinnantd6d11712010-05-20 15:11:466195 template <class _CharT, class _Traits, class _RT>
6196 friend
6197 basic_istream<_CharT, _Traits>&
6198 operator>>(basic_istream<_CharT, _Traits>& __is,
6199 piecewise_constant_distribution<_RT>& __x);
6200};
6201
6202template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:096203typename piecewise_constant_distribution<_RealType>::param_type &
6204piecewise_constant_distribution<_RealType>::param_type::operator=
6205 (const param_type& __rhs)
6206{
6207// These can throw
6208 __b_.reserve (__rhs.__b_.size ());
6209 __densities_.reserve(__rhs.__densities_.size());
6210 __areas_.reserve (__rhs.__areas_.size());
6211
6212// These can not throw
6213 __b_ = __rhs.__b_;
6214 __densities_ = __rhs.__densities_;
6215 __areas_ = __rhs.__areas_;
6216 return *this;
6217}
6218
6219template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:466220void
6221piecewise_constant_distribution<_RealType>::param_type::__init()
6222{
Howard Hinnant2a592542010-05-24 00:35:406223 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:196224 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:406225 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:366226 result_type());
Howard Hinnant2a592542010-05-24 00:35:406227 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6228 __densities_[__i] /= __total_area;
6229 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:366230 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:196231 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:406232 __areas_.begin() + 1);
6233 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6234 __densities_.back() = 1 - __areas_.back(); // correct round off error
6235 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6236 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6237 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:466238}
6239
6240template<class _RealType>
6241piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:406242 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:346243 __densities_(1, 1.0),
6244 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:466245{
6246 __b_[1] = 1;
6247}
6248
6249template<class _RealType>
6250template<class _InputIteratorB, class _InputIteratorW>
6251piecewise_constant_distribution<_RealType>::param_type::param_type(
6252 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6253 : __b_(__fB, __lB)
6254{
6255 if (__b_.size() < 2)
6256 {
6257 __b_.resize(2);
6258 __b_[0] = 0;
6259 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:406260 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:346261 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:466262 }
6263 else
6264 {
Howard Hinnant2a592542010-05-24 00:35:406265 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:466266 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:406267 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:466268 __init();
6269 }
6270}
6271
Howard Hinnante3e32912011-08-12 21:56:026272#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6273
Howard Hinnantd6d11712010-05-20 15:11:466274template<class _RealType>
6275template<class _UnaryOperation>
6276piecewise_constant_distribution<_RealType>::param_type::param_type(
6277 initializer_list<result_type> __bl, _UnaryOperation __fw)
6278 : __b_(__bl.begin(), __bl.end())
6279{
6280 if (__b_.size() < 2)
6281 {
6282 __b_.resize(2);
6283 __b_[0] = 0;
6284 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:406285 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:346286 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:466287 }
6288 else
6289 {
Howard Hinnant2a592542010-05-24 00:35:406290 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:466291 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:406292 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:466293 __init();
6294 }
6295}
6296
Howard Hinnante3e32912011-08-12 21:56:026297#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6298
Howard Hinnantd6d11712010-05-20 15:11:466299template<class _RealType>
6300template<class _UnaryOperation>
6301piecewise_constant_distribution<_RealType>::param_type::param_type(
6302 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6303 : __b_(__nw == 0 ? 2 : __nw + 1)
6304{
6305 size_t __n = __b_.size() - 1;
6306 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:406307 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:466308 for (size_t __i = 0; __i < __n; ++__i)
6309 {
6310 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:406311 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:466312 }
6313 __b_[__n] = __xmax;
6314 __init();
6315}
6316
6317template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:466318template<class _URNG>
6319_RealType
6320piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6321{
6322 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:466323 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:196324 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:366325 __u) - __p.__areas_.begin() - 1;
6326 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:466327}
6328
6329template <class _CharT, class _Traits, class _RT>
6330basic_ostream<_CharT, _Traits>&
6331operator<<(basic_ostream<_CharT, _Traits>& __os,
6332 const piecewise_constant_distribution<_RT>& __x)
6333{
Howard Hinnant9c0df142012-10-30 19:06:596334 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:406335 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6336 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:466337 _CharT __sp = __os.widen(' ');
6338 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:406339 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:466340 __os << __n;
6341 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:406342 __os << __sp << __x.__p_.__b_[__i];
6343 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:466344 __os << __sp << __n;
6345 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:406346 __os << __sp << __x.__p_.__densities_[__i];
6347 __n = __x.__p_.__areas_.size();
6348 __os << __sp << __n;
6349 for (size_t __i = 0; __i < __n; ++__i)
6350 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:466351 return __os;
6352}
6353
6354template <class _CharT, class _Traits, class _RT>
6355basic_istream<_CharT, _Traits>&
6356operator>>(basic_istream<_CharT, _Traits>& __is,
6357 piecewise_constant_distribution<_RT>& __x)
6358{
6359 typedef piecewise_constant_distribution<_RT> _Eng;
6360 typedef typename _Eng::result_type result_type;
6361 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:596362 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd6d11712010-05-20 15:11:466363 __is.flags(ios_base::dec | ios_base::skipws);
6364 size_t __n;
6365 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:466366 vector<result_type> __b(__n);
6367 for (size_t __i = 0; __i < __n; ++__i)
6368 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:406369 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:366370 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:406371 for (size_t __i = 0; __i < __n; ++__i)
6372 __is >> __densities[__i];
6373 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:366374 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:406375 for (size_t __i = 0; __i < __n; ++__i)
6376 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:466377 if (!__is.fail())
6378 {
Howard Hinnantd6d11712010-05-20 15:11:466379 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:406380 swap(__x.__p_.__densities_, __densities);
6381 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:466382 }
6383 return __is;
6384}
6385
Howard Hinnant54305402010-05-25 00:27:346386// piecewise_linear_distribution
6387
6388template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:346389class _LIBCPP_TYPE_VIS_ONLY piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:346390{
6391public:
6392 // types
6393 typedef _RealType result_type;
6394
Howard Hinnant0f678bd2013-08-12 18:38:346395 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant54305402010-05-25 00:27:346396 {
Howard Hinnant54305402010-05-25 00:27:346397 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:366398 vector<result_type> __densities_;
6399 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:346400 public:
6401 typedef piecewise_linear_distribution distribution_type;
6402
6403 param_type();
6404 template<class _InputIteratorB, class _InputIteratorW>
6405 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6406 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:026407#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:346408 template<class _UnaryOperation>
6409 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:026410#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:346411 template<class _UnaryOperation>
6412 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6413 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:096414 param_type & operator=(const param_type& __rhs);
6415
Howard Hinnantb9af2ea2010-09-22 18:02:386416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346417 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:366419 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:346420
Howard Hinnantb9af2ea2010-09-22 18:02:386421 friend _LIBCPP_INLINE_VISIBILITY
6422 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:346423 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386424 friend _LIBCPP_INLINE_VISIBILITY
6425 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:346426 {return !(__x == __y);}
6427
6428 private:
6429 void __init();
6430
6431 friend class piecewise_linear_distribution;
6432
6433 template <class _CharT, class _Traits, class _RT>
6434 friend
6435 basic_ostream<_CharT, _Traits>&
6436 operator<<(basic_ostream<_CharT, _Traits>& __os,
6437 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:436438
Howard Hinnant54305402010-05-25 00:27:346439 template <class _CharT, class _Traits, class _RT>
6440 friend
6441 basic_istream<_CharT, _Traits>&
6442 operator>>(basic_istream<_CharT, _Traits>& __is,
6443 piecewise_linear_distribution<_RT>& __x);
6444 };
6445
6446private:
6447 param_type __p_;
6448
6449public:
6450 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:386451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346452 piecewise_linear_distribution() {}
6453 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:386454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346455 piecewise_linear_distribution(_InputIteratorB __fB,
6456 _InputIteratorB __lB,
6457 _InputIteratorW __fW)
6458 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:436459
Howard Hinnante3e32912011-08-12 21:56:026460#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:346461 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:386462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346463 piecewise_linear_distribution(initializer_list<result_type> __bl,
6464 _UnaryOperation __fw)
6465 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:026466#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:346467
6468 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:386469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346470 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6471 result_type __xmax, _UnaryOperation __fw)
6472 : __p_(__nw, __xmin, __xmax, __fw) {}
6473
Howard Hinnantb9af2ea2010-09-22 18:02:386474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346475 explicit piecewise_linear_distribution(const param_type& __p)
6476 : __p_(__p) {}
6477
Howard Hinnantb9af2ea2010-09-22 18:02:386478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346479 void reset() {}
6480
6481 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:386482 template<class _URNG>
6483 _LIBCPP_INLINE_VISIBILITY
6484 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:346485 {return (*this)(__g, __p_);}
6486 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6487
6488 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:386489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346490 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:386491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:366492 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:346493
Howard Hinnantb9af2ea2010-09-22 18:02:386494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346495 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346497 void param(const param_type& __p) {__p_ = __p;}
6498
Howard Hinnantb9af2ea2010-09-22 18:02:386499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346500 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:386501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:346502 result_type max() const {return __p_.__b_.back();}
6503
Howard Hinnantb9af2ea2010-09-22 18:02:386504 friend _LIBCPP_INLINE_VISIBILITY
6505 bool operator==(const piecewise_linear_distribution& __x,
6506 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:346507 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:386508 friend _LIBCPP_INLINE_VISIBILITY
6509 bool operator!=(const piecewise_linear_distribution& __x,
6510 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:346511 {return !(__x == __y);}
6512
6513 template <class _CharT, class _Traits, class _RT>
6514 friend
6515 basic_ostream<_CharT, _Traits>&
6516 operator<<(basic_ostream<_CharT, _Traits>& __os,
6517 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:436518
Howard Hinnant54305402010-05-25 00:27:346519 template <class _CharT, class _Traits, class _RT>
6520 friend
6521 basic_istream<_CharT, _Traits>&
6522 operator>>(basic_istream<_CharT, _Traits>& __is,
6523 piecewise_linear_distribution<_RT>& __x);
6524};
6525
6526template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:096527typename piecewise_linear_distribution<_RealType>::param_type &
6528piecewise_linear_distribution<_RealType>::param_type::operator=
6529 (const param_type& __rhs)
6530{
6531// These can throw
6532 __b_.reserve (__rhs.__b_.size ());
6533 __densities_.reserve(__rhs.__densities_.size());
6534 __areas_.reserve (__rhs.__areas_.size());
6535
6536// These can not throw
6537 __b_ = __rhs.__b_;
6538 __densities_ = __rhs.__densities_;
6539 __areas_ = __rhs.__areas_;
6540 return *this;
6541}
6542
6543
6544template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:346545void
6546piecewise_linear_distribution<_RealType>::param_type::__init()
6547{
Howard Hinnant9650b6c2010-11-18 17:01:366548 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnant99968442011-11-29 18:15:506549 result_type _Sp = 0;
Howard Hinnant54305402010-05-25 00:27:346550 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6551 {
6552 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6553 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnant99968442011-11-29 18:15:506554 _Sp += __areas_[__i];
Howard Hinnant54305402010-05-25 00:27:346555 }
6556 for (size_t __i = __areas_.size(); __i > 1;)
6557 {
6558 --__i;
Howard Hinnant99968442011-11-29 18:15:506559 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnant54305402010-05-25 00:27:346560 }
6561 __areas_[0] = 0;
6562 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6563 __areas_[__i] += __areas_[__i-1];
6564 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnant99968442011-11-29 18:15:506565 __densities_[__i] /= _Sp;
Howard Hinnant54305402010-05-25 00:27:346566}
6567
6568template<class _RealType>
6569piecewise_linear_distribution<_RealType>::param_type::param_type()
6570 : __b_(2),
6571 __densities_(2, 1.0),
6572 __areas_(1, 0.0)
6573{
6574 __b_[1] = 1;
6575}
6576
6577template<class _RealType>
6578template<class _InputIteratorB, class _InputIteratorW>
6579piecewise_linear_distribution<_RealType>::param_type::param_type(
6580 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6581 : __b_(__fB, __lB)
6582{
6583 if (__b_.size() < 2)
6584 {
6585 __b_.resize(2);
6586 __b_[0] = 0;
6587 __b_[1] = 1;
6588 __densities_.assign(2, 1.0);
6589 __areas_.assign(1, 0.0);
6590 }
6591 else
6592 {
6593 __densities_.reserve(__b_.size());
6594 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6595 __densities_.push_back(*__fW);
6596 __init();
6597 }
6598}
6599
Howard Hinnante3e32912011-08-12 21:56:026600#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6601
Howard Hinnant54305402010-05-25 00:27:346602template<class _RealType>
6603template<class _UnaryOperation>
6604piecewise_linear_distribution<_RealType>::param_type::param_type(
6605 initializer_list<result_type> __bl, _UnaryOperation __fw)
6606 : __b_(__bl.begin(), __bl.end())
6607{
6608 if (__b_.size() < 2)
6609 {
6610 __b_.resize(2);
6611 __b_[0] = 0;
6612 __b_[1] = 1;
6613 __densities_.assign(2, 1.0);
6614 __areas_.assign(1, 0.0);
6615 }
6616 else
6617 {
6618 __densities_.reserve(__b_.size());
6619 for (size_t __i = 0; __i < __b_.size(); ++__i)
6620 __densities_.push_back(__fw(__b_[__i]));
6621 __init();
6622 }
6623}
6624
Howard Hinnante3e32912011-08-12 21:56:026625#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6626
Howard Hinnant54305402010-05-25 00:27:346627template<class _RealType>
6628template<class _UnaryOperation>
6629piecewise_linear_distribution<_RealType>::param_type::param_type(
6630 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6631 : __b_(__nw == 0 ? 2 : __nw + 1)
6632{
6633 size_t __n = __b_.size() - 1;
6634 result_type __d = (__xmax - __xmin) / __n;
6635 __densities_.reserve(__b_.size());
6636 for (size_t __i = 0; __i < __n; ++__i)
6637 {
6638 __b_[__i] = __xmin + __i * __d;
6639 __densities_.push_back(__fw(__b_[__i]));
6640 }
6641 __b_[__n] = __xmax;
6642 __densities_.push_back(__fw(__b_[__n]));
6643 __init();
6644}
6645
6646template<class _RealType>
6647template<class _URNG>
6648_RealType
6649piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6650{
6651 typedef uniform_real_distribution<result_type> _Gen;
6652 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:196653 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:366654 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:346655 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:366656 const result_type __dk = __p.__densities_[__k];
6657 const result_type __dk1 = __p.__densities_[__k+1];
6658 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:346659 const result_type __bk = __p.__b_[__k];
6660 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:366661 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:346662 const result_type __bk1 = __p.__b_[__k+1];
6663 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:366664 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:196665 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:366666 __deltad;
Howard Hinnant54305402010-05-25 00:27:346667}
6668
6669template <class _CharT, class _Traits, class _RT>
6670basic_ostream<_CharT, _Traits>&
6671operator<<(basic_ostream<_CharT, _Traits>& __os,
6672 const piecewise_linear_distribution<_RT>& __x)
6673{
Howard Hinnant9c0df142012-10-30 19:06:596674 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant54305402010-05-25 00:27:346675 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6676 ios_base::scientific);
6677 _CharT __sp = __os.widen(' ');
6678 __os.fill(__sp);
6679 size_t __n = __x.__p_.__b_.size();
6680 __os << __n;
6681 for (size_t __i = 0; __i < __n; ++__i)
6682 __os << __sp << __x.__p_.__b_[__i];
6683 __n = __x.__p_.__densities_.size();
6684 __os << __sp << __n;
6685 for (size_t __i = 0; __i < __n; ++__i)
6686 __os << __sp << __x.__p_.__densities_[__i];
6687 __n = __x.__p_.__areas_.size();
6688 __os << __sp << __n;
6689 for (size_t __i = 0; __i < __n; ++__i)
6690 __os << __sp << __x.__p_.__areas_[__i];
6691 return __os;
6692}
6693
6694template <class _CharT, class _Traits, class _RT>
6695basic_istream<_CharT, _Traits>&
6696operator>>(basic_istream<_CharT, _Traits>& __is,
6697 piecewise_linear_distribution<_RT>& __x)
6698{
6699 typedef piecewise_linear_distribution<_RT> _Eng;
6700 typedef typename _Eng::result_type result_type;
6701 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:596702 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant54305402010-05-25 00:27:346703 __is.flags(ios_base::dec | ios_base::skipws);
6704 size_t __n;
6705 __is >> __n;
6706 vector<result_type> __b(__n);
6707 for (size_t __i = 0; __i < __n; ++__i)
6708 __is >> __b[__i];
6709 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:366710 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:346711 for (size_t __i = 0; __i < __n; ++__i)
6712 __is >> __densities[__i];
6713 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:366714 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:346715 for (size_t __i = 0; __i < __n; ++__i)
6716 __is >> __areas[__i];
6717 if (!__is.fail())
6718 {
6719 swap(__x.__p_.__b_, __b);
6720 swap(__x.__p_.__densities_, __densities);
6721 swap(__x.__p_.__areas_, __areas);
6722 }
6723 return __is;
6724}
6725
Howard Hinnantbc8d3f92010-05-11 19:42:166726_LIBCPP_END_NAMESPACE_STD
6727
6728#endif // _LIBCPP_RANDOM