File indexing completed on 2026-05-03 08:13:37
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H
0010 #define _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H
0011
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__random/is_valid.h>
0014 #include <__cxx03/__random/uniform_real_distribution.h>
0015 #include <__cxx03/iosfwd>
0016
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 # pragma GCC system_header
0019 #endif
0020
0021 _LIBCPP_PUSH_MACROS
0022 #include <__cxx03/__undef_macros>
0023
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025
0026 class _LIBCPP_TEMPLATE_VIS bernoulli_distribution {
0027 public:
0028
0029 typedef bool result_type;
0030
0031 class _LIBCPP_TEMPLATE_VIS param_type {
0032 double __p_;
0033
0034 public:
0035 typedef bernoulli_distribution distribution_type;
0036
0037 _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
0038
0039 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
0040
0041 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0042 return __x.__p_ == __y.__p_;
0043 }
0044 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0045 };
0046
0047 private:
0048 param_type __p_;
0049
0050 public:
0051
0052 #ifndef _LIBCPP_CXX03_LANG
0053 _LIBCPP_HIDE_FROM_ABI bernoulli_distribution() : bernoulli_distribution(0.5) {}
0054 _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
0055 #else
0056 _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
0057 #endif
0058 _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
0059 _LIBCPP_HIDE_FROM_ABI void reset() {}
0060
0061
0062 template <class _URNG>
0063 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0064 return (*this)(__g, __p_);
0065 }
0066 template <class _URNG>
0067 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0068
0069
0070 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
0071
0072 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0073 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0074
0075 _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }
0076 _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }
0077
0078 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
0079 return __x.__p_ == __y.__p_;
0080 }
0081 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
0082 return !(__x == __y);
0083 }
0084 };
0085
0086 template <class _URNG>
0087 inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) {
0088 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0089 uniform_real_distribution<double> __gen;
0090 return __gen(__g) < __p.p();
0091 }
0092
0093 template <class _CharT, class _Traits>
0094 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0095 operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) {
0096 __save_flags<_CharT, _Traits> __lx(__os);
0097 typedef basic_ostream<_CharT, _Traits> _OStream;
0098 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0099 _CharT __sp = __os.widen(' ');
0100 __os.fill(__sp);
0101 return __os << __x.p();
0102 }
0103
0104 template <class _CharT, class _Traits>
0105 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0106 operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) {
0107 typedef bernoulli_distribution _Eng;
0108 typedef typename _Eng::param_type param_type;
0109 __save_flags<_CharT, _Traits> __lx(__is);
0110 typedef basic_istream<_CharT, _Traits> _Istream;
0111 __is.flags(_Istream::dec | _Istream::skipws);
0112 double __p;
0113 __is >> __p;
0114 if (!__is.fail())
0115 __x.param(param_type(__p));
0116 return __is;
0117 }
0118
0119 _LIBCPP_END_NAMESPACE_STD
0120
0121 _LIBCPP_POP_MACROS
0122
0123 #endif