discrete_distribution.h 8.3 KB

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