xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__random/piecewise_constant_distribution.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #ifndef _LIBCPP___CXX03___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
11 
12 #include <__cxx03/__algorithm/upper_bound.h>
13 #include <__cxx03/__config>
14 #include <__cxx03/__random/is_valid.h>
15 #include <__cxx03/__random/uniform_real_distribution.h>
16 #include <__cxx03/iosfwd>
17 #include <__cxx03/numeric>
18 #include <__cxx03/vector>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__cxx03/__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template <class _RealType = double>
30 class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution {
31   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32                 "RealType must be a supported floating-point type");
33 
34 public:
35   // types
36   typedef _RealType result_type;
37 
38   class _LIBCPP_TEMPLATE_VIS param_type {
39     vector<result_type> __b_;
40     vector<result_type> __densities_;
41     vector<result_type> __areas_;
42 
43   public:
44     typedef piecewise_constant_distribution distribution_type;
45 
46     _LIBCPP_HIDE_FROM_ABI param_type();
47     template <class _InputIteratorB, class _InputIteratorW>
48     _LIBCPP_HIDE_FROM_ABI param_type(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w);
49     template <class _UnaryOperation>
50     _LIBCPP_HIDE_FROM_ABI param_type(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw);
51     _LIBCPP_HIDE_FROM_ABI param_type(param_type const&) = default;
52     _LIBCPP_HIDE_FROM_ABI param_type& operator=(const param_type& __rhs);
53 
intervals()54     _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __b_; }
densities()55     _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __densities_; }
56 
57     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
58       return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;
59     }
60     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
61 
62   private:
63     _LIBCPP_HIDE_FROM_ABI void __init();
64 
65     friend class piecewise_constant_distribution;
66 
67     template <class _CharT, class _Traits, class _RT>
68     friend basic_ostream<_CharT, _Traits>&
69     operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x);
70 
71     template <class _CharT, class _Traits, class _RT>
72     friend basic_istream<_CharT, _Traits>&
73     operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x);
74   };
75 
76 private:
77   param_type __p_;
78 
79 public:
80   // constructor and reset functions
piecewise_constant_distribution()81   _LIBCPP_HIDE_FROM_ABI piecewise_constant_distribution() {}
82   template <class _InputIteratorB, class _InputIteratorW>
83   _LIBCPP_HIDE_FROM_ABI
piecewise_constant_distribution(_InputIteratorB __f_b,_InputIteratorB __l_b,_InputIteratorW __f_w)84   piecewise_constant_distribution(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
85       : __p_(__f_b, __l_b, __f_w) {}
86 
87   template <class _UnaryOperation>
88   _LIBCPP_HIDE_FROM_ABI
piecewise_constant_distribution(size_t __nw,result_type __xmin,result_type __xmax,_UnaryOperation __fw)89   piecewise_constant_distribution(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
90       : __p_(__nw, __xmin, __xmax, __fw) {}
91 
piecewise_constant_distribution(const param_type & __p)92   _LIBCPP_HIDE_FROM_ABI explicit piecewise_constant_distribution(const param_type& __p) : __p_(__p) {}
93 
reset()94   _LIBCPP_HIDE_FROM_ABI void reset() {}
95 
96   // generating functions
97   template <class _URNG>
operator()98   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
99     return (*this)(__g, __p_);
100   }
101   template <class _URNG>
102   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
103 
104   // property functions
intervals()105   _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __p_.intervals(); }
densities()106   _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __p_.densities(); }
107 
param()108   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)109   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
110 
min()111   _LIBCPP_HIDE_FROM_ABI result_type min() const { return __p_.__b_.front(); }
max()112   _LIBCPP_HIDE_FROM_ABI result_type max() const { return __p_.__b_.back(); }
113 
114   friend _LIBCPP_HIDE_FROM_ABI bool
115   operator==(const piecewise_constant_distribution& __x, const piecewise_constant_distribution& __y) {
116     return __x.__p_ == __y.__p_;
117   }
118   friend _LIBCPP_HIDE_FROM_ABI bool
119   operator!=(const piecewise_constant_distribution& __x, const piecewise_constant_distribution& __y) {
120     return !(__x == __y);
121   }
122 
123   template <class _CharT, class _Traits, class _RT>
124   friend basic_ostream<_CharT, _Traits>&
125   operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x);
126 
127   template <class _CharT, class _Traits, class _RT>
128   friend basic_istream<_CharT, _Traits>&
129   operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x);
130 };
131 
132 template <class _RealType>
133 typename piecewise_constant_distribution<_RealType>::param_type&
134 piecewise_constant_distribution<_RealType>::param_type::operator=(const param_type& __rhs) {
135   //  These can throw
136   __b_.reserve(__rhs.__b_.size());
137   __densities_.reserve(__rhs.__densities_.size());
138   __areas_.reserve(__rhs.__areas_.size());
139 
140   //  These can not throw
141   __b_         = __rhs.__b_;
142   __densities_ = __rhs.__densities_;
143   __areas_     = __rhs.__areas_;
144   return *this;
145 }
146 
147 template <class _RealType>
__init()148 void piecewise_constant_distribution<_RealType>::param_type::__init() {
149   // __densities_ contains non-normalized areas
150   result_type __total_area = std::accumulate(__densities_.begin(), __densities_.end(), result_type());
151   for (size_t __i = 0; __i < __densities_.size(); ++__i)
152     __densities_[__i] /= __total_area;
153   // __densities_ contains normalized areas
154   __areas_.assign(__densities_.size(), result_type());
155   std::partial_sum(__densities_.begin(), __densities_.end() - 1, __areas_.begin() + 1);
156   // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
157   __densities_.back() = 1 - __areas_.back(); // correct round off error
158   for (size_t __i = 0; __i < __densities_.size(); ++__i)
159     __densities_[__i] /= (__b_[__i + 1] - __b_[__i]);
160   // __densities_ now contains __densities_
161 }
162 
163 template <class _RealType>
param_type()164 piecewise_constant_distribution<_RealType>::param_type::param_type() : __b_(2), __densities_(1, 1.0), __areas_(1, 0.0) {
165   __b_[1] = 1;
166 }
167 
168 template <class _RealType>
169 template <class _InputIteratorB, class _InputIteratorW>
param_type(_InputIteratorB __f_b,_InputIteratorB __l_b,_InputIteratorW __f_w)170 piecewise_constant_distribution<_RealType>::param_type::param_type(
171     _InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
172     : __b_(__f_b, __l_b) {
173   if (__b_.size() < 2) {
174     __b_.resize(2);
175     __b_[0] = 0;
176     __b_[1] = 1;
177     __densities_.assign(1, 1.0);
178     __areas_.assign(1, 0.0);
179   } else {
180     __densities_.reserve(__b_.size() - 1);
181     for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__f_w)
182       __densities_.push_back(*__f_w);
183     __init();
184   }
185 }
186 
187 template <class _RealType>
188 template <class _UnaryOperation>
param_type(size_t __nw,result_type __xmin,result_type __xmax,_UnaryOperation __fw)189 piecewise_constant_distribution<_RealType>::param_type::param_type(
190     size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
191     : __b_(__nw == 0 ? 2 : __nw + 1) {
192   size_t __n      = __b_.size() - 1;
193   result_type __d = (__xmax - __xmin) / __n;
194   __densities_.reserve(__n);
195   for (size_t __i = 0; __i < __n; ++__i) {
196     __b_[__i] = __xmin + __i * __d;
197     __densities_.push_back(__fw(__b_[__i] + __d * .5));
198   }
199   __b_[__n] = __xmax;
200   __init();
201 }
202 
203 template <class _RealType>
204 template <class _URNG>
operator()205 _RealType piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
206   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
207   typedef uniform_real_distribution<result_type> _Gen;
208   result_type __u = _Gen()(__g);
209   ptrdiff_t __k   = std::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), __u) - __p.__areas_.begin() - 1;
210   return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
211 }
212 
213 template <class _CharT, class _Traits, class _RT>
214 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
215 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x) {
216   __save_flags<_CharT, _Traits> __lx(__os);
217   typedef basic_ostream<_CharT, _Traits> _OStream;
218   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
219   _CharT __sp = __os.widen(' ');
220   __os.fill(__sp);
221   size_t __n = __x.__p_.__b_.size();
222   __os << __n;
223   for (size_t __i = 0; __i < __n; ++__i)
224     __os << __sp << __x.__p_.__b_[__i];
225   __n = __x.__p_.__densities_.size();
226   __os << __sp << __n;
227   for (size_t __i = 0; __i < __n; ++__i)
228     __os << __sp << __x.__p_.__densities_[__i];
229   __n = __x.__p_.__areas_.size();
230   __os << __sp << __n;
231   for (size_t __i = 0; __i < __n; ++__i)
232     __os << __sp << __x.__p_.__areas_[__i];
233   return __os;
234 }
235 
236 template <class _CharT, class _Traits, class _RT>
237 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
238 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x) {
239   typedef piecewise_constant_distribution<_RT> _Eng;
240   typedef typename _Eng::result_type result_type;
241   __save_flags<_CharT, _Traits> __lx(__is);
242   typedef basic_istream<_CharT, _Traits> _Istream;
243   __is.flags(_Istream::dec | _Istream::skipws);
244   size_t __n;
245   __is >> __n;
246   vector<result_type> __b(__n);
247   for (size_t __i = 0; __i < __n; ++__i)
248     __is >> __b[__i];
249   __is >> __n;
250   vector<result_type> __densities(__n);
251   for (size_t __i = 0; __i < __n; ++__i)
252     __is >> __densities[__i];
253   __is >> __n;
254   vector<result_type> __areas(__n);
255   for (size_t __i = 0; __i < __n; ++__i)
256     __is >> __areas[__i];
257   if (!__is.fail()) {
258     swap(__x.__p_.__b_, __b);
259     swap(__x.__p_.__densities_, __densities);
260     swap(__x.__p_.__areas_, __areas);
261   }
262   return __is;
263 }
264 
265 _LIBCPP_END_NAMESPACE_STD
266 
267 _LIBCPP_POP_MACROS
268 
269 #endif // _LIBCPP___CXX03___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
270