1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
13 #include <__random/is_valid.h>
14 #include <__random/uniform_real_distribution.h>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
22 #include <__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
30 typedef bool result_type
;
32 class _LIBCPP_TEMPLATE_VIS param_type
36 typedef bernoulli_distribution distribution_type
;
38 _LIBCPP_INLINE_VISIBILITY
39 explicit param_type(double __p
= 0.5) : __p_(__p
) {}
41 _LIBCPP_INLINE_VISIBILITY
42 double p() const {return __p_
;}
44 friend _LIBCPP_INLINE_VISIBILITY
45 bool operator==(const param_type
& __x
, const param_type
& __y
)
46 {return __x
.__p_
== __y
.__p_
;}
47 friend _LIBCPP_INLINE_VISIBILITY
48 bool operator!=(const param_type
& __x
, const param_type
& __y
)
49 {return !(__x
== __y
);}
56 // constructors and reset functions
57 #ifndef _LIBCPP_CXX03_LANG
58 _LIBCPP_INLINE_VISIBILITY
59 bernoulli_distribution() : bernoulli_distribution(0.5) {}
60 _LIBCPP_INLINE_VISIBILITY
61 explicit bernoulli_distribution(double __p
) : __p_(param_type(__p
)) {}
63 _LIBCPP_INLINE_VISIBILITY
64 explicit bernoulli_distribution(double __p
= 0.5) : __p_(param_type(__p
)) {}
66 _LIBCPP_INLINE_VISIBILITY
67 explicit bernoulli_distribution(const param_type
& __p
) : __p_(__p
) {}
68 _LIBCPP_INLINE_VISIBILITY
71 // generating functions
73 _LIBCPP_INLINE_VISIBILITY
74 result_type
operator()(_URNG
& __g
)
75 {return (*this)(__g
, __p_
);}
76 template<class _URNG
> _LIBCPP_INLINE_VISIBILITY result_type
operator()(_URNG
& __g
, const param_type
& __p
);
79 _LIBCPP_INLINE_VISIBILITY
80 double p() const {return __p_
.p();}
82 _LIBCPP_INLINE_VISIBILITY
83 param_type
param() const {return __p_
;}
84 _LIBCPP_INLINE_VISIBILITY
85 void param(const param_type
& __p
) {__p_
= __p
;}
87 _LIBCPP_INLINE_VISIBILITY
88 result_type
min() const {return false;}
89 _LIBCPP_INLINE_VISIBILITY
90 result_type
max() const {return true;}
92 friend _LIBCPP_INLINE_VISIBILITY
93 bool operator==(const bernoulli_distribution
& __x
,
94 const bernoulli_distribution
& __y
)
95 {return __x
.__p_
== __y
.__p_
;}
96 friend _LIBCPP_INLINE_VISIBILITY
97 bool operator!=(const bernoulli_distribution
& __x
,
98 const bernoulli_distribution
& __y
)
99 {return !(__x
== __y
);}
102 template<class _URNG
>
104 bernoulli_distribution::result_type
105 bernoulli_distribution::operator()(_URNG
& __g
, const param_type
& __p
)
107 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
108 uniform_real_distribution
<double> __gen
;
109 return __gen(__g
) < __p
.p();
112 template <class _CharT
, class _Traits
>
113 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
114 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const bernoulli_distribution
& __x
)
116 __save_flags
<_CharT
, _Traits
> __lx(__os
);
117 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
118 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
|
119 _OStream::scientific
);
120 _CharT __sp
= __os
.widen(' ');
122 return __os
<< __x
.p();
125 template <class _CharT
, class _Traits
>
126 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
127 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, bernoulli_distribution
& __x
)
129 typedef bernoulli_distribution _Eng
;
130 typedef typename
_Eng::param_type param_type
;
131 __save_flags
<_CharT
, _Traits
> __lx(__is
);
132 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
133 __is
.flags(_Istream::dec
| _Istream::skipws
);
137 __x
.param(param_type(__p
));
141 _LIBCPP_END_NAMESPACE_STD
145 #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H