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___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/is_valid.h>
14 #include <__cxx03/__random/uniform_real_distribution.h>
15 #include <__cxx03/iosfwd>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
22 #include <__cxx03/__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
{
29 typedef bool result_type
;
31 class _LIBCPP_TEMPLATE_VIS param_type
{
35 typedef bernoulli_distribution distribution_type
;
37 _LIBCPP_HIDE_FROM_ABI
explicit param_type(double __p
= 0.5) : __p_(__p
) {}
39 _LIBCPP_HIDE_FROM_ABI
double p() const { return __p_
; }
41 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const param_type
& __x
, const param_type
& __y
) {
42 return __x
.__p_
== __y
.__p_
;
44 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const param_type
& __x
, const param_type
& __y
) { return !(__x
== __y
); }
51 // constructors and reset functions
52 #ifndef _LIBCPP_CXX03_LANG
53 _LIBCPP_HIDE_FROM_ABI
bernoulli_distribution() : bernoulli_distribution(0.5) {}
54 _LIBCPP_HIDE_FROM_ABI
explicit bernoulli_distribution(double __p
) : __p_(param_type(__p
)) {}
56 _LIBCPP_HIDE_FROM_ABI
explicit bernoulli_distribution(double __p
= 0.5) : __p_(param_type(__p
)) {}
58 _LIBCPP_HIDE_FROM_ABI
explicit bernoulli_distribution(const param_type
& __p
) : __p_(__p
) {}
59 _LIBCPP_HIDE_FROM_ABI
void reset() {}
61 // generating functions
62 template <class _URNG
>
63 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
) {
64 return (*this)(__g
, __p_
);
66 template <class _URNG
>
67 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
);
70 _LIBCPP_HIDE_FROM_ABI
double p() const { return __p_
.p(); }
72 _LIBCPP_HIDE_FROM_ABI param_type
param() const { return __p_
; }
73 _LIBCPP_HIDE_FROM_ABI
void param(const param_type
& __p
) { __p_
= __p
; }
75 _LIBCPP_HIDE_FROM_ABI result_type
min() const { return false; }
76 _LIBCPP_HIDE_FROM_ABI result_type
max() const { return true; }
78 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const bernoulli_distribution
& __x
, const bernoulli_distribution
& __y
) {
79 return __x
.__p_
== __y
.__p_
;
81 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const bernoulli_distribution
& __x
, const bernoulli_distribution
& __y
) {
86 template <class _URNG
>
87 inline bernoulli_distribution::result_type
bernoulli_distribution::operator()(_URNG
& __g
, const param_type
& __p
) {
88 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
89 uniform_real_distribution
<double> __gen
;
90 return __gen(__g
) < __p
.p();
93 template <class _CharT
, class _Traits
>
94 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
95 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const bernoulli_distribution
& __x
) {
96 __save_flags
<_CharT
, _Traits
> __lx(__os
);
97 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
98 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
| _OStream::scientific
);
99 _CharT __sp
= __os
.widen(' ');
101 return __os
<< __x
.p();
104 template <class _CharT
, class _Traits
>
105 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
106 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, bernoulli_distribution
& __x
) {
107 typedef bernoulli_distribution _Eng
;
108 typedef typename
_Eng::param_type param_type
;
109 __save_flags
<_CharT
, _Traits
> __lx(__is
);
110 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
111 __is
.flags(_Istream::dec
| _Istream::skipws
);
115 __x
.param(param_type(__p
));
119 _LIBCPP_END_NAMESPACE_STD
123 #endif // _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H