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> result_type operator()(_URNG& __g, const param_type& __p); 88 89 // property functions 90 _LIBCPP_INLINE_VISIBILITY 91 result_type a() const {return __p_.a();} 92 _LIBCPP_INLINE_VISIBILITY 93 result_type b() const {return __p_.b();} 94 95 _LIBCPP_INLINE_VISIBILITY 96 param_type param() const {return __p_;} 97 _LIBCPP_INLINE_VISIBILITY 98 void param(const param_type& __p) {__p_ = __p;} 99 100 _LIBCPP_INLINE_VISIBILITY 101 result_type min() const {return -numeric_limits<result_type>::infinity();} 102 _LIBCPP_INLINE_VISIBILITY 103 result_type max() const {return numeric_limits<result_type>::infinity();} 104 105 friend _LIBCPP_INLINE_VISIBILITY 106 bool operator==(const extreme_value_distribution& __x, 107 const extreme_value_distribution& __y) 108 {return __x.__p_ == __y.__p_;} 109 friend _LIBCPP_INLINE_VISIBILITY 110 bool operator!=(const extreme_value_distribution& __x, 111 const extreme_value_distribution& __y) 112 {return !(__x == __y);} 113 }; 114 115 template<class _RealType> 116 template<class _URNG> 117 _RealType 118 extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) 119 { 120 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 121 return __p.a() - __p.b() * 122 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g))); 123 } 124 125 template <class _CharT, class _Traits, class _RT> 126 basic_ostream<_CharT, _Traits>& 127 operator<<(basic_ostream<_CharT, _Traits>& __os, 128 const extreme_value_distribution<_RT>& __x) 129 { 130 __save_flags<_CharT, _Traits> __lx(__os); 131 typedef basic_ostream<_CharT, _Traits> _OStream; 132 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | 133 _OStream::scientific); 134 _CharT __sp = __os.widen(' '); 135 __os.fill(__sp); 136 __os << __x.a() << __sp << __x.b(); 137 return __os; 138 } 139 140 template <class _CharT, class _Traits, class _RT> 141 basic_istream<_CharT, _Traits>& 142 operator>>(basic_istream<_CharT, _Traits>& __is, 143 extreme_value_distribution<_RT>& __x) 144 { 145 typedef extreme_value_distribution<_RT> _Eng; 146 typedef typename _Eng::result_type result_type; 147 typedef typename _Eng::param_type param_type; 148 __save_flags<_CharT, _Traits> __lx(__is); 149 typedef basic_istream<_CharT, _Traits> _Istream; 150 __is.flags(_Istream::dec | _Istream::skipws); 151 result_type __a; 152 result_type __b; 153 __is >> __a >> __b; 154 if (!__is.fail()) 155 __x.param(param_type(__a, __b)); 156 return __is; 157 } 158 159 _LIBCPP_END_NAMESPACE_STD 160 161 _LIBCPP_POP_MACROS 162 163 #endif // _LIBCPP___RANDOM_EXTREME_VALUE_DISTRIBUTION_H 164