discrete_distribution.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
  27. public:
  28. // types
  29. typedef _IntType result_type;
  30. class _LIBCPP_TEMPLATE_VIS param_type {
  31. vector<double> __p_;
  32. public:
  33. typedef discrete_distribution distribution_type;
  34. _LIBCPP_HIDE_FROM_ABI param_type() {}
  35. template <class _InputIterator>
  36. _LIBCPP_HIDE_FROM_ABI param_type(_InputIterator __f, _InputIterator __l) : __p_(__f, __l) {
  37. __init();
  38. }
  39. #ifndef _LIBCPP_CXX03_LANG
  40. _LIBCPP_HIDE_FROM_ABI param_type(initializer_list<double> __wl) : __p_(__wl.begin(), __wl.end()) { __init(); }
  41. #endif // _LIBCPP_CXX03_LANG
  42. template <class _UnaryOperation>
  43. _LIBCPP_HIDE_FROM_ABI param_type(size_t __nw, double __xmin, double __xmax, _UnaryOperation __fw);
  44. _LIBCPP_HIDE_FROM_ABI vector<double> probabilities() const;
  45. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
  46. return __x.__p_ == __y.__p_;
  47. }
  48. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
  49. private:
  50. _LIBCPP_HIDE_FROM_ABI void __init();
  51. friend class discrete_distribution;
  52. template <class _CharT, class _Traits, class _IT>
  53. friend basic_ostream<_CharT, _Traits>&
  54. operator<<(basic_ostream<_CharT, _Traits>& __os, const discrete_distribution<_IT>& __x);
  55. template <class _CharT, class _Traits, class _IT>
  56. friend basic_istream<_CharT, _Traits>&
  57. operator>>(basic_istream<_CharT, _Traits>& __is, discrete_distribution<_IT>& __x);
  58. };
  59. private:
  60. param_type __p_;
  61. public:
  62. // constructor and reset functions
  63. _LIBCPP_HIDE_FROM_ABI discrete_distribution() {}
  64. template <class _InputIterator>
  65. _LIBCPP_HIDE_FROM_ABI discrete_distribution(_InputIterator __f, _InputIterator __l) : __p_(__f, __l) {}
  66. #ifndef _LIBCPP_CXX03_LANG
  67. _LIBCPP_HIDE_FROM_ABI discrete_distribution(initializer_list<double> __wl) : __p_(__wl) {}
  68. #endif // _LIBCPP_CXX03_LANG
  69. template <class _UnaryOperation>
  70. _LIBCPP_HIDE_FROM_ABI discrete_distribution(size_t __nw, double __xmin, double __xmax, _UnaryOperation __fw)
  71. : __p_(__nw, __xmin, __xmax, __fw) {}
  72. _LIBCPP_HIDE_FROM_ABI explicit discrete_distribution(const param_type& __p) : __p_(__p) {}
  73. _LIBCPP_HIDE_FROM_ABI void reset() {}
  74. // generating functions
  75. template <class _URNG>
  76. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
  77. return (*this)(__g, __p_);
  78. }
  79. template <class _URNG>
  80. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  81. // property functions
  82. _LIBCPP_HIDE_FROM_ABI vector<double> probabilities() const { return __p_.probabilities(); }
  83. _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
  84. _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
  85. _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
  86. _LIBCPP_HIDE_FROM_ABI result_type max() const { return __p_.__p_.size(); }
  87. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const discrete_distribution& __x, const discrete_distribution& __y) {
  88. return __x.__p_ == __y.__p_;
  89. }
  90. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const discrete_distribution& __x, const discrete_distribution& __y) {
  91. return !(__x == __y);
  92. }
  93. template <class _CharT, class _Traits, class _IT>
  94. friend basic_ostream<_CharT, _Traits>&
  95. operator<<(basic_ostream<_CharT, _Traits>& __os, const discrete_distribution<_IT>& __x);
  96. template <class _CharT, class _Traits, class _IT>
  97. friend basic_istream<_CharT, _Traits>&
  98. operator>>(basic_istream<_CharT, _Traits>& __is, discrete_distribution<_IT>& __x);
  99. };
  100. template <class _IntType>
  101. template <class _UnaryOperation>
  102. discrete_distribution<_IntType>::param_type::param_type(
  103. size_t __nw, double __xmin, double __xmax, _UnaryOperation __fw) {
  104. if (__nw > 1) {
  105. __p_.reserve(__nw - 1);
  106. double __d = (__xmax - __xmin) / __nw;
  107. double __d2 = __d / 2;
  108. for (size_t __k = 0; __k < __nw; ++__k)
  109. __p_.push_back(__fw(__xmin + __k * __d + __d2));
  110. __init();
  111. }
  112. }
  113. template <class _IntType>
  114. void discrete_distribution<_IntType>::param_type::__init() {
  115. if (!__p_.empty()) {
  116. if (__p_.size() > 1) {
  117. double __s = std::accumulate(__p_.begin(), __p_.end(), 0.0);
  118. for (vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); __i < __e; ++__i)
  119. *__i /= __s;
  120. vector<double> __t(__p_.size() - 1);
  121. std::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
  122. swap(__p_, __t);
  123. } else {
  124. __p_.clear();
  125. __p_.shrink_to_fit();
  126. }
  127. }
  128. }
  129. template <class _IntType>
  130. vector<double> discrete_distribution<_IntType>::param_type::probabilities() const {
  131. size_t __n = __p_.size();
  132. vector<double> __p(__n + 1);
  133. std::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
  134. if (__n > 0)
  135. __p[__n] = 1 - __p_[__n - 1];
  136. else
  137. __p[0] = 1;
  138. return __p;
  139. }
  140. template <class _IntType>
  141. template <class _URNG>
  142. _IntType discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) {
  143. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  144. uniform_real_distribution<double> __gen;
  145. return static_cast<_IntType>(std::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - __p.__p_.begin());
  146. }
  147. template <class _CharT, class _Traits, class _IT>
  148. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  149. operator<<(basic_ostream<_CharT, _Traits>& __os, const discrete_distribution<_IT>& __x) {
  150. __save_flags<_CharT, _Traits> __lx(__os);
  151. typedef basic_ostream<_CharT, _Traits> _OStream;
  152. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
  153. _CharT __sp = __os.widen(' ');
  154. __os.fill(__sp);
  155. size_t __n = __x.__p_.__p_.size();
  156. __os << __n;
  157. for (size_t __i = 0; __i < __n; ++__i)
  158. __os << __sp << __x.__p_.__p_[__i];
  159. return __os;
  160. }
  161. template <class _CharT, class _Traits, class _IT>
  162. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  163. operator>>(basic_istream<_CharT, _Traits>& __is, discrete_distribution<_IT>& __x) {
  164. __save_flags<_CharT, _Traits> __lx(__is);
  165. typedef basic_istream<_CharT, _Traits> _Istream;
  166. __is.flags(_Istream::dec | _Istream::skipws);
  167. size_t __n;
  168. __is >> __n;
  169. vector<double> __p(__n);
  170. for (size_t __i = 0; __i < __n; ++__i)
  171. __is >> __p[__i];
  172. if (!__is.fail())
  173. swap(__x.__p_.__p_, __p);
  174. return __is;
  175. }
  176. _LIBCPP_END_NAMESPACE_STD
  177. _LIBCPP_POP_MACROS
  178. #endif // _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H