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