]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/random.tcc
re PR libstdc++/51795 (linear_congruential_engine doesn't work correctly)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / random.tcc
1 // random number generation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file bits/random.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{random}
28 */
29
30 #ifndef _RANDOM_TCC
31 #define _RANDOM_TCC 1
32
33 #include <numeric> // std::accumulate and std::partial_sum
34
35 namespace std _GLIBCXX_VISIBILITY(default)
36 {
37 /*
38 * (Further) implementation-space details.
39 */
40 namespace __detail
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43
44 // General case for x = (ax + c) mod m -- use Schrage's algorithm to
45 // avoid integer overflow.
46 //
47 // Because a and c are compile-time integral constants the compiler
48 // kindly elides any unreachable paths.
49 //
50 // Preconditions: a > 0, m > 0.
51 //
52 // XXX FIXME: as-is, only works correctly for __m % __a < __m / __a.
53 //
54 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
55 struct _Mod
56 {
57 static _Tp
58 __calc(_Tp __x)
59 {
60 if (__a == 1)
61 __x %= __m;
62 else
63 {
64 static const _Tp __q = __m / __a;
65 static const _Tp __r = __m % __a;
66
67 _Tp __t1 = __a * (__x % __q);
68 _Tp __t2 = __r * (__x / __q);
69 if (__t1 >= __t2)
70 __x = __t1 - __t2;
71 else
72 __x = __m - __t2 + __t1;
73 }
74
75 if (__c != 0)
76 {
77 const _Tp __d = __m - __x;
78 if (__d > __c)
79 __x += __c;
80 else
81 __x = __c - __d;
82 }
83 return __x;
84 }
85 };
86
87 // Special case for m == 0 -- use unsigned integer overflow as modulo
88 // operator.
89 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
90 struct _Mod<_Tp, __m, __a, __c, true>
91 {
92 static _Tp
93 __calc(_Tp __x)
94 { return __a * __x + __c; }
95 };
96
97 template<typename _InputIterator, typename _OutputIterator,
98 typename _UnaryOperation>
99 _OutputIterator
100 __transform(_InputIterator __first, _InputIterator __last,
101 _OutputIterator __result, _UnaryOperation __unary_op)
102 {
103 for (; __first != __last; ++__first, ++__result)
104 *__result = __unary_op(*__first);
105 return __result;
106 }
107
108 _GLIBCXX_END_NAMESPACE_VERSION
109 } // namespace __detail
110
111 _GLIBCXX_BEGIN_NAMESPACE_VERSION
112
113 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
114 constexpr _UIntType
115 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
116
117 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
118 constexpr _UIntType
119 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
120
121 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
122 constexpr _UIntType
123 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
124
125 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
126 constexpr _UIntType
127 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
128
129 /**
130 * Seeds the LCR with integral value @p __s, adjusted so that the
131 * ring identity is never a member of the convergence set.
132 */
133 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
134 void
135 linear_congruential_engine<_UIntType, __a, __c, __m>::
136 seed(result_type __s)
137 {
138 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
139 && (__detail::__mod<_UIntType, __m>(__s) == 0))
140 _M_x = 1;
141 else
142 _M_x = __detail::__mod<_UIntType, __m>(__s);
143 }
144
145 /**
146 * Seeds the LCR engine with a value generated by @p __q.
147 */
148 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
149 template<typename _Sseq>
150 typename std::enable_if<std::is_class<_Sseq>::value>::type
151 linear_congruential_engine<_UIntType, __a, __c, __m>::
152 seed(_Sseq& __q)
153 {
154 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
155 : std::__lg(__m);
156 const _UIntType __k = (__k0 + 31) / 32;
157 uint_least32_t __arr[__k + 3];
158 __q.generate(__arr + 0, __arr + __k + 3);
159 _UIntType __factor = 1u;
160 _UIntType __sum = 0u;
161 for (size_t __j = 0; __j < __k; ++__j)
162 {
163 __sum += __arr[__j + 3] * __factor;
164 __factor *= __detail::_Shift<_UIntType, 32>::__value;
165 }
166 seed(__sum);
167 }
168
169 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
170 typename _CharT, typename _Traits>
171 std::basic_ostream<_CharT, _Traits>&
172 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
173 const linear_congruential_engine<_UIntType,
174 __a, __c, __m>& __lcr)
175 {
176 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
177 typedef typename __ostream_type::ios_base __ios_base;
178
179 const typename __ios_base::fmtflags __flags = __os.flags();
180 const _CharT __fill = __os.fill();
181 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
182 __os.fill(__os.widen(' '));
183
184 __os << __lcr._M_x;
185
186 __os.flags(__flags);
187 __os.fill(__fill);
188 return __os;
189 }
190
191 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
192 typename _CharT, typename _Traits>
193 std::basic_istream<_CharT, _Traits>&
194 operator>>(std::basic_istream<_CharT, _Traits>& __is,
195 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
196 {
197 typedef std::basic_istream<_CharT, _Traits> __istream_type;
198 typedef typename __istream_type::ios_base __ios_base;
199
200 const typename __ios_base::fmtflags __flags = __is.flags();
201 __is.flags(__ios_base::dec);
202
203 __is >> __lcr._M_x;
204
205 __is.flags(__flags);
206 return __is;
207 }
208
209
210 template<typename _UIntType,
211 size_t __w, size_t __n, size_t __m, size_t __r,
212 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
213 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
214 _UIntType __f>
215 constexpr size_t
216 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
217 __s, __b, __t, __c, __l, __f>::word_size;
218
219 template<typename _UIntType,
220 size_t __w, size_t __n, size_t __m, size_t __r,
221 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
222 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
223 _UIntType __f>
224 constexpr size_t
225 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
226 __s, __b, __t, __c, __l, __f>::state_size;
227
228 template<typename _UIntType,
229 size_t __w, size_t __n, size_t __m, size_t __r,
230 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
231 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
232 _UIntType __f>
233 constexpr size_t
234 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
235 __s, __b, __t, __c, __l, __f>::shift_size;
236
237 template<typename _UIntType,
238 size_t __w, size_t __n, size_t __m, size_t __r,
239 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
240 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
241 _UIntType __f>
242 constexpr size_t
243 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
244 __s, __b, __t, __c, __l, __f>::mask_bits;
245
246 template<typename _UIntType,
247 size_t __w, size_t __n, size_t __m, size_t __r,
248 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
249 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
250 _UIntType __f>
251 constexpr _UIntType
252 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
253 __s, __b, __t, __c, __l, __f>::xor_mask;
254
255 template<typename _UIntType,
256 size_t __w, size_t __n, size_t __m, size_t __r,
257 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
258 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
259 _UIntType __f>
260 constexpr size_t
261 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
262 __s, __b, __t, __c, __l, __f>::tempering_u;
263
264 template<typename _UIntType,
265 size_t __w, size_t __n, size_t __m, size_t __r,
266 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
267 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
268 _UIntType __f>
269 constexpr _UIntType
270 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
271 __s, __b, __t, __c, __l, __f>::tempering_d;
272
273 template<typename _UIntType,
274 size_t __w, size_t __n, size_t __m, size_t __r,
275 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
276 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
277 _UIntType __f>
278 constexpr size_t
279 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
280 __s, __b, __t, __c, __l, __f>::tempering_s;
281
282 template<typename _UIntType,
283 size_t __w, size_t __n, size_t __m, size_t __r,
284 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
285 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
286 _UIntType __f>
287 constexpr _UIntType
288 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
289 __s, __b, __t, __c, __l, __f>::tempering_b;
290
291 template<typename _UIntType,
292 size_t __w, size_t __n, size_t __m, size_t __r,
293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
294 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
295 _UIntType __f>
296 constexpr size_t
297 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
298 __s, __b, __t, __c, __l, __f>::tempering_t;
299
300 template<typename _UIntType,
301 size_t __w, size_t __n, size_t __m, size_t __r,
302 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
303 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
304 _UIntType __f>
305 constexpr _UIntType
306 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
307 __s, __b, __t, __c, __l, __f>::tempering_c;
308
309 template<typename _UIntType,
310 size_t __w, size_t __n, size_t __m, size_t __r,
311 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
312 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
313 _UIntType __f>
314 constexpr size_t
315 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
316 __s, __b, __t, __c, __l, __f>::tempering_l;
317
318 template<typename _UIntType,
319 size_t __w, size_t __n, size_t __m, size_t __r,
320 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
321 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
322 _UIntType __f>
323 constexpr _UIntType
324 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
325 __s, __b, __t, __c, __l, __f>::
326 initialization_multiplier;
327
328 template<typename _UIntType,
329 size_t __w, size_t __n, size_t __m, size_t __r,
330 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
331 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
332 _UIntType __f>
333 constexpr _UIntType
334 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
335 __s, __b, __t, __c, __l, __f>::default_seed;
336
337 template<typename _UIntType,
338 size_t __w, size_t __n, size_t __m, size_t __r,
339 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
340 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
341 _UIntType __f>
342 void
343 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
344 __s, __b, __t, __c, __l, __f>::
345 seed(result_type __sd)
346 {
347 _M_x[0] = __detail::__mod<_UIntType,
348 __detail::_Shift<_UIntType, __w>::__value>(__sd);
349
350 for (size_t __i = 1; __i < state_size; ++__i)
351 {
352 _UIntType __x = _M_x[__i - 1];
353 __x ^= __x >> (__w - 2);
354 __x *= __f;
355 __x += __detail::__mod<_UIntType, __n>(__i);
356 _M_x[__i] = __detail::__mod<_UIntType,
357 __detail::_Shift<_UIntType, __w>::__value>(__x);
358 }
359 _M_p = state_size;
360 }
361
362 template<typename _UIntType,
363 size_t __w, size_t __n, size_t __m, size_t __r,
364 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
365 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
366 _UIntType __f>
367 template<typename _Sseq>
368 typename std::enable_if<std::is_class<_Sseq>::value>::type
369 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
370 __s, __b, __t, __c, __l, __f>::
371 seed(_Sseq& __q)
372 {
373 const _UIntType __upper_mask = (~_UIntType()) << __r;
374 const size_t __k = (__w + 31) / 32;
375 uint_least32_t __arr[__n * __k];
376 __q.generate(__arr + 0, __arr + __n * __k);
377
378 bool __zero = true;
379 for (size_t __i = 0; __i < state_size; ++__i)
380 {
381 _UIntType __factor = 1u;
382 _UIntType __sum = 0u;
383 for (size_t __j = 0; __j < __k; ++__j)
384 {
385 __sum += __arr[__k * __i + __j] * __factor;
386 __factor *= __detail::_Shift<_UIntType, 32>::__value;
387 }
388 _M_x[__i] = __detail::__mod<_UIntType,
389 __detail::_Shift<_UIntType, __w>::__value>(__sum);
390
391 if (__zero)
392 {
393 if (__i == 0)
394 {
395 if ((_M_x[0] & __upper_mask) != 0u)
396 __zero = false;
397 }
398 else if (_M_x[__i] != 0u)
399 __zero = false;
400 }
401 }
402 if (__zero)
403 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
404 }
405
406 template<typename _UIntType, size_t __w,
407 size_t __n, size_t __m, size_t __r,
408 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
409 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
410 _UIntType __f>
411 typename
412 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
413 __s, __b, __t, __c, __l, __f>::result_type
414 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
415 __s, __b, __t, __c, __l, __f>::
416 operator()()
417 {
418 // Reload the vector - cost is O(n) amortized over n calls.
419 if (_M_p >= state_size)
420 {
421 const _UIntType __upper_mask = (~_UIntType()) << __r;
422 const _UIntType __lower_mask = ~__upper_mask;
423
424 for (size_t __k = 0; __k < (__n - __m); ++__k)
425 {
426 _UIntType __y = ((_M_x[__k] & __upper_mask)
427 | (_M_x[__k + 1] & __lower_mask));
428 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
429 ^ ((__y & 0x01) ? __a : 0));
430 }
431
432 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
433 {
434 _UIntType __y = ((_M_x[__k] & __upper_mask)
435 | (_M_x[__k + 1] & __lower_mask));
436 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
437 ^ ((__y & 0x01) ? __a : 0));
438 }
439
440 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
441 | (_M_x[0] & __lower_mask));
442 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
443 ^ ((__y & 0x01) ? __a : 0));
444 _M_p = 0;
445 }
446
447 // Calculate o(x(i)).
448 result_type __z = _M_x[_M_p++];
449 __z ^= (__z >> __u) & __d;
450 __z ^= (__z << __s) & __b;
451 __z ^= (__z << __t) & __c;
452 __z ^= (__z >> __l);
453
454 return __z;
455 }
456
457 template<typename _UIntType, size_t __w,
458 size_t __n, size_t __m, size_t __r,
459 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
460 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
461 _UIntType __f, typename _CharT, typename _Traits>
462 std::basic_ostream<_CharT, _Traits>&
463 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
464 const mersenne_twister_engine<_UIntType, __w, __n, __m,
465 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
466 {
467 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
468 typedef typename __ostream_type::ios_base __ios_base;
469
470 const typename __ios_base::fmtflags __flags = __os.flags();
471 const _CharT __fill = __os.fill();
472 const _CharT __space = __os.widen(' ');
473 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
474 __os.fill(__space);
475
476 for (size_t __i = 0; __i < __n; ++__i)
477 __os << __x._M_x[__i] << __space;
478 __os << __x._M_p;
479
480 __os.flags(__flags);
481 __os.fill(__fill);
482 return __os;
483 }
484
485 template<typename _UIntType, size_t __w,
486 size_t __n, size_t __m, size_t __r,
487 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
488 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
489 _UIntType __f, typename _CharT, typename _Traits>
490 std::basic_istream<_CharT, _Traits>&
491 operator>>(std::basic_istream<_CharT, _Traits>& __is,
492 mersenne_twister_engine<_UIntType, __w, __n, __m,
493 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
494 {
495 typedef std::basic_istream<_CharT, _Traits> __istream_type;
496 typedef typename __istream_type::ios_base __ios_base;
497
498 const typename __ios_base::fmtflags __flags = __is.flags();
499 __is.flags(__ios_base::dec | __ios_base::skipws);
500
501 for (size_t __i = 0; __i < __n; ++__i)
502 __is >> __x._M_x[__i];
503 __is >> __x._M_p;
504
505 __is.flags(__flags);
506 return __is;
507 }
508
509
510 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
511 constexpr size_t
512 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
513
514 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
515 constexpr size_t
516 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
517
518 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
519 constexpr size_t
520 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
521
522 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
523 constexpr _UIntType
524 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
525
526 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
527 void
528 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
529 seed(result_type __value)
530 {
531 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
532 __lcg(__value == 0u ? default_seed : __value);
533
534 const size_t __n = (__w + 31) / 32;
535
536 for (size_t __i = 0; __i < long_lag; ++__i)
537 {
538 _UIntType __sum = 0u;
539 _UIntType __factor = 1u;
540 for (size_t __j = 0; __j < __n; ++__j)
541 {
542 __sum += __detail::__mod<uint_least32_t,
543 __detail::_Shift<uint_least32_t, 32>::__value>
544 (__lcg()) * __factor;
545 __factor *= __detail::_Shift<_UIntType, 32>::__value;
546 }
547 _M_x[__i] = __detail::__mod<_UIntType,
548 __detail::_Shift<_UIntType, __w>::__value>(__sum);
549 }
550 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
551 _M_p = 0;
552 }
553
554 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
555 template<typename _Sseq>
556 typename std::enable_if<std::is_class<_Sseq>::value>::type
557 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
558 seed(_Sseq& __q)
559 {
560 const size_t __k = (__w + 31) / 32;
561 uint_least32_t __arr[__r * __k];
562 __q.generate(__arr + 0, __arr + __r * __k);
563
564 for (size_t __i = 0; __i < long_lag; ++__i)
565 {
566 _UIntType __sum = 0u;
567 _UIntType __factor = 1u;
568 for (size_t __j = 0; __j < __k; ++__j)
569 {
570 __sum += __arr[__k * __i + __j] * __factor;
571 __factor *= __detail::_Shift<_UIntType, 32>::__value;
572 }
573 _M_x[__i] = __detail::__mod<_UIntType,
574 __detail::_Shift<_UIntType, __w>::__value>(__sum);
575 }
576 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
577 _M_p = 0;
578 }
579
580 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
581 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
582 result_type
583 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
584 operator()()
585 {
586 // Derive short lag index from current index.
587 long __ps = _M_p - short_lag;
588 if (__ps < 0)
589 __ps += long_lag;
590
591 // Calculate new x(i) without overflow or division.
592 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
593 // cannot overflow.
594 _UIntType __xi;
595 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
596 {
597 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
598 _M_carry = 0;
599 }
600 else
601 {
602 __xi = (__detail::_Shift<_UIntType, __w>::__value
603 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
604 _M_carry = 1;
605 }
606 _M_x[_M_p] = __xi;
607
608 // Adjust current index to loop around in ring buffer.
609 if (++_M_p >= long_lag)
610 _M_p = 0;
611
612 return __xi;
613 }
614
615 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
616 typename _CharT, typename _Traits>
617 std::basic_ostream<_CharT, _Traits>&
618 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
619 const subtract_with_carry_engine<_UIntType,
620 __w, __s, __r>& __x)
621 {
622 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
623 typedef typename __ostream_type::ios_base __ios_base;
624
625 const typename __ios_base::fmtflags __flags = __os.flags();
626 const _CharT __fill = __os.fill();
627 const _CharT __space = __os.widen(' ');
628 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
629 __os.fill(__space);
630
631 for (size_t __i = 0; __i < __r; ++__i)
632 __os << __x._M_x[__i] << __space;
633 __os << __x._M_carry << __space << __x._M_p;
634
635 __os.flags(__flags);
636 __os.fill(__fill);
637 return __os;
638 }
639
640 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
641 typename _CharT, typename _Traits>
642 std::basic_istream<_CharT, _Traits>&
643 operator>>(std::basic_istream<_CharT, _Traits>& __is,
644 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
645 {
646 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
647 typedef typename __istream_type::ios_base __ios_base;
648
649 const typename __ios_base::fmtflags __flags = __is.flags();
650 __is.flags(__ios_base::dec | __ios_base::skipws);
651
652 for (size_t __i = 0; __i < __r; ++__i)
653 __is >> __x._M_x[__i];
654 __is >> __x._M_carry;
655 __is >> __x._M_p;
656
657 __is.flags(__flags);
658 return __is;
659 }
660
661
662 template<typename _RandomNumberEngine, size_t __p, size_t __r>
663 constexpr size_t
664 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
665
666 template<typename _RandomNumberEngine, size_t __p, size_t __r>
667 constexpr size_t
668 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
669
670 template<typename _RandomNumberEngine, size_t __p, size_t __r>
671 typename discard_block_engine<_RandomNumberEngine,
672 __p, __r>::result_type
673 discard_block_engine<_RandomNumberEngine, __p, __r>::
674 operator()()
675 {
676 if (_M_n >= used_block)
677 {
678 _M_b.discard(block_size - _M_n);
679 _M_n = 0;
680 }
681 ++_M_n;
682 return _M_b();
683 }
684
685 template<typename _RandomNumberEngine, size_t __p, size_t __r,
686 typename _CharT, typename _Traits>
687 std::basic_ostream<_CharT, _Traits>&
688 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
689 const discard_block_engine<_RandomNumberEngine,
690 __p, __r>& __x)
691 {
692 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
693 typedef typename __ostream_type::ios_base __ios_base;
694
695 const typename __ios_base::fmtflags __flags = __os.flags();
696 const _CharT __fill = __os.fill();
697 const _CharT __space = __os.widen(' ');
698 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
699 __os.fill(__space);
700
701 __os << __x.base() << __space << __x._M_n;
702
703 __os.flags(__flags);
704 __os.fill(__fill);
705 return __os;
706 }
707
708 template<typename _RandomNumberEngine, size_t __p, size_t __r,
709 typename _CharT, typename _Traits>
710 std::basic_istream<_CharT, _Traits>&
711 operator>>(std::basic_istream<_CharT, _Traits>& __is,
712 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
713 {
714 typedef std::basic_istream<_CharT, _Traits> __istream_type;
715 typedef typename __istream_type::ios_base __ios_base;
716
717 const typename __ios_base::fmtflags __flags = __is.flags();
718 __is.flags(__ios_base::dec | __ios_base::skipws);
719
720 __is >> __x._M_b >> __x._M_n;
721
722 __is.flags(__flags);
723 return __is;
724 }
725
726
727 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
728 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
729 result_type
730 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
731 operator()()
732 {
733 const long double __r = static_cast<long double>(_M_b.max())
734 - static_cast<long double>(_M_b.min()) + 1.0L;
735 const result_type __m = std::log(__r) / std::log(2.0L);
736 result_type __n, __n0, __y0, __y1, __s0, __s1;
737 for (size_t __i = 0; __i < 2; ++__i)
738 {
739 __n = (__w + __m - 1) / __m + __i;
740 __n0 = __n - __w % __n;
741 const result_type __w0 = __w / __n;
742 const result_type __w1 = __w0 + 1;
743 __s0 = result_type(1) << __w0;
744 __s1 = result_type(1) << __w1;
745 __y0 = __s0 * (__r / __s0);
746 __y1 = __s1 * (__r / __s1);
747 if (__r - __y0 <= __y0 / __n)
748 break;
749 }
750
751 result_type __sum = 0;
752 for (size_t __k = 0; __k < __n0; ++__k)
753 {
754 result_type __u;
755 do
756 __u = _M_b() - _M_b.min();
757 while (__u >= __y0);
758 __sum = __s0 * __sum + __u % __s0;
759 }
760 for (size_t __k = __n0; __k < __n; ++__k)
761 {
762 result_type __u;
763 do
764 __u = _M_b() - _M_b.min();
765 while (__u >= __y1);
766 __sum = __s1 * __sum + __u % __s1;
767 }
768 return __sum;
769 }
770
771
772 template<typename _RandomNumberEngine, size_t __k>
773 constexpr size_t
774 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
775
776 template<typename _RandomNumberEngine, size_t __k>
777 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
778 shuffle_order_engine<_RandomNumberEngine, __k>::
779 operator()()
780 {
781 size_t __j = __k * ((_M_y - _M_b.min())
782 / (_M_b.max() - _M_b.min() + 1.0L));
783 _M_y = _M_v[__j];
784 _M_v[__j] = _M_b();
785
786 return _M_y;
787 }
788
789 template<typename _RandomNumberEngine, size_t __k,
790 typename _CharT, typename _Traits>
791 std::basic_ostream<_CharT, _Traits>&
792 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
793 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
794 {
795 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
796 typedef typename __ostream_type::ios_base __ios_base;
797
798 const typename __ios_base::fmtflags __flags = __os.flags();
799 const _CharT __fill = __os.fill();
800 const _CharT __space = __os.widen(' ');
801 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
802 __os.fill(__space);
803
804 __os << __x.base();
805 for (size_t __i = 0; __i < __k; ++__i)
806 __os << __space << __x._M_v[__i];
807 __os << __space << __x._M_y;
808
809 __os.flags(__flags);
810 __os.fill(__fill);
811 return __os;
812 }
813
814 template<typename _RandomNumberEngine, size_t __k,
815 typename _CharT, typename _Traits>
816 std::basic_istream<_CharT, _Traits>&
817 operator>>(std::basic_istream<_CharT, _Traits>& __is,
818 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
819 {
820 typedef std::basic_istream<_CharT, _Traits> __istream_type;
821 typedef typename __istream_type::ios_base __ios_base;
822
823 const typename __ios_base::fmtflags __flags = __is.flags();
824 __is.flags(__ios_base::dec | __ios_base::skipws);
825
826 __is >> __x._M_b;
827 for (size_t __i = 0; __i < __k; ++__i)
828 __is >> __x._M_v[__i];
829 __is >> __x._M_y;
830
831 __is.flags(__flags);
832 return __is;
833 }
834
835
836 template<typename _IntType>
837 template<typename _UniformRandomNumberGenerator>
838 typename uniform_int_distribution<_IntType>::result_type
839 uniform_int_distribution<_IntType>::
840 operator()(_UniformRandomNumberGenerator& __urng,
841 const param_type& __param)
842 {
843 typedef typename std::make_unsigned<typename
844 _UniformRandomNumberGenerator::result_type>::type __urngtype;
845 typedef typename std::make_unsigned<result_type>::type __utype;
846 typedef typename std::conditional<(sizeof(__urngtype)
847 > sizeof(__utype)),
848 __urngtype, __utype>::type __uctype;
849
850 const __uctype __urngmin = __urng.min();
851 const __uctype __urngmax = __urng.max();
852 const __uctype __urngrange = __urngmax - __urngmin;
853 const __uctype __urange
854 = __uctype(__param.b()) - __uctype(__param.a());
855
856 __uctype __ret;
857
858 if (__urngrange > __urange)
859 {
860 // downscaling
861 const __uctype __uerange = __urange + 1; // __urange can be zero
862 const __uctype __scaling = __urngrange / __uerange;
863 const __uctype __past = __uerange * __scaling;
864 do
865 __ret = __uctype(__urng()) - __urngmin;
866 while (__ret >= __past);
867 __ret /= __scaling;
868 }
869 else if (__urngrange < __urange)
870 {
871 // upscaling
872 /*
873 Note that every value in [0, urange]
874 can be written uniquely as
875
876 (urngrange + 1) * high + low
877
878 where
879
880 high in [0, urange / (urngrange + 1)]
881
882 and
883
884 low in [0, urngrange].
885 */
886 __uctype __tmp; // wraparound control
887 do
888 {
889 const __uctype __uerngrange = __urngrange + 1;
890 __tmp = (__uerngrange * operator()
891 (__urng, param_type(0, __urange / __uerngrange)));
892 __ret = __tmp + (__uctype(__urng()) - __urngmin);
893 }
894 while (__ret > __urange || __ret < __tmp);
895 }
896 else
897 __ret = __uctype(__urng()) - __urngmin;
898
899 return __ret + __param.a();
900 }
901
902 template<typename _IntType, typename _CharT, typename _Traits>
903 std::basic_ostream<_CharT, _Traits>&
904 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
905 const uniform_int_distribution<_IntType>& __x)
906 {
907 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
908 typedef typename __ostream_type::ios_base __ios_base;
909
910 const typename __ios_base::fmtflags __flags = __os.flags();
911 const _CharT __fill = __os.fill();
912 const _CharT __space = __os.widen(' ');
913 __os.flags(__ios_base::scientific | __ios_base::left);
914 __os.fill(__space);
915
916 __os << __x.a() << __space << __x.b();
917
918 __os.flags(__flags);
919 __os.fill(__fill);
920 return __os;
921 }
922
923 template<typename _IntType, typename _CharT, typename _Traits>
924 std::basic_istream<_CharT, _Traits>&
925 operator>>(std::basic_istream<_CharT, _Traits>& __is,
926 uniform_int_distribution<_IntType>& __x)
927 {
928 typedef std::basic_istream<_CharT, _Traits> __istream_type;
929 typedef typename __istream_type::ios_base __ios_base;
930
931 const typename __ios_base::fmtflags __flags = __is.flags();
932 __is.flags(__ios_base::dec | __ios_base::skipws);
933
934 _IntType __a, __b;
935 __is >> __a >> __b;
936 __x.param(typename uniform_int_distribution<_IntType>::
937 param_type(__a, __b));
938
939 __is.flags(__flags);
940 return __is;
941 }
942
943
944 template<typename _RealType, typename _CharT, typename _Traits>
945 std::basic_ostream<_CharT, _Traits>&
946 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
947 const uniform_real_distribution<_RealType>& __x)
948 {
949 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
950 typedef typename __ostream_type::ios_base __ios_base;
951
952 const typename __ios_base::fmtflags __flags = __os.flags();
953 const _CharT __fill = __os.fill();
954 const std::streamsize __precision = __os.precision();
955 const _CharT __space = __os.widen(' ');
956 __os.flags(__ios_base::scientific | __ios_base::left);
957 __os.fill(__space);
958 __os.precision(std::numeric_limits<_RealType>::max_digits10);
959
960 __os << __x.a() << __space << __x.b();
961
962 __os.flags(__flags);
963 __os.fill(__fill);
964 __os.precision(__precision);
965 return __os;
966 }
967
968 template<typename _RealType, typename _CharT, typename _Traits>
969 std::basic_istream<_CharT, _Traits>&
970 operator>>(std::basic_istream<_CharT, _Traits>& __is,
971 uniform_real_distribution<_RealType>& __x)
972 {
973 typedef std::basic_istream<_CharT, _Traits> __istream_type;
974 typedef typename __istream_type::ios_base __ios_base;
975
976 const typename __ios_base::fmtflags __flags = __is.flags();
977 __is.flags(__ios_base::skipws);
978
979 _RealType __a, __b;
980 __is >> __a >> __b;
981 __x.param(typename uniform_real_distribution<_RealType>::
982 param_type(__a, __b));
983
984 __is.flags(__flags);
985 return __is;
986 }
987
988
989 template<typename _CharT, typename _Traits>
990 std::basic_ostream<_CharT, _Traits>&
991 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
992 const bernoulli_distribution& __x)
993 {
994 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
995 typedef typename __ostream_type::ios_base __ios_base;
996
997 const typename __ios_base::fmtflags __flags = __os.flags();
998 const _CharT __fill = __os.fill();
999 const std::streamsize __precision = __os.precision();
1000 __os.flags(__ios_base::scientific | __ios_base::left);
1001 __os.fill(__os.widen(' '));
1002 __os.precision(std::numeric_limits<double>::max_digits10);
1003
1004 __os << __x.p();
1005
1006 __os.flags(__flags);
1007 __os.fill(__fill);
1008 __os.precision(__precision);
1009 return __os;
1010 }
1011
1012
1013 template<typename _IntType>
1014 template<typename _UniformRandomNumberGenerator>
1015 typename geometric_distribution<_IntType>::result_type
1016 geometric_distribution<_IntType>::
1017 operator()(_UniformRandomNumberGenerator& __urng,
1018 const param_type& __param)
1019 {
1020 // About the epsilon thing see this thread:
1021 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1022 const double __naf =
1023 (1 - std::numeric_limits<double>::epsilon()) / 2;
1024 // The largest _RealType convertible to _IntType.
1025 const double __thr =
1026 std::numeric_limits<_IntType>::max() + __naf;
1027 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1028 __aurng(__urng);
1029
1030 double __cand;
1031 do
1032 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1033 while (__cand >= __thr);
1034
1035 return result_type(__cand + __naf);
1036 }
1037
1038 template<typename _IntType,
1039 typename _CharT, typename _Traits>
1040 std::basic_ostream<_CharT, _Traits>&
1041 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1042 const geometric_distribution<_IntType>& __x)
1043 {
1044 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1045 typedef typename __ostream_type::ios_base __ios_base;
1046
1047 const typename __ios_base::fmtflags __flags = __os.flags();
1048 const _CharT __fill = __os.fill();
1049 const std::streamsize __precision = __os.precision();
1050 __os.flags(__ios_base::scientific | __ios_base::left);
1051 __os.fill(__os.widen(' '));
1052 __os.precision(std::numeric_limits<double>::max_digits10);
1053
1054 __os << __x.p();
1055
1056 __os.flags(__flags);
1057 __os.fill(__fill);
1058 __os.precision(__precision);
1059 return __os;
1060 }
1061
1062 template<typename _IntType,
1063 typename _CharT, typename _Traits>
1064 std::basic_istream<_CharT, _Traits>&
1065 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1066 geometric_distribution<_IntType>& __x)
1067 {
1068 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1069 typedef typename __istream_type::ios_base __ios_base;
1070
1071 const typename __ios_base::fmtflags __flags = __is.flags();
1072 __is.flags(__ios_base::skipws);
1073
1074 double __p;
1075 __is >> __p;
1076 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1077
1078 __is.flags(__flags);
1079 return __is;
1080 }
1081
1082 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1083 template<typename _IntType>
1084 template<typename _UniformRandomNumberGenerator>
1085 typename negative_binomial_distribution<_IntType>::result_type
1086 negative_binomial_distribution<_IntType>::
1087 operator()(_UniformRandomNumberGenerator& __urng)
1088 {
1089 const double __y = _M_gd(__urng);
1090
1091 // XXX Is the constructor too slow?
1092 std::poisson_distribution<result_type> __poisson(__y);
1093 return __poisson(__urng);
1094 }
1095
1096 template<typename _IntType>
1097 template<typename _UniformRandomNumberGenerator>
1098 typename negative_binomial_distribution<_IntType>::result_type
1099 negative_binomial_distribution<_IntType>::
1100 operator()(_UniformRandomNumberGenerator& __urng,
1101 const param_type& __p)
1102 {
1103 typedef typename std::gamma_distribution<result_type>::param_type
1104 param_type;
1105
1106 const double __y =
1107 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1108
1109 std::poisson_distribution<result_type> __poisson(__y);
1110 return __poisson(__urng);
1111 }
1112
1113 template<typename _IntType, typename _CharT, typename _Traits>
1114 std::basic_ostream<_CharT, _Traits>&
1115 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1116 const negative_binomial_distribution<_IntType>& __x)
1117 {
1118 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1119 typedef typename __ostream_type::ios_base __ios_base;
1120
1121 const typename __ios_base::fmtflags __flags = __os.flags();
1122 const _CharT __fill = __os.fill();
1123 const std::streamsize __precision = __os.precision();
1124 const _CharT __space = __os.widen(' ');
1125 __os.flags(__ios_base::scientific | __ios_base::left);
1126 __os.fill(__os.widen(' '));
1127 __os.precision(std::numeric_limits<double>::max_digits10);
1128
1129 __os << __x.k() << __space << __x.p()
1130 << __space << __x._M_gd;
1131
1132 __os.flags(__flags);
1133 __os.fill(__fill);
1134 __os.precision(__precision);
1135 return __os;
1136 }
1137
1138 template<typename _IntType, typename _CharT, typename _Traits>
1139 std::basic_istream<_CharT, _Traits>&
1140 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1141 negative_binomial_distribution<_IntType>& __x)
1142 {
1143 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1144 typedef typename __istream_type::ios_base __ios_base;
1145
1146 const typename __ios_base::fmtflags __flags = __is.flags();
1147 __is.flags(__ios_base::skipws);
1148
1149 _IntType __k;
1150 double __p;
1151 __is >> __k >> __p >> __x._M_gd;
1152 __x.param(typename negative_binomial_distribution<_IntType>::
1153 param_type(__k, __p));
1154
1155 __is.flags(__flags);
1156 return __is;
1157 }
1158
1159
1160 template<typename _IntType>
1161 void
1162 poisson_distribution<_IntType>::param_type::
1163 _M_initialize()
1164 {
1165 #if _GLIBCXX_USE_C99_MATH_TR1
1166 if (_M_mean >= 12)
1167 {
1168 const double __m = std::floor(_M_mean);
1169 _M_lm_thr = std::log(_M_mean);
1170 _M_lfm = std::lgamma(__m + 1);
1171 _M_sm = std::sqrt(__m);
1172
1173 const double __pi_4 = 0.7853981633974483096156608458198757L;
1174 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1175 / __pi_4));
1176 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1177 const double __cx = 2 * __m + _M_d;
1178 _M_scx = std::sqrt(__cx / 2);
1179 _M_1cx = 1 / __cx;
1180
1181 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1182 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1183 / _M_d;
1184 }
1185 else
1186 #endif
1187 _M_lm_thr = std::exp(-_M_mean);
1188 }
1189
1190 /**
1191 * A rejection algorithm when mean >= 12 and a simple method based
1192 * upon the multiplication of uniform random variates otherwise.
1193 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1194 * is defined.
1195 *
1196 * Reference:
1197 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1198 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1199 */
1200 template<typename _IntType>
1201 template<typename _UniformRandomNumberGenerator>
1202 typename poisson_distribution<_IntType>::result_type
1203 poisson_distribution<_IntType>::
1204 operator()(_UniformRandomNumberGenerator& __urng,
1205 const param_type& __param)
1206 {
1207 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1208 __aurng(__urng);
1209 #if _GLIBCXX_USE_C99_MATH_TR1
1210 if (__param.mean() >= 12)
1211 {
1212 double __x;
1213
1214 // See comments above...
1215 const double __naf =
1216 (1 - std::numeric_limits<double>::epsilon()) / 2;
1217 const double __thr =
1218 std::numeric_limits<_IntType>::max() + __naf;
1219
1220 const double __m = std::floor(__param.mean());
1221 // sqrt(pi / 2)
1222 const double __spi_2 = 1.2533141373155002512078826424055226L;
1223 const double __c1 = __param._M_sm * __spi_2;
1224 const double __c2 = __param._M_c2b + __c1;
1225 const double __c3 = __c2 + 1;
1226 const double __c4 = __c3 + 1;
1227 // e^(1 / 78)
1228 const double __e178 = 1.0129030479320018583185514777512983L;
1229 const double __c5 = __c4 + __e178;
1230 const double __c = __param._M_cb + __c5;
1231 const double __2cx = 2 * (2 * __m + __param._M_d);
1232
1233 bool __reject = true;
1234 do
1235 {
1236 const double __u = __c * __aurng();
1237 const double __e = -std::log(__aurng());
1238
1239 double __w = 0.0;
1240
1241 if (__u <= __c1)
1242 {
1243 const double __n = _M_nd(__urng);
1244 const double __y = -std::abs(__n) * __param._M_sm - 1;
1245 __x = std::floor(__y);
1246 __w = -__n * __n / 2;
1247 if (__x < -__m)
1248 continue;
1249 }
1250 else if (__u <= __c2)
1251 {
1252 const double __n = _M_nd(__urng);
1253 const double __y = 1 + std::abs(__n) * __param._M_scx;
1254 __x = std::ceil(__y);
1255 __w = __y * (2 - __y) * __param._M_1cx;
1256 if (__x > __param._M_d)
1257 continue;
1258 }
1259 else if (__u <= __c3)
1260 // NB: This case not in the book, nor in the Errata,
1261 // but should be ok...
1262 __x = -1;
1263 else if (__u <= __c4)
1264 __x = 0;
1265 else if (__u <= __c5)
1266 __x = 1;
1267 else
1268 {
1269 const double __v = -std::log(__aurng());
1270 const double __y = __param._M_d
1271 + __v * __2cx / __param._M_d;
1272 __x = std::ceil(__y);
1273 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1274 }
1275
1276 __reject = (__w - __e - __x * __param._M_lm_thr
1277 > __param._M_lfm - std::lgamma(__x + __m + 1));
1278
1279 __reject |= __x + __m >= __thr;
1280
1281 } while (__reject);
1282
1283 return result_type(__x + __m + __naf);
1284 }
1285 else
1286 #endif
1287 {
1288 _IntType __x = 0;
1289 double __prod = 1.0;
1290
1291 do
1292 {
1293 __prod *= __aurng();
1294 __x += 1;
1295 }
1296 while (__prod > __param._M_lm_thr);
1297
1298 return __x - 1;
1299 }
1300 }
1301
1302 template<typename _IntType,
1303 typename _CharT, typename _Traits>
1304 std::basic_ostream<_CharT, _Traits>&
1305 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1306 const poisson_distribution<_IntType>& __x)
1307 {
1308 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1309 typedef typename __ostream_type::ios_base __ios_base;
1310
1311 const typename __ios_base::fmtflags __flags = __os.flags();
1312 const _CharT __fill = __os.fill();
1313 const std::streamsize __precision = __os.precision();
1314 const _CharT __space = __os.widen(' ');
1315 __os.flags(__ios_base::scientific | __ios_base::left);
1316 __os.fill(__space);
1317 __os.precision(std::numeric_limits<double>::max_digits10);
1318
1319 __os << __x.mean() << __space << __x._M_nd;
1320
1321 __os.flags(__flags);
1322 __os.fill(__fill);
1323 __os.precision(__precision);
1324 return __os;
1325 }
1326
1327 template<typename _IntType,
1328 typename _CharT, typename _Traits>
1329 std::basic_istream<_CharT, _Traits>&
1330 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1331 poisson_distribution<_IntType>& __x)
1332 {
1333 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1334 typedef typename __istream_type::ios_base __ios_base;
1335
1336 const typename __ios_base::fmtflags __flags = __is.flags();
1337 __is.flags(__ios_base::skipws);
1338
1339 double __mean;
1340 __is >> __mean >> __x._M_nd;
1341 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1342
1343 __is.flags(__flags);
1344 return __is;
1345 }
1346
1347
1348 template<typename _IntType>
1349 void
1350 binomial_distribution<_IntType>::param_type::
1351 _M_initialize()
1352 {
1353 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1354
1355 _M_easy = true;
1356
1357 #if _GLIBCXX_USE_C99_MATH_TR1
1358 if (_M_t * __p12 >= 8)
1359 {
1360 _M_easy = false;
1361 const double __np = std::floor(_M_t * __p12);
1362 const double __pa = __np / _M_t;
1363 const double __1p = 1 - __pa;
1364
1365 const double __pi_4 = 0.7853981633974483096156608458198757L;
1366 const double __d1x =
1367 std::sqrt(__np * __1p * std::log(32 * __np
1368 / (81 * __pi_4 * __1p)));
1369 _M_d1 = std::round(std::max(1.0, __d1x));
1370 const double __d2x =
1371 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1372 / (__pi_4 * __pa)));
1373 _M_d2 = std::round(std::max(1.0, __d2x));
1374
1375 // sqrt(pi / 2)
1376 const double __spi_2 = 1.2533141373155002512078826424055226L;
1377 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1378 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1379 _M_c = 2 * _M_d1 / __np;
1380 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1381 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1382 const double __s1s = _M_s1 * _M_s1;
1383 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1384 * 2 * __s1s / _M_d1
1385 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1386 const double __s2s = _M_s2 * _M_s2;
1387 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1388 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1389 _M_lf = (std::lgamma(__np + 1)
1390 + std::lgamma(_M_t - __np + 1));
1391 _M_lp1p = std::log(__pa / __1p);
1392
1393 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1394 }
1395 else
1396 #endif
1397 _M_q = -std::log(1 - __p12);
1398 }
1399
1400 template<typename _IntType>
1401 template<typename _UniformRandomNumberGenerator>
1402 typename binomial_distribution<_IntType>::result_type
1403 binomial_distribution<_IntType>::
1404 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1405 {
1406 _IntType __x = 0;
1407 double __sum = 0.0;
1408 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1409 __aurng(__urng);
1410
1411 do
1412 {
1413 const double __e = -std::log(__aurng());
1414 __sum += __e / (__t - __x);
1415 __x += 1;
1416 }
1417 while (__sum <= _M_param._M_q);
1418
1419 return __x - 1;
1420 }
1421
1422 /**
1423 * A rejection algorithm when t * p >= 8 and a simple waiting time
1424 * method - the second in the referenced book - otherwise.
1425 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1426 * is defined.
1427 *
1428 * Reference:
1429 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1430 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1431 */
1432 template<typename _IntType>
1433 template<typename _UniformRandomNumberGenerator>
1434 typename binomial_distribution<_IntType>::result_type
1435 binomial_distribution<_IntType>::
1436 operator()(_UniformRandomNumberGenerator& __urng,
1437 const param_type& __param)
1438 {
1439 result_type __ret;
1440 const _IntType __t = __param.t();
1441 const double __p = __param.p();
1442 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1443 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1444 __aurng(__urng);
1445
1446 #if _GLIBCXX_USE_C99_MATH_TR1
1447 if (!__param._M_easy)
1448 {
1449 double __x;
1450
1451 // See comments above...
1452 const double __naf =
1453 (1 - std::numeric_limits<double>::epsilon()) / 2;
1454 const double __thr =
1455 std::numeric_limits<_IntType>::max() + __naf;
1456
1457 const double __np = std::floor(__t * __p12);
1458
1459 // sqrt(pi / 2)
1460 const double __spi_2 = 1.2533141373155002512078826424055226L;
1461 const double __a1 = __param._M_a1;
1462 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1463 const double __a123 = __param._M_a123;
1464 const double __s1s = __param._M_s1 * __param._M_s1;
1465 const double __s2s = __param._M_s2 * __param._M_s2;
1466
1467 bool __reject;
1468 do
1469 {
1470 const double __u = __param._M_s * __aurng();
1471
1472 double __v;
1473
1474 if (__u <= __a1)
1475 {
1476 const double __n = _M_nd(__urng);
1477 const double __y = __param._M_s1 * std::abs(__n);
1478 __reject = __y >= __param._M_d1;
1479 if (!__reject)
1480 {
1481 const double __e = -std::log(__aurng());
1482 __x = std::floor(__y);
1483 __v = -__e - __n * __n / 2 + __param._M_c;
1484 }
1485 }
1486 else if (__u <= __a12)
1487 {
1488 const double __n = _M_nd(__urng);
1489 const double __y = __param._M_s2 * std::abs(__n);
1490 __reject = __y >= __param._M_d2;
1491 if (!__reject)
1492 {
1493 const double __e = -std::log(__aurng());
1494 __x = std::floor(-__y);
1495 __v = -__e - __n * __n / 2;
1496 }
1497 }
1498 else if (__u <= __a123)
1499 {
1500 const double __e1 = -std::log(__aurng());
1501 const double __e2 = -std::log(__aurng());
1502
1503 const double __y = __param._M_d1
1504 + 2 * __s1s * __e1 / __param._M_d1;
1505 __x = std::floor(__y);
1506 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1507 -__y / (2 * __s1s)));
1508 __reject = false;
1509 }
1510 else
1511 {
1512 const double __e1 = -std::log(__aurng());
1513 const double __e2 = -std::log(__aurng());
1514
1515 const double __y = __param._M_d2
1516 + 2 * __s2s * __e1 / __param._M_d2;
1517 __x = std::floor(-__y);
1518 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1519 __reject = false;
1520 }
1521
1522 __reject = __reject || __x < -__np || __x > __t - __np;
1523 if (!__reject)
1524 {
1525 const double __lfx =
1526 std::lgamma(__np + __x + 1)
1527 + std::lgamma(__t - (__np + __x) + 1);
1528 __reject = __v > __param._M_lf - __lfx
1529 + __x * __param._M_lp1p;
1530 }
1531
1532 __reject |= __x + __np >= __thr;
1533 }
1534 while (__reject);
1535
1536 __x += __np + __naf;
1537
1538 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1539 __ret = _IntType(__x) + __z;
1540 }
1541 else
1542 #endif
1543 __ret = _M_waiting(__urng, __t);
1544
1545 if (__p12 != __p)
1546 __ret = __t - __ret;
1547 return __ret;
1548 }
1549
1550 template<typename _IntType,
1551 typename _CharT, typename _Traits>
1552 std::basic_ostream<_CharT, _Traits>&
1553 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1554 const binomial_distribution<_IntType>& __x)
1555 {
1556 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1557 typedef typename __ostream_type::ios_base __ios_base;
1558
1559 const typename __ios_base::fmtflags __flags = __os.flags();
1560 const _CharT __fill = __os.fill();
1561 const std::streamsize __precision = __os.precision();
1562 const _CharT __space = __os.widen(' ');
1563 __os.flags(__ios_base::scientific | __ios_base::left);
1564 __os.fill(__space);
1565 __os.precision(std::numeric_limits<double>::max_digits10);
1566
1567 __os << __x.t() << __space << __x.p()
1568 << __space << __x._M_nd;
1569
1570 __os.flags(__flags);
1571 __os.fill(__fill);
1572 __os.precision(__precision);
1573 return __os;
1574 }
1575
1576 template<typename _IntType,
1577 typename _CharT, typename _Traits>
1578 std::basic_istream<_CharT, _Traits>&
1579 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1580 binomial_distribution<_IntType>& __x)
1581 {
1582 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1583 typedef typename __istream_type::ios_base __ios_base;
1584
1585 const typename __ios_base::fmtflags __flags = __is.flags();
1586 __is.flags(__ios_base::dec | __ios_base::skipws);
1587
1588 _IntType __t;
1589 double __p;
1590 __is >> __t >> __p >> __x._M_nd;
1591 __x.param(typename binomial_distribution<_IntType>::
1592 param_type(__t, __p));
1593
1594 __is.flags(__flags);
1595 return __is;
1596 }
1597
1598
1599 template<typename _RealType, typename _CharT, typename _Traits>
1600 std::basic_ostream<_CharT, _Traits>&
1601 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1602 const exponential_distribution<_RealType>& __x)
1603 {
1604 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1605 typedef typename __ostream_type::ios_base __ios_base;
1606
1607 const typename __ios_base::fmtflags __flags = __os.flags();
1608 const _CharT __fill = __os.fill();
1609 const std::streamsize __precision = __os.precision();
1610 __os.flags(__ios_base::scientific | __ios_base::left);
1611 __os.fill(__os.widen(' '));
1612 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1613
1614 __os << __x.lambda();
1615
1616 __os.flags(__flags);
1617 __os.fill(__fill);
1618 __os.precision(__precision);
1619 return __os;
1620 }
1621
1622 template<typename _RealType, typename _CharT, typename _Traits>
1623 std::basic_istream<_CharT, _Traits>&
1624 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1625 exponential_distribution<_RealType>& __x)
1626 {
1627 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1628 typedef typename __istream_type::ios_base __ios_base;
1629
1630 const typename __ios_base::fmtflags __flags = __is.flags();
1631 __is.flags(__ios_base::dec | __ios_base::skipws);
1632
1633 _RealType __lambda;
1634 __is >> __lambda;
1635 __x.param(typename exponential_distribution<_RealType>::
1636 param_type(__lambda));
1637
1638 __is.flags(__flags);
1639 return __is;
1640 }
1641
1642
1643 /**
1644 * Polar method due to Marsaglia.
1645 *
1646 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1647 * New York, 1986, Ch. V, Sect. 4.4.
1648 */
1649 template<typename _RealType>
1650 template<typename _UniformRandomNumberGenerator>
1651 typename normal_distribution<_RealType>::result_type
1652 normal_distribution<_RealType>::
1653 operator()(_UniformRandomNumberGenerator& __urng,
1654 const param_type& __param)
1655 {
1656 result_type __ret;
1657 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1658 __aurng(__urng);
1659
1660 if (_M_saved_available)
1661 {
1662 _M_saved_available = false;
1663 __ret = _M_saved;
1664 }
1665 else
1666 {
1667 result_type __x, __y, __r2;
1668 do
1669 {
1670 __x = result_type(2.0) * __aurng() - 1.0;
1671 __y = result_type(2.0) * __aurng() - 1.0;
1672 __r2 = __x * __x + __y * __y;
1673 }
1674 while (__r2 > 1.0 || __r2 == 0.0);
1675
1676 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1677 _M_saved = __x * __mult;
1678 _M_saved_available = true;
1679 __ret = __y * __mult;
1680 }
1681
1682 __ret = __ret * __param.stddev() + __param.mean();
1683 return __ret;
1684 }
1685
1686 template<typename _RealType>
1687 bool
1688 operator==(const std::normal_distribution<_RealType>& __d1,
1689 const std::normal_distribution<_RealType>& __d2)
1690 {
1691 if (__d1._M_param == __d2._M_param
1692 && __d1._M_saved_available == __d2._M_saved_available)
1693 {
1694 if (__d1._M_saved_available
1695 && __d1._M_saved == __d2._M_saved)
1696 return true;
1697 else if(!__d1._M_saved_available)
1698 return true;
1699 else
1700 return false;
1701 }
1702 else
1703 return false;
1704 }
1705
1706 template<typename _RealType, typename _CharT, typename _Traits>
1707 std::basic_ostream<_CharT, _Traits>&
1708 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1709 const normal_distribution<_RealType>& __x)
1710 {
1711 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1712 typedef typename __ostream_type::ios_base __ios_base;
1713
1714 const typename __ios_base::fmtflags __flags = __os.flags();
1715 const _CharT __fill = __os.fill();
1716 const std::streamsize __precision = __os.precision();
1717 const _CharT __space = __os.widen(' ');
1718 __os.flags(__ios_base::scientific | __ios_base::left);
1719 __os.fill(__space);
1720 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1721
1722 __os << __x.mean() << __space << __x.stddev()
1723 << __space << __x._M_saved_available;
1724 if (__x._M_saved_available)
1725 __os << __space << __x._M_saved;
1726
1727 __os.flags(__flags);
1728 __os.fill(__fill);
1729 __os.precision(__precision);
1730 return __os;
1731 }
1732
1733 template<typename _RealType, typename _CharT, typename _Traits>
1734 std::basic_istream<_CharT, _Traits>&
1735 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1736 normal_distribution<_RealType>& __x)
1737 {
1738 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1739 typedef typename __istream_type::ios_base __ios_base;
1740
1741 const typename __ios_base::fmtflags __flags = __is.flags();
1742 __is.flags(__ios_base::dec | __ios_base::skipws);
1743
1744 double __mean, __stddev;
1745 __is >> __mean >> __stddev
1746 >> __x._M_saved_available;
1747 if (__x._M_saved_available)
1748 __is >> __x._M_saved;
1749 __x.param(typename normal_distribution<_RealType>::
1750 param_type(__mean, __stddev));
1751
1752 __is.flags(__flags);
1753 return __is;
1754 }
1755
1756
1757 template<typename _RealType, typename _CharT, typename _Traits>
1758 std::basic_ostream<_CharT, _Traits>&
1759 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1760 const lognormal_distribution<_RealType>& __x)
1761 {
1762 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1763 typedef typename __ostream_type::ios_base __ios_base;
1764
1765 const typename __ios_base::fmtflags __flags = __os.flags();
1766 const _CharT __fill = __os.fill();
1767 const std::streamsize __precision = __os.precision();
1768 const _CharT __space = __os.widen(' ');
1769 __os.flags(__ios_base::scientific | __ios_base::left);
1770 __os.fill(__space);
1771 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1772
1773 __os << __x.m() << __space << __x.s()
1774 << __space << __x._M_nd;
1775
1776 __os.flags(__flags);
1777 __os.fill(__fill);
1778 __os.precision(__precision);
1779 return __os;
1780 }
1781
1782 template<typename _RealType, typename _CharT, typename _Traits>
1783 std::basic_istream<_CharT, _Traits>&
1784 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1785 lognormal_distribution<_RealType>& __x)
1786 {
1787 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1788 typedef typename __istream_type::ios_base __ios_base;
1789
1790 const typename __ios_base::fmtflags __flags = __is.flags();
1791 __is.flags(__ios_base::dec | __ios_base::skipws);
1792
1793 _RealType __m, __s;
1794 __is >> __m >> __s >> __x._M_nd;
1795 __x.param(typename lognormal_distribution<_RealType>::
1796 param_type(__m, __s));
1797
1798 __is.flags(__flags);
1799 return __is;
1800 }
1801
1802
1803 template<typename _RealType, typename _CharT, typename _Traits>
1804 std::basic_ostream<_CharT, _Traits>&
1805 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1806 const chi_squared_distribution<_RealType>& __x)
1807 {
1808 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1809 typedef typename __ostream_type::ios_base __ios_base;
1810
1811 const typename __ios_base::fmtflags __flags = __os.flags();
1812 const _CharT __fill = __os.fill();
1813 const std::streamsize __precision = __os.precision();
1814 const _CharT __space = __os.widen(' ');
1815 __os.flags(__ios_base::scientific | __ios_base::left);
1816 __os.fill(__space);
1817 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1818
1819 __os << __x.n() << __space << __x._M_gd;
1820
1821 __os.flags(__flags);
1822 __os.fill(__fill);
1823 __os.precision(__precision);
1824 return __os;
1825 }
1826
1827 template<typename _RealType, typename _CharT, typename _Traits>
1828 std::basic_istream<_CharT, _Traits>&
1829 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1830 chi_squared_distribution<_RealType>& __x)
1831 {
1832 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1833 typedef typename __istream_type::ios_base __ios_base;
1834
1835 const typename __ios_base::fmtflags __flags = __is.flags();
1836 __is.flags(__ios_base::dec | __ios_base::skipws);
1837
1838 _RealType __n;
1839 __is >> __n >> __x._M_gd;
1840 __x.param(typename chi_squared_distribution<_RealType>::
1841 param_type(__n));
1842
1843 __is.flags(__flags);
1844 return __is;
1845 }
1846
1847
1848 template<typename _RealType>
1849 template<typename _UniformRandomNumberGenerator>
1850 typename cauchy_distribution<_RealType>::result_type
1851 cauchy_distribution<_RealType>::
1852 operator()(_UniformRandomNumberGenerator& __urng,
1853 const param_type& __p)
1854 {
1855 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1856 __aurng(__urng);
1857 _RealType __u;
1858 do
1859 __u = __aurng();
1860 while (__u == 0.5);
1861
1862 const _RealType __pi = 3.1415926535897932384626433832795029L;
1863 return __p.a() + __p.b() * std::tan(__pi * __u);
1864 }
1865
1866 template<typename _RealType, typename _CharT, typename _Traits>
1867 std::basic_ostream<_CharT, _Traits>&
1868 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1869 const cauchy_distribution<_RealType>& __x)
1870 {
1871 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1872 typedef typename __ostream_type::ios_base __ios_base;
1873
1874 const typename __ios_base::fmtflags __flags = __os.flags();
1875 const _CharT __fill = __os.fill();
1876 const std::streamsize __precision = __os.precision();
1877 const _CharT __space = __os.widen(' ');
1878 __os.flags(__ios_base::scientific | __ios_base::left);
1879 __os.fill(__space);
1880 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1881
1882 __os << __x.a() << __space << __x.b();
1883
1884 __os.flags(__flags);
1885 __os.fill(__fill);
1886 __os.precision(__precision);
1887 return __os;
1888 }
1889
1890 template<typename _RealType, typename _CharT, typename _Traits>
1891 std::basic_istream<_CharT, _Traits>&
1892 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1893 cauchy_distribution<_RealType>& __x)
1894 {
1895 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1896 typedef typename __istream_type::ios_base __ios_base;
1897
1898 const typename __ios_base::fmtflags __flags = __is.flags();
1899 __is.flags(__ios_base::dec | __ios_base::skipws);
1900
1901 _RealType __a, __b;
1902 __is >> __a >> __b;
1903 __x.param(typename cauchy_distribution<_RealType>::
1904 param_type(__a, __b));
1905
1906 __is.flags(__flags);
1907 return __is;
1908 }
1909
1910
1911 template<typename _RealType, typename _CharT, typename _Traits>
1912 std::basic_ostream<_CharT, _Traits>&
1913 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1914 const fisher_f_distribution<_RealType>& __x)
1915 {
1916 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1917 typedef typename __ostream_type::ios_base __ios_base;
1918
1919 const typename __ios_base::fmtflags __flags = __os.flags();
1920 const _CharT __fill = __os.fill();
1921 const std::streamsize __precision = __os.precision();
1922 const _CharT __space = __os.widen(' ');
1923 __os.flags(__ios_base::scientific | __ios_base::left);
1924 __os.fill(__space);
1925 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1926
1927 __os << __x.m() << __space << __x.n()
1928 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1929
1930 __os.flags(__flags);
1931 __os.fill(__fill);
1932 __os.precision(__precision);
1933 return __os;
1934 }
1935
1936 template<typename _RealType, typename _CharT, typename _Traits>
1937 std::basic_istream<_CharT, _Traits>&
1938 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1939 fisher_f_distribution<_RealType>& __x)
1940 {
1941 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1942 typedef typename __istream_type::ios_base __ios_base;
1943
1944 const typename __ios_base::fmtflags __flags = __is.flags();
1945 __is.flags(__ios_base::dec | __ios_base::skipws);
1946
1947 _RealType __m, __n;
1948 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1949 __x.param(typename fisher_f_distribution<_RealType>::
1950 param_type(__m, __n));
1951
1952 __is.flags(__flags);
1953 return __is;
1954 }
1955
1956
1957 template<typename _RealType, typename _CharT, typename _Traits>
1958 std::basic_ostream<_CharT, _Traits>&
1959 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1960 const student_t_distribution<_RealType>& __x)
1961 {
1962 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1963 typedef typename __ostream_type::ios_base __ios_base;
1964
1965 const typename __ios_base::fmtflags __flags = __os.flags();
1966 const _CharT __fill = __os.fill();
1967 const std::streamsize __precision = __os.precision();
1968 const _CharT __space = __os.widen(' ');
1969 __os.flags(__ios_base::scientific | __ios_base::left);
1970 __os.fill(__space);
1971 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1972
1973 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1974
1975 __os.flags(__flags);
1976 __os.fill(__fill);
1977 __os.precision(__precision);
1978 return __os;
1979 }
1980
1981 template<typename _RealType, typename _CharT, typename _Traits>
1982 std::basic_istream<_CharT, _Traits>&
1983 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1984 student_t_distribution<_RealType>& __x)
1985 {
1986 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1987 typedef typename __istream_type::ios_base __ios_base;
1988
1989 const typename __ios_base::fmtflags __flags = __is.flags();
1990 __is.flags(__ios_base::dec | __ios_base::skipws);
1991
1992 _RealType __n;
1993 __is >> __n >> __x._M_nd >> __x._M_gd;
1994 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1995
1996 __is.flags(__flags);
1997 return __is;
1998 }
1999
2000
2001 template<typename _RealType>
2002 void
2003 gamma_distribution<_RealType>::param_type::
2004 _M_initialize()
2005 {
2006 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2007
2008 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2009 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2010 }
2011
2012 /**
2013 * Marsaglia, G. and Tsang, W. W.
2014 * "A Simple Method for Generating Gamma Variables"
2015 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2016 */
2017 template<typename _RealType>
2018 template<typename _UniformRandomNumberGenerator>
2019 typename gamma_distribution<_RealType>::result_type
2020 gamma_distribution<_RealType>::
2021 operator()(_UniformRandomNumberGenerator& __urng,
2022 const param_type& __param)
2023 {
2024 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2025 __aurng(__urng);
2026
2027 result_type __u, __v, __n;
2028 const result_type __a1 = (__param._M_malpha
2029 - _RealType(1.0) / _RealType(3.0));
2030
2031 do
2032 {
2033 do
2034 {
2035 __n = _M_nd(__urng);
2036 __v = result_type(1.0) + __param._M_a2 * __n;
2037 }
2038 while (__v <= 0.0);
2039
2040 __v = __v * __v * __v;
2041 __u = __aurng();
2042 }
2043 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2044 && (std::log(__u) > (0.5 * __n * __n + __a1
2045 * (1.0 - __v + std::log(__v)))));
2046
2047 if (__param.alpha() == __param._M_malpha)
2048 return __a1 * __v * __param.beta();
2049 else
2050 {
2051 do
2052 __u = __aurng();
2053 while (__u == 0.0);
2054
2055 return (std::pow(__u, result_type(1.0) / __param.alpha())
2056 * __a1 * __v * __param.beta());
2057 }
2058 }
2059
2060 template<typename _RealType, typename _CharT, typename _Traits>
2061 std::basic_ostream<_CharT, _Traits>&
2062 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2063 const gamma_distribution<_RealType>& __x)
2064 {
2065 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2066 typedef typename __ostream_type::ios_base __ios_base;
2067
2068 const typename __ios_base::fmtflags __flags = __os.flags();
2069 const _CharT __fill = __os.fill();
2070 const std::streamsize __precision = __os.precision();
2071 const _CharT __space = __os.widen(' ');
2072 __os.flags(__ios_base::scientific | __ios_base::left);
2073 __os.fill(__space);
2074 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2075
2076 __os << __x.alpha() << __space << __x.beta()
2077 << __space << __x._M_nd;
2078
2079 __os.flags(__flags);
2080 __os.fill(__fill);
2081 __os.precision(__precision);
2082 return __os;
2083 }
2084
2085 template<typename _RealType, typename _CharT, typename _Traits>
2086 std::basic_istream<_CharT, _Traits>&
2087 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2088 gamma_distribution<_RealType>& __x)
2089 {
2090 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2091 typedef typename __istream_type::ios_base __ios_base;
2092
2093 const typename __ios_base::fmtflags __flags = __is.flags();
2094 __is.flags(__ios_base::dec | __ios_base::skipws);
2095
2096 _RealType __alpha_val, __beta_val;
2097 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2098 __x.param(typename gamma_distribution<_RealType>::
2099 param_type(__alpha_val, __beta_val));
2100
2101 __is.flags(__flags);
2102 return __is;
2103 }
2104
2105
2106 template<typename _RealType>
2107 template<typename _UniformRandomNumberGenerator>
2108 typename weibull_distribution<_RealType>::result_type
2109 weibull_distribution<_RealType>::
2110 operator()(_UniformRandomNumberGenerator& __urng,
2111 const param_type& __p)
2112 {
2113 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2114 __aurng(__urng);
2115 return __p.b() * std::pow(-std::log(__aurng()),
2116 result_type(1) / __p.a());
2117 }
2118
2119 template<typename _RealType, typename _CharT, typename _Traits>
2120 std::basic_ostream<_CharT, _Traits>&
2121 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2122 const weibull_distribution<_RealType>& __x)
2123 {
2124 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2125 typedef typename __ostream_type::ios_base __ios_base;
2126
2127 const typename __ios_base::fmtflags __flags = __os.flags();
2128 const _CharT __fill = __os.fill();
2129 const std::streamsize __precision = __os.precision();
2130 const _CharT __space = __os.widen(' ');
2131 __os.flags(__ios_base::scientific | __ios_base::left);
2132 __os.fill(__space);
2133 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2134
2135 __os << __x.a() << __space << __x.b();
2136
2137 __os.flags(__flags);
2138 __os.fill(__fill);
2139 __os.precision(__precision);
2140 return __os;
2141 }
2142
2143 template<typename _RealType, typename _CharT, typename _Traits>
2144 std::basic_istream<_CharT, _Traits>&
2145 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2146 weibull_distribution<_RealType>& __x)
2147 {
2148 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2149 typedef typename __istream_type::ios_base __ios_base;
2150
2151 const typename __ios_base::fmtflags __flags = __is.flags();
2152 __is.flags(__ios_base::dec | __ios_base::skipws);
2153
2154 _RealType __a, __b;
2155 __is >> __a >> __b;
2156 __x.param(typename weibull_distribution<_RealType>::
2157 param_type(__a, __b));
2158
2159 __is.flags(__flags);
2160 return __is;
2161 }
2162
2163
2164 template<typename _RealType>
2165 template<typename _UniformRandomNumberGenerator>
2166 typename extreme_value_distribution<_RealType>::result_type
2167 extreme_value_distribution<_RealType>::
2168 operator()(_UniformRandomNumberGenerator& __urng,
2169 const param_type& __p)
2170 {
2171 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2172 __aurng(__urng);
2173 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2174 }
2175
2176 template<typename _RealType, typename _CharT, typename _Traits>
2177 std::basic_ostream<_CharT, _Traits>&
2178 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2179 const extreme_value_distribution<_RealType>& __x)
2180 {
2181 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2182 typedef typename __ostream_type::ios_base __ios_base;
2183
2184 const typename __ios_base::fmtflags __flags = __os.flags();
2185 const _CharT __fill = __os.fill();
2186 const std::streamsize __precision = __os.precision();
2187 const _CharT __space = __os.widen(' ');
2188 __os.flags(__ios_base::scientific | __ios_base::left);
2189 __os.fill(__space);
2190 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2191
2192 __os << __x.a() << __space << __x.b();
2193
2194 __os.flags(__flags);
2195 __os.fill(__fill);
2196 __os.precision(__precision);
2197 return __os;
2198 }
2199
2200 template<typename _RealType, typename _CharT, typename _Traits>
2201 std::basic_istream<_CharT, _Traits>&
2202 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2203 extreme_value_distribution<_RealType>& __x)
2204 {
2205 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2206 typedef typename __istream_type::ios_base __ios_base;
2207
2208 const typename __ios_base::fmtflags __flags = __is.flags();
2209 __is.flags(__ios_base::dec | __ios_base::skipws);
2210
2211 _RealType __a, __b;
2212 __is >> __a >> __b;
2213 __x.param(typename extreme_value_distribution<_RealType>::
2214 param_type(__a, __b));
2215
2216 __is.flags(__flags);
2217 return __is;
2218 }
2219
2220
2221 template<typename _IntType>
2222 void
2223 discrete_distribution<_IntType>::param_type::
2224 _M_initialize()
2225 {
2226 if (_M_prob.size() < 2)
2227 {
2228 _M_prob.clear();
2229 return;
2230 }
2231
2232 const double __sum = std::accumulate(_M_prob.begin(),
2233 _M_prob.end(), 0.0);
2234 // Now normalize the probabilites.
2235 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2236 std::bind2nd(std::divides<double>(), __sum));
2237 // Accumulate partial sums.
2238 _M_cp.reserve(_M_prob.size());
2239 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2240 std::back_inserter(_M_cp));
2241 // Make sure the last cumulative probability is one.
2242 _M_cp[_M_cp.size() - 1] = 1.0;
2243 }
2244
2245 template<typename _IntType>
2246 template<typename _Func>
2247 discrete_distribution<_IntType>::param_type::
2248 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2249 : _M_prob(), _M_cp()
2250 {
2251 const size_t __n = __nw == 0 ? 1 : __nw;
2252 const double __delta = (__xmax - __xmin) / __n;
2253
2254 _M_prob.reserve(__n);
2255 for (size_t __k = 0; __k < __nw; ++__k)
2256 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2257
2258 _M_initialize();
2259 }
2260
2261 template<typename _IntType>
2262 template<typename _UniformRandomNumberGenerator>
2263 typename discrete_distribution<_IntType>::result_type
2264 discrete_distribution<_IntType>::
2265 operator()(_UniformRandomNumberGenerator& __urng,
2266 const param_type& __param)
2267 {
2268 if (__param._M_cp.empty())
2269 return result_type(0);
2270
2271 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2272 __aurng(__urng);
2273
2274 const double __p = __aurng();
2275 auto __pos = std::lower_bound(__param._M_cp.begin(),
2276 __param._M_cp.end(), __p);
2277
2278 return __pos - __param._M_cp.begin();
2279 }
2280
2281 template<typename _IntType, typename _CharT, typename _Traits>
2282 std::basic_ostream<_CharT, _Traits>&
2283 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2284 const discrete_distribution<_IntType>& __x)
2285 {
2286 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2287 typedef typename __ostream_type::ios_base __ios_base;
2288
2289 const typename __ios_base::fmtflags __flags = __os.flags();
2290 const _CharT __fill = __os.fill();
2291 const std::streamsize __precision = __os.precision();
2292 const _CharT __space = __os.widen(' ');
2293 __os.flags(__ios_base::scientific | __ios_base::left);
2294 __os.fill(__space);
2295 __os.precision(std::numeric_limits<double>::max_digits10);
2296
2297 std::vector<double> __prob = __x.probabilities();
2298 __os << __prob.size();
2299 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2300 __os << __space << *__dit;
2301
2302 __os.flags(__flags);
2303 __os.fill(__fill);
2304 __os.precision(__precision);
2305 return __os;
2306 }
2307
2308 template<typename _IntType, typename _CharT, typename _Traits>
2309 std::basic_istream<_CharT, _Traits>&
2310 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2311 discrete_distribution<_IntType>& __x)
2312 {
2313 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2314 typedef typename __istream_type::ios_base __ios_base;
2315
2316 const typename __ios_base::fmtflags __flags = __is.flags();
2317 __is.flags(__ios_base::dec | __ios_base::skipws);
2318
2319 size_t __n;
2320 __is >> __n;
2321
2322 std::vector<double> __prob_vec;
2323 __prob_vec.reserve(__n);
2324 for (; __n != 0; --__n)
2325 {
2326 double __prob;
2327 __is >> __prob;
2328 __prob_vec.push_back(__prob);
2329 }
2330
2331 __x.param(typename discrete_distribution<_IntType>::
2332 param_type(__prob_vec.begin(), __prob_vec.end()));
2333
2334 __is.flags(__flags);
2335 return __is;
2336 }
2337
2338
2339 template<typename _RealType>
2340 void
2341 piecewise_constant_distribution<_RealType>::param_type::
2342 _M_initialize()
2343 {
2344 if (_M_int.size() < 2
2345 || (_M_int.size() == 2
2346 && _M_int[0] == _RealType(0)
2347 && _M_int[1] == _RealType(1)))
2348 {
2349 _M_int.clear();
2350 _M_den.clear();
2351 return;
2352 }
2353
2354 const double __sum = std::accumulate(_M_den.begin(),
2355 _M_den.end(), 0.0);
2356
2357 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2358 std::bind2nd(std::divides<double>(), __sum));
2359
2360 _M_cp.reserve(_M_den.size());
2361 std::partial_sum(_M_den.begin(), _M_den.end(),
2362 std::back_inserter(_M_cp));
2363
2364 // Make sure the last cumulative probability is one.
2365 _M_cp[_M_cp.size() - 1] = 1.0;
2366
2367 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2368 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2369 }
2370
2371 template<typename _RealType>
2372 template<typename _InputIteratorB, typename _InputIteratorW>
2373 piecewise_constant_distribution<_RealType>::param_type::
2374 param_type(_InputIteratorB __bbegin,
2375 _InputIteratorB __bend,
2376 _InputIteratorW __wbegin)
2377 : _M_int(), _M_den(), _M_cp()
2378 {
2379 if (__bbegin != __bend)
2380 {
2381 for (;;)
2382 {
2383 _M_int.push_back(*__bbegin);
2384 ++__bbegin;
2385 if (__bbegin == __bend)
2386 break;
2387
2388 _M_den.push_back(*__wbegin);
2389 ++__wbegin;
2390 }
2391 }
2392
2393 _M_initialize();
2394 }
2395
2396 template<typename _RealType>
2397 template<typename _Func>
2398 piecewise_constant_distribution<_RealType>::param_type::
2399 param_type(initializer_list<_RealType> __bl, _Func __fw)
2400 : _M_int(), _M_den(), _M_cp()
2401 {
2402 _M_int.reserve(__bl.size());
2403 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2404 _M_int.push_back(*__biter);
2405
2406 _M_den.reserve(_M_int.size() - 1);
2407 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2408 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2409
2410 _M_initialize();
2411 }
2412
2413 template<typename _RealType>
2414 template<typename _Func>
2415 piecewise_constant_distribution<_RealType>::param_type::
2416 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2417 : _M_int(), _M_den(), _M_cp()
2418 {
2419 const size_t __n = __nw == 0 ? 1 : __nw;
2420 const _RealType __delta = (__xmax - __xmin) / __n;
2421
2422 _M_int.reserve(__n + 1);
2423 for (size_t __k = 0; __k <= __nw; ++__k)
2424 _M_int.push_back(__xmin + __k * __delta);
2425
2426 _M_den.reserve(__n);
2427 for (size_t __k = 0; __k < __nw; ++__k)
2428 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2429
2430 _M_initialize();
2431 }
2432
2433 template<typename _RealType>
2434 template<typename _UniformRandomNumberGenerator>
2435 typename piecewise_constant_distribution<_RealType>::result_type
2436 piecewise_constant_distribution<_RealType>::
2437 operator()(_UniformRandomNumberGenerator& __urng,
2438 const param_type& __param)
2439 {
2440 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2441 __aurng(__urng);
2442
2443 const double __p = __aurng();
2444 if (__param._M_cp.empty())
2445 return __p;
2446
2447 auto __pos = std::lower_bound(__param._M_cp.begin(),
2448 __param._M_cp.end(), __p);
2449 const size_t __i = __pos - __param._M_cp.begin();
2450
2451 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2452
2453 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2454 }
2455
2456 template<typename _RealType, typename _CharT, typename _Traits>
2457 std::basic_ostream<_CharT, _Traits>&
2458 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2459 const piecewise_constant_distribution<_RealType>& __x)
2460 {
2461 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2462 typedef typename __ostream_type::ios_base __ios_base;
2463
2464 const typename __ios_base::fmtflags __flags = __os.flags();
2465 const _CharT __fill = __os.fill();
2466 const std::streamsize __precision = __os.precision();
2467 const _CharT __space = __os.widen(' ');
2468 __os.flags(__ios_base::scientific | __ios_base::left);
2469 __os.fill(__space);
2470 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2471
2472 std::vector<_RealType> __int = __x.intervals();
2473 __os << __int.size() - 1;
2474
2475 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2476 __os << __space << *__xit;
2477
2478 std::vector<double> __den = __x.densities();
2479 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2480 __os << __space << *__dit;
2481
2482 __os.flags(__flags);
2483 __os.fill(__fill);
2484 __os.precision(__precision);
2485 return __os;
2486 }
2487
2488 template<typename _RealType, typename _CharT, typename _Traits>
2489 std::basic_istream<_CharT, _Traits>&
2490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2491 piecewise_constant_distribution<_RealType>& __x)
2492 {
2493 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2494 typedef typename __istream_type::ios_base __ios_base;
2495
2496 const typename __ios_base::fmtflags __flags = __is.flags();
2497 __is.flags(__ios_base::dec | __ios_base::skipws);
2498
2499 size_t __n;
2500 __is >> __n;
2501
2502 std::vector<_RealType> __int_vec;
2503 __int_vec.reserve(__n + 1);
2504 for (size_t __i = 0; __i <= __n; ++__i)
2505 {
2506 _RealType __int;
2507 __is >> __int;
2508 __int_vec.push_back(__int);
2509 }
2510
2511 std::vector<double> __den_vec;
2512 __den_vec.reserve(__n);
2513 for (size_t __i = 0; __i < __n; ++__i)
2514 {
2515 double __den;
2516 __is >> __den;
2517 __den_vec.push_back(__den);
2518 }
2519
2520 __x.param(typename piecewise_constant_distribution<_RealType>::
2521 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2522
2523 __is.flags(__flags);
2524 return __is;
2525 }
2526
2527
2528 template<typename _RealType>
2529 void
2530 piecewise_linear_distribution<_RealType>::param_type::
2531 _M_initialize()
2532 {
2533 if (_M_int.size() < 2
2534 || (_M_int.size() == 2
2535 && _M_int[0] == _RealType(0)
2536 && _M_int[1] == _RealType(1)
2537 && _M_den[0] == _M_den[1]))
2538 {
2539 _M_int.clear();
2540 _M_den.clear();
2541 return;
2542 }
2543
2544 double __sum = 0.0;
2545 _M_cp.reserve(_M_int.size() - 1);
2546 _M_m.reserve(_M_int.size() - 1);
2547 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2548 {
2549 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2550 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2551 _M_cp.push_back(__sum);
2552 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2553 }
2554
2555 // Now normalize the densities...
2556 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2557 std::bind2nd(std::divides<double>(), __sum));
2558 // ... and partial sums...
2559 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2560 std::bind2nd(std::divides<double>(), __sum));
2561 // ... and slopes.
2562 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2563 std::bind2nd(std::divides<double>(), __sum));
2564 // Make sure the last cumulative probablility is one.
2565 _M_cp[_M_cp.size() - 1] = 1.0;
2566 }
2567
2568 template<typename _RealType>
2569 template<typename _InputIteratorB, typename _InputIteratorW>
2570 piecewise_linear_distribution<_RealType>::param_type::
2571 param_type(_InputIteratorB __bbegin,
2572 _InputIteratorB __bend,
2573 _InputIteratorW __wbegin)
2574 : _M_int(), _M_den(), _M_cp(), _M_m()
2575 {
2576 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2577 {
2578 _M_int.push_back(*__bbegin);
2579 _M_den.push_back(*__wbegin);
2580 }
2581
2582 _M_initialize();
2583 }
2584
2585 template<typename _RealType>
2586 template<typename _Func>
2587 piecewise_linear_distribution<_RealType>::param_type::
2588 param_type(initializer_list<_RealType> __bl, _Func __fw)
2589 : _M_int(), _M_den(), _M_cp(), _M_m()
2590 {
2591 _M_int.reserve(__bl.size());
2592 _M_den.reserve(__bl.size());
2593 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2594 {
2595 _M_int.push_back(*__biter);
2596 _M_den.push_back(__fw(*__biter));
2597 }
2598
2599 _M_initialize();
2600 }
2601
2602 template<typename _RealType>
2603 template<typename _Func>
2604 piecewise_linear_distribution<_RealType>::param_type::
2605 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2606 : _M_int(), _M_den(), _M_cp(), _M_m()
2607 {
2608 const size_t __n = __nw == 0 ? 1 : __nw;
2609 const _RealType __delta = (__xmax - __xmin) / __n;
2610
2611 _M_int.reserve(__n + 1);
2612 _M_den.reserve(__n + 1);
2613 for (size_t __k = 0; __k <= __nw; ++__k)
2614 {
2615 _M_int.push_back(__xmin + __k * __delta);
2616 _M_den.push_back(__fw(_M_int[__k] + __delta));
2617 }
2618
2619 _M_initialize();
2620 }
2621
2622 template<typename _RealType>
2623 template<typename _UniformRandomNumberGenerator>
2624 typename piecewise_linear_distribution<_RealType>::result_type
2625 piecewise_linear_distribution<_RealType>::
2626 operator()(_UniformRandomNumberGenerator& __urng,
2627 const param_type& __param)
2628 {
2629 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2630 __aurng(__urng);
2631
2632 const double __p = __aurng();
2633 if (__param._M_cp.empty())
2634 return __p;
2635
2636 auto __pos = std::lower_bound(__param._M_cp.begin(),
2637 __param._M_cp.end(), __p);
2638 const size_t __i = __pos - __param._M_cp.begin();
2639
2640 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2641
2642 const double __a = 0.5 * __param._M_m[__i];
2643 const double __b = __param._M_den[__i];
2644 const double __cm = __p - __pref;
2645
2646 _RealType __x = __param._M_int[__i];
2647 if (__a == 0)
2648 __x += __cm / __b;
2649 else
2650 {
2651 const double __d = __b * __b + 4.0 * __a * __cm;
2652 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2653 }
2654
2655 return __x;
2656 }
2657
2658 template<typename _RealType, typename _CharT, typename _Traits>
2659 std::basic_ostream<_CharT, _Traits>&
2660 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2661 const piecewise_linear_distribution<_RealType>& __x)
2662 {
2663 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2664 typedef typename __ostream_type::ios_base __ios_base;
2665
2666 const typename __ios_base::fmtflags __flags = __os.flags();
2667 const _CharT __fill = __os.fill();
2668 const std::streamsize __precision = __os.precision();
2669 const _CharT __space = __os.widen(' ');
2670 __os.flags(__ios_base::scientific | __ios_base::left);
2671 __os.fill(__space);
2672 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2673
2674 std::vector<_RealType> __int = __x.intervals();
2675 __os << __int.size() - 1;
2676
2677 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2678 __os << __space << *__xit;
2679
2680 std::vector<double> __den = __x.densities();
2681 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2682 __os << __space << *__dit;
2683
2684 __os.flags(__flags);
2685 __os.fill(__fill);
2686 __os.precision(__precision);
2687 return __os;
2688 }
2689
2690 template<typename _RealType, typename _CharT, typename _Traits>
2691 std::basic_istream<_CharT, _Traits>&
2692 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2693 piecewise_linear_distribution<_RealType>& __x)
2694 {
2695 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2696 typedef typename __istream_type::ios_base __ios_base;
2697
2698 const typename __ios_base::fmtflags __flags = __is.flags();
2699 __is.flags(__ios_base::dec | __ios_base::skipws);
2700
2701 size_t __n;
2702 __is >> __n;
2703
2704 std::vector<_RealType> __int_vec;
2705 __int_vec.reserve(__n + 1);
2706 for (size_t __i = 0; __i <= __n; ++__i)
2707 {
2708 _RealType __int;
2709 __is >> __int;
2710 __int_vec.push_back(__int);
2711 }
2712
2713 std::vector<double> __den_vec;
2714 __den_vec.reserve(__n + 1);
2715 for (size_t __i = 0; __i <= __n; ++__i)
2716 {
2717 double __den;
2718 __is >> __den;
2719 __den_vec.push_back(__den);
2720 }
2721
2722 __x.param(typename piecewise_linear_distribution<_RealType>::
2723 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2724
2725 __is.flags(__flags);
2726 return __is;
2727 }
2728
2729
2730 template<typename _IntType>
2731 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2732 {
2733 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2734 _M_v.push_back(__detail::__mod<result_type,
2735 __detail::_Shift<result_type, 32>::__value>(*__iter));
2736 }
2737
2738 template<typename _InputIterator>
2739 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2740 {
2741 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2742 _M_v.push_back(__detail::__mod<result_type,
2743 __detail::_Shift<result_type, 32>::__value>(*__iter));
2744 }
2745
2746 template<typename _RandomAccessIterator>
2747 void
2748 seed_seq::generate(_RandomAccessIterator __begin,
2749 _RandomAccessIterator __end)
2750 {
2751 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2752 _Type;
2753
2754 if (__begin == __end)
2755 return;
2756
2757 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2758
2759 const size_t __n = __end - __begin;
2760 const size_t __s = _M_v.size();
2761 const size_t __t = (__n >= 623) ? 11
2762 : (__n >= 68) ? 7
2763 : (__n >= 39) ? 5
2764 : (__n >= 7) ? 3
2765 : (__n - 1) / 2;
2766 const size_t __p = (__n - __t) / 2;
2767 const size_t __q = __p + __t;
2768 const size_t __m = std::max(__s + 1, __n);
2769
2770 for (size_t __k = 0; __k < __m; ++__k)
2771 {
2772 _Type __arg = (__begin[__k % __n]
2773 ^ __begin[(__k + __p) % __n]
2774 ^ __begin[(__k - 1) % __n]);
2775 _Type __r1 = __arg ^ (__arg >> 27);
2776 __r1 = __detail::__mod<_Type,
2777 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
2778 _Type __r2 = __r1;
2779 if (__k == 0)
2780 __r2 += __s;
2781 else if (__k <= __s)
2782 __r2 += __k % __n + _M_v[__k - 1];
2783 else
2784 __r2 += __k % __n;
2785 __r2 = __detail::__mod<_Type,
2786 __detail::_Shift<_Type, 32>::__value>(__r2);
2787 __begin[(__k + __p) % __n] += __r1;
2788 __begin[(__k + __q) % __n] += __r2;
2789 __begin[__k % __n] = __r2;
2790 }
2791
2792 for (size_t __k = __m; __k < __m + __n; ++__k)
2793 {
2794 _Type __arg = (__begin[__k % __n]
2795 + __begin[(__k + __p) % __n]
2796 + __begin[(__k - 1) % __n]);
2797 _Type __r3 = __arg ^ (__arg >> 27);
2798 __r3 = __detail::__mod<_Type,
2799 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
2800 _Type __r4 = __r3 - __k % __n;
2801 __r4 = __detail::__mod<_Type,
2802 __detail::_Shift<_Type, 32>::__value>(__r4);
2803 __begin[(__k + __p) % __n] ^= __r3;
2804 __begin[(__k + __q) % __n] ^= __r4;
2805 __begin[__k % __n] = __r4;
2806 }
2807 }
2808
2809 template<typename _RealType, size_t __bits,
2810 typename _UniformRandomNumberGenerator>
2811 _RealType
2812 generate_canonical(_UniformRandomNumberGenerator& __urng)
2813 {
2814 const size_t __b
2815 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2816 __bits);
2817 const long double __r = static_cast<long double>(__urng.max())
2818 - static_cast<long double>(__urng.min()) + 1.0L;
2819 const size_t __log2r = std::log(__r) / std::log(2.0L);
2820 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2821 _RealType __sum = _RealType(0);
2822 _RealType __tmp = _RealType(1);
2823 for (; __k != 0; --__k)
2824 {
2825 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2826 __tmp *= __r;
2827 }
2828 return __sum / __tmp;
2829 }
2830
2831 _GLIBCXX_END_NAMESPACE_VERSION
2832 } // namespace
2833
2834 #endif