File indexing completed on 2026-05-03 08:14:00
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
0011
0012 #include <__algorithm/upper_bound.h>
0013 #include <__config>
0014 #include <__cstddef/ptrdiff_t.h>
0015 #include <__random/is_valid.h>
0016 #include <__random/uniform_real_distribution.h>
0017 #include <__vector/vector.h>
0018 #include <initializer_list>
0019 #include <iosfwd>
0020 #include <numeric>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_PUSH_MACROS
0027 #include <__undef_macros>
0028
0029 _LIBCPP_BEGIN_NAMESPACE_STD
0030
0031 template <class _RealType = double>
0032 class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution {
0033 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0034 "RealType must be a supported floating-point type");
0035
0036 public:
0037
0038 typedef _RealType result_type;
0039
0040 class _LIBCPP_TEMPLATE_VIS param_type {
0041 vector<result_type> __b_;
0042 vector<result_type> __densities_;
0043 vector<result_type> __areas_;
0044
0045 public:
0046 typedef piecewise_constant_distribution distribution_type;
0047
0048 _LIBCPP_HIDE_FROM_ABI param_type();
0049 template <class _InputIteratorB, class _InputIteratorW>
0050 _LIBCPP_HIDE_FROM_ABI param_type(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w);
0051 #ifndef _LIBCPP_CXX03_LANG
0052 template <class _UnaryOperation>
0053 _LIBCPP_HIDE_FROM_ABI param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
0054 #endif
0055 template <class _UnaryOperation>
0056 _LIBCPP_HIDE_FROM_ABI param_type(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw);
0057 _LIBCPP_HIDE_FROM_ABI param_type(param_type const&) = default;
0058 _LIBCPP_HIDE_FROM_ABI param_type& operator=(const param_type& __rhs);
0059
0060 _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __b_; }
0061 _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __densities_; }
0062
0063 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0064 return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;
0065 }
0066 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0067
0068 private:
0069 _LIBCPP_HIDE_FROM_ABI void __init();
0070
0071 friend class piecewise_constant_distribution;
0072
0073 template <class _CharT, class _Traits, class _RT>
0074 friend basic_ostream<_CharT, _Traits>&
0075 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x);
0076
0077 template <class _CharT, class _Traits, class _RT>
0078 friend basic_istream<_CharT, _Traits>&
0079 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x);
0080 };
0081
0082 private:
0083 param_type __p_;
0084
0085 public:
0086
0087 _LIBCPP_HIDE_FROM_ABI piecewise_constant_distribution() {}
0088 template <class _InputIteratorB, class _InputIteratorW>
0089 _LIBCPP_HIDE_FROM_ABI
0090 piecewise_constant_distribution(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
0091 : __p_(__f_b, __l_b, __f_w) {}
0092
0093 #ifndef _LIBCPP_CXX03_LANG
0094 template <class _UnaryOperation>
0095 _LIBCPP_HIDE_FROM_ABI piecewise_constant_distribution(initializer_list<result_type> __bl, _UnaryOperation __fw)
0096 : __p_(__bl, __fw) {}
0097 #endif
0098
0099 template <class _UnaryOperation>
0100 _LIBCPP_HIDE_FROM_ABI
0101 piecewise_constant_distribution(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
0102 : __p_(__nw, __xmin, __xmax, __fw) {}
0103
0104 _LIBCPP_HIDE_FROM_ABI explicit piecewise_constant_distribution(const param_type& __p) : __p_(__p) {}
0105
0106 _LIBCPP_HIDE_FROM_ABI void reset() {}
0107
0108
0109 template <class _URNG>
0110 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0111 return (*this)(__g, __p_);
0112 }
0113 template <class _URNG>
0114 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0115
0116
0117 _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __p_.intervals(); }
0118 _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __p_.densities(); }
0119
0120 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0121 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0122
0123 _LIBCPP_HIDE_FROM_ABI result_type min() const { return __p_.__b_.front(); }
0124 _LIBCPP_HIDE_FROM_ABI result_type max() const { return __p_.__b_.back(); }
0125
0126 friend _LIBCPP_HIDE_FROM_ABI bool
0127 operator==(const piecewise_constant_distribution& __x, const piecewise_constant_distribution& __y) {
0128 return __x.__p_ == __y.__p_;
0129 }
0130 friend _LIBCPP_HIDE_FROM_ABI bool
0131 operator!=(const piecewise_constant_distribution& __x, const piecewise_constant_distribution& __y) {
0132 return !(__x == __y);
0133 }
0134
0135 template <class _CharT, class _Traits, class _RT>
0136 friend basic_ostream<_CharT, _Traits>&
0137 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x);
0138
0139 template <class _CharT, class _Traits, class _RT>
0140 friend basic_istream<_CharT, _Traits>&
0141 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x);
0142 };
0143
0144 template <class _RealType>
0145 typename piecewise_constant_distribution<_RealType>::param_type&
0146 piecewise_constant_distribution<_RealType>::param_type::operator=(const param_type& __rhs) {
0147
0148 __b_.reserve(__rhs.__b_.size());
0149 __densities_.reserve(__rhs.__densities_.size());
0150 __areas_.reserve(__rhs.__areas_.size());
0151
0152
0153 __b_ = __rhs.__b_;
0154 __densities_ = __rhs.__densities_;
0155 __areas_ = __rhs.__areas_;
0156 return *this;
0157 }
0158
0159 template <class _RealType>
0160 void piecewise_constant_distribution<_RealType>::param_type::__init() {
0161
0162 result_type __total_area = std::accumulate(__densities_.begin(), __densities_.end(), result_type());
0163 for (size_t __i = 0; __i < __densities_.size(); ++__i)
0164 __densities_[__i] /= __total_area;
0165
0166 __areas_.assign(__densities_.size(), result_type());
0167 std::partial_sum(__densities_.begin(), __densities_.end() - 1, __areas_.begin() + 1);
0168
0169 __densities_.back() = 1 - __areas_.back();
0170 for (size_t __i = 0; __i < __densities_.size(); ++__i)
0171 __densities_[__i] /= (__b_[__i + 1] - __b_[__i]);
0172
0173 }
0174
0175 template <class _RealType>
0176 piecewise_constant_distribution<_RealType>::param_type::param_type() : __b_(2), __densities_(1, 1.0), __areas_(1, 0.0) {
0177 __b_[1] = 1;
0178 }
0179
0180 template <class _RealType>
0181 template <class _InputIteratorB, class _InputIteratorW>
0182 piecewise_constant_distribution<_RealType>::param_type::param_type(
0183 _InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
0184 : __b_(__f_b, __l_b) {
0185 if (__b_.size() < 2) {
0186 __b_.resize(2);
0187 __b_[0] = 0;
0188 __b_[1] = 1;
0189 __densities_.assign(1, 1.0);
0190 __areas_.assign(1, 0.0);
0191 } else {
0192 __densities_.reserve(__b_.size() - 1);
0193 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__f_w)
0194 __densities_.push_back(*__f_w);
0195 __init();
0196 }
0197 }
0198
0199 #ifndef _LIBCPP_CXX03_LANG
0200
0201 template <class _RealType>
0202 template <class _UnaryOperation>
0203 piecewise_constant_distribution<_RealType>::param_type::param_type(
0204 initializer_list<result_type> __bl, _UnaryOperation __fw)
0205 : __b_(__bl.begin(), __bl.end()) {
0206 if (__b_.size() < 2) {
0207 __b_.resize(2);
0208 __b_[0] = 0;
0209 __b_[1] = 1;
0210 __densities_.assign(1, 1.0);
0211 __areas_.assign(1, 0.0);
0212 } else {
0213 __densities_.reserve(__b_.size() - 1);
0214 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
0215 __densities_.push_back(__fw((__b_[__i + 1] + __b_[__i]) * .5));
0216 __init();
0217 }
0218 }
0219
0220 #endif
0221
0222 template <class _RealType>
0223 template <class _UnaryOperation>
0224 piecewise_constant_distribution<_RealType>::param_type::param_type(
0225 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
0226 : __b_(__nw == 0 ? 2 : __nw + 1) {
0227 size_t __n = __b_.size() - 1;
0228 result_type __d = (__xmax - __xmin) / __n;
0229 __densities_.reserve(__n);
0230 for (size_t __i = 0; __i < __n; ++__i) {
0231 __b_[__i] = __xmin + __i * __d;
0232 __densities_.push_back(__fw(__b_[__i] + __d * .5));
0233 }
0234 __b_[__n] = __xmax;
0235 __init();
0236 }
0237
0238 template <class _RealType>
0239 template <class _URNG>
0240 _RealType piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0241 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0242 typedef uniform_real_distribution<result_type> _Gen;
0243 result_type __u = _Gen()(__g);
0244 ptrdiff_t __k = std::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), __u) - __p.__areas_.begin() - 1;
0245 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
0246 }
0247
0248 template <class _CharT, class _Traits, class _RT>
0249 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0250 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x) {
0251 __save_flags<_CharT, _Traits> __lx(__os);
0252 typedef basic_ostream<_CharT, _Traits> _OStream;
0253 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0254 _CharT __sp = __os.widen(' ');
0255 __os.fill(__sp);
0256 size_t __n = __x.__p_.__b_.size();
0257 __os << __n;
0258 for (size_t __i = 0; __i < __n; ++__i)
0259 __os << __sp << __x.__p_.__b_[__i];
0260 __n = __x.__p_.__densities_.size();
0261 __os << __sp << __n;
0262 for (size_t __i = 0; __i < __n; ++__i)
0263 __os << __sp << __x.__p_.__densities_[__i];
0264 __n = __x.__p_.__areas_.size();
0265 __os << __sp << __n;
0266 for (size_t __i = 0; __i < __n; ++__i)
0267 __os << __sp << __x.__p_.__areas_[__i];
0268 return __os;
0269 }
0270
0271 template <class _CharT, class _Traits, class _RT>
0272 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0273 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x) {
0274 typedef piecewise_constant_distribution<_RT> _Eng;
0275 typedef typename _Eng::result_type result_type;
0276 __save_flags<_CharT, _Traits> __lx(__is);
0277 typedef basic_istream<_CharT, _Traits> _Istream;
0278 __is.flags(_Istream::dec | _Istream::skipws);
0279 size_t __n;
0280 __is >> __n;
0281 vector<result_type> __b(__n);
0282 for (size_t __i = 0; __i < __n; ++__i)
0283 __is >> __b[__i];
0284 __is >> __n;
0285 vector<result_type> __densities(__n);
0286 for (size_t __i = 0; __i < __n; ++__i)
0287 __is >> __densities[__i];
0288 __is >> __n;
0289 vector<result_type> __areas(__n);
0290 for (size_t __i = 0; __i < __n; ++__i)
0291 __is >> __areas[__i];
0292 if (!__is.fail()) {
0293 swap(__x.__p_.__b_, __b);
0294 swap(__x.__p_.__densities_, __densities);
0295 swap(__x.__p_.__areas_, __areas);
0296 }
0297 return __is;
0298 }
0299
0300 _LIBCPP_END_NAMESPACE_STD
0301
0302 _LIBCPP_POP_MACROS
0303
0304 #endif