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_LINEAR_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_PIECEWISE_LINEAR_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/cmath>
17 #include <__cxx03/iosfwd>
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_linear_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_linear_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_linear_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_linear_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_linear_distribution<_RT>& __x);
74 };
75
76 private:
77 param_type __p_;
78
79 public:
80 // constructor and reset functions
piecewise_linear_distribution()81 _LIBCPP_HIDE_FROM_ABI piecewise_linear_distribution() {}
82 template <class _InputIteratorB, class _InputIteratorW>
83 _LIBCPP_HIDE_FROM_ABI
piecewise_linear_distribution(_InputIteratorB __f_b,_InputIteratorB __l_b,_InputIteratorW __f_w)84 piecewise_linear_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_linear_distribution(size_t __nw,result_type __xmin,result_type __xmax,_UnaryOperation __fw)89 piecewise_linear_distribution(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
90 : __p_(__nw, __xmin, __xmax, __fw) {}
91
piecewise_linear_distribution(const param_type & __p)92 _LIBCPP_HIDE_FROM_ABI explicit piecewise_linear_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_linear_distribution& __x, const piecewise_linear_distribution& __y) {
116 return __x.__p_ == __y.__p_;
117 }
118 friend _LIBCPP_HIDE_FROM_ABI bool
119 operator!=(const piecewise_linear_distribution& __x, const piecewise_linear_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_linear_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_linear_distribution<_RT>& __x);
130 };
131
132 template <class _RealType>
133 typename piecewise_linear_distribution<_RealType>::param_type&
134 piecewise_linear_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_linear_distribution<_RealType>::param_type::__init() {
149 __areas_.assign(__densities_.size() - 1, result_type());
150 result_type __sp = 0;
151 for (size_t __i = 0; __i < __areas_.size(); ++__i) {
152 __areas_[__i] = (__densities_[__i + 1] + __densities_[__i]) * (__b_[__i + 1] - __b_[__i]) * .5;
153 __sp += __areas_[__i];
154 }
155 for (size_t __i = __areas_.size(); __i > 1;) {
156 --__i;
157 __areas_[__i] = __areas_[__i - 1] / __sp;
158 }
159 __areas_[0] = 0;
160 for (size_t __i = 1; __i < __areas_.size(); ++__i)
161 __areas_[__i] += __areas_[__i - 1];
162 for (size_t __i = 0; __i < __densities_.size(); ++__i)
163 __densities_[__i] /= __sp;
164 }
165
166 template <class _RealType>
param_type()167 piecewise_linear_distribution<_RealType>::param_type::param_type() : __b_(2), __densities_(2, 1.0), __areas_(1, 0.0) {
168 __b_[1] = 1;
169 }
170
171 template <class _RealType>
172 template <class _InputIteratorB, class _InputIteratorW>
param_type(_InputIteratorB __f_b,_InputIteratorB __l_b,_InputIteratorW __f_w)173 piecewise_linear_distribution<_RealType>::param_type::param_type(
174 _InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
175 : __b_(__f_b, __l_b) {
176 if (__b_.size() < 2) {
177 __b_.resize(2);
178 __b_[0] = 0;
179 __b_[1] = 1;
180 __densities_.assign(2, 1.0);
181 __areas_.assign(1, 0.0);
182 } else {
183 __densities_.reserve(__b_.size());
184 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__f_w)
185 __densities_.push_back(*__f_w);
186 __init();
187 }
188 }
189
190 template <class _RealType>
191 template <class _UnaryOperation>
param_type(size_t __nw,result_type __xmin,result_type __xmax,_UnaryOperation __fw)192 piecewise_linear_distribution<_RealType>::param_type::param_type(
193 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
194 : __b_(__nw == 0 ? 2 : __nw + 1) {
195 size_t __n = __b_.size() - 1;
196 result_type __d = (__xmax - __xmin) / __n;
197 __densities_.reserve(__b_.size());
198 for (size_t __i = 0; __i < __n; ++__i) {
199 __b_[__i] = __xmin + __i * __d;
200 __densities_.push_back(__fw(__b_[__i]));
201 }
202 __b_[__n] = __xmax;
203 __densities_.push_back(__fw(__b_[__n]));
204 __init();
205 }
206
207 template <class _RealType>
208 template <class _URNG>
operator()209 _RealType piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
210 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
211 typedef uniform_real_distribution<result_type> _Gen;
212 result_type __u = _Gen()(__g);
213 ptrdiff_t __k = std::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), __u) - __p.__areas_.begin() - 1;
214 __u -= __p.__areas_[__k];
215 const result_type __dk = __p.__densities_[__k];
216 const result_type __dk1 = __p.__densities_[__k + 1];
217 const result_type __deltad = __dk1 - __dk;
218 const result_type __bk = __p.__b_[__k];
219 if (__deltad == 0)
220 return __u / __dk + __bk;
221 const result_type __bk1 = __p.__b_[__k + 1];
222 const result_type __deltab = __bk1 - __bk;
223 return (__bk * __dk1 - __bk1 * __dk + std::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / __deltad;
224 }
225
226 template <class _CharT, class _Traits, class _RT>
227 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
228 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_linear_distribution<_RT>& __x) {
229 __save_flags<_CharT, _Traits> __lx(__os);
230 typedef basic_ostream<_CharT, _Traits> _OStream;
231 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
232 _CharT __sp = __os.widen(' ');
233 __os.fill(__sp);
234 size_t __n = __x.__p_.__b_.size();
235 __os << __n;
236 for (size_t __i = 0; __i < __n; ++__i)
237 __os << __sp << __x.__p_.__b_[__i];
238 __n = __x.__p_.__densities_.size();
239 __os << __sp << __n;
240 for (size_t __i = 0; __i < __n; ++__i)
241 __os << __sp << __x.__p_.__densities_[__i];
242 __n = __x.__p_.__areas_.size();
243 __os << __sp << __n;
244 for (size_t __i = 0; __i < __n; ++__i)
245 __os << __sp << __x.__p_.__areas_[__i];
246 return __os;
247 }
248
249 template <class _CharT, class _Traits, class _RT>
250 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
251 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_linear_distribution<_RT>& __x) {
252 typedef piecewise_linear_distribution<_RT> _Eng;
253 typedef typename _Eng::result_type result_type;
254 __save_flags<_CharT, _Traits> __lx(__is);
255 typedef basic_istream<_CharT, _Traits> _Istream;
256 __is.flags(_Istream::dec | _Istream::skipws);
257 size_t __n;
258 __is >> __n;
259 vector<result_type> __b(__n);
260 for (size_t __i = 0; __i < __n; ++__i)
261 __is >> __b[__i];
262 __is >> __n;
263 vector<result_type> __densities(__n);
264 for (size_t __i = 0; __i < __n; ++__i)
265 __is >> __densities[__i];
266 __is >> __n;
267 vector<result_type> __areas(__n);
268 for (size_t __i = 0; __i < __n; ++__i)
269 __is >> __areas[__i];
270 if (!__is.fail()) {
271 swap(__x.__p_.__b_, __b);
272 swap(__x.__p_.__densities_, __densities);
273 swap(__x.__p_.__areas_, __areas);
274 }
275 return __is;
276 }
277
278 _LIBCPP_END_NAMESPACE_STD
279
280 _LIBCPP_POP_MACROS
281
282 #endif // _LIBCPP___CXX03___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
283