Path: blob/main/contrib/llvm-project/libcxx/include/__random/bernoulli_distribution.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H9#define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H1011#include <__config>12#include <__random/is_valid.h>13#include <__random/uniform_real_distribution.h>14#include <iosfwd>1516#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif1920_LIBCPP_PUSH_MACROS21#include <__undef_macros>2223_LIBCPP_BEGIN_NAMESPACE_STD2425class _LIBCPP_TEMPLATE_VIS bernoulli_distribution {26public:27// types28typedef bool result_type;2930class _LIBCPP_TEMPLATE_VIS param_type {31double __p_;3233public:34typedef bernoulli_distribution distribution_type;3536_LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}3738_LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }3940friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {41return __x.__p_ == __y.__p_;42}43friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }44};4546private:47param_type __p_;4849public:50// constructors and reset functions51#ifndef _LIBCPP_CXX03_LANG52_LIBCPP_HIDE_FROM_ABI bernoulli_distribution() : bernoulli_distribution(0.5) {}53_LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}54#else55_LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}56#endif57_LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}58_LIBCPP_HIDE_FROM_ABI void reset() {}5960// generating functions61template <class _URNG>62_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {63return (*this)(__g, __p_);64}65template <class _URNG>66_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);6768// property functions69_LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }7071_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }72_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }7374_LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }75_LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }7677friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {78return __x.__p_ == __y.__p_;79}80friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {81return !(__x == __y);82}83};8485template <class _URNG>86inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) {87static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");88uniform_real_distribution<double> __gen;89return __gen(__g) < __p.p();90}9192template <class _CharT, class _Traits>93_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&94operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) {95__save_flags<_CharT, _Traits> __lx(__os);96typedef basic_ostream<_CharT, _Traits> _OStream;97__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);98_CharT __sp = __os.widen(' ');99__os.fill(__sp);100return __os << __x.p();101}102103template <class _CharT, class _Traits>104_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&105operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) {106typedef bernoulli_distribution _Eng;107typedef typename _Eng::param_type param_type;108__save_flags<_CharT, _Traits> __lx(__is);109typedef basic_istream<_CharT, _Traits> _Istream;110__is.flags(_Istream::dec | _Istream::skipws);111double __p;112__is >> __p;113if (!__is.fail())114__x.param(param_type(__p));115return __is;116}117118_LIBCPP_END_NAMESPACE_STD119120_LIBCPP_POP_MACROS121122#endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H123124125