brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.4 KiB · cd22234 Raw
153 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___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H10#define _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H11 12#include <__cxx03/__assert>13#include <__cxx03/__config>14#include <__cxx03/__random/bernoulli_distribution.h>15#include <__cxx03/__random/gamma_distribution.h>16#include <__cxx03/__random/is_valid.h>17#include <__cxx03/__random/poisson_distribution.h>18#include <__cxx03/iosfwd>19#include <__cxx03/limits>20 21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22#  pragma GCC system_header23#endif24 25_LIBCPP_PUSH_MACROS26#include <__cxx03/__undef_macros>27 28_LIBCPP_BEGIN_NAMESPACE_STD29 30template <class _IntType = int>31class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution {32  static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");33 34public:35  // types36  typedef _IntType result_type;37 38  class _LIBCPP_TEMPLATE_VIS param_type {39    result_type __k_;40    double __p_;41 42  public:43    typedef negative_binomial_distribution distribution_type;44 45    _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __k = 1, double __p = 0.5) : __k_(__k), __p_(__p) {}46 47    _LIBCPP_HIDE_FROM_ABI result_type k() const { return __k_; }48    _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }49 50    friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {51      return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;52    }53    friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }54  };55 56private:57  param_type __p_;58 59public:60  // constructor and reset functions61  _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)62      : __p_(__k, __p) {}63  _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}64  _LIBCPP_HIDE_FROM_ABI void reset() {}65 66  // generating functions67  template <class _URNG>68  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {69    return (*this)(__g, __p_);70  }71  template <class _URNG>72  _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);73 74  // property functions75  _LIBCPP_HIDE_FROM_ABI result_type k() const { return __p_.k(); }76  _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }77 78  _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }79  _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }80 81  _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }82  _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }83 84  friend _LIBCPP_HIDE_FROM_ABI bool85  operator==(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {86    return __x.__p_ == __y.__p_;87  }88  friend _LIBCPP_HIDE_FROM_ABI bool89  operator!=(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {90    return !(__x == __y);91  }92};93 94template <class _IntType>95template <class _URNG>96_IntType negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {97  static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");98  result_type __k = __pr.k();99  double __p      = __pr.p();100  // When the number of bits in _IntType is small, we are too likely to101  // overflow __f below to use this technique.102  if (__k <= 21 * __p && sizeof(_IntType) > 1) {103    bernoulli_distribution __gen(__p);104    result_type __f = 0;105    result_type __s = 0;106    while (__s < __k) {107      if (__gen(__urng))108        ++__s;109      else110        ++__f;111    }112    _LIBCPP_ASSERT_INTERNAL(__f >= 0,113                            "std::negative_binomial_distribution should never produce negative values. "114                            "This is almost certainly a signed integer overflow issue on __f.");115    return __f;116  }117  return poisson_distribution<result_type>(gamma_distribution<double>(__k, (1 - __p) / __p)(__urng))(__urng);118}119 120template <class _CharT, class _Traits, class _IntType>121_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&122operator<<(basic_ostream<_CharT, _Traits>& __os, const negative_binomial_distribution<_IntType>& __x) {123  __save_flags<_CharT, _Traits> __lx(__os);124  typedef basic_ostream<_CharT, _Traits> _OStream;125  __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);126  _CharT __sp = __os.widen(' ');127  __os.fill(__sp);128  return __os << __x.k() << __sp << __x.p();129}130 131template <class _CharT, class _Traits, class _IntType>132_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&133operator>>(basic_istream<_CharT, _Traits>& __is, negative_binomial_distribution<_IntType>& __x) {134  typedef negative_binomial_distribution<_IntType> _Eng;135  typedef typename _Eng::result_type result_type;136  typedef typename _Eng::param_type param_type;137  __save_flags<_CharT, _Traits> __lx(__is);138  typedef basic_istream<_CharT, _Traits> _Istream;139  __is.flags(_Istream::dec | _Istream::skipws);140  result_type __k;141  double __p;142  __is >> __k >> __p;143  if (!__is.fail())144    __x.param(param_type(__k, __p));145  return __is;146}147 148_LIBCPP_END_NAMESPACE_STD149 150_LIBCPP_POP_MACROS151 152#endif // _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H153