xref: /freebsd/contrib/llvm-project/libcxx/include/__random/bernoulli_distribution.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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_BERNOULLI_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
11 
12 #include <__config>
13 #include <__random/uniform_real_distribution.h>
14 #include <iosfwd>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
18 #endif
19 
20 _LIBCPP_PUSH_MACROS
21 #include <__undef_macros>
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
26 {
27 public:
28     // types
29     typedef bool result_type;
30 
31     class _LIBCPP_TEMPLATE_VIS param_type
32     {
33         double __p_;
34     public:
35         typedef bernoulli_distribution distribution_type;
36 
37         _LIBCPP_INLINE_VISIBILITY
38         explicit param_type(double __p = 0.5) : __p_(__p) {}
39 
40         _LIBCPP_INLINE_VISIBILITY
41         double p() const {return __p_;}
42 
43         friend _LIBCPP_INLINE_VISIBILITY
44             bool operator==(const param_type& __x, const param_type& __y)
45             {return __x.__p_ == __y.__p_;}
46         friend _LIBCPP_INLINE_VISIBILITY
47             bool operator!=(const param_type& __x, const param_type& __y)
48             {return !(__x == __y);}
49     };
50 
51 private:
52     param_type __p_;
53 
54 public:
55     // constructors and reset functions
56 #ifndef _LIBCPP_CXX03_LANG
57     _LIBCPP_INLINE_VISIBILITY
58     bernoulli_distribution() : bernoulli_distribution(0.5) {}
59     _LIBCPP_INLINE_VISIBILITY
60     explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
61 #else
62     _LIBCPP_INLINE_VISIBILITY
63     explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
64 #endif
65     _LIBCPP_INLINE_VISIBILITY
66     explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
67     _LIBCPP_INLINE_VISIBILITY
68     void reset() {}
69 
70     // generating functions
71     template<class _URNG>
72         _LIBCPP_INLINE_VISIBILITY
73         result_type operator()(_URNG& __g)
74         {return (*this)(__g, __p_);}
75     template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
76 
77     // property functions
78     _LIBCPP_INLINE_VISIBILITY
79     double p() const {return __p_.p();}
80 
81     _LIBCPP_INLINE_VISIBILITY
82     param_type param() const {return __p_;}
83     _LIBCPP_INLINE_VISIBILITY
84     void param(const param_type& __p) {__p_ = __p;}
85 
86     _LIBCPP_INLINE_VISIBILITY
87     result_type min() const {return false;}
88     _LIBCPP_INLINE_VISIBILITY
89     result_type max() const {return true;}
90 
91     friend _LIBCPP_INLINE_VISIBILITY
92         bool operator==(const bernoulli_distribution& __x,
93                         const bernoulli_distribution& __y)
94         {return __x.__p_ == __y.__p_;}
95     friend _LIBCPP_INLINE_VISIBILITY
96         bool operator!=(const bernoulli_distribution& __x,
97                         const bernoulli_distribution& __y)
98         {return !(__x == __y);}
99 };
100 
101 template<class _URNG>
102 inline
103 bernoulli_distribution::result_type
104 bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
105 {
106     uniform_real_distribution<double> __gen;
107     return __gen(__g) < __p.p();
108 }
109 
110 template <class _CharT, class _Traits>
111 basic_ostream<_CharT, _Traits>&
112 operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
113 {
114     __save_flags<_CharT, _Traits> __lx(__os);
115     typedef basic_ostream<_CharT, _Traits> _OStream;
116     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
117                _OStream::scientific);
118     _CharT __sp = __os.widen(' ');
119     __os.fill(__sp);
120     return __os << __x.p();
121 }
122 
123 template <class _CharT, class _Traits>
124 basic_istream<_CharT, _Traits>&
125 operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
126 {
127     typedef bernoulli_distribution _Eng;
128     typedef typename _Eng::param_type param_type;
129     __save_flags<_CharT, _Traits> __lx(__is);
130     typedef basic_istream<_CharT, _Traits> _Istream;
131     __is.flags(_Istream::dec | _Istream::skipws);
132     double __p;
133     __is >> __p;
134     if (!__is.fail())
135         __x.param(param_type(__p));
136     return __is;
137 }
138 
139 _LIBCPP_END_NAMESPACE_STD
140 
141 _LIBCPP_POP_MACROS
142 
143 #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
144