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