brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 8d1bfad Raw
124 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H10#define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H11 12#include <__config>13#include <__random/is_valid.h>14#include <__random/uniform_real_distribution.h>15#include <iosfwd>16 17#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)18#  pragma GCC system_header19#endif20 21_LIBCPP_PUSH_MACROS22#include <__undef_macros>23 24_LIBCPP_BEGIN_NAMESPACE_STD25 26class bernoulli_distribution {27public:28  // types29  typedef bool result_type;30 31  class 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 47private:48  param_type __p_;49 50public:51  // constructors and reset functions52#ifndef _LIBCPP_CXX03_LANG53  _LIBCPP_HIDE_FROM_ABI bernoulli_distribution() : bernoulli_distribution(0.5) {}54  _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}55#else56  _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}57#endif58  _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}59  _LIBCPP_HIDE_FROM_ABI void reset() {}60 61  // generating functions62  template <class _URNG>63  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {64    return (*this)(__g, __p_);65  }66  template <class _URNG>67  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);68 69  // property functions70  _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }71 72  _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }73  _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }74 75  _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }76  _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }77 78  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {79    return __x.__p_ == __y.__p_;80  }81  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {82    return !(__x == __y);83  }84};85 86template <class _URNG>87inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) {88  static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");89  uniform_real_distribution<double> __gen;90  return __gen(__g) < __p.p();91}92 93template <class _CharT, class _Traits>94_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&95operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) {96  __save_flags<_CharT, _Traits> __lx(__os);97  typedef basic_ostream<_CharT, _Traits> _OStream;98  __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);99  _CharT __sp = __os.widen(' ');100  __os.fill(__sp);101  return __os << __x.p();102}103 104template <class _CharT, class _Traits>105_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&106operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) {107  typedef bernoulli_distribution _Eng;108  typedef typename _Eng::param_type param_type;109  __save_flags<_CharT, _Traits> __lx(__is);110  typedef basic_istream<_CharT, _Traits> _Istream;111  __is.flags(_Istream::dec | _Istream::skipws);112  double __p;113  __is >> __p;114  if (!__is.fail())115    __x.param(param_type(__p));116  return __is;117}118 119_LIBCPP_END_NAMESPACE_STD120 121_LIBCPP_POP_MACROS122 123#endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H124