| 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/is_valid.h> |
| 14 | #include <__random/uniform_real_distribution.h> |
| 15 | #include <iosfwd> |
| 16 | |
| 17 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 18 | # pragma GCC system_header |
| 19 | #endif |
| 20 | |
| 21 | _LIBCPP_PUSH_MACROS |
| 22 | #include <__undef_macros> |
| 23 | |
| 24 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 25 | |
| 26 | class bernoulli_distribution { |
| 27 | public: |
| 28 | // types |
| 29 | typedef bool result_type; |
| 30 | |
| 31 | class param_type { |
| 32 | double __p_; |
| 33 | |
| 34 | public: |
| 35 | typedef bernoulli_distribution distribution_type; |
| 36 | |
| 37 | _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 38 | |
| 39 | _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; } |
| 40 | |
| 41 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { |
| 42 | return __x.__p_ == __y.__p_; |
| 43 | } |
| 44 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } |
| 45 | }; |
| 46 | |
| 47 | private: |
| 48 | param_type __p_; |
| 49 | |
| 50 | public: |
| 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)) {} |
| 55 | #else |
| 56 | _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {} |
| 57 | #endif |
| 58 | _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
| 59 | _LIBCPP_HIDE_FROM_ABI void reset() {} |
| 60 | |
| 61 | // generating functions |
| 62 | template <class _URNG> |
| 63 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { |
| 64 | return (*this)(__g, __p_); |
| 65 | } |
| 66 | template <class _URNG> |
| 67 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); |
| 68 | |
| 69 | // property functions |
| 70 | _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); } |
| 71 | |
| 72 | _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } |
| 73 | _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } |
| 74 | |
| 75 | _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; } |
| 76 | _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; } |
| 77 | |
| 78 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) { |
| 79 | return __x.__p_ == __y.__p_; |
| 80 | } |
| 81 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) { |
| 82 | return !(__x == __y); |
| 83 | } |
| 84 | }; |
| 85 | |
| 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(); |
| 91 | } |
| 92 | |
| 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(' '); |
| 100 | __os.fill(__sp); |
| 101 | return __os << __x.p(); |
| 102 | } |
| 103 | |
| 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); |
| 112 | double __p; |
| 113 | __is >> __p; |
| 114 | if (!__is.fail()) |
| 115 | __x.param(p: param_type(__p)); |
| 116 | return __is; |
| 117 | } |
| 118 | |
| 119 | _LIBCPP_END_NAMESPACE_STD |
| 120 | |
| 121 | _LIBCPP_POP_MACROS |
| 122 | |
| 123 | #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H |
| 124 | |