You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3317 lines
101KB

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