Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __random / bernoulli_distribution.h
blobe38d3dfb894a37a6fb6a8931c7056b00fcbb8a34
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
12 #include <__config>
13 #include <__random/is_valid.h>
14 #include <__random/uniform_real_distribution.h>
15 #include <iosfwd>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
19 #endif
21 _LIBCPP_PUSH_MACROS
22 #include <__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
28 public:
29 // types
30 typedef bool result_type;
32 class _LIBCPP_TEMPLATE_VIS param_type
34 double __p_;
35 public:
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);}
52 private:
53 param_type __p_;
55 public:
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)) {}
62 #else
63 _LIBCPP_INLINE_VISIBILITY
64 explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
65 #endif
66 _LIBCPP_INLINE_VISIBILITY
67 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
68 _LIBCPP_INLINE_VISIBILITY
69 void reset() {}
71 // generating functions
72 template<class _URNG>
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);
78 // property functions
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>
103 inline
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(' ');
121 __os.fill(__sp);
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);
134 double __p;
135 __is >> __p;
136 if (!__is.fail())
137 __x.param(param_type(__p));
138 return __is;
141 _LIBCPP_END_NAMESPACE_STD
143 _LIBCPP_POP_MACROS
145 #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H