1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
11344cef66SArthur O'Dwyer
12344cef66SArthur O'Dwyer #include <__config>
137624552eSArthur O'Dwyer #include <__random/is_valid.h>
14344cef66SArthur O'Dwyer #include <__random/uniform_real_distribution.h>
15344cef66SArthur O'Dwyer #include <iosfwd>
16344cef66SArthur O'Dwyer
17344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18344cef66SArthur O'Dwyer # pragma GCC system_header
19344cef66SArthur O'Dwyer #endif
20344cef66SArthur O'Dwyer
21344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
22344cef66SArthur O'Dwyer #include <__undef_macros>
23344cef66SArthur O'Dwyer
24344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
25344cef66SArthur O'Dwyer
26*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS bernoulli_distribution {
27344cef66SArthur O'Dwyer public:
28344cef66SArthur O'Dwyer // types
29344cef66SArthur O'Dwyer typedef bool result_type;
30344cef66SArthur O'Dwyer
31*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS param_type {
32344cef66SArthur O'Dwyer double __p_;
33*9783f28cSLouis Dionne
34344cef66SArthur O'Dwyer public:
35344cef66SArthur O'Dwyer typedef bernoulli_distribution distribution_type;
36344cef66SArthur O'Dwyer
__p_(__p)37*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
38344cef66SArthur O'Dwyer
p()39*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
40344cef66SArthur O'Dwyer
41*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
42*9783f28cSLouis Dionne return __x.__p_ == __y.__p_;
43*9783f28cSLouis Dionne }
44*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
45344cef66SArthur O'Dwyer };
46344cef66SArthur O'Dwyer
47344cef66SArthur O'Dwyer private:
48344cef66SArthur O'Dwyer param_type __p_;
49344cef66SArthur O'Dwyer
50344cef66SArthur O'Dwyer public:
51344cef66SArthur O'Dwyer // constructors and reset functions
52344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
bernoulli_distribution()53*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI bernoulli_distribution() : bernoulli_distribution(0.5) {}
bernoulli_distribution(double __p)54*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
55344cef66SArthur O'Dwyer #else
56*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
57344cef66SArthur O'Dwyer #endif
bernoulli_distribution(const param_type & __p)58*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
reset()59*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void reset() {}
60344cef66SArthur O'Dwyer
61344cef66SArthur O'Dwyer // generating functions
62344cef66SArthur O'Dwyer template <class _URNG>
operator()63*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
64*9783f28cSLouis Dionne return (*this)(__g, __p_);
65*9783f28cSLouis Dionne }
66*9783f28cSLouis Dionne template <class _URNG>
67*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
68344cef66SArthur O'Dwyer
69344cef66SArthur O'Dwyer // property functions
p()70*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
71344cef66SArthur O'Dwyer
param()72*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)73*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
74344cef66SArthur O'Dwyer
min()75*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }
max()76*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }
77344cef66SArthur O'Dwyer
78*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
79*9783f28cSLouis Dionne return __x.__p_ == __y.__p_;
80*9783f28cSLouis Dionne }
81*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
82*9783f28cSLouis Dionne return !(__x == __y);
83*9783f28cSLouis Dionne }
84344cef66SArthur O'Dwyer };
85344cef66SArthur O'Dwyer
86344cef66SArthur O'Dwyer template <class _URNG>
operator()87*9783f28cSLouis Dionne inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) {
887624552eSArthur O'Dwyer static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
89344cef66SArthur O'Dwyer uniform_real_distribution<double> __gen;
90344cef66SArthur O'Dwyer return __gen(__g) < __p.p();
91344cef66SArthur O'Dwyer }
92344cef66SArthur O'Dwyer
93344cef66SArthur O'Dwyer template <class _CharT, class _Traits>
9480c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
95*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) {
96344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__os);
97344cef66SArthur O'Dwyer typedef basic_ostream<_CharT, _Traits> _OStream;
98*9783f28cSLouis Dionne __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
99344cef66SArthur O'Dwyer _CharT __sp = __os.widen(' ');
100344cef66SArthur O'Dwyer __os.fill(__sp);
101344cef66SArthur O'Dwyer return __os << __x.p();
102344cef66SArthur O'Dwyer }
103344cef66SArthur O'Dwyer
104344cef66SArthur O'Dwyer template <class _CharT, class _Traits>
10580c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
106*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) {
107344cef66SArthur O'Dwyer typedef bernoulli_distribution _Eng;
108344cef66SArthur O'Dwyer typedef typename _Eng::param_type param_type;
109344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__is);
110344cef66SArthur O'Dwyer typedef basic_istream<_CharT, _Traits> _Istream;
111344cef66SArthur O'Dwyer __is.flags(_Istream::dec | _Istream::skipws);
112344cef66SArthur O'Dwyer double __p;
113344cef66SArthur O'Dwyer __is >> __p;
114344cef66SArthur O'Dwyer if (!__is.fail())
115344cef66SArthur O'Dwyer __x.param(param_type(__p));
116344cef66SArthur O'Dwyer return __is;
117344cef66SArthur O'Dwyer }
118344cef66SArthur O'Dwyer
119344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
120344cef66SArthur O'Dwyer
121344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
122344cef66SArthur O'Dwyer
123344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
124