]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/random.tcc
Optimize bulk mode for normal_distribution<double> for SSE3.
[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 void
396 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
397 __s, __b, __t, __c, __l, __f>::
398 _M_gen_rand(void)
399 {
400 const _UIntType __upper_mask = (~_UIntType()) << __r;
401 const _UIntType __lower_mask = ~__upper_mask;
402
403 for (size_t __k = 0; __k < (__n - __m); ++__k)
404 {
405 _UIntType __y = ((_M_x[__k] & __upper_mask)
406 | (_M_x[__k + 1] & __lower_mask));
407 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
408 ^ ((__y & 0x01) ? __a : 0));
409 }
410
411 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
412 {
413 _UIntType __y = ((_M_x[__k] & __upper_mask)
414 | (_M_x[__k + 1] & __lower_mask));
415 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
416 ^ ((__y & 0x01) ? __a : 0));
417 }
418
419 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
420 | (_M_x[0] & __lower_mask));
421 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
422 ^ ((__y & 0x01) ? __a : 0));
423 _M_p = 0;
424 }
425
426 template<typename _UIntType, size_t __w,
427 size_t __n, size_t __m, size_t __r,
428 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
429 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
430 _UIntType __f>
431 void
432 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
433 __s, __b, __t, __c, __l, __f>::
434 discard(unsigned long long __z)
435 {
436 while (__z > state_size - _M_p)
437 {
438 __z -= state_size - _M_p;
439 _M_gen_rand();
440 }
441 _M_p += __z;
442 }
443
444 template<typename _UIntType, size_t __w,
445 size_t __n, size_t __m, size_t __r,
446 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
447 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
448 _UIntType __f>
449 typename
450 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
451 __s, __b, __t, __c, __l, __f>::result_type
452 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
453 __s, __b, __t, __c, __l, __f>::
454 operator()()
455 {
456 // Reload the vector - cost is O(n) amortized over n calls.
457 if (_M_p >= state_size)
458 _M_gen_rand();
459
460 // Calculate o(x(i)).
461 result_type __z = _M_x[_M_p++];
462 __z ^= (__z >> __u) & __d;
463 __z ^= (__z << __s) & __b;
464 __z ^= (__z << __t) & __c;
465 __z ^= (__z >> __l);
466
467 return __z;
468 }
469
470 template<typename _UIntType, size_t __w,
471 size_t __n, size_t __m, size_t __r,
472 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
473 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
474 _UIntType __f, typename _CharT, typename _Traits>
475 std::basic_ostream<_CharT, _Traits>&
476 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
477 const mersenne_twister_engine<_UIntType, __w, __n, __m,
478 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
479 {
480 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
481 typedef typename __ostream_type::ios_base __ios_base;
482
483 const typename __ios_base::fmtflags __flags = __os.flags();
484 const _CharT __fill = __os.fill();
485 const _CharT __space = __os.widen(' ');
486 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
487 __os.fill(__space);
488
489 for (size_t __i = 0; __i < __n; ++__i)
490 __os << __x._M_x[__i] << __space;
491 __os << __x._M_p;
492
493 __os.flags(__flags);
494 __os.fill(__fill);
495 return __os;
496 }
497
498 template<typename _UIntType, size_t __w,
499 size_t __n, size_t __m, size_t __r,
500 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
501 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
502 _UIntType __f, typename _CharT, typename _Traits>
503 std::basic_istream<_CharT, _Traits>&
504 operator>>(std::basic_istream<_CharT, _Traits>& __is,
505 mersenne_twister_engine<_UIntType, __w, __n, __m,
506 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
507 {
508 typedef std::basic_istream<_CharT, _Traits> __istream_type;
509 typedef typename __istream_type::ios_base __ios_base;
510
511 const typename __ios_base::fmtflags __flags = __is.flags();
512 __is.flags(__ios_base::dec | __ios_base::skipws);
513
514 for (size_t __i = 0; __i < __n; ++__i)
515 __is >> __x._M_x[__i];
516 __is >> __x._M_p;
517
518 __is.flags(__flags);
519 return __is;
520 }
521
522
523 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
524 constexpr size_t
525 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
526
527 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
528 constexpr size_t
529 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
530
531 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
532 constexpr size_t
533 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
534
535 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
536 constexpr _UIntType
537 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
538
539 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
540 void
541 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
542 seed(result_type __value)
543 {
544 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
545 __lcg(__value == 0u ? default_seed : __value);
546
547 const size_t __n = (__w + 31) / 32;
548
549 for (size_t __i = 0; __i < long_lag; ++__i)
550 {
551 _UIntType __sum = 0u;
552 _UIntType __factor = 1u;
553 for (size_t __j = 0; __j < __n; ++__j)
554 {
555 __sum += __detail::__mod<uint_least32_t,
556 __detail::_Shift<uint_least32_t, 32>::__value>
557 (__lcg()) * __factor;
558 __factor *= __detail::_Shift<_UIntType, 32>::__value;
559 }
560 _M_x[__i] = __detail::__mod<_UIntType,
561 __detail::_Shift<_UIntType, __w>::__value>(__sum);
562 }
563 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
564 _M_p = 0;
565 }
566
567 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
568 template<typename _Sseq>
569 typename std::enable_if<std::is_class<_Sseq>::value>::type
570 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
571 seed(_Sseq& __q)
572 {
573 const size_t __k = (__w + 31) / 32;
574 uint_least32_t __arr[__r * __k];
575 __q.generate(__arr + 0, __arr + __r * __k);
576
577 for (size_t __i = 0; __i < long_lag; ++__i)
578 {
579 _UIntType __sum = 0u;
580 _UIntType __factor = 1u;
581 for (size_t __j = 0; __j < __k; ++__j)
582 {
583 __sum += __arr[__k * __i + __j] * __factor;
584 __factor *= __detail::_Shift<_UIntType, 32>::__value;
585 }
586 _M_x[__i] = __detail::__mod<_UIntType,
587 __detail::_Shift<_UIntType, __w>::__value>(__sum);
588 }
589 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
590 _M_p = 0;
591 }
592
593 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
594 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
595 result_type
596 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
597 operator()()
598 {
599 // Derive short lag index from current index.
600 long __ps = _M_p - short_lag;
601 if (__ps < 0)
602 __ps += long_lag;
603
604 // Calculate new x(i) without overflow or division.
605 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
606 // cannot overflow.
607 _UIntType __xi;
608 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
609 {
610 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
611 _M_carry = 0;
612 }
613 else
614 {
615 __xi = (__detail::_Shift<_UIntType, __w>::__value
616 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
617 _M_carry = 1;
618 }
619 _M_x[_M_p] = __xi;
620
621 // Adjust current index to loop around in ring buffer.
622 if (++_M_p >= long_lag)
623 _M_p = 0;
624
625 return __xi;
626 }
627
628 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
629 typename _CharT, typename _Traits>
630 std::basic_ostream<_CharT, _Traits>&
631 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
632 const subtract_with_carry_engine<_UIntType,
633 __w, __s, __r>& __x)
634 {
635 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
636 typedef typename __ostream_type::ios_base __ios_base;
637
638 const typename __ios_base::fmtflags __flags = __os.flags();
639 const _CharT __fill = __os.fill();
640 const _CharT __space = __os.widen(' ');
641 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
642 __os.fill(__space);
643
644 for (size_t __i = 0; __i < __r; ++__i)
645 __os << __x._M_x[__i] << __space;
646 __os << __x._M_carry << __space << __x._M_p;
647
648 __os.flags(__flags);
649 __os.fill(__fill);
650 return __os;
651 }
652
653 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
654 typename _CharT, typename _Traits>
655 std::basic_istream<_CharT, _Traits>&
656 operator>>(std::basic_istream<_CharT, _Traits>& __is,
657 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
658 {
659 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
660 typedef typename __istream_type::ios_base __ios_base;
661
662 const typename __ios_base::fmtflags __flags = __is.flags();
663 __is.flags(__ios_base::dec | __ios_base::skipws);
664
665 for (size_t __i = 0; __i < __r; ++__i)
666 __is >> __x._M_x[__i];
667 __is >> __x._M_carry;
668 __is >> __x._M_p;
669
670 __is.flags(__flags);
671 return __is;
672 }
673
674
675 template<typename _RandomNumberEngine, size_t __p, size_t __r>
676 constexpr size_t
677 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
678
679 template<typename _RandomNumberEngine, size_t __p, size_t __r>
680 constexpr size_t
681 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
682
683 template<typename _RandomNumberEngine, size_t __p, size_t __r>
684 typename discard_block_engine<_RandomNumberEngine,
685 __p, __r>::result_type
686 discard_block_engine<_RandomNumberEngine, __p, __r>::
687 operator()()
688 {
689 if (_M_n >= used_block)
690 {
691 _M_b.discard(block_size - _M_n);
692 _M_n = 0;
693 }
694 ++_M_n;
695 return _M_b();
696 }
697
698 template<typename _RandomNumberEngine, size_t __p, size_t __r,
699 typename _CharT, typename _Traits>
700 std::basic_ostream<_CharT, _Traits>&
701 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
702 const discard_block_engine<_RandomNumberEngine,
703 __p, __r>& __x)
704 {
705 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
706 typedef typename __ostream_type::ios_base __ios_base;
707
708 const typename __ios_base::fmtflags __flags = __os.flags();
709 const _CharT __fill = __os.fill();
710 const _CharT __space = __os.widen(' ');
711 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
712 __os.fill(__space);
713
714 __os << __x.base() << __space << __x._M_n;
715
716 __os.flags(__flags);
717 __os.fill(__fill);
718 return __os;
719 }
720
721 template<typename _RandomNumberEngine, size_t __p, size_t __r,
722 typename _CharT, typename _Traits>
723 std::basic_istream<_CharT, _Traits>&
724 operator>>(std::basic_istream<_CharT, _Traits>& __is,
725 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
726 {
727 typedef std::basic_istream<_CharT, _Traits> __istream_type;
728 typedef typename __istream_type::ios_base __ios_base;
729
730 const typename __ios_base::fmtflags __flags = __is.flags();
731 __is.flags(__ios_base::dec | __ios_base::skipws);
732
733 __is >> __x._M_b >> __x._M_n;
734
735 __is.flags(__flags);
736 return __is;
737 }
738
739
740 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
741 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
742 result_type
743 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
744 operator()()
745 {
746 typedef typename _RandomNumberEngine::result_type _Eresult_type;
747 const _Eresult_type __r
748 = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
749 ? _M_b.max() - _M_b.min() + 1 : 0);
750 const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
751 const unsigned __m = __r ? std::__lg(__r) : __edig;
752
753 typedef typename std::common_type<_Eresult_type, result_type>::type
754 __ctype;
755 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
756
757 unsigned __n, __n0;
758 __ctype __s0, __s1, __y0, __y1;
759
760 for (size_t __i = 0; __i < 2; ++__i)
761 {
762 __n = (__w + __m - 1) / __m + __i;
763 __n0 = __n - __w % __n;
764 const unsigned __w0 = __w / __n; // __w0 <= __m
765
766 __s0 = 0;
767 __s1 = 0;
768 if (__w0 < __cdig)
769 {
770 __s0 = __ctype(1) << __w0;
771 __s1 = __s0 << 1;
772 }
773
774 __y0 = 0;
775 __y1 = 0;
776 if (__r)
777 {
778 __y0 = __s0 * (__r / __s0);
779 if (__s1)
780 __y1 = __s1 * (__r / __s1);
781
782 if (__r - __y0 <= __y0 / __n)
783 break;
784 }
785 else
786 break;
787 }
788
789 result_type __sum = 0;
790 for (size_t __k = 0; __k < __n0; ++__k)
791 {
792 __ctype __u;
793 do
794 __u = _M_b() - _M_b.min();
795 while (__y0 && __u >= __y0);
796 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
797 }
798 for (size_t __k = __n0; __k < __n; ++__k)
799 {
800 __ctype __u;
801 do
802 __u = _M_b() - _M_b.min();
803 while (__y1 && __u >= __y1);
804 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
805 }
806 return __sum;
807 }
808
809
810 template<typename _RandomNumberEngine, size_t __k>
811 constexpr size_t
812 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
813
814 template<typename _RandomNumberEngine, size_t __k>
815 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
816 shuffle_order_engine<_RandomNumberEngine, __k>::
817 operator()()
818 {
819 size_t __j = __k * ((_M_y - _M_b.min())
820 / (_M_b.max() - _M_b.min() + 1.0L));
821 _M_y = _M_v[__j];
822 _M_v[__j] = _M_b();
823
824 return _M_y;
825 }
826
827 template<typename _RandomNumberEngine, size_t __k,
828 typename _CharT, typename _Traits>
829 std::basic_ostream<_CharT, _Traits>&
830 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
831 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
832 {
833 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
834 typedef typename __ostream_type::ios_base __ios_base;
835
836 const typename __ios_base::fmtflags __flags = __os.flags();
837 const _CharT __fill = __os.fill();
838 const _CharT __space = __os.widen(' ');
839 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
840 __os.fill(__space);
841
842 __os << __x.base();
843 for (size_t __i = 0; __i < __k; ++__i)
844 __os << __space << __x._M_v[__i];
845 __os << __space << __x._M_y;
846
847 __os.flags(__flags);
848 __os.fill(__fill);
849 return __os;
850 }
851
852 template<typename _RandomNumberEngine, size_t __k,
853 typename _CharT, typename _Traits>
854 std::basic_istream<_CharT, _Traits>&
855 operator>>(std::basic_istream<_CharT, _Traits>& __is,
856 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
857 {
858 typedef std::basic_istream<_CharT, _Traits> __istream_type;
859 typedef typename __istream_type::ios_base __ios_base;
860
861 const typename __ios_base::fmtflags __flags = __is.flags();
862 __is.flags(__ios_base::dec | __ios_base::skipws);
863
864 __is >> __x._M_b;
865 for (size_t __i = 0; __i < __k; ++__i)
866 __is >> __x._M_v[__i];
867 __is >> __x._M_y;
868
869 __is.flags(__flags);
870 return __is;
871 }
872
873
874 template<typename _IntType>
875 template<typename _UniformRandomNumberGenerator>
876 typename uniform_int_distribution<_IntType>::result_type
877 uniform_int_distribution<_IntType>::
878 operator()(_UniformRandomNumberGenerator& __urng,
879 const param_type& __param)
880 {
881 typedef typename _UniformRandomNumberGenerator::result_type
882 _Gresult_type;
883 typedef typename std::make_unsigned<result_type>::type __utype;
884 typedef typename std::common_type<_Gresult_type, __utype>::type
885 __uctype;
886
887 const __uctype __urngmin = __urng.min();
888 const __uctype __urngmax = __urng.max();
889 const __uctype __urngrange = __urngmax - __urngmin;
890 const __uctype __urange
891 = __uctype(__param.b()) - __uctype(__param.a());
892
893 __uctype __ret;
894
895 if (__urngrange > __urange)
896 {
897 // downscaling
898 const __uctype __uerange = __urange + 1; // __urange can be zero
899 const __uctype __scaling = __urngrange / __uerange;
900 const __uctype __past = __uerange * __scaling;
901 do
902 __ret = __uctype(__urng()) - __urngmin;
903 while (__ret >= __past);
904 __ret /= __scaling;
905 }
906 else if (__urngrange < __urange)
907 {
908 // upscaling
909 /*
910 Note that every value in [0, urange]
911 can be written uniquely as
912
913 (urngrange + 1) * high + low
914
915 where
916
917 high in [0, urange / (urngrange + 1)]
918
919 and
920
921 low in [0, urngrange].
922 */
923 __uctype __tmp; // wraparound control
924 do
925 {
926 const __uctype __uerngrange = __urngrange + 1;
927 __tmp = (__uerngrange * operator()
928 (__urng, param_type(0, __urange / __uerngrange)));
929 __ret = __tmp + (__uctype(__urng()) - __urngmin);
930 }
931 while (__ret > __urange || __ret < __tmp);
932 }
933 else
934 __ret = __uctype(__urng()) - __urngmin;
935
936 return __ret + __param.a();
937 }
938
939
940 template<typename _IntType>
941 template<typename _ForwardIterator,
942 typename _UniformRandomNumberGenerator>
943 void
944 uniform_int_distribution<_IntType>::
945 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
946 _UniformRandomNumberGenerator& __urng,
947 const param_type& __param)
948 {
949 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
950 typedef typename _UniformRandomNumberGenerator::result_type
951 _Gresult_type;
952 typedef typename std::make_unsigned<result_type>::type __utype;
953 typedef typename std::common_type<_Gresult_type, __utype>::type
954 __uctype;
955
956 const __uctype __urngmin = __urng.min();
957 const __uctype __urngmax = __urng.max();
958 const __uctype __urngrange = __urngmax - __urngmin;
959 const __uctype __urange
960 = __uctype(__param.b()) - __uctype(__param.a());
961
962 __uctype __ret;
963
964 if (__urngrange > __urange)
965 {
966 if (__detail::_Power_of_2(__urngrange + 1)
967 && __detail::_Power_of_2(__urange + 1))
968 {
969 while (__f != __t)
970 {
971 __ret = __uctype(__urng()) - __urngmin;
972 *__f++ = (__ret & __urange) + __param.a();
973 }
974 }
975 else
976 {
977 // downscaling
978 const __uctype __uerange = __urange + 1; // __urange can be zero
979 const __uctype __scaling = __urngrange / __uerange;
980 const __uctype __past = __uerange * __scaling;
981 while (__f != __t)
982 {
983 do
984 __ret = __uctype(__urng()) - __urngmin;
985 while (__ret >= __past);
986 *__f++ = __ret / __scaling + __param.a();
987 }
988 }
989 }
990 else if (__urngrange < __urange)
991 {
992 // upscaling
993 /*
994 Note that every value in [0, urange]
995 can be written uniquely as
996
997 (urngrange + 1) * high + low
998
999 where
1000
1001 high in [0, urange / (urngrange + 1)]
1002
1003 and
1004
1005 low in [0, urngrange].
1006 */
1007 __uctype __tmp; // wraparound control
1008 while (__f != __t)
1009 {
1010 do
1011 {
1012 const __uctype __uerngrange = __urngrange + 1;
1013 __tmp = (__uerngrange * operator()
1014 (__urng, param_type(0, __urange / __uerngrange)));
1015 __ret = __tmp + (__uctype(__urng()) - __urngmin);
1016 }
1017 while (__ret > __urange || __ret < __tmp);
1018 *__f++ = __ret;
1019 }
1020 }
1021 else
1022 while (__f != __t)
1023 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
1024 }
1025
1026 template<typename _IntType, typename _CharT, typename _Traits>
1027 std::basic_ostream<_CharT, _Traits>&
1028 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1029 const uniform_int_distribution<_IntType>& __x)
1030 {
1031 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1032 typedef typename __ostream_type::ios_base __ios_base;
1033
1034 const typename __ios_base::fmtflags __flags = __os.flags();
1035 const _CharT __fill = __os.fill();
1036 const _CharT __space = __os.widen(' ');
1037 __os.flags(__ios_base::scientific | __ios_base::left);
1038 __os.fill(__space);
1039
1040 __os << __x.a() << __space << __x.b();
1041
1042 __os.flags(__flags);
1043 __os.fill(__fill);
1044 return __os;
1045 }
1046
1047 template<typename _IntType, typename _CharT, typename _Traits>
1048 std::basic_istream<_CharT, _Traits>&
1049 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1050 uniform_int_distribution<_IntType>& __x)
1051 {
1052 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1053 typedef typename __istream_type::ios_base __ios_base;
1054
1055 const typename __ios_base::fmtflags __flags = __is.flags();
1056 __is.flags(__ios_base::dec | __ios_base::skipws);
1057
1058 _IntType __a, __b;
1059 __is >> __a >> __b;
1060 __x.param(typename uniform_int_distribution<_IntType>::
1061 param_type(__a, __b));
1062
1063 __is.flags(__flags);
1064 return __is;
1065 }
1066
1067
1068 template<typename _RealType>
1069 template<typename _ForwardIterator,
1070 typename _UniformRandomNumberGenerator>
1071 void
1072 uniform_real_distribution<_RealType>::
1073 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1074 _UniformRandomNumberGenerator& __urng,
1075 const param_type& __p)
1076 {
1077 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1078 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1079 __aurng(__urng);
1080 auto __range = __p.b() - __p.a();
1081 while (__f != __t)
1082 *__f++ = __aurng() * __range + __p.a();
1083 }
1084
1085 template<typename _RealType, typename _CharT, typename _Traits>
1086 std::basic_ostream<_CharT, _Traits>&
1087 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1088 const uniform_real_distribution<_RealType>& __x)
1089 {
1090 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1091 typedef typename __ostream_type::ios_base __ios_base;
1092
1093 const typename __ios_base::fmtflags __flags = __os.flags();
1094 const _CharT __fill = __os.fill();
1095 const std::streamsize __precision = __os.precision();
1096 const _CharT __space = __os.widen(' ');
1097 __os.flags(__ios_base::scientific | __ios_base::left);
1098 __os.fill(__space);
1099 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1100
1101 __os << __x.a() << __space << __x.b();
1102
1103 __os.flags(__flags);
1104 __os.fill(__fill);
1105 __os.precision(__precision);
1106 return __os;
1107 }
1108
1109 template<typename _RealType, typename _CharT, typename _Traits>
1110 std::basic_istream<_CharT, _Traits>&
1111 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1112 uniform_real_distribution<_RealType>& __x)
1113 {
1114 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1115 typedef typename __istream_type::ios_base __ios_base;
1116
1117 const typename __ios_base::fmtflags __flags = __is.flags();
1118 __is.flags(__ios_base::skipws);
1119
1120 _RealType __a, __b;
1121 __is >> __a >> __b;
1122 __x.param(typename uniform_real_distribution<_RealType>::
1123 param_type(__a, __b));
1124
1125 __is.flags(__flags);
1126 return __is;
1127 }
1128
1129
1130 template<typename _ForwardIterator,
1131 typename _UniformRandomNumberGenerator>
1132 void
1133 std::bernoulli_distribution::
1134 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1135 _UniformRandomNumberGenerator& __urng,
1136 const param_type& __p)
1137 {
1138 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1139 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1140 __aurng(__urng);
1141 auto __limit = __p.p() * (__aurng.max() - __aurng.min());
1142
1143 while (__f != __t)
1144 *__f++ = (__aurng() - __aurng.min()) < __limit;
1145 }
1146
1147 template<typename _CharT, typename _Traits>
1148 std::basic_ostream<_CharT, _Traits>&
1149 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1150 const bernoulli_distribution& __x)
1151 {
1152 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1153 typedef typename __ostream_type::ios_base __ios_base;
1154
1155 const typename __ios_base::fmtflags __flags = __os.flags();
1156 const _CharT __fill = __os.fill();
1157 const std::streamsize __precision = __os.precision();
1158 __os.flags(__ios_base::scientific | __ios_base::left);
1159 __os.fill(__os.widen(' '));
1160 __os.precision(std::numeric_limits<double>::max_digits10);
1161
1162 __os << __x.p();
1163
1164 __os.flags(__flags);
1165 __os.fill(__fill);
1166 __os.precision(__precision);
1167 return __os;
1168 }
1169
1170
1171 template<typename _IntType>
1172 template<typename _UniformRandomNumberGenerator>
1173 typename geometric_distribution<_IntType>::result_type
1174 geometric_distribution<_IntType>::
1175 operator()(_UniformRandomNumberGenerator& __urng,
1176 const param_type& __param)
1177 {
1178 // About the epsilon thing see this thread:
1179 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1180 const double __naf =
1181 (1 - std::numeric_limits<double>::epsilon()) / 2;
1182 // The largest _RealType convertible to _IntType.
1183 const double __thr =
1184 std::numeric_limits<_IntType>::max() + __naf;
1185 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1186 __aurng(__urng);
1187
1188 double __cand;
1189 do
1190 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1191 while (__cand >= __thr);
1192
1193 return result_type(__cand + __naf);
1194 }
1195
1196 template<typename _IntType>
1197 template<typename _ForwardIterator,
1198 typename _UniformRandomNumberGenerator>
1199 void
1200 geometric_distribution<_IntType>::
1201 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1202 _UniformRandomNumberGenerator& __urng,
1203 const param_type& __param)
1204 {
1205 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1206 // About the epsilon thing see this thread:
1207 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1208 const double __naf =
1209 (1 - std::numeric_limits<double>::epsilon()) / 2;
1210 // The largest _RealType convertible to _IntType.
1211 const double __thr =
1212 std::numeric_limits<_IntType>::max() + __naf;
1213 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1214 __aurng(__urng);
1215
1216 while (__f != __t)
1217 {
1218 double __cand;
1219 do
1220 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1221 while (__cand >= __thr);
1222
1223 *__f++ = __cand + __naf;
1224 }
1225 }
1226
1227 template<typename _IntType,
1228 typename _CharT, typename _Traits>
1229 std::basic_ostream<_CharT, _Traits>&
1230 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1231 const geometric_distribution<_IntType>& __x)
1232 {
1233 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1234 typedef typename __ostream_type::ios_base __ios_base;
1235
1236 const typename __ios_base::fmtflags __flags = __os.flags();
1237 const _CharT __fill = __os.fill();
1238 const std::streamsize __precision = __os.precision();
1239 __os.flags(__ios_base::scientific | __ios_base::left);
1240 __os.fill(__os.widen(' '));
1241 __os.precision(std::numeric_limits<double>::max_digits10);
1242
1243 __os << __x.p();
1244
1245 __os.flags(__flags);
1246 __os.fill(__fill);
1247 __os.precision(__precision);
1248 return __os;
1249 }
1250
1251 template<typename _IntType,
1252 typename _CharT, typename _Traits>
1253 std::basic_istream<_CharT, _Traits>&
1254 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1255 geometric_distribution<_IntType>& __x)
1256 {
1257 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1258 typedef typename __istream_type::ios_base __ios_base;
1259
1260 const typename __ios_base::fmtflags __flags = __is.flags();
1261 __is.flags(__ios_base::skipws);
1262
1263 double __p;
1264 __is >> __p;
1265 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1266
1267 __is.flags(__flags);
1268 return __is;
1269 }
1270
1271 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1272 template<typename _IntType>
1273 template<typename _UniformRandomNumberGenerator>
1274 typename negative_binomial_distribution<_IntType>::result_type
1275 negative_binomial_distribution<_IntType>::
1276 operator()(_UniformRandomNumberGenerator& __urng)
1277 {
1278 const double __y = _M_gd(__urng);
1279
1280 // XXX Is the constructor too slow?
1281 std::poisson_distribution<result_type> __poisson(__y);
1282 return __poisson(__urng);
1283 }
1284
1285 template<typename _IntType>
1286 template<typename _UniformRandomNumberGenerator>
1287 typename negative_binomial_distribution<_IntType>::result_type
1288 negative_binomial_distribution<_IntType>::
1289 operator()(_UniformRandomNumberGenerator& __urng,
1290 const param_type& __p)
1291 {
1292 typedef typename std::gamma_distribution<result_type>::param_type
1293 param_type;
1294
1295 const double __y =
1296 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1297
1298 std::poisson_distribution<result_type> __poisson(__y);
1299 return __poisson(__urng);
1300 }
1301
1302 template<typename _IntType>
1303 template<typename _ForwardIterator,
1304 typename _UniformRandomNumberGenerator>
1305 void
1306 negative_binomial_distribution<_IntType>::
1307 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1308 _UniformRandomNumberGenerator& __urng)
1309 {
1310 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1311 while (__f != __t)
1312 {
1313 const double __y = _M_gd(__urng);
1314
1315 // XXX Is the constructor too slow?
1316 std::poisson_distribution<result_type> __poisson(__y);
1317 *__f++ = __poisson(__urng);
1318 }
1319 }
1320
1321 template<typename _IntType>
1322 template<typename _ForwardIterator,
1323 typename _UniformRandomNumberGenerator>
1324 void
1325 negative_binomial_distribution<_IntType>::
1326 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1327 _UniformRandomNumberGenerator& __urng,
1328 const param_type& __p)
1329 {
1330 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1331 typename std::gamma_distribution<result_type>::param_type
1332 __p2(__p.k(), (1.0 - __p.p()) / __p.p());
1333
1334 while (__f != __t)
1335 {
1336 const double __y = _M_gd(__urng, __p2);
1337
1338 std::poisson_distribution<result_type> __poisson(__y);
1339 *__f++ = __poisson(__urng);
1340 }
1341 }
1342
1343 template<typename _IntType, typename _CharT, typename _Traits>
1344 std::basic_ostream<_CharT, _Traits>&
1345 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1346 const negative_binomial_distribution<_IntType>& __x)
1347 {
1348 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1349 typedef typename __ostream_type::ios_base __ios_base;
1350
1351 const typename __ios_base::fmtflags __flags = __os.flags();
1352 const _CharT __fill = __os.fill();
1353 const std::streamsize __precision = __os.precision();
1354 const _CharT __space = __os.widen(' ');
1355 __os.flags(__ios_base::scientific | __ios_base::left);
1356 __os.fill(__os.widen(' '));
1357 __os.precision(std::numeric_limits<double>::max_digits10);
1358
1359 __os << __x.k() << __space << __x.p()
1360 << __space << __x._M_gd;
1361
1362 __os.flags(__flags);
1363 __os.fill(__fill);
1364 __os.precision(__precision);
1365 return __os;
1366 }
1367
1368 template<typename _IntType, typename _CharT, typename _Traits>
1369 std::basic_istream<_CharT, _Traits>&
1370 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1371 negative_binomial_distribution<_IntType>& __x)
1372 {
1373 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1374 typedef typename __istream_type::ios_base __ios_base;
1375
1376 const typename __ios_base::fmtflags __flags = __is.flags();
1377 __is.flags(__ios_base::skipws);
1378
1379 _IntType __k;
1380 double __p;
1381 __is >> __k >> __p >> __x._M_gd;
1382 __x.param(typename negative_binomial_distribution<_IntType>::
1383 param_type(__k, __p));
1384
1385 __is.flags(__flags);
1386 return __is;
1387 }
1388
1389
1390 template<typename _IntType>
1391 void
1392 poisson_distribution<_IntType>::param_type::
1393 _M_initialize()
1394 {
1395 #if _GLIBCXX_USE_C99_MATH_TR1
1396 if (_M_mean >= 12)
1397 {
1398 const double __m = std::floor(_M_mean);
1399 _M_lm_thr = std::log(_M_mean);
1400 _M_lfm = std::lgamma(__m + 1);
1401 _M_sm = std::sqrt(__m);
1402
1403 const double __pi_4 = 0.7853981633974483096156608458198757L;
1404 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1405 / __pi_4));
1406 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1407 const double __cx = 2 * __m + _M_d;
1408 _M_scx = std::sqrt(__cx / 2);
1409 _M_1cx = 1 / __cx;
1410
1411 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1412 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1413 / _M_d;
1414 }
1415 else
1416 #endif
1417 _M_lm_thr = std::exp(-_M_mean);
1418 }
1419
1420 /**
1421 * A rejection algorithm when mean >= 12 and a simple method based
1422 * upon the multiplication of uniform random variates otherwise.
1423 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1424 * is defined.
1425 *
1426 * Reference:
1427 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1428 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1429 */
1430 template<typename _IntType>
1431 template<typename _UniformRandomNumberGenerator>
1432 typename poisson_distribution<_IntType>::result_type
1433 poisson_distribution<_IntType>::
1434 operator()(_UniformRandomNumberGenerator& __urng,
1435 const param_type& __param)
1436 {
1437 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1438 __aurng(__urng);
1439 #if _GLIBCXX_USE_C99_MATH_TR1
1440 if (__param.mean() >= 12)
1441 {
1442 double __x;
1443
1444 // See comments above...
1445 const double __naf =
1446 (1 - std::numeric_limits<double>::epsilon()) / 2;
1447 const double __thr =
1448 std::numeric_limits<_IntType>::max() + __naf;
1449
1450 const double __m = std::floor(__param.mean());
1451 // sqrt(pi / 2)
1452 const double __spi_2 = 1.2533141373155002512078826424055226L;
1453 const double __c1 = __param._M_sm * __spi_2;
1454 const double __c2 = __param._M_c2b + __c1;
1455 const double __c3 = __c2 + 1;
1456 const double __c4 = __c3 + 1;
1457 // e^(1 / 78)
1458 const double __e178 = 1.0129030479320018583185514777512983L;
1459 const double __c5 = __c4 + __e178;
1460 const double __c = __param._M_cb + __c5;
1461 const double __2cx = 2 * (2 * __m + __param._M_d);
1462
1463 bool __reject = true;
1464 do
1465 {
1466 const double __u = __c * __aurng();
1467 const double __e = -std::log(__aurng());
1468
1469 double __w = 0.0;
1470
1471 if (__u <= __c1)
1472 {
1473 const double __n = _M_nd(__urng);
1474 const double __y = -std::abs(__n) * __param._M_sm - 1;
1475 __x = std::floor(__y);
1476 __w = -__n * __n / 2;
1477 if (__x < -__m)
1478 continue;
1479 }
1480 else if (__u <= __c2)
1481 {
1482 const double __n = _M_nd(__urng);
1483 const double __y = 1 + std::abs(__n) * __param._M_scx;
1484 __x = std::ceil(__y);
1485 __w = __y * (2 - __y) * __param._M_1cx;
1486 if (__x > __param._M_d)
1487 continue;
1488 }
1489 else if (__u <= __c3)
1490 // NB: This case not in the book, nor in the Errata,
1491 // but should be ok...
1492 __x = -1;
1493 else if (__u <= __c4)
1494 __x = 0;
1495 else if (__u <= __c5)
1496 __x = 1;
1497 else
1498 {
1499 const double __v = -std::log(__aurng());
1500 const double __y = __param._M_d
1501 + __v * __2cx / __param._M_d;
1502 __x = std::ceil(__y);
1503 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1504 }
1505
1506 __reject = (__w - __e - __x * __param._M_lm_thr
1507 > __param._M_lfm - std::lgamma(__x + __m + 1));
1508
1509 __reject |= __x + __m >= __thr;
1510
1511 } while (__reject);
1512
1513 return result_type(__x + __m + __naf);
1514 }
1515 else
1516 #endif
1517 {
1518 _IntType __x = 0;
1519 double __prod = 1.0;
1520
1521 do
1522 {
1523 __prod *= __aurng();
1524 __x += 1;
1525 }
1526 while (__prod > __param._M_lm_thr);
1527
1528 return __x - 1;
1529 }
1530 }
1531
1532 template<typename _IntType>
1533 template<typename _ForwardIterator,
1534 typename _UniformRandomNumberGenerator>
1535 void
1536 poisson_distribution<_IntType>::
1537 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1538 _UniformRandomNumberGenerator& __urng,
1539 const param_type& __param)
1540 {
1541 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1542 // We could duplicate everything from operator()...
1543 while (__f != __t)
1544 *__f++ = this->operator()(__urng, __param);
1545 }
1546
1547 template<typename _IntType,
1548 typename _CharT, typename _Traits>
1549 std::basic_ostream<_CharT, _Traits>&
1550 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1551 const poisson_distribution<_IntType>& __x)
1552 {
1553 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1554 typedef typename __ostream_type::ios_base __ios_base;
1555
1556 const typename __ios_base::fmtflags __flags = __os.flags();
1557 const _CharT __fill = __os.fill();
1558 const std::streamsize __precision = __os.precision();
1559 const _CharT __space = __os.widen(' ');
1560 __os.flags(__ios_base::scientific | __ios_base::left);
1561 __os.fill(__space);
1562 __os.precision(std::numeric_limits<double>::max_digits10);
1563
1564 __os << __x.mean() << __space << __x._M_nd;
1565
1566 __os.flags(__flags);
1567 __os.fill(__fill);
1568 __os.precision(__precision);
1569 return __os;
1570 }
1571
1572 template<typename _IntType,
1573 typename _CharT, typename _Traits>
1574 std::basic_istream<_CharT, _Traits>&
1575 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1576 poisson_distribution<_IntType>& __x)
1577 {
1578 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1579 typedef typename __istream_type::ios_base __ios_base;
1580
1581 const typename __ios_base::fmtflags __flags = __is.flags();
1582 __is.flags(__ios_base::skipws);
1583
1584 double __mean;
1585 __is >> __mean >> __x._M_nd;
1586 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1587
1588 __is.flags(__flags);
1589 return __is;
1590 }
1591
1592
1593 template<typename _IntType>
1594 void
1595 binomial_distribution<_IntType>::param_type::
1596 _M_initialize()
1597 {
1598 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1599
1600 _M_easy = true;
1601
1602 #if _GLIBCXX_USE_C99_MATH_TR1
1603 if (_M_t * __p12 >= 8)
1604 {
1605 _M_easy = false;
1606 const double __np = std::floor(_M_t * __p12);
1607 const double __pa = __np / _M_t;
1608 const double __1p = 1 - __pa;
1609
1610 const double __pi_4 = 0.7853981633974483096156608458198757L;
1611 const double __d1x =
1612 std::sqrt(__np * __1p * std::log(32 * __np
1613 / (81 * __pi_4 * __1p)));
1614 _M_d1 = std::round(std::max(1.0, __d1x));
1615 const double __d2x =
1616 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1617 / (__pi_4 * __pa)));
1618 _M_d2 = std::round(std::max(1.0, __d2x));
1619
1620 // sqrt(pi / 2)
1621 const double __spi_2 = 1.2533141373155002512078826424055226L;
1622 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1623 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1624 _M_c = 2 * _M_d1 / __np;
1625 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1626 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1627 const double __s1s = _M_s1 * _M_s1;
1628 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1629 * 2 * __s1s / _M_d1
1630 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1631 const double __s2s = _M_s2 * _M_s2;
1632 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1633 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1634 _M_lf = (std::lgamma(__np + 1)
1635 + std::lgamma(_M_t - __np + 1));
1636 _M_lp1p = std::log(__pa / __1p);
1637
1638 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1639 }
1640 else
1641 #endif
1642 _M_q = -std::log(1 - __p12);
1643 }
1644
1645 template<typename _IntType>
1646 template<typename _UniformRandomNumberGenerator>
1647 typename binomial_distribution<_IntType>::result_type
1648 binomial_distribution<_IntType>::
1649 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1650 {
1651 _IntType __x = 0;
1652 double __sum = 0.0;
1653 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1654 __aurng(__urng);
1655
1656 do
1657 {
1658 const double __e = -std::log(__aurng());
1659 __sum += __e / (__t - __x);
1660 __x += 1;
1661 }
1662 while (__sum <= _M_param._M_q);
1663
1664 return __x - 1;
1665 }
1666
1667 /**
1668 * A rejection algorithm when t * p >= 8 and a simple waiting time
1669 * method - the second in the referenced book - otherwise.
1670 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1671 * is defined.
1672 *
1673 * Reference:
1674 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1675 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1676 */
1677 template<typename _IntType>
1678 template<typename _UniformRandomNumberGenerator>
1679 typename binomial_distribution<_IntType>::result_type
1680 binomial_distribution<_IntType>::
1681 operator()(_UniformRandomNumberGenerator& __urng,
1682 const param_type& __param)
1683 {
1684 result_type __ret;
1685 const _IntType __t = __param.t();
1686 const double __p = __param.p();
1687 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1688 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1689 __aurng(__urng);
1690
1691 #if _GLIBCXX_USE_C99_MATH_TR1
1692 if (!__param._M_easy)
1693 {
1694 double __x;
1695
1696 // See comments above...
1697 const double __naf =
1698 (1 - std::numeric_limits<double>::epsilon()) / 2;
1699 const double __thr =
1700 std::numeric_limits<_IntType>::max() + __naf;
1701
1702 const double __np = std::floor(__t * __p12);
1703
1704 // sqrt(pi / 2)
1705 const double __spi_2 = 1.2533141373155002512078826424055226L;
1706 const double __a1 = __param._M_a1;
1707 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1708 const double __a123 = __param._M_a123;
1709 const double __s1s = __param._M_s1 * __param._M_s1;
1710 const double __s2s = __param._M_s2 * __param._M_s2;
1711
1712 bool __reject;
1713 do
1714 {
1715 const double __u = __param._M_s * __aurng();
1716
1717 double __v;
1718
1719 if (__u <= __a1)
1720 {
1721 const double __n = _M_nd(__urng);
1722 const double __y = __param._M_s1 * std::abs(__n);
1723 __reject = __y >= __param._M_d1;
1724 if (!__reject)
1725 {
1726 const double __e = -std::log(__aurng());
1727 __x = std::floor(__y);
1728 __v = -__e - __n * __n / 2 + __param._M_c;
1729 }
1730 }
1731 else if (__u <= __a12)
1732 {
1733 const double __n = _M_nd(__urng);
1734 const double __y = __param._M_s2 * std::abs(__n);
1735 __reject = __y >= __param._M_d2;
1736 if (!__reject)
1737 {
1738 const double __e = -std::log(__aurng());
1739 __x = std::floor(-__y);
1740 __v = -__e - __n * __n / 2;
1741 }
1742 }
1743 else if (__u <= __a123)
1744 {
1745 const double __e1 = -std::log(__aurng());
1746 const double __e2 = -std::log(__aurng());
1747
1748 const double __y = __param._M_d1
1749 + 2 * __s1s * __e1 / __param._M_d1;
1750 __x = std::floor(__y);
1751 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1752 -__y / (2 * __s1s)));
1753 __reject = false;
1754 }
1755 else
1756 {
1757 const double __e1 = -std::log(__aurng());
1758 const double __e2 = -std::log(__aurng());
1759
1760 const double __y = __param._M_d2
1761 + 2 * __s2s * __e1 / __param._M_d2;
1762 __x = std::floor(-__y);
1763 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1764 __reject = false;
1765 }
1766
1767 __reject = __reject || __x < -__np || __x > __t - __np;
1768 if (!__reject)
1769 {
1770 const double __lfx =
1771 std::lgamma(__np + __x + 1)
1772 + std::lgamma(__t - (__np + __x) + 1);
1773 __reject = __v > __param._M_lf - __lfx
1774 + __x * __param._M_lp1p;
1775 }
1776
1777 __reject |= __x + __np >= __thr;
1778 }
1779 while (__reject);
1780
1781 __x += __np + __naf;
1782
1783 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1784 __ret = _IntType(__x) + __z;
1785 }
1786 else
1787 #endif
1788 __ret = _M_waiting(__urng, __t);
1789
1790 if (__p12 != __p)
1791 __ret = __t - __ret;
1792 return __ret;
1793 }
1794
1795 template<typename _IntType>
1796 template<typename _ForwardIterator,
1797 typename _UniformRandomNumberGenerator>
1798 void
1799 binomial_distribution<_IntType>::
1800 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1801 _UniformRandomNumberGenerator& __urng,
1802 const param_type& __param)
1803 {
1804 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1805 // We could duplicate everything from operator()...
1806 while (__f != __t)
1807 *__f++ = this->operator()(__urng, __param);
1808 }
1809
1810 template<typename _IntType,
1811 typename _CharT, typename _Traits>
1812 std::basic_ostream<_CharT, _Traits>&
1813 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1814 const binomial_distribution<_IntType>& __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<double>::max_digits10);
1826
1827 __os << __x.t() << __space << __x.p()
1828 << __space << __x._M_nd;
1829
1830 __os.flags(__flags);
1831 __os.fill(__fill);
1832 __os.precision(__precision);
1833 return __os;
1834 }
1835
1836 template<typename _IntType,
1837 typename _CharT, typename _Traits>
1838 std::basic_istream<_CharT, _Traits>&
1839 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1840 binomial_distribution<_IntType>& __x)
1841 {
1842 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1843 typedef typename __istream_type::ios_base __ios_base;
1844
1845 const typename __ios_base::fmtflags __flags = __is.flags();
1846 __is.flags(__ios_base::dec | __ios_base::skipws);
1847
1848 _IntType __t;
1849 double __p;
1850 __is >> __t >> __p >> __x._M_nd;
1851 __x.param(typename binomial_distribution<_IntType>::
1852 param_type(__t, __p));
1853
1854 __is.flags(__flags);
1855 return __is;
1856 }
1857
1858
1859 template<typename _RealType>
1860 template<typename _ForwardIterator,
1861 typename _UniformRandomNumberGenerator>
1862 void
1863 std::exponential_distribution<_RealType>::
1864 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1865 _UniformRandomNumberGenerator& __urng,
1866 const param_type& __p)
1867 {
1868 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1869 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1870 __aurng(__urng);
1871 while (__f != __t)
1872 *__f++ = -std::log(__aurng()) / __p.lambda();
1873 }
1874
1875 template<typename _RealType, typename _CharT, typename _Traits>
1876 std::basic_ostream<_CharT, _Traits>&
1877 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1878 const exponential_distribution<_RealType>& __x)
1879 {
1880 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1881 typedef typename __ostream_type::ios_base __ios_base;
1882
1883 const typename __ios_base::fmtflags __flags = __os.flags();
1884 const _CharT __fill = __os.fill();
1885 const std::streamsize __precision = __os.precision();
1886 __os.flags(__ios_base::scientific | __ios_base::left);
1887 __os.fill(__os.widen(' '));
1888 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1889
1890 __os << __x.lambda();
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 exponential_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 __lambda;
1910 __is >> __lambda;
1911 __x.param(typename exponential_distribution<_RealType>::
1912 param_type(__lambda));
1913
1914 __is.flags(__flags);
1915 return __is;
1916 }
1917
1918
1919 /**
1920 * Polar method due to Marsaglia.
1921 *
1922 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1923 * New York, 1986, Ch. V, Sect. 4.4.
1924 */
1925 template<typename _RealType>
1926 template<typename _UniformRandomNumberGenerator>
1927 typename normal_distribution<_RealType>::result_type
1928 normal_distribution<_RealType>::
1929 operator()(_UniformRandomNumberGenerator& __urng,
1930 const param_type& __param)
1931 {
1932 result_type __ret;
1933 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1934 __aurng(__urng);
1935
1936 if (_M_saved_available)
1937 {
1938 _M_saved_available = false;
1939 __ret = _M_saved;
1940 }
1941 else
1942 {
1943 result_type __x, __y, __r2;
1944 do
1945 {
1946 __x = result_type(2.0) * __aurng() - 1.0;
1947 __y = result_type(2.0) * __aurng() - 1.0;
1948 __r2 = __x * __x + __y * __y;
1949 }
1950 while (__r2 > 1.0 || __r2 == 0.0);
1951
1952 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1953 _M_saved = __x * __mult;
1954 _M_saved_available = true;
1955 __ret = __y * __mult;
1956 }
1957
1958 __ret = __ret * __param.stddev() + __param.mean();
1959 return __ret;
1960 }
1961
1962 template<typename _RealType>
1963 template<typename _ForwardIterator,
1964 typename _UniformRandomNumberGenerator>
1965 void
1966 normal_distribution<_RealType>::
1967 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1968 _UniformRandomNumberGenerator& __urng,
1969 const param_type& __param)
1970 {
1971 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1972
1973 if (__f == __t)
1974 return;
1975
1976 if (_M_saved_available)
1977 {
1978 _M_saved_available = false;
1979 *__f++ = _M_saved * __param.stddev() + __param.mean();
1980
1981 if (__f == __t)
1982 return;
1983 }
1984
1985 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1986 __aurng(__urng);
1987
1988 while (__f + 1 < __t)
1989 {
1990 result_type __x, __y, __r2;
1991 do
1992 {
1993 __x = result_type(2.0) * __aurng() - 1.0;
1994 __y = result_type(2.0) * __aurng() - 1.0;
1995 __r2 = __x * __x + __y * __y;
1996 }
1997 while (__r2 > 1.0 || __r2 == 0.0);
1998
1999 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
2000 *__f++ = __y * __mult * __param.stddev() + __param.mean();
2001 *__f++ = __x * __mult * __param.stddev() + __param.mean();
2002 }
2003
2004 if (__f != __t)
2005 {
2006 result_type __x, __y, __r2;
2007 do
2008 {
2009 __x = result_type(2.0) * __aurng() - 1.0;
2010 __y = result_type(2.0) * __aurng() - 1.0;
2011 __r2 = __x * __x + __y * __y;
2012 }
2013 while (__r2 > 1.0 || __r2 == 0.0);
2014
2015 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
2016 _M_saved = __x * __mult;
2017 _M_saved_available = true;
2018 *__f = __y * __mult * __param.stddev() + __param.mean();
2019 }
2020 }
2021
2022 template<typename _RealType>
2023 bool
2024 operator==(const std::normal_distribution<_RealType>& __d1,
2025 const std::normal_distribution<_RealType>& __d2)
2026 {
2027 if (__d1._M_param == __d2._M_param
2028 && __d1._M_saved_available == __d2._M_saved_available)
2029 {
2030 if (__d1._M_saved_available
2031 && __d1._M_saved == __d2._M_saved)
2032 return true;
2033 else if(!__d1._M_saved_available)
2034 return true;
2035 else
2036 return false;
2037 }
2038 else
2039 return false;
2040 }
2041
2042 template<typename _RealType, typename _CharT, typename _Traits>
2043 std::basic_ostream<_CharT, _Traits>&
2044 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2045 const normal_distribution<_RealType>& __x)
2046 {
2047 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2048 typedef typename __ostream_type::ios_base __ios_base;
2049
2050 const typename __ios_base::fmtflags __flags = __os.flags();
2051 const _CharT __fill = __os.fill();
2052 const std::streamsize __precision = __os.precision();
2053 const _CharT __space = __os.widen(' ');
2054 __os.flags(__ios_base::scientific | __ios_base::left);
2055 __os.fill(__space);
2056 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2057
2058 __os << __x.mean() << __space << __x.stddev()
2059 << __space << __x._M_saved_available;
2060 if (__x._M_saved_available)
2061 __os << __space << __x._M_saved;
2062
2063 __os.flags(__flags);
2064 __os.fill(__fill);
2065 __os.precision(__precision);
2066 return __os;
2067 }
2068
2069 template<typename _RealType, typename _CharT, typename _Traits>
2070 std::basic_istream<_CharT, _Traits>&
2071 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2072 normal_distribution<_RealType>& __x)
2073 {
2074 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2075 typedef typename __istream_type::ios_base __ios_base;
2076
2077 const typename __ios_base::fmtflags __flags = __is.flags();
2078 __is.flags(__ios_base::dec | __ios_base::skipws);
2079
2080 double __mean, __stddev;
2081 __is >> __mean >> __stddev
2082 >> __x._M_saved_available;
2083 if (__x._M_saved_available)
2084 __is >> __x._M_saved;
2085 __x.param(typename normal_distribution<_RealType>::
2086 param_type(__mean, __stddev));
2087
2088 __is.flags(__flags);
2089 return __is;
2090 }
2091
2092
2093 template<typename _RealType>
2094 template<typename _ForwardIterator,
2095 typename _UniformRandomNumberGenerator>
2096 void
2097 lognormal_distribution<_RealType>::
2098 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2099 _UniformRandomNumberGenerator& __urng,
2100 const param_type& __p)
2101 {
2102 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2103 while (__f != __t)
2104 *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m());
2105 }
2106
2107 template<typename _RealType, typename _CharT, typename _Traits>
2108 std::basic_ostream<_CharT, _Traits>&
2109 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2110 const lognormal_distribution<_RealType>& __x)
2111 {
2112 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2113 typedef typename __ostream_type::ios_base __ios_base;
2114
2115 const typename __ios_base::fmtflags __flags = __os.flags();
2116 const _CharT __fill = __os.fill();
2117 const std::streamsize __precision = __os.precision();
2118 const _CharT __space = __os.widen(' ');
2119 __os.flags(__ios_base::scientific | __ios_base::left);
2120 __os.fill(__space);
2121 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2122
2123 __os << __x.m() << __space << __x.s()
2124 << __space << __x._M_nd;
2125
2126 __os.flags(__flags);
2127 __os.fill(__fill);
2128 __os.precision(__precision);
2129 return __os;
2130 }
2131
2132 template<typename _RealType, typename _CharT, typename _Traits>
2133 std::basic_istream<_CharT, _Traits>&
2134 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2135 lognormal_distribution<_RealType>& __x)
2136 {
2137 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2138 typedef typename __istream_type::ios_base __ios_base;
2139
2140 const typename __ios_base::fmtflags __flags = __is.flags();
2141 __is.flags(__ios_base::dec | __ios_base::skipws);
2142
2143 _RealType __m, __s;
2144 __is >> __m >> __s >> __x._M_nd;
2145 __x.param(typename lognormal_distribution<_RealType>::
2146 param_type(__m, __s));
2147
2148 __is.flags(__flags);
2149 return __is;
2150 }
2151
2152
2153 template<typename _RealType>
2154 template<typename _ForwardIterator,
2155 typename _UniformRandomNumberGenerator>
2156 void
2157 std::chi_squared_distribution<_RealType>::
2158 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2159 _UniformRandomNumberGenerator& __urng,
2160 typename std::gamma_distribution<result_type>::param_type&
2161 __p)
2162 {
2163 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2164 while (__f != __t)
2165 *__f++ = 2 * _M_gd(__urng, __p);
2166 }
2167
2168 template<typename _RealType, typename _CharT, typename _Traits>
2169 std::basic_ostream<_CharT, _Traits>&
2170 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2171 const chi_squared_distribution<_RealType>& __x)
2172 {
2173 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2174 typedef typename __ostream_type::ios_base __ios_base;
2175
2176 const typename __ios_base::fmtflags __flags = __os.flags();
2177 const _CharT __fill = __os.fill();
2178 const std::streamsize __precision = __os.precision();
2179 const _CharT __space = __os.widen(' ');
2180 __os.flags(__ios_base::scientific | __ios_base::left);
2181 __os.fill(__space);
2182 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2183
2184 __os << __x.n() << __space << __x._M_gd;
2185
2186 __os.flags(__flags);
2187 __os.fill(__fill);
2188 __os.precision(__precision);
2189 return __os;
2190 }
2191
2192 template<typename _RealType, typename _CharT, typename _Traits>
2193 std::basic_istream<_CharT, _Traits>&
2194 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2195 chi_squared_distribution<_RealType>& __x)
2196 {
2197 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2198 typedef typename __istream_type::ios_base __ios_base;
2199
2200 const typename __ios_base::fmtflags __flags = __is.flags();
2201 __is.flags(__ios_base::dec | __ios_base::skipws);
2202
2203 _RealType __n;
2204 __is >> __n >> __x._M_gd;
2205 __x.param(typename chi_squared_distribution<_RealType>::
2206 param_type(__n));
2207
2208 __is.flags(__flags);
2209 return __is;
2210 }
2211
2212
2213 template<typename _RealType>
2214 template<typename _UniformRandomNumberGenerator>
2215 typename cauchy_distribution<_RealType>::result_type
2216 cauchy_distribution<_RealType>::
2217 operator()(_UniformRandomNumberGenerator& __urng,
2218 const param_type& __p)
2219 {
2220 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2221 __aurng(__urng);
2222 _RealType __u;
2223 do
2224 __u = __aurng();
2225 while (__u == 0.5);
2226
2227 const _RealType __pi = 3.1415926535897932384626433832795029L;
2228 return __p.a() + __p.b() * std::tan(__pi * __u);
2229 }
2230
2231 template<typename _RealType>
2232 template<typename _ForwardIterator,
2233 typename _UniformRandomNumberGenerator>
2234 void
2235 cauchy_distribution<_RealType>::
2236 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2237 _UniformRandomNumberGenerator& __urng,
2238 const param_type& __p)
2239 {
2240 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2241 const _RealType __pi = 3.1415926535897932384626433832795029L;
2242 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2243 __aurng(__urng);
2244 while (__f != __t)
2245 {
2246 _RealType __u;
2247 do
2248 __u = __aurng();
2249 while (__u == 0.5);
2250
2251 *__f++ = __p.a() + __p.b() * std::tan(__pi * __u);
2252 }
2253 }
2254
2255 template<typename _RealType, typename _CharT, typename _Traits>
2256 std::basic_ostream<_CharT, _Traits>&
2257 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2258 const cauchy_distribution<_RealType>& __x)
2259 {
2260 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2261 typedef typename __ostream_type::ios_base __ios_base;
2262
2263 const typename __ios_base::fmtflags __flags = __os.flags();
2264 const _CharT __fill = __os.fill();
2265 const std::streamsize __precision = __os.precision();
2266 const _CharT __space = __os.widen(' ');
2267 __os.flags(__ios_base::scientific | __ios_base::left);
2268 __os.fill(__space);
2269 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2270
2271 __os << __x.a() << __space << __x.b();
2272
2273 __os.flags(__flags);
2274 __os.fill(__fill);
2275 __os.precision(__precision);
2276 return __os;
2277 }
2278
2279 template<typename _RealType, typename _CharT, typename _Traits>
2280 std::basic_istream<_CharT, _Traits>&
2281 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2282 cauchy_distribution<_RealType>& __x)
2283 {
2284 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2285 typedef typename __istream_type::ios_base __ios_base;
2286
2287 const typename __ios_base::fmtflags __flags = __is.flags();
2288 __is.flags(__ios_base::dec | __ios_base::skipws);
2289
2290 _RealType __a, __b;
2291 __is >> __a >> __b;
2292 __x.param(typename cauchy_distribution<_RealType>::
2293 param_type(__a, __b));
2294
2295 __is.flags(__flags);
2296 return __is;
2297 }
2298
2299
2300 template<typename _RealType>
2301 template<typename _ForwardIterator,
2302 typename _UniformRandomNumberGenerator>
2303 void
2304 std::fisher_f_distribution<_RealType>::
2305 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2306 _UniformRandomNumberGenerator& __urng)
2307 {
2308 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2309 while (__f != __t)
2310 *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()));
2311 }
2312
2313 template<typename _RealType>
2314 template<typename _ForwardIterator,
2315 typename _UniformRandomNumberGenerator>
2316 void
2317 std::fisher_f_distribution<_RealType>::
2318 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2319 _UniformRandomNumberGenerator& __urng,
2320 const param_type& __p)
2321 {
2322 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2323 typedef typename std::gamma_distribution<result_type>::param_type
2324 param_type;
2325 param_type __p1(__p.m() / 2);
2326 param_type __p2(__p.n() / 2);
2327 while (__f != __t)
2328 *__f++ = ((_M_gd_x(__urng, __p1) * n())
2329 / (_M_gd_y(__urng, __p2) * m()));
2330 }
2331
2332 template<typename _RealType, typename _CharT, typename _Traits>
2333 std::basic_ostream<_CharT, _Traits>&
2334 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2335 const fisher_f_distribution<_RealType>& __x)
2336 {
2337 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2338 typedef typename __ostream_type::ios_base __ios_base;
2339
2340 const typename __ios_base::fmtflags __flags = __os.flags();
2341 const _CharT __fill = __os.fill();
2342 const std::streamsize __precision = __os.precision();
2343 const _CharT __space = __os.widen(' ');
2344 __os.flags(__ios_base::scientific | __ios_base::left);
2345 __os.fill(__space);
2346 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2347
2348 __os << __x.m() << __space << __x.n()
2349 << __space << __x._M_gd_x << __space << __x._M_gd_y;
2350
2351 __os.flags(__flags);
2352 __os.fill(__fill);
2353 __os.precision(__precision);
2354 return __os;
2355 }
2356
2357 template<typename _RealType, typename _CharT, typename _Traits>
2358 std::basic_istream<_CharT, _Traits>&
2359 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2360 fisher_f_distribution<_RealType>& __x)
2361 {
2362 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2363 typedef typename __istream_type::ios_base __ios_base;
2364
2365 const typename __ios_base::fmtflags __flags = __is.flags();
2366 __is.flags(__ios_base::dec | __ios_base::skipws);
2367
2368 _RealType __m, __n;
2369 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
2370 __x.param(typename fisher_f_distribution<_RealType>::
2371 param_type(__m, __n));
2372
2373 __is.flags(__flags);
2374 return __is;
2375 }
2376
2377
2378 template<typename _RealType>
2379 template<typename _ForwardIterator,
2380 typename _UniformRandomNumberGenerator>
2381 void
2382 std::student_t_distribution<_RealType>::
2383 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2384 _UniformRandomNumberGenerator& __urng)
2385 {
2386 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2387 while (__f != __t)
2388 *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng));
2389 }
2390
2391 template<typename _RealType>
2392 template<typename _ForwardIterator,
2393 typename _UniformRandomNumberGenerator>
2394 void
2395 std::student_t_distribution<_RealType>::
2396 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2397 _UniformRandomNumberGenerator& __urng,
2398 const param_type& __p)
2399 {
2400 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2401 typename std::gamma_distribution<result_type>::param_type
2402 __p2(__p.n() / 2, 2);
2403 while (__f != __t)
2404 *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2));
2405 }
2406
2407 template<typename _RealType, typename _CharT, typename _Traits>
2408 std::basic_ostream<_CharT, _Traits>&
2409 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2410 const student_t_distribution<_RealType>& __x)
2411 {
2412 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2413 typedef typename __ostream_type::ios_base __ios_base;
2414
2415 const typename __ios_base::fmtflags __flags = __os.flags();
2416 const _CharT __fill = __os.fill();
2417 const std::streamsize __precision = __os.precision();
2418 const _CharT __space = __os.widen(' ');
2419 __os.flags(__ios_base::scientific | __ios_base::left);
2420 __os.fill(__space);
2421 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2422
2423 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
2424
2425 __os.flags(__flags);
2426 __os.fill(__fill);
2427 __os.precision(__precision);
2428 return __os;
2429 }
2430
2431 template<typename _RealType, typename _CharT, typename _Traits>
2432 std::basic_istream<_CharT, _Traits>&
2433 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2434 student_t_distribution<_RealType>& __x)
2435 {
2436 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2437 typedef typename __istream_type::ios_base __ios_base;
2438
2439 const typename __ios_base::fmtflags __flags = __is.flags();
2440 __is.flags(__ios_base::dec | __ios_base::skipws);
2441
2442 _RealType __n;
2443 __is >> __n >> __x._M_nd >> __x._M_gd;
2444 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
2445
2446 __is.flags(__flags);
2447 return __is;
2448 }
2449
2450
2451 template<typename _RealType>
2452 void
2453 gamma_distribution<_RealType>::param_type::
2454 _M_initialize()
2455 {
2456 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2457
2458 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2459 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2460 }
2461
2462 /**
2463 * Marsaglia, G. and Tsang, W. W.
2464 * "A Simple Method for Generating Gamma Variables"
2465 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2466 */
2467 template<typename _RealType>
2468 template<typename _UniformRandomNumberGenerator>
2469 typename gamma_distribution<_RealType>::result_type
2470 gamma_distribution<_RealType>::
2471 operator()(_UniformRandomNumberGenerator& __urng,
2472 const param_type& __param)
2473 {
2474 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2475 __aurng(__urng);
2476
2477 result_type __u, __v, __n;
2478 const result_type __a1 = (__param._M_malpha
2479 - _RealType(1.0) / _RealType(3.0));
2480
2481 do
2482 {
2483 do
2484 {
2485 __n = _M_nd(__urng);
2486 __v = result_type(1.0) + __param._M_a2 * __n;
2487 }
2488 while (__v <= 0.0);
2489
2490 __v = __v * __v * __v;
2491 __u = __aurng();
2492 }
2493 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2494 && (std::log(__u) > (0.5 * __n * __n + __a1
2495 * (1.0 - __v + std::log(__v)))));
2496
2497 if (__param.alpha() == __param._M_malpha)
2498 return __a1 * __v * __param.beta();
2499 else
2500 {
2501 do
2502 __u = __aurng();
2503 while (__u == 0.0);
2504
2505 return (std::pow(__u, result_type(1.0) / __param.alpha())
2506 * __a1 * __v * __param.beta());
2507 }
2508 }
2509
2510 template<typename _RealType>
2511 template<typename _ForwardIterator,
2512 typename _UniformRandomNumberGenerator>
2513 void
2514 gamma_distribution<_RealType>::
2515 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2516 _UniformRandomNumberGenerator& __urng,
2517 const param_type& __param)
2518 {
2519 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2520 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2521 __aurng(__urng);
2522
2523 result_type __u, __v, __n;
2524 const result_type __a1 = (__param._M_malpha
2525 - _RealType(1.0) / _RealType(3.0));
2526
2527 if (__param.alpha() == __param._M_malpha)
2528 while (__f != __t)
2529 {
2530 do
2531 {
2532 do
2533 {
2534 __n = _M_nd(__urng);
2535 __v = result_type(1.0) + __param._M_a2 * __n;
2536 }
2537 while (__v <= 0.0);
2538
2539 __v = __v * __v * __v;
2540 __u = __aurng();
2541 }
2542 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2543 && (std::log(__u) > (0.5 * __n * __n + __a1
2544 * (1.0 - __v + std::log(__v)))));
2545
2546 *__f++ = __a1 * __v * __param.beta();
2547 }
2548 else
2549 while (__f != __t)
2550 {
2551 do
2552 {
2553 do
2554 {
2555 __n = _M_nd(__urng);
2556 __v = result_type(1.0) + __param._M_a2 * __n;
2557 }
2558 while (__v <= 0.0);
2559
2560 __v = __v * __v * __v;
2561 __u = __aurng();
2562 }
2563 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2564 && (std::log(__u) > (0.5 * __n * __n + __a1
2565 * (1.0 - __v + std::log(__v)))));
2566
2567 do
2568 __u = __aurng();
2569 while (__u == 0.0);
2570
2571 *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha())
2572 * __a1 * __v * __param.beta());
2573 }
2574 }
2575
2576 template<typename _RealType, typename _CharT, typename _Traits>
2577 std::basic_ostream<_CharT, _Traits>&
2578 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2579 const gamma_distribution<_RealType>& __x)
2580 {
2581 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2582 typedef typename __ostream_type::ios_base __ios_base;
2583
2584 const typename __ios_base::fmtflags __flags = __os.flags();
2585 const _CharT __fill = __os.fill();
2586 const std::streamsize __precision = __os.precision();
2587 const _CharT __space = __os.widen(' ');
2588 __os.flags(__ios_base::scientific | __ios_base::left);
2589 __os.fill(__space);
2590 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2591
2592 __os << __x.alpha() << __space << __x.beta()
2593 << __space << __x._M_nd;
2594
2595 __os.flags(__flags);
2596 __os.fill(__fill);
2597 __os.precision(__precision);
2598 return __os;
2599 }
2600
2601 template<typename _RealType, typename _CharT, typename _Traits>
2602 std::basic_istream<_CharT, _Traits>&
2603 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2604 gamma_distribution<_RealType>& __x)
2605 {
2606 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2607 typedef typename __istream_type::ios_base __ios_base;
2608
2609 const typename __ios_base::fmtflags __flags = __is.flags();
2610 __is.flags(__ios_base::dec | __ios_base::skipws);
2611
2612 _RealType __alpha_val, __beta_val;
2613 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2614 __x.param(typename gamma_distribution<_RealType>::
2615 param_type(__alpha_val, __beta_val));
2616
2617 __is.flags(__flags);
2618 return __is;
2619 }
2620
2621
2622 template<typename _RealType>
2623 template<typename _UniformRandomNumberGenerator>
2624 typename weibull_distribution<_RealType>::result_type
2625 weibull_distribution<_RealType>::
2626 operator()(_UniformRandomNumberGenerator& __urng,
2627 const param_type& __p)
2628 {
2629 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2630 __aurng(__urng);
2631 return __p.b() * std::pow(-std::log(__aurng()),
2632 result_type(1) / __p.a());
2633 }
2634
2635 template<typename _RealType>
2636 template<typename _ForwardIterator,
2637 typename _UniformRandomNumberGenerator>
2638 void
2639 weibull_distribution<_RealType>::
2640 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2641 _UniformRandomNumberGenerator& __urng,
2642 const param_type& __p)
2643 {
2644 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2645 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2646 __aurng(__urng);
2647 auto inv_a = result_type(1) / __p.a();
2648
2649 while (__f != __t)
2650 *__f++ = __p.b() * std::pow(-std::log(__aurng()), inv_a);
2651 }
2652
2653 template<typename _RealType, typename _CharT, typename _Traits>
2654 std::basic_ostream<_CharT, _Traits>&
2655 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2656 const weibull_distribution<_RealType>& __x)
2657 {
2658 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2659 typedef typename __ostream_type::ios_base __ios_base;
2660
2661 const typename __ios_base::fmtflags __flags = __os.flags();
2662 const _CharT __fill = __os.fill();
2663 const std::streamsize __precision = __os.precision();
2664 const _CharT __space = __os.widen(' ');
2665 __os.flags(__ios_base::scientific | __ios_base::left);
2666 __os.fill(__space);
2667 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2668
2669 __os << __x.a() << __space << __x.b();
2670
2671 __os.flags(__flags);
2672 __os.fill(__fill);
2673 __os.precision(__precision);
2674 return __os;
2675 }
2676
2677 template<typename _RealType, typename _CharT, typename _Traits>
2678 std::basic_istream<_CharT, _Traits>&
2679 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2680 weibull_distribution<_RealType>& __x)
2681 {
2682 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2683 typedef typename __istream_type::ios_base __ios_base;
2684
2685 const typename __ios_base::fmtflags __flags = __is.flags();
2686 __is.flags(__ios_base::dec | __ios_base::skipws);
2687
2688 _RealType __a, __b;
2689 __is >> __a >> __b;
2690 __x.param(typename weibull_distribution<_RealType>::
2691 param_type(__a, __b));
2692
2693 __is.flags(__flags);
2694 return __is;
2695 }
2696
2697
2698 template<typename _RealType>
2699 template<typename _UniformRandomNumberGenerator>
2700 typename extreme_value_distribution<_RealType>::result_type
2701 extreme_value_distribution<_RealType>::
2702 operator()(_UniformRandomNumberGenerator& __urng,
2703 const param_type& __p)
2704 {
2705 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2706 __aurng(__urng);
2707 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2708 }
2709
2710 template<typename _RealType>
2711 template<typename _ForwardIterator,
2712 typename _UniformRandomNumberGenerator>
2713 void
2714 extreme_value_distribution<_RealType>::
2715 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2716 _UniformRandomNumberGenerator& __urng,
2717 const param_type& __p)
2718 {
2719 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2720 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2721 __aurng(__urng);
2722
2723 while (__f != __t)
2724 *__f++ = __p.a() - __p.b() * std::log(-std::log(__aurng()));
2725 }
2726
2727 template<typename _RealType, typename _CharT, typename _Traits>
2728 std::basic_ostream<_CharT, _Traits>&
2729 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2730 const extreme_value_distribution<_RealType>& __x)
2731 {
2732 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2733 typedef typename __ostream_type::ios_base __ios_base;
2734
2735 const typename __ios_base::fmtflags __flags = __os.flags();
2736 const _CharT __fill = __os.fill();
2737 const std::streamsize __precision = __os.precision();
2738 const _CharT __space = __os.widen(' ');
2739 __os.flags(__ios_base::scientific | __ios_base::left);
2740 __os.fill(__space);
2741 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2742
2743 __os << __x.a() << __space << __x.b();
2744
2745 __os.flags(__flags);
2746 __os.fill(__fill);
2747 __os.precision(__precision);
2748 return __os;
2749 }
2750
2751 template<typename _RealType, typename _CharT, typename _Traits>
2752 std::basic_istream<_CharT, _Traits>&
2753 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2754 extreme_value_distribution<_RealType>& __x)
2755 {
2756 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2757 typedef typename __istream_type::ios_base __ios_base;
2758
2759 const typename __ios_base::fmtflags __flags = __is.flags();
2760 __is.flags(__ios_base::dec | __ios_base::skipws);
2761
2762 _RealType __a, __b;
2763 __is >> __a >> __b;
2764 __x.param(typename extreme_value_distribution<_RealType>::
2765 param_type(__a, __b));
2766
2767 __is.flags(__flags);
2768 return __is;
2769 }
2770
2771
2772 template<typename _IntType>
2773 void
2774 discrete_distribution<_IntType>::param_type::
2775 _M_initialize()
2776 {
2777 if (_M_prob.size() < 2)
2778 {
2779 _M_prob.clear();
2780 return;
2781 }
2782
2783 const double __sum = std::accumulate(_M_prob.begin(),
2784 _M_prob.end(), 0.0);
2785 // Now normalize the probabilites.
2786 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2787 std::bind2nd(std::divides<double>(), __sum));
2788 // Accumulate partial sums.
2789 _M_cp.reserve(_M_prob.size());
2790 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2791 std::back_inserter(_M_cp));
2792 // Make sure the last cumulative probability is one.
2793 _M_cp[_M_cp.size() - 1] = 1.0;
2794 }
2795
2796 template<typename _IntType>
2797 template<typename _Func>
2798 discrete_distribution<_IntType>::param_type::
2799 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2800 : _M_prob(), _M_cp()
2801 {
2802 const size_t __n = __nw == 0 ? 1 : __nw;
2803 const double __delta = (__xmax - __xmin) / __n;
2804
2805 _M_prob.reserve(__n);
2806 for (size_t __k = 0; __k < __nw; ++__k)
2807 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2808
2809 _M_initialize();
2810 }
2811
2812 template<typename _IntType>
2813 template<typename _UniformRandomNumberGenerator>
2814 typename discrete_distribution<_IntType>::result_type
2815 discrete_distribution<_IntType>::
2816 operator()(_UniformRandomNumberGenerator& __urng,
2817 const param_type& __param)
2818 {
2819 if (__param._M_cp.empty())
2820 return result_type(0);
2821
2822 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2823 __aurng(__urng);
2824
2825 const double __p = __aurng();
2826 auto __pos = std::lower_bound(__param._M_cp.begin(),
2827 __param._M_cp.end(), __p);
2828
2829 return __pos - __param._M_cp.begin();
2830 }
2831
2832 template<typename _IntType>
2833 template<typename _ForwardIterator,
2834 typename _UniformRandomNumberGenerator>
2835 void
2836 discrete_distribution<_IntType>::
2837 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2838 _UniformRandomNumberGenerator& __urng,
2839 const param_type& __param)
2840 {
2841 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2842
2843 if (__param._M_cp.empty())
2844 {
2845 while (__f != __t)
2846 *__f++ = result_type(0);
2847 return;
2848 }
2849
2850 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2851 __aurng(__urng);
2852
2853 while (__f != __t)
2854 {
2855 const double __p = __aurng();
2856 auto __pos = std::lower_bound(__param._M_cp.begin(),
2857 __param._M_cp.end(), __p);
2858
2859 *__f++ = __pos - __param._M_cp.begin();
2860 }
2861 }
2862
2863 template<typename _IntType, typename _CharT, typename _Traits>
2864 std::basic_ostream<_CharT, _Traits>&
2865 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2866 const discrete_distribution<_IntType>& __x)
2867 {
2868 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2869 typedef typename __ostream_type::ios_base __ios_base;
2870
2871 const typename __ios_base::fmtflags __flags = __os.flags();
2872 const _CharT __fill = __os.fill();
2873 const std::streamsize __precision = __os.precision();
2874 const _CharT __space = __os.widen(' ');
2875 __os.flags(__ios_base::scientific | __ios_base::left);
2876 __os.fill(__space);
2877 __os.precision(std::numeric_limits<double>::max_digits10);
2878
2879 std::vector<double> __prob = __x.probabilities();
2880 __os << __prob.size();
2881 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2882 __os << __space << *__dit;
2883
2884 __os.flags(__flags);
2885 __os.fill(__fill);
2886 __os.precision(__precision);
2887 return __os;
2888 }
2889
2890 template<typename _IntType, typename _CharT, typename _Traits>
2891 std::basic_istream<_CharT, _Traits>&
2892 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2893 discrete_distribution<_IntType>& __x)
2894 {
2895 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2896 typedef typename __istream_type::ios_base __ios_base;
2897
2898 const typename __ios_base::fmtflags __flags = __is.flags();
2899 __is.flags(__ios_base::dec | __ios_base::skipws);
2900
2901 size_t __n;
2902 __is >> __n;
2903
2904 std::vector<double> __prob_vec;
2905 __prob_vec.reserve(__n);
2906 for (; __n != 0; --__n)
2907 {
2908 double __prob;
2909 __is >> __prob;
2910 __prob_vec.push_back(__prob);
2911 }
2912
2913 __x.param(typename discrete_distribution<_IntType>::
2914 param_type(__prob_vec.begin(), __prob_vec.end()));
2915
2916 __is.flags(__flags);
2917 return __is;
2918 }
2919
2920
2921 template<typename _RealType>
2922 void
2923 piecewise_constant_distribution<_RealType>::param_type::
2924 _M_initialize()
2925 {
2926 if (_M_int.size() < 2
2927 || (_M_int.size() == 2
2928 && _M_int[0] == _RealType(0)
2929 && _M_int[1] == _RealType(1)))
2930 {
2931 _M_int.clear();
2932 _M_den.clear();
2933 return;
2934 }
2935
2936 const double __sum = std::accumulate(_M_den.begin(),
2937 _M_den.end(), 0.0);
2938
2939 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2940 std::bind2nd(std::divides<double>(), __sum));
2941
2942 _M_cp.reserve(_M_den.size());
2943 std::partial_sum(_M_den.begin(), _M_den.end(),
2944 std::back_inserter(_M_cp));
2945
2946 // Make sure the last cumulative probability is one.
2947 _M_cp[_M_cp.size() - 1] = 1.0;
2948
2949 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2950 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2951 }
2952
2953 template<typename _RealType>
2954 template<typename _InputIteratorB, typename _InputIteratorW>
2955 piecewise_constant_distribution<_RealType>::param_type::
2956 param_type(_InputIteratorB __bbegin,
2957 _InputIteratorB __bend,
2958 _InputIteratorW __wbegin)
2959 : _M_int(), _M_den(), _M_cp()
2960 {
2961 if (__bbegin != __bend)
2962 {
2963 for (;;)
2964 {
2965 _M_int.push_back(*__bbegin);
2966 ++__bbegin;
2967 if (__bbegin == __bend)
2968 break;
2969
2970 _M_den.push_back(*__wbegin);
2971 ++__wbegin;
2972 }
2973 }
2974
2975 _M_initialize();
2976 }
2977
2978 template<typename _RealType>
2979 template<typename _Func>
2980 piecewise_constant_distribution<_RealType>::param_type::
2981 param_type(initializer_list<_RealType> __bl, _Func __fw)
2982 : _M_int(), _M_den(), _M_cp()
2983 {
2984 _M_int.reserve(__bl.size());
2985 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2986 _M_int.push_back(*__biter);
2987
2988 _M_den.reserve(_M_int.size() - 1);
2989 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2990 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2991
2992 _M_initialize();
2993 }
2994
2995 template<typename _RealType>
2996 template<typename _Func>
2997 piecewise_constant_distribution<_RealType>::param_type::
2998 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2999 : _M_int(), _M_den(), _M_cp()
3000 {
3001 const size_t __n = __nw == 0 ? 1 : __nw;
3002 const _RealType __delta = (__xmax - __xmin) / __n;
3003
3004 _M_int.reserve(__n + 1);
3005 for (size_t __k = 0; __k <= __nw; ++__k)
3006 _M_int.push_back(__xmin + __k * __delta);
3007
3008 _M_den.reserve(__n);
3009 for (size_t __k = 0; __k < __nw; ++__k)
3010 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
3011
3012 _M_initialize();
3013 }
3014
3015 template<typename _RealType>
3016 template<typename _UniformRandomNumberGenerator>
3017 typename piecewise_constant_distribution<_RealType>::result_type
3018 piecewise_constant_distribution<_RealType>::
3019 operator()(_UniformRandomNumberGenerator& __urng,
3020 const param_type& __param)
3021 {
3022 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3023 __aurng(__urng);
3024
3025 const double __p = __aurng();
3026 if (__param._M_cp.empty())
3027 return __p;
3028
3029 auto __pos = std::lower_bound(__param._M_cp.begin(),
3030 __param._M_cp.end(), __p);
3031 const size_t __i = __pos - __param._M_cp.begin();
3032
3033 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3034
3035 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
3036 }
3037
3038 template<typename _RealType>
3039 template<typename _ForwardIterator,
3040 typename _UniformRandomNumberGenerator>
3041 void
3042 piecewise_constant_distribution<_RealType>::
3043 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3044 _UniformRandomNumberGenerator& __urng,
3045 const param_type& __param)
3046 {
3047 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3048 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3049 __aurng(__urng);
3050
3051 if (__param._M_cp.empty())
3052 {
3053 while (__f != __t)
3054 *__f++ = __aurng();
3055 return;
3056 }
3057
3058 while (__f != __t)
3059 {
3060 const double __p = __aurng();
3061
3062 auto __pos = std::lower_bound(__param._M_cp.begin(),
3063 __param._M_cp.end(), __p);
3064 const size_t __i = __pos - __param._M_cp.begin();
3065
3066 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3067
3068 *__f++ = (__param._M_int[__i]
3069 + (__p - __pref) / __param._M_den[__i]);
3070 }
3071 }
3072
3073 template<typename _RealType, typename _CharT, typename _Traits>
3074 std::basic_ostream<_CharT, _Traits>&
3075 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3076 const piecewise_constant_distribution<_RealType>& __x)
3077 {
3078 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
3079 typedef typename __ostream_type::ios_base __ios_base;
3080
3081 const typename __ios_base::fmtflags __flags = __os.flags();
3082 const _CharT __fill = __os.fill();
3083 const std::streamsize __precision = __os.precision();
3084 const _CharT __space = __os.widen(' ');
3085 __os.flags(__ios_base::scientific | __ios_base::left);
3086 __os.fill(__space);
3087 __os.precision(std::numeric_limits<_RealType>::max_digits10);
3088
3089 std::vector<_RealType> __int = __x.intervals();
3090 __os << __int.size() - 1;
3091
3092 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3093 __os << __space << *__xit;
3094
3095 std::vector<double> __den = __x.densities();
3096 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3097 __os << __space << *__dit;
3098
3099 __os.flags(__flags);
3100 __os.fill(__fill);
3101 __os.precision(__precision);
3102 return __os;
3103 }
3104
3105 template<typename _RealType, typename _CharT, typename _Traits>
3106 std::basic_istream<_CharT, _Traits>&
3107 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3108 piecewise_constant_distribution<_RealType>& __x)
3109 {
3110 typedef std::basic_istream<_CharT, _Traits> __istream_type;
3111 typedef typename __istream_type::ios_base __ios_base;
3112
3113 const typename __ios_base::fmtflags __flags = __is.flags();
3114 __is.flags(__ios_base::dec | __ios_base::skipws);
3115
3116 size_t __n;
3117 __is >> __n;
3118
3119 std::vector<_RealType> __int_vec;
3120 __int_vec.reserve(__n + 1);
3121 for (size_t __i = 0; __i <= __n; ++__i)
3122 {
3123 _RealType __int;
3124 __is >> __int;
3125 __int_vec.push_back(__int);
3126 }
3127
3128 std::vector<double> __den_vec;
3129 __den_vec.reserve(__n);
3130 for (size_t __i = 0; __i < __n; ++__i)
3131 {
3132 double __den;
3133 __is >> __den;
3134 __den_vec.push_back(__den);
3135 }
3136
3137 __x.param(typename piecewise_constant_distribution<_RealType>::
3138 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
3139
3140 __is.flags(__flags);
3141 return __is;
3142 }
3143
3144
3145 template<typename _RealType>
3146 void
3147 piecewise_linear_distribution<_RealType>::param_type::
3148 _M_initialize()
3149 {
3150 if (_M_int.size() < 2
3151 || (_M_int.size() == 2
3152 && _M_int[0] == _RealType(0)
3153 && _M_int[1] == _RealType(1)
3154 && _M_den[0] == _M_den[1]))
3155 {
3156 _M_int.clear();
3157 _M_den.clear();
3158 return;
3159 }
3160
3161 double __sum = 0.0;
3162 _M_cp.reserve(_M_int.size() - 1);
3163 _M_m.reserve(_M_int.size() - 1);
3164 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
3165 {
3166 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
3167 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
3168 _M_cp.push_back(__sum);
3169 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
3170 }
3171
3172 // Now normalize the densities...
3173 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
3174 std::bind2nd(std::divides<double>(), __sum));
3175 // ... and partial sums...
3176 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
3177 std::bind2nd(std::divides<double>(), __sum));
3178 // ... and slopes.
3179 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
3180 std::bind2nd(std::divides<double>(), __sum));
3181 // Make sure the last cumulative probablility is one.
3182 _M_cp[_M_cp.size() - 1] = 1.0;
3183 }
3184
3185 template<typename _RealType>
3186 template<typename _InputIteratorB, typename _InputIteratorW>
3187 piecewise_linear_distribution<_RealType>::param_type::
3188 param_type(_InputIteratorB __bbegin,
3189 _InputIteratorB __bend,
3190 _InputIteratorW __wbegin)
3191 : _M_int(), _M_den(), _M_cp(), _M_m()
3192 {
3193 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
3194 {
3195 _M_int.push_back(*__bbegin);
3196 _M_den.push_back(*__wbegin);
3197 }
3198
3199 _M_initialize();
3200 }
3201
3202 template<typename _RealType>
3203 template<typename _Func>
3204 piecewise_linear_distribution<_RealType>::param_type::
3205 param_type(initializer_list<_RealType> __bl, _Func __fw)
3206 : _M_int(), _M_den(), _M_cp(), _M_m()
3207 {
3208 _M_int.reserve(__bl.size());
3209 _M_den.reserve(__bl.size());
3210 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
3211 {
3212 _M_int.push_back(*__biter);
3213 _M_den.push_back(__fw(*__biter));
3214 }
3215
3216 _M_initialize();
3217 }
3218
3219 template<typename _RealType>
3220 template<typename _Func>
3221 piecewise_linear_distribution<_RealType>::param_type::
3222 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
3223 : _M_int(), _M_den(), _M_cp(), _M_m()
3224 {
3225 const size_t __n = __nw == 0 ? 1 : __nw;
3226 const _RealType __delta = (__xmax - __xmin) / __n;
3227
3228 _M_int.reserve(__n + 1);
3229 _M_den.reserve(__n + 1);
3230 for (size_t __k = 0; __k <= __nw; ++__k)
3231 {
3232 _M_int.push_back(__xmin + __k * __delta);
3233 _M_den.push_back(__fw(_M_int[__k] + __delta));
3234 }
3235
3236 _M_initialize();
3237 }
3238
3239 template<typename _RealType>
3240 template<typename _UniformRandomNumberGenerator>
3241 typename piecewise_linear_distribution<_RealType>::result_type
3242 piecewise_linear_distribution<_RealType>::
3243 operator()(_UniformRandomNumberGenerator& __urng,
3244 const param_type& __param)
3245 {
3246 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3247 __aurng(__urng);
3248
3249 const double __p = __aurng();
3250 if (__param._M_cp.empty())
3251 return __p;
3252
3253 auto __pos = std::lower_bound(__param._M_cp.begin(),
3254 __param._M_cp.end(), __p);
3255 const size_t __i = __pos - __param._M_cp.begin();
3256
3257 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3258
3259 const double __a = 0.5 * __param._M_m[__i];
3260 const double __b = __param._M_den[__i];
3261 const double __cm = __p - __pref;
3262
3263 _RealType __x = __param._M_int[__i];
3264 if (__a == 0)
3265 __x += __cm / __b;
3266 else
3267 {
3268 const double __d = __b * __b + 4.0 * __a * __cm;
3269 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
3270 }
3271
3272 return __x;
3273 }
3274
3275 template<typename _RealType>
3276 template<typename _ForwardIterator,
3277 typename _UniformRandomNumberGenerator>
3278 void
3279 piecewise_linear_distribution<_RealType>::
3280 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3281 _UniformRandomNumberGenerator& __urng,
3282 const param_type& __param)
3283 {
3284 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3285 // We could duplicate everything from operator()...
3286 while (__f != __t)
3287 *__f++ = this->operator()(__urng, __param);
3288 }
3289
3290 template<typename _RealType, typename _CharT, typename _Traits>
3291 std::basic_ostream<_CharT, _Traits>&
3292 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3293 const piecewise_linear_distribution<_RealType>& __x)
3294 {
3295 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
3296 typedef typename __ostream_type::ios_base __ios_base;
3297
3298 const typename __ios_base::fmtflags __flags = __os.flags();
3299 const _CharT __fill = __os.fill();
3300 const std::streamsize __precision = __os.precision();
3301 const _CharT __space = __os.widen(' ');
3302 __os.flags(__ios_base::scientific | __ios_base::left);
3303 __os.fill(__space);
3304 __os.precision(std::numeric_limits<_RealType>::max_digits10);
3305
3306 std::vector<_RealType> __int = __x.intervals();
3307 __os << __int.size() - 1;
3308
3309 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3310 __os << __space << *__xit;
3311
3312 std::vector<double> __den = __x.densities();
3313 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3314 __os << __space << *__dit;
3315
3316 __os.flags(__flags);
3317 __os.fill(__fill);
3318 __os.precision(__precision);
3319 return __os;
3320 }
3321
3322 template<typename _RealType, typename _CharT, typename _Traits>
3323 std::basic_istream<_CharT, _Traits>&
3324 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3325 piecewise_linear_distribution<_RealType>& __x)
3326 {
3327 typedef std::basic_istream<_CharT, _Traits> __istream_type;
3328 typedef typename __istream_type::ios_base __ios_base;
3329
3330 const typename __ios_base::fmtflags __flags = __is.flags();
3331 __is.flags(__ios_base::dec | __ios_base::skipws);
3332
3333 size_t __n;
3334 __is >> __n;
3335
3336 std::vector<_RealType> __int_vec;
3337 __int_vec.reserve(__n + 1);
3338 for (size_t __i = 0; __i <= __n; ++__i)
3339 {
3340 _RealType __int;
3341 __is >> __int;
3342 __int_vec.push_back(__int);
3343 }
3344
3345 std::vector<double> __den_vec;
3346 __den_vec.reserve(__n + 1);
3347 for (size_t __i = 0; __i <= __n; ++__i)
3348 {
3349 double __den;
3350 __is >> __den;
3351 __den_vec.push_back(__den);
3352 }
3353
3354 __x.param(typename piecewise_linear_distribution<_RealType>::
3355 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
3356
3357 __is.flags(__flags);
3358 return __is;
3359 }
3360
3361
3362 template<typename _IntType>
3363 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
3364 {
3365 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
3366 _M_v.push_back(__detail::__mod<result_type,
3367 __detail::_Shift<result_type, 32>::__value>(*__iter));
3368 }
3369
3370 template<typename _InputIterator>
3371 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
3372 {
3373 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
3374 _M_v.push_back(__detail::__mod<result_type,
3375 __detail::_Shift<result_type, 32>::__value>(*__iter));
3376 }
3377
3378 template<typename _RandomAccessIterator>
3379 void
3380 seed_seq::generate(_RandomAccessIterator __begin,
3381 _RandomAccessIterator __end)
3382 {
3383 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3384 _Type;
3385
3386 if (__begin == __end)
3387 return;
3388
3389 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
3390
3391 const size_t __n = __end - __begin;
3392 const size_t __s = _M_v.size();
3393 const size_t __t = (__n >= 623) ? 11
3394 : (__n >= 68) ? 7
3395 : (__n >= 39) ? 5
3396 : (__n >= 7) ? 3
3397 : (__n - 1) / 2;
3398 const size_t __p = (__n - __t) / 2;
3399 const size_t __q = __p + __t;
3400 const size_t __m = std::max(size_t(__s + 1), __n);
3401
3402 for (size_t __k = 0; __k < __m; ++__k)
3403 {
3404 _Type __arg = (__begin[__k % __n]
3405 ^ __begin[(__k + __p) % __n]
3406 ^ __begin[(__k - 1) % __n]);
3407 _Type __r1 = __arg ^ (__arg >> 27);
3408 __r1 = __detail::__mod<_Type,
3409 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
3410 _Type __r2 = __r1;
3411 if (__k == 0)
3412 __r2 += __s;
3413 else if (__k <= __s)
3414 __r2 += __k % __n + _M_v[__k - 1];
3415 else
3416 __r2 += __k % __n;
3417 __r2 = __detail::__mod<_Type,
3418 __detail::_Shift<_Type, 32>::__value>(__r2);
3419 __begin[(__k + __p) % __n] += __r1;
3420 __begin[(__k + __q) % __n] += __r2;
3421 __begin[__k % __n] = __r2;
3422 }
3423
3424 for (size_t __k = __m; __k < __m + __n; ++__k)
3425 {
3426 _Type __arg = (__begin[__k % __n]
3427 + __begin[(__k + __p) % __n]
3428 + __begin[(__k - 1) % __n]);
3429 _Type __r3 = __arg ^ (__arg >> 27);
3430 __r3 = __detail::__mod<_Type,
3431 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
3432 _Type __r4 = __r3 - __k % __n;
3433 __r4 = __detail::__mod<_Type,
3434 __detail::_Shift<_Type, 32>::__value>(__r4);
3435 __begin[(__k + __p) % __n] ^= __r3;
3436 __begin[(__k + __q) % __n] ^= __r4;
3437 __begin[__k % __n] = __r4;
3438 }
3439 }
3440
3441 template<typename _RealType, size_t __bits,
3442 typename _UniformRandomNumberGenerator>
3443 _RealType
3444 generate_canonical(_UniformRandomNumberGenerator& __urng)
3445 {
3446 const size_t __b
3447 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
3448 __bits);
3449 const long double __r = static_cast<long double>(__urng.max())
3450 - static_cast<long double>(__urng.min()) + 1.0L;
3451 const size_t __log2r = std::log(__r) / std::log(2.0L);
3452 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
3453 _RealType __sum = _RealType(0);
3454 _RealType __tmp = _RealType(1);
3455 for (; __k != 0; --__k)
3456 {
3457 __sum += _RealType(__urng() - __urng.min()) * __tmp;
3458 __tmp *= __r;
3459 }
3460 return __sum / __tmp;
3461 }
3462
3463 _GLIBCXX_END_NAMESPACE_VERSION
3464 } // namespace
3465
3466 #endif