1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
12 #include <__algorithm/upper_bound.h>
14 #include <__random/is_valid.h>
15 #include <__random/uniform_real_distribution.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
25 #include <__undef_macros>
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 template<class _RealType
= double>
30 class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
34 typedef _RealType result_type
;
36 class _LIBCPP_TEMPLATE_VIS param_type
38 vector
<result_type
> __b_
;
39 vector
<result_type
> __densities_
;
40 vector
<result_type
> __areas_
;
42 typedef piecewise_linear_distribution distribution_type
;
44 _LIBCPP_HIDE_FROM_ABI
param_type();
45 template<class _InputIteratorB
, class _InputIteratorW
>
46 _LIBCPP_HIDE_FROM_ABI
param_type(_InputIteratorB __f_b
, _InputIteratorB __l_b
,
47 _InputIteratorW __f_w
);
48 #ifndef _LIBCPP_CXX03_LANG
49 template<class _UnaryOperation
>
50 _LIBCPP_HIDE_FROM_ABI
param_type(initializer_list
<result_type
> __bl
, _UnaryOperation __fw
);
51 #endif // _LIBCPP_CXX03_LANG
52 template<class _UnaryOperation
>
53 _LIBCPP_HIDE_FROM_ABI
param_type(size_t __nw
, result_type __xmin
, result_type __xmax
,
54 _UnaryOperation __fw
);
55 _LIBCPP_HIDE_FROM_ABI
param_type(param_type
const&) = default;
56 _LIBCPP_HIDE_FROM_ABI param_type
& operator=(const param_type
& __rhs
);
58 _LIBCPP_INLINE_VISIBILITY
59 vector
<result_type
> intervals() const {return __b_
;}
60 _LIBCPP_INLINE_VISIBILITY
61 vector
<result_type
> densities() const {return __densities_
;}
63 friend _LIBCPP_INLINE_VISIBILITY
64 bool operator==(const param_type
& __x
, const param_type
& __y
)
65 {return __x
.__densities_
== __y
.__densities_
&& __x
.__b_
== __y
.__b_
;}
66 friend _LIBCPP_INLINE_VISIBILITY
67 bool operator!=(const param_type
& __x
, const param_type
& __y
)
68 {return !(__x
== __y
);}
71 _LIBCPP_HIDE_FROM_ABI
void __init();
73 friend class piecewise_linear_distribution
;
75 template <class _CharT
, class _Traits
, class _RT
>
77 basic_ostream
<_CharT
, _Traits
>&
78 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
79 const piecewise_linear_distribution
<_RT
>& __x
);
81 template <class _CharT
, class _Traits
, class _RT
>
83 basic_istream
<_CharT
, _Traits
>&
84 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
85 piecewise_linear_distribution
<_RT
>& __x
);
92 // constructor and reset functions
93 _LIBCPP_INLINE_VISIBILITY
94 piecewise_linear_distribution() {}
95 template<class _InputIteratorB
, class _InputIteratorW
>
96 _LIBCPP_INLINE_VISIBILITY
97 piecewise_linear_distribution(_InputIteratorB __f_b
,
98 _InputIteratorB __l_b
,
99 _InputIteratorW __f_w
)
100 : __p_(__f_b
, __l_b
, __f_w
) {}
102 #ifndef _LIBCPP_CXX03_LANG
103 template<class _UnaryOperation
>
104 _LIBCPP_INLINE_VISIBILITY
105 piecewise_linear_distribution(initializer_list
<result_type
> __bl
,
106 _UnaryOperation __fw
)
107 : __p_(__bl
, __fw
) {}
108 #endif // _LIBCPP_CXX03_LANG
110 template<class _UnaryOperation
>
111 _LIBCPP_INLINE_VISIBILITY
112 piecewise_linear_distribution(size_t __nw
, result_type __xmin
,
113 result_type __xmax
, _UnaryOperation __fw
)
114 : __p_(__nw
, __xmin
, __xmax
, __fw
) {}
116 _LIBCPP_INLINE_VISIBILITY
117 explicit piecewise_linear_distribution(const param_type
& __p
)
120 _LIBCPP_INLINE_VISIBILITY
123 // generating functions
124 template<class _URNG
>
125 _LIBCPP_INLINE_VISIBILITY
126 result_type
operator()(_URNG
& __g
)
127 {return (*this)(__g
, __p_
);}
128 template<class _URNG
>
129 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
);
131 // property functions
132 _LIBCPP_INLINE_VISIBILITY
133 vector
<result_type
> intervals() const {return __p_
.intervals();}
134 _LIBCPP_INLINE_VISIBILITY
135 vector
<result_type
> densities() const {return __p_
.densities();}
137 _LIBCPP_INLINE_VISIBILITY
138 param_type
param() const {return __p_
;}
139 _LIBCPP_INLINE_VISIBILITY
140 void param(const param_type
& __p
) {__p_
= __p
;}
142 _LIBCPP_INLINE_VISIBILITY
143 result_type
min() const {return __p_
.__b_
.front();}
144 _LIBCPP_INLINE_VISIBILITY
145 result_type
max() const {return __p_
.__b_
.back();}
147 friend _LIBCPP_INLINE_VISIBILITY
148 bool operator==(const piecewise_linear_distribution
& __x
,
149 const piecewise_linear_distribution
& __y
)
150 {return __x
.__p_
== __y
.__p_
;}
151 friend _LIBCPP_INLINE_VISIBILITY
152 bool operator!=(const piecewise_linear_distribution
& __x
,
153 const piecewise_linear_distribution
& __y
)
154 {return !(__x
== __y
);}
156 template <class _CharT
, class _Traits
, class _RT
>
158 basic_ostream
<_CharT
, _Traits
>&
159 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
160 const piecewise_linear_distribution
<_RT
>& __x
);
162 template <class _CharT
, class _Traits
, class _RT
>
164 basic_istream
<_CharT
, _Traits
>&
165 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
166 piecewise_linear_distribution
<_RT
>& __x
);
169 template<class _RealType
>
170 typename piecewise_linear_distribution
<_RealType
>::param_type
&
171 piecewise_linear_distribution
<_RealType
>::param_type::operator=
172 (const param_type
& __rhs
)
175 __b_
.reserve (__rhs
.__b_
.size ());
176 __densities_
.reserve(__rhs
.__densities_
.size());
177 __areas_
.reserve (__rhs
.__areas_
.size());
179 // These can not throw
181 __densities_
= __rhs
.__densities_
;
182 __areas_
= __rhs
.__areas_
;
187 template<class _RealType
>
189 piecewise_linear_distribution
<_RealType
>::param_type::__init()
191 __areas_
.assign(__densities_
.size() - 1, result_type());
192 result_type __sp
= 0;
193 for (size_t __i
= 0; __i
< __areas_
.size(); ++__i
)
195 __areas_
[__i
] = (__densities_
[__i
+1] + __densities_
[__i
]) *
196 (__b_
[__i
+1] - __b_
[__i
]) * .5;
197 __sp
+= __areas_
[__i
];
199 for (size_t __i
= __areas_
.size(); __i
> 1;)
202 __areas_
[__i
] = __areas_
[__i
-1] / __sp
;
205 for (size_t __i
= 1; __i
< __areas_
.size(); ++__i
)
206 __areas_
[__i
] += __areas_
[__i
-1];
207 for (size_t __i
= 0; __i
< __densities_
.size(); ++__i
)
208 __densities_
[__i
] /= __sp
;
211 template<class _RealType
>
212 piecewise_linear_distribution
<_RealType
>::param_type::param_type()
214 __densities_(2, 1.0),
220 template<class _RealType
>
221 template<class _InputIteratorB
, class _InputIteratorW
>
222 piecewise_linear_distribution
<_RealType
>::param_type::param_type(
223 _InputIteratorB __f_b
, _InputIteratorB __l_b
, _InputIteratorW __f_w
)
231 __densities_
.assign(2, 1.0);
232 __areas_
.assign(1, 0.0);
236 __densities_
.reserve(__b_
.size());
237 for (size_t __i
= 0; __i
< __b_
.size(); ++__i
, ++__f_w
)
238 __densities_
.push_back(*__f_w
);
243 #ifndef _LIBCPP_CXX03_LANG
245 template<class _RealType
>
246 template<class _UnaryOperation
>
247 piecewise_linear_distribution
<_RealType
>::param_type::param_type(
248 initializer_list
<result_type
> __bl
, _UnaryOperation __fw
)
249 : __b_(__bl
.begin(), __bl
.end())
256 __densities_
.assign(2, 1.0);
257 __areas_
.assign(1, 0.0);
261 __densities_
.reserve(__b_
.size());
262 for (size_t __i
= 0; __i
< __b_
.size(); ++__i
)
263 __densities_
.push_back(__fw(__b_
[__i
]));
268 #endif // _LIBCPP_CXX03_LANG
270 template<class _RealType
>
271 template<class _UnaryOperation
>
272 piecewise_linear_distribution
<_RealType
>::param_type::param_type(
273 size_t __nw
, result_type __xmin
, result_type __xmax
, _UnaryOperation __fw
)
274 : __b_(__nw
== 0 ? 2 : __nw
+ 1)
276 size_t __n
= __b_
.size() - 1;
277 result_type __d
= (__xmax
- __xmin
) / __n
;
278 __densities_
.reserve(__b_
.size());
279 for (size_t __i
= 0; __i
< __n
; ++__i
)
281 __b_
[__i
] = __xmin
+ __i
* __d
;
282 __densities_
.push_back(__fw(__b_
[__i
]));
285 __densities_
.push_back(__fw(__b_
[__n
]));
289 template<class _RealType
>
290 template<class _URNG
>
292 piecewise_linear_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
)
294 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
295 typedef uniform_real_distribution
<result_type
> _Gen
;
296 result_type __u
= _Gen()(__g
);
297 ptrdiff_t __k
= _VSTD::upper_bound(__p
.__areas_
.begin(), __p
.__areas_
.end(),
298 __u
) - __p
.__areas_
.begin() - 1;
299 __u
-= __p
.__areas_
[__k
];
300 const result_type __dk
= __p
.__densities_
[__k
];
301 const result_type __dk1
= __p
.__densities_
[__k
+1];
302 const result_type __deltad
= __dk1
- __dk
;
303 const result_type __bk
= __p
.__b_
[__k
];
305 return __u
/ __dk
+ __bk
;
306 const result_type __bk1
= __p
.__b_
[__k
+1];
307 const result_type __deltab
= __bk1
- __bk
;
308 return (__bk
* __dk1
- __bk1
* __dk
+
309 _VSTD::sqrt(__deltab
* (__deltab
* __dk
* __dk
+ 2 * __deltad
* __u
))) /
313 template <class _CharT
, class _Traits
, class _RT
>
314 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
315 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
316 const piecewise_linear_distribution
<_RT
>& __x
)
318 __save_flags
<_CharT
, _Traits
> __lx(__os
);
319 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
320 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
|
321 _OStream::scientific
);
322 _CharT __sp
= __os
.widen(' ');
324 size_t __n
= __x
.__p_
.__b_
.size();
326 for (size_t __i
= 0; __i
< __n
; ++__i
)
327 __os
<< __sp
<< __x
.__p_
.__b_
[__i
];
328 __n
= __x
.__p_
.__densities_
.size();
330 for (size_t __i
= 0; __i
< __n
; ++__i
)
331 __os
<< __sp
<< __x
.__p_
.__densities_
[__i
];
332 __n
= __x
.__p_
.__areas_
.size();
334 for (size_t __i
= 0; __i
< __n
; ++__i
)
335 __os
<< __sp
<< __x
.__p_
.__areas_
[__i
];
339 template <class _CharT
, class _Traits
, class _RT
>
340 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
341 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
342 piecewise_linear_distribution
<_RT
>& __x
)
344 typedef piecewise_linear_distribution
<_RT
> _Eng
;
345 typedef typename
_Eng::result_type result_type
;
346 __save_flags
<_CharT
, _Traits
> __lx(__is
);
347 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
348 __is
.flags(_Istream::dec
| _Istream::skipws
);
351 vector
<result_type
> __b(__n
);
352 for (size_t __i
= 0; __i
< __n
; ++__i
)
355 vector
<result_type
> __densities(__n
);
356 for (size_t __i
= 0; __i
< __n
; ++__i
)
357 __is
>> __densities
[__i
];
359 vector
<result_type
> __areas(__n
);
360 for (size_t __i
= 0; __i
< __n
; ++__i
)
361 __is
>> __areas
[__i
];
364 swap(__x
.__p_
.__b_
, __b
);
365 swap(__x
.__p_
.__densities_
, __densities
);
366 swap(__x
.__p_
.__areas_
, __areas
);
371 _LIBCPP_END_NAMESPACE_STD
375 #endif // _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H