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