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