xref: /freebsd/contrib/llvm-project/libcxx/include/__random/extreme_value_distribution.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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_EXTREME_VALUE_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H
11 
12 #include <__config>
13 #include <__random/is_valid.h>
14 #include <__random/uniform_real_distribution.h>
15 #include <cmath>
16 #include <iosfwd>
17 #include <limits>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template<class _RealType = double>
29 class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
30 {
31 public:
32     // types
33     typedef _RealType result_type;
34 
35     class _LIBCPP_TEMPLATE_VIS param_type
36     {
37         result_type __a_;
38         result_type __b_;
39     public:
40         typedef extreme_value_distribution distribution_type;
41 
42         _LIBCPP_INLINE_VISIBILITY
43         explicit param_type(result_type __a = 0, result_type __b = 1)
44             : __a_(__a), __b_(__b) {}
45 
46         _LIBCPP_INLINE_VISIBILITY
47         result_type a() const {return __a_;}
48         _LIBCPP_INLINE_VISIBILITY
49         result_type b() const {return __b_;}
50 
51         friend _LIBCPP_INLINE_VISIBILITY
52             bool operator==(const param_type& __x, const param_type& __y)
53             {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
54         friend _LIBCPP_INLINE_VISIBILITY
55             bool operator!=(const param_type& __x, const param_type& __y)
56             {return !(__x == __y);}
57     };
58 
59 private:
60     param_type __p_;
61 
62 public:
63     // constructor and reset functions
64 #ifndef _LIBCPP_CXX03_LANG
65     _LIBCPP_INLINE_VISIBILITY
66     extreme_value_distribution() : extreme_value_distribution(0) {}
67     _LIBCPP_INLINE_VISIBILITY
68     explicit extreme_value_distribution(result_type __a, result_type __b = 1)
69         : __p_(param_type(__a, __b)) {}
70 #else
71     _LIBCPP_INLINE_VISIBILITY
72     explicit extreme_value_distribution(result_type __a = 0,
73                                         result_type __b = 1)
74         : __p_(param_type(__a, __b)) {}
75 #endif
76     _LIBCPP_INLINE_VISIBILITY
77     explicit extreme_value_distribution(const param_type& __p)
78         : __p_(__p) {}
79     _LIBCPP_INLINE_VISIBILITY
80     void reset() {}
81 
82     // generating functions
83     template<class _URNG>
84         _LIBCPP_INLINE_VISIBILITY
85         result_type operator()(_URNG& __g)
86         {return (*this)(__g, __p_);}
87     template<class _URNG>
88     _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
89 
90     // property functions
91     _LIBCPP_INLINE_VISIBILITY
92     result_type a() const {return __p_.a();}
93     _LIBCPP_INLINE_VISIBILITY
94     result_type b() const {return __p_.b();}
95 
96     _LIBCPP_INLINE_VISIBILITY
97     param_type param() const {return __p_;}
98     _LIBCPP_INLINE_VISIBILITY
99     void param(const param_type& __p) {__p_ = __p;}
100 
101     _LIBCPP_INLINE_VISIBILITY
102     result_type min() const {return -numeric_limits<result_type>::infinity();}
103     _LIBCPP_INLINE_VISIBILITY
104     result_type max() const {return numeric_limits<result_type>::infinity();}
105 
106     friend _LIBCPP_INLINE_VISIBILITY
107         bool operator==(const extreme_value_distribution& __x,
108                         const extreme_value_distribution& __y)
109         {return __x.__p_ == __y.__p_;}
110     friend _LIBCPP_INLINE_VISIBILITY
111         bool operator!=(const extreme_value_distribution& __x,
112                         const extreme_value_distribution& __y)
113         {return !(__x == __y);}
114 };
115 
116 template<class _RealType>
117 template<class _URNG>
118 _RealType
119 extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
120 {
121     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
122     return __p.a() - __p.b() *
123          _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
124 }
125 
126 template <class _CharT, class _Traits, class _RT>
127 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
128 operator<<(basic_ostream<_CharT, _Traits>& __os,
129            const extreme_value_distribution<_RT>& __x)
130 {
131     __save_flags<_CharT, _Traits> __lx(__os);
132     typedef basic_ostream<_CharT, _Traits> _OStream;
133     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
134                _OStream::scientific);
135     _CharT __sp = __os.widen(' ');
136     __os.fill(__sp);
137     __os << __x.a() << __sp << __x.b();
138     return __os;
139 }
140 
141 template <class _CharT, class _Traits, class _RT>
142 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
143 operator>>(basic_istream<_CharT, _Traits>& __is,
144            extreme_value_distribution<_RT>& __x)
145 {
146     typedef extreme_value_distribution<_RT> _Eng;
147     typedef typename _Eng::result_type result_type;
148     typedef typename _Eng::param_type param_type;
149     __save_flags<_CharT, _Traits> __lx(__is);
150     typedef basic_istream<_CharT, _Traits> _Istream;
151     __is.flags(_Istream::dec | _Istream::skipws);
152     result_type __a;
153     result_type __b;
154     __is >> __a >> __b;
155     if (!__is.fail())
156         __x.param(param_type(__a, __b));
157     return __is;
158 }
159 
160 _LIBCPP_END_NAMESPACE_STD
161 
162 _LIBCPP_POP_MACROS
163 
164 #endif // _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H
165