discrete_distribution.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H
  10. #include <__algorithm/upper_bound.h>
  11. #include <__config>
  12. #include <__random/uniform_real_distribution.h>
  13. #include <cstddef>
  14. #include <iosfwd>
  15. #include <numeric>
  16. #include <vector>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template<class _IntType = int>
  24. class _LIBCPP_TEMPLATE_VIS discrete_distribution
  25. {
  26. public:
  27. // types
  28. typedef _IntType result_type;
  29. class _LIBCPP_TEMPLATE_VIS param_type
  30. {
  31. vector<double> __p_;
  32. public:
  33. typedef discrete_distribution distribution_type;
  34. _LIBCPP_INLINE_VISIBILITY
  35. param_type() {}
  36. template<class _InputIterator>
  37. _LIBCPP_INLINE_VISIBILITY
  38. param_type(_InputIterator __f, _InputIterator __l)
  39. : __p_(__f, __l) {__init();}
  40. #ifndef _LIBCPP_CXX03_LANG
  41. _LIBCPP_INLINE_VISIBILITY
  42. param_type(initializer_list<double> __wl)
  43. : __p_(__wl.begin(), __wl.end()) {__init();}
  44. #endif // _LIBCPP_CXX03_LANG
  45. template<class _UnaryOperation>
  46. param_type(size_t __nw, double __xmin, double __xmax,
  47. _UnaryOperation __fw);
  48. vector<double> probabilities() const;
  49. friend _LIBCPP_INLINE_VISIBILITY
  50. bool operator==(const param_type& __x, const param_type& __y)
  51. {return __x.__p_ == __y.__p_;}
  52. friend _LIBCPP_INLINE_VISIBILITY
  53. bool operator!=(const param_type& __x, const param_type& __y)
  54. {return !(__x == __y);}
  55. private:
  56. void __init();
  57. friend class discrete_distribution;
  58. template <class _CharT, class _Traits, class _IT>
  59. friend
  60. basic_ostream<_CharT, _Traits>&
  61. operator<<(basic_ostream<_CharT, _Traits>& __os,
  62. const discrete_distribution<_IT>& __x);
  63. template <class _CharT, class _Traits, class _IT>
  64. friend
  65. basic_istream<_CharT, _Traits>&
  66. operator>>(basic_istream<_CharT, _Traits>& __is,
  67. discrete_distribution<_IT>& __x);
  68. };
  69. private:
  70. param_type __p_;
  71. public:
  72. // constructor and reset functions
  73. _LIBCPP_INLINE_VISIBILITY
  74. discrete_distribution() {}
  75. template<class _InputIterator>
  76. _LIBCPP_INLINE_VISIBILITY
  77. discrete_distribution(_InputIterator __f, _InputIterator __l)
  78. : __p_(__f, __l) {}
  79. #ifndef _LIBCPP_CXX03_LANG
  80. _LIBCPP_INLINE_VISIBILITY
  81. discrete_distribution(initializer_list<double> __wl)
  82. : __p_(__wl) {}
  83. #endif // _LIBCPP_CXX03_LANG
  84. template<class _UnaryOperation>
  85. _LIBCPP_INLINE_VISIBILITY
  86. discrete_distribution(size_t __nw, double __xmin, double __xmax,
  87. _UnaryOperation __fw)
  88. : __p_(__nw, __xmin, __xmax, __fw) {}
  89. _LIBCPP_INLINE_VISIBILITY
  90. explicit discrete_distribution(const param_type& __p)
  91. : __p_(__p) {}
  92. _LIBCPP_INLINE_VISIBILITY
  93. void reset() {}
  94. // generating functions
  95. template<class _URNG>
  96. _LIBCPP_INLINE_VISIBILITY
  97. result_type operator()(_URNG& __g)
  98. {return (*this)(__g, __p_);}
  99. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  100. // property functions
  101. _LIBCPP_INLINE_VISIBILITY
  102. vector<double> probabilities() const {return __p_.probabilities();}
  103. _LIBCPP_INLINE_VISIBILITY
  104. param_type param() const {return __p_;}
  105. _LIBCPP_INLINE_VISIBILITY
  106. void param(const param_type& __p) {__p_ = __p;}
  107. _LIBCPP_INLINE_VISIBILITY
  108. result_type min() const {return 0;}
  109. _LIBCPP_INLINE_VISIBILITY
  110. result_type max() const {return __p_.__p_.size();}
  111. friend _LIBCPP_INLINE_VISIBILITY
  112. bool operator==(const discrete_distribution& __x,
  113. const discrete_distribution& __y)
  114. {return __x.__p_ == __y.__p_;}
  115. friend _LIBCPP_INLINE_VISIBILITY
  116. bool operator!=(const discrete_distribution& __x,
  117. const discrete_distribution& __y)
  118. {return !(__x == __y);}
  119. template <class _CharT, class _Traits, class _IT>
  120. friend
  121. basic_ostream<_CharT, _Traits>&
  122. operator<<(basic_ostream<_CharT, _Traits>& __os,
  123. const discrete_distribution<_IT>& __x);
  124. template <class _CharT, class _Traits, class _IT>
  125. friend
  126. basic_istream<_CharT, _Traits>&
  127. operator>>(basic_istream<_CharT, _Traits>& __is,
  128. discrete_distribution<_IT>& __x);
  129. };
  130. template<class _IntType>
  131. template<class _UnaryOperation>
  132. discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
  133. double __xmin,
  134. double __xmax,
  135. _UnaryOperation __fw)
  136. {
  137. if (__nw > 1)
  138. {
  139. __p_.reserve(__nw - 1);
  140. double __d = (__xmax - __xmin) / __nw;
  141. double __d2 = __d / 2;
  142. for (size_t __k = 0; __k < __nw; ++__k)
  143. __p_.push_back(__fw(__xmin + __k * __d + __d2));
  144. __init();
  145. }
  146. }
  147. template<class _IntType>
  148. void
  149. discrete_distribution<_IntType>::param_type::__init()
  150. {
  151. if (!__p_.empty())
  152. {
  153. if (__p_.size() > 1)
  154. {
  155. double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
  156. for (vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); __i < __e; ++__i)
  157. *__i /= __s;
  158. vector<double> __t(__p_.size() - 1);
  159. _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
  160. swap(__p_, __t);
  161. }
  162. else
  163. {
  164. __p_.clear();
  165. __p_.shrink_to_fit();
  166. }
  167. }
  168. }
  169. template<class _IntType>
  170. vector<double>
  171. discrete_distribution<_IntType>::param_type::probabilities() const
  172. {
  173. size_t __n = __p_.size();
  174. vector<double> __p(__n+1);
  175. _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
  176. if (__n > 0)
  177. __p[__n] = 1 - __p_[__n-1];
  178. else
  179. __p[0] = 1;
  180. return __p;
  181. }
  182. template<class _IntType>
  183. template<class _URNG>
  184. _IntType
  185. discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
  186. {
  187. uniform_real_distribution<double> __gen;
  188. return static_cast<_IntType>(
  189. _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
  190. __p.__p_.begin());
  191. }
  192. template <class _CharT, class _Traits, class _IT>
  193. basic_ostream<_CharT, _Traits>&
  194. operator<<(basic_ostream<_CharT, _Traits>& __os,
  195. const discrete_distribution<_IT>& __x)
  196. {
  197. __save_flags<_CharT, _Traits> __lx(__os);
  198. typedef basic_ostream<_CharT, _Traits> _OStream;
  199. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  200. _OStream::scientific);
  201. _CharT __sp = __os.widen(' ');
  202. __os.fill(__sp);
  203. size_t __n = __x.__p_.__p_.size();
  204. __os << __n;
  205. for (size_t __i = 0; __i < __n; ++__i)
  206. __os << __sp << __x.__p_.__p_[__i];
  207. return __os;
  208. }
  209. template <class _CharT, class _Traits, class _IT>
  210. basic_istream<_CharT, _Traits>&
  211. operator>>(basic_istream<_CharT, _Traits>& __is,
  212. discrete_distribution<_IT>& __x)
  213. {
  214. __save_flags<_CharT, _Traits> __lx(__is);
  215. typedef basic_istream<_CharT, _Traits> _Istream;
  216. __is.flags(_Istream::dec | _Istream::skipws);
  217. size_t __n;
  218. __is >> __n;
  219. vector<double> __p(__n);
  220. for (size_t __i = 0; __i < __n; ++__i)
  221. __is >> __p[__i];
  222. if (!__is.fail())
  223. swap(__x.__p_.__p_, __p);
  224. return __is;
  225. }
  226. _LIBCPP_END_NAMESPACE_STD
  227. _LIBCPP_POP_MACROS
  228. #endif // _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H