Warning: This file is not a C or C++ file. It does not have highlighting.
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___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H |
10 | #define _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H |
11 | |
12 | #include <__cxx03/__config> |
13 | #include <__cxx03/__random/is_valid.h> |
14 | #include <__cxx03/__random/uniform_real_distribution.h> |
15 | #include <__cxx03/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 <__cxx03/__undef_macros> |
23 | |
24 | _LIBCPP_BEGIN_NAMESPACE_STD |
25 | |
26 | class _LIBCPP_TEMPLATE_VIS bernoulli_distribution { |
27 | public: |
28 | // types |
29 | typedef bool result_type; |
30 | |
31 | class _LIBCPP_TEMPLATE_VIS 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 | _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {} |
53 | _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
54 | _LIBCPP_HIDE_FROM_ABI void reset() {} |
55 | |
56 | // generating functions |
57 | template <class _URNG> |
58 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { |
59 | return (*this)(__g, __p_); |
60 | } |
61 | template <class _URNG> |
62 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); |
63 | |
64 | // property functions |
65 | _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); } |
66 | |
67 | _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } |
68 | _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } |
69 | |
70 | _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; } |
71 | _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; } |
72 | |
73 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) { |
74 | return __x.__p_ == __y.__p_; |
75 | } |
76 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) { |
77 | return !(__x == __y); |
78 | } |
79 | }; |
80 | |
81 | template <class _URNG> |
82 | inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) { |
83 | static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); |
84 | uniform_real_distribution<double> __gen; |
85 | return __gen(__g) < __p.p(); |
86 | } |
87 | |
88 | template <class _CharT, class _Traits> |
89 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
90 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) { |
91 | __save_flags<_CharT, _Traits> __lx(__os); |
92 | typedef basic_ostream<_CharT, _Traits> _OStream; |
93 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); |
94 | _CharT __sp = __os.widen(' '); |
95 | __os.fill(__sp); |
96 | return __os << __x.p(); |
97 | } |
98 | |
99 | template <class _CharT, class _Traits> |
100 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
101 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) { |
102 | typedef bernoulli_distribution _Eng; |
103 | typedef typename _Eng::param_type param_type; |
104 | __save_flags<_CharT, _Traits> __lx(__is); |
105 | typedef basic_istream<_CharT, _Traits> _Istream; |
106 | __is.flags(_Istream::dec | _Istream::skipws); |
107 | double __p; |
108 | __is >> __p; |
109 | if (!__is.fail()) |
110 | __x.param(param_type(__p)); |
111 | return __is; |
112 | } |
113 | |
114 | _LIBCPP_END_NAMESPACE_STD |
115 | |
116 | _LIBCPP_POP_MACROS |
117 | |
118 | #endif // _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H |
119 |
Warning: This file is not a C or C++ file. It does not have highlighting.