xref: /llvm-project/libcxx/include/__random/extreme_value_distribution.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer 
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H
11344cef66SArthur O'Dwyer 
12344cef66SArthur O'Dwyer #include <__config>
137624552eSArthur O'Dwyer #include <__random/is_valid.h>
14344cef66SArthur O'Dwyer #include <__random/uniform_real_distribution.h>
15344cef66SArthur O'Dwyer #include <cmath>
16344cef66SArthur O'Dwyer #include <iosfwd>
17344cef66SArthur O'Dwyer #include <limits>
18344cef66SArthur O'Dwyer 
19344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20344cef66SArthur O'Dwyer #  pragma GCC system_header
21344cef66SArthur O'Dwyer #endif
22344cef66SArthur O'Dwyer 
23344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
24344cef66SArthur O'Dwyer #include <__undef_macros>
25344cef66SArthur O'Dwyer 
26344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
27344cef66SArthur O'Dwyer 
28344cef66SArthur O'Dwyer template <class _RealType = double>
29*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS extreme_value_distribution {
30727fef79SNhat Nguyen   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31727fef79SNhat Nguyen                 "RealType must be a supported floating-point type");
32727fef79SNhat Nguyen 
33344cef66SArthur O'Dwyer public:
34344cef66SArthur O'Dwyer   // types
35344cef66SArthur O'Dwyer   typedef _RealType result_type;
36344cef66SArthur O'Dwyer 
37*9783f28cSLouis Dionne   class _LIBCPP_TEMPLATE_VIS param_type {
38344cef66SArthur O'Dwyer     result_type __a_;
39344cef66SArthur O'Dwyer     result_type __b_;
40*9783f28cSLouis Dionne 
41344cef66SArthur O'Dwyer   public:
42344cef66SArthur O'Dwyer     typedef extreme_value_distribution distribution_type;
43344cef66SArthur O'Dwyer 
__a_(__a)44*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}
45344cef66SArthur O'Dwyer 
a()46*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
b()47*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
48344cef66SArthur O'Dwyer 
49*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
50*9783f28cSLouis Dionne       return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
51*9783f28cSLouis Dionne     }
52*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
53344cef66SArthur O'Dwyer   };
54344cef66SArthur O'Dwyer 
55344cef66SArthur O'Dwyer private:
56344cef66SArthur O'Dwyer   param_type __p_;
57344cef66SArthur O'Dwyer 
58344cef66SArthur O'Dwyer public:
59344cef66SArthur O'Dwyer   // constructor and reset functions
60344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
extreme_value_distribution()61*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI extreme_value_distribution() : extreme_value_distribution(0) {}
62*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit extreme_value_distribution(result_type __a, result_type __b = 1)
__p_(param_type (__a,__b))63344cef66SArthur O'Dwyer       : __p_(param_type(__a, __b)) {}
64344cef66SArthur O'Dwyer #else
65*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
66344cef66SArthur O'Dwyer       : __p_(param_type(__a, __b)) {}
67344cef66SArthur O'Dwyer #endif
extreme_value_distribution(const param_type & __p)68*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit extreme_value_distribution(const param_type& __p) : __p_(__p) {}
reset()69*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void reset() {}
70344cef66SArthur O'Dwyer 
71344cef66SArthur O'Dwyer   // generating functions
72344cef66SArthur O'Dwyer   template <class _URNG>
operator()73*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
74*9783f28cSLouis Dionne     return (*this)(__g, __p_);
75*9783f28cSLouis Dionne   }
7683ce1397SNikolas Klauser   template <class _URNG>
7783ce1397SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
78344cef66SArthur O'Dwyer 
79344cef66SArthur O'Dwyer   // property functions
a()80*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
b()81*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
82344cef66SArthur O'Dwyer 
param()83*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)84*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
85344cef66SArthur O'Dwyer 
min()86*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
max()87*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
88344cef66SArthur O'Dwyer 
89*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool
90*9783f28cSLouis Dionne   operator==(const extreme_value_distribution& __x, const extreme_value_distribution& __y) {
91*9783f28cSLouis Dionne     return __x.__p_ == __y.__p_;
92*9783f28cSLouis Dionne   }
93*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool
94*9783f28cSLouis Dionne   operator!=(const extreme_value_distribution& __x, const extreme_value_distribution& __y) {
95*9783f28cSLouis Dionne     return !(__x == __y);
96*9783f28cSLouis Dionne   }
97344cef66SArthur O'Dwyer };
98344cef66SArthur O'Dwyer 
99344cef66SArthur O'Dwyer template <class _RealType>
100344cef66SArthur O'Dwyer template <class _URNG>
operator()101*9783f28cSLouis Dionne _RealType extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
1027624552eSArthur O'Dwyer   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
103*9783f28cSLouis Dionne   return __p.a() - __p.b() * std::log(-std::log(1 - uniform_real_distribution<result_type>()(__g)));
104344cef66SArthur O'Dwyer }
105344cef66SArthur O'Dwyer 
106344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
10780c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
108*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const extreme_value_distribution<_RT>& __x) {
109344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__os);
110344cef66SArthur O'Dwyer   typedef basic_ostream<_CharT, _Traits> _OStream;
111*9783f28cSLouis Dionne   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
112344cef66SArthur O'Dwyer   _CharT __sp = __os.widen(' ');
113344cef66SArthur O'Dwyer   __os.fill(__sp);
114344cef66SArthur O'Dwyer   __os << __x.a() << __sp << __x.b();
115344cef66SArthur O'Dwyer   return __os;
116344cef66SArthur O'Dwyer }
117344cef66SArthur O'Dwyer 
118344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
11980c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
120*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, extreme_value_distribution<_RT>& __x) {
121344cef66SArthur O'Dwyer   typedef extreme_value_distribution<_RT> _Eng;
122344cef66SArthur O'Dwyer   typedef typename _Eng::result_type result_type;
123344cef66SArthur O'Dwyer   typedef typename _Eng::param_type param_type;
124344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__is);
125344cef66SArthur O'Dwyer   typedef basic_istream<_CharT, _Traits> _Istream;
126344cef66SArthur O'Dwyer   __is.flags(_Istream::dec | _Istream::skipws);
127344cef66SArthur O'Dwyer   result_type __a;
128344cef66SArthur O'Dwyer   result_type __b;
129344cef66SArthur O'Dwyer   __is >> __a >> __b;
130344cef66SArthur O'Dwyer   if (!__is.fail())
131344cef66SArthur O'Dwyer     __x.param(param_type(__a, __b));
132344cef66SArthur O'Dwyer   return __is;
133344cef66SArthur O'Dwyer }
134344cef66SArthur O'Dwyer 
135344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
136344cef66SArthur O'Dwyer 
137344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
138344cef66SArthur O'Dwyer 
139344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H
140