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_UNIFORM_REAL_DISTRIBUTION_H 10 #define _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H 11 12 #include <__config> 13 #include <__random/generate_canonical.h> 14 #include <iosfwd> 15 #include <limits> 16 #include <type_traits> 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 uniform_real_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 uniform_real_distribution distribution_type; 40 41 _LIBCPP_INLINE_VISIBILITY 42 explicit param_type(result_type __a = 0, 43 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 // constructors and reset functions 64 #ifndef _LIBCPP_CXX03_LANG 65 _LIBCPP_INLINE_VISIBILITY 66 uniform_real_distribution() : uniform_real_distribution(0) {} 67 explicit uniform_real_distribution(result_type __a, result_type __b = 1) 68 : __p_(param_type(__a, __b)) {} 69 #else 70 _LIBCPP_INLINE_VISIBILITY 71 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) 72 : __p_(param_type(__a, __b)) {} 73 #endif 74 _LIBCPP_INLINE_VISIBILITY 75 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} 76 _LIBCPP_INLINE_VISIBILITY 77 void reset() {} 78 79 // generating functions 80 template<class _URNG> 81 _LIBCPP_INLINE_VISIBILITY 82 result_type operator()(_URNG& __g) 83 {return (*this)(__g, __p_);} 84 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); 85 86 // property functions 87 _LIBCPP_INLINE_VISIBILITY 88 result_type a() const {return __p_.a();} 89 _LIBCPP_INLINE_VISIBILITY 90 result_type b() const {return __p_.b();} 91 92 _LIBCPP_INLINE_VISIBILITY 93 param_type param() const {return __p_;} 94 _LIBCPP_INLINE_VISIBILITY 95 void param(const param_type& __p) {__p_ = __p;} 96 97 _LIBCPP_INLINE_VISIBILITY 98 result_type min() const {return a();} 99 _LIBCPP_INLINE_VISIBILITY 100 result_type max() const {return b();} 101 102 friend _LIBCPP_INLINE_VISIBILITY 103 bool operator==(const uniform_real_distribution& __x, 104 const uniform_real_distribution& __y) 105 {return __x.__p_ == __y.__p_;} 106 friend _LIBCPP_INLINE_VISIBILITY 107 bool operator!=(const uniform_real_distribution& __x, 108 const uniform_real_distribution& __y) 109 {return !(__x == __y);} 110 }; 111 112 template<class _RealType> 113 template<class _URNG> 114 inline 115 typename uniform_real_distribution<_RealType>::result_type 116 uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) 117 { 118 return (__p.b() - __p.a()) 119 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) 120 + __p.a(); 121 } 122 123 template <class _CharT, class _Traits, class _RT> 124 basic_ostream<_CharT, _Traits>& 125 operator<<(basic_ostream<_CharT, _Traits>& __os, 126 const uniform_real_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 return __os << __x.a() << __sp << __x.b(); 135 } 136 137 template <class _CharT, class _Traits, class _RT> 138 basic_istream<_CharT, _Traits>& 139 operator>>(basic_istream<_CharT, _Traits>& __is, 140 uniform_real_distribution<_RT>& __x) 141 { 142 typedef uniform_real_distribution<_RT> _Eng; 143 typedef typename _Eng::result_type result_type; 144 typedef typename _Eng::param_type param_type; 145 __save_flags<_CharT, _Traits> __lx(__is); 146 typedef basic_istream<_CharT, _Traits> _Istream; 147 __is.flags(_Istream::dec | _Istream::skipws); 148 result_type __a; 149 result_type __b; 150 __is >> __a >> __b; 151 if (!__is.fail()) 152 __x.param(param_type(__a, __b)); 153 return __is; 154 } 155 156 _LIBCPP_END_NAMESPACE_STD 157 158 _LIBCPP_POP_MACROS 159 160 #endif // _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H 161