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 
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     _LIBCPP_HIDE_FROM_ABI 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     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
119     return (__p.b() - __p.a())
120         * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
121         + __p.a();
122 }
123 
124 template <class _CharT, class _Traits, class _RT>
125 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
126 operator<<(basic_ostream<_CharT, _Traits>& __os,
127            const uniform_real_distribution<_RT>& __x)
128 {
129     __save_flags<_CharT, _Traits> __lx(__os);
130     typedef basic_ostream<_CharT, _Traits> _OStream;
131     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
132                _OStream::scientific);
133     _CharT __sp = __os.widen(' ');
134     __os.fill(__sp);
135     return __os << __x.a() << __sp << __x.b();
136 }
137 
138 template <class _CharT, class _Traits, class _RT>
139 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
140 operator>>(basic_istream<_CharT, _Traits>& __is,
141            uniform_real_distribution<_RT>& __x)
142 {
143     typedef uniform_real_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_UNIFORM_REAL_DISTRIBUTION_H
162