14824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 24824e7fdSDimitry Andric // 34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64824e7fdSDimitry Andric // 74824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 84824e7fdSDimitry Andric 94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H 104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H 114824e7fdSDimitry Andric 124824e7fdSDimitry Andric #include <__config> 1381ad6265SDimitry Andric #include <__random/is_valid.h> 144824e7fdSDimitry Andric #include <__random/uniform_real_distribution.h> 154824e7fdSDimitry Andric #include <iosfwd> 164824e7fdSDimitry Andric 174824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 184824e7fdSDimitry Andric # pragma GCC system_header 194824e7fdSDimitry Andric #endif 204824e7fdSDimitry Andric 214824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS 224824e7fdSDimitry Andric #include <__undef_macros> 234824e7fdSDimitry Andric 244824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 254824e7fdSDimitry Andric 264824e7fdSDimitry Andric class _LIBCPP_TEMPLATE_VIS bernoulli_distribution 274824e7fdSDimitry Andric { 284824e7fdSDimitry Andric public: 294824e7fdSDimitry Andric // types 304824e7fdSDimitry Andric typedef bool result_type; 314824e7fdSDimitry Andric 324824e7fdSDimitry Andric class _LIBCPP_TEMPLATE_VIS param_type 334824e7fdSDimitry Andric { 344824e7fdSDimitry Andric double __p_; 354824e7fdSDimitry Andric public: 364824e7fdSDimitry Andric typedef bernoulli_distribution distribution_type; 374824e7fdSDimitry Andric 384824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 394824e7fdSDimitry Andric explicit param_type(double __p = 0.5) : __p_(__p) {} 404824e7fdSDimitry Andric 414824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 424824e7fdSDimitry Andric double p() const {return __p_;} 434824e7fdSDimitry Andric 444824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 454824e7fdSDimitry Andric bool operator==(const param_type& __x, const param_type& __y) 464824e7fdSDimitry Andric {return __x.__p_ == __y.__p_;} 474824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 484824e7fdSDimitry Andric bool operator!=(const param_type& __x, const param_type& __y) 494824e7fdSDimitry Andric {return !(__x == __y);} 504824e7fdSDimitry Andric }; 514824e7fdSDimitry Andric 524824e7fdSDimitry Andric private: 534824e7fdSDimitry Andric param_type __p_; 544824e7fdSDimitry Andric 554824e7fdSDimitry Andric public: 564824e7fdSDimitry Andric // constructors and reset functions 574824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG 584824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 594824e7fdSDimitry Andric bernoulli_distribution() : bernoulli_distribution(0.5) {} 604824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 614824e7fdSDimitry Andric explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {} 624824e7fdSDimitry Andric #else 634824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 644824e7fdSDimitry Andric explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {} 654824e7fdSDimitry Andric #endif 664824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 674824e7fdSDimitry Andric explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} 684824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 694824e7fdSDimitry Andric void reset() {} 704824e7fdSDimitry Andric 714824e7fdSDimitry Andric // generating functions 724824e7fdSDimitry Andric template<class _URNG> 734824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 744824e7fdSDimitry Andric result_type operator()(_URNG& __g) 754824e7fdSDimitry Andric {return (*this)(__g, __p_);} 764824e7fdSDimitry Andric template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); 774824e7fdSDimitry Andric 784824e7fdSDimitry Andric // property functions 794824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 804824e7fdSDimitry Andric double p() const {return __p_.p();} 814824e7fdSDimitry Andric 824824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 834824e7fdSDimitry Andric param_type param() const {return __p_;} 844824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 854824e7fdSDimitry Andric void param(const param_type& __p) {__p_ = __p;} 864824e7fdSDimitry Andric 874824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 884824e7fdSDimitry Andric result_type min() const {return false;} 894824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 904824e7fdSDimitry Andric result_type max() const {return true;} 914824e7fdSDimitry Andric 924824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 934824e7fdSDimitry Andric bool operator==(const bernoulli_distribution& __x, 944824e7fdSDimitry Andric const bernoulli_distribution& __y) 954824e7fdSDimitry Andric {return __x.__p_ == __y.__p_;} 964824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 974824e7fdSDimitry Andric bool operator!=(const bernoulli_distribution& __x, 984824e7fdSDimitry Andric const bernoulli_distribution& __y) 994824e7fdSDimitry Andric {return !(__x == __y);} 1004824e7fdSDimitry Andric }; 1014824e7fdSDimitry Andric 1024824e7fdSDimitry Andric template<class _URNG> 1034824e7fdSDimitry Andric inline 1044824e7fdSDimitry Andric bernoulli_distribution::result_type 1054824e7fdSDimitry Andric bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) 1064824e7fdSDimitry Andric { 10781ad6265SDimitry Andric static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 1084824e7fdSDimitry Andric uniform_real_distribution<double> __gen; 1094824e7fdSDimitry Andric return __gen(__g) < __p.p(); 1104824e7fdSDimitry Andric } 1114824e7fdSDimitry Andric 1124824e7fdSDimitry Andric template <class _CharT, class _Traits> 113*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1144824e7fdSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) 1154824e7fdSDimitry Andric { 1164824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__os); 1174824e7fdSDimitry Andric typedef basic_ostream<_CharT, _Traits> _OStream; 1184824e7fdSDimitry Andric __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | 1194824e7fdSDimitry Andric _OStream::scientific); 1204824e7fdSDimitry Andric _CharT __sp = __os.widen(' '); 1214824e7fdSDimitry Andric __os.fill(__sp); 1224824e7fdSDimitry Andric return __os << __x.p(); 1234824e7fdSDimitry Andric } 1244824e7fdSDimitry Andric 1254824e7fdSDimitry Andric template <class _CharT, class _Traits> 126*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 1274824e7fdSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) 1284824e7fdSDimitry Andric { 1294824e7fdSDimitry Andric typedef bernoulli_distribution _Eng; 1304824e7fdSDimitry Andric typedef typename _Eng::param_type param_type; 1314824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__is); 1324824e7fdSDimitry Andric typedef basic_istream<_CharT, _Traits> _Istream; 1334824e7fdSDimitry Andric __is.flags(_Istream::dec | _Istream::skipws); 1344824e7fdSDimitry Andric double __p; 1354824e7fdSDimitry Andric __is >> __p; 1364824e7fdSDimitry Andric if (!__is.fail()) 1374824e7fdSDimitry Andric __x.param(param_type(__p)); 1384824e7fdSDimitry Andric return __is; 1394824e7fdSDimitry Andric } 1404824e7fdSDimitry Andric 1414824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 1424824e7fdSDimitry Andric 1434824e7fdSDimitry Andric _LIBCPP_POP_MACROS 1444824e7fdSDimitry Andric 1454824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H 146