File indexing completed on 2026-05-03 08:13:39
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
0010 #define _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
0011
0012 #include <__cxx03/__assert>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__random/bernoulli_distribution.h>
0015 #include <__cxx03/__random/gamma_distribution.h>
0016 #include <__cxx03/__random/is_valid.h>
0017 #include <__cxx03/__random/poisson_distribution.h>
0018 #include <__cxx03/iosfwd>
0019 #include <__cxx03/limits>
0020
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 # pragma GCC system_header
0023 #endif
0024
0025 _LIBCPP_PUSH_MACROS
0026 #include <__cxx03/__undef_macros>
0027
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029
0030 template <class _IntType = int>
0031 class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution {
0032 static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
0033
0034 public:
0035
0036 typedef _IntType result_type;
0037
0038 class _LIBCPP_TEMPLATE_VIS param_type {
0039 result_type __k_;
0040 double __p_;
0041
0042 public:
0043 typedef negative_binomial_distribution distribution_type;
0044
0045 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __k = 1, double __p = 0.5) : __k_(__k), __p_(__p) {}
0046
0047 _LIBCPP_HIDE_FROM_ABI result_type k() const { return __k_; }
0048 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
0049
0050 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0051 return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;
0052 }
0053 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0054 };
0055
0056 private:
0057 param_type __p_;
0058
0059 public:
0060
0061 #ifndef _LIBCPP_CXX03_LANG
0062 _LIBCPP_HIDE_FROM_ABI negative_binomial_distribution() : negative_binomial_distribution(1) {}
0063 _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k, double __p = 0.5) : __p_(__k, __p) {}
0064 #else
0065 _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
0066 : __p_(__k, __p) {}
0067 #endif
0068 _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
0069 _LIBCPP_HIDE_FROM_ABI void reset() {}
0070
0071
0072 template <class _URNG>
0073 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0074 return (*this)(__g, __p_);
0075 }
0076 template <class _URNG>
0077 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0078
0079
0080 _LIBCPP_HIDE_FROM_ABI result_type k() const { return __p_.k(); }
0081 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
0082
0083 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0084 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0085
0086 _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
0087 _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
0088
0089 friend _LIBCPP_HIDE_FROM_ABI bool
0090 operator==(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
0091 return __x.__p_ == __y.__p_;
0092 }
0093 friend _LIBCPP_HIDE_FROM_ABI bool
0094 operator!=(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
0095 return !(__x == __y);
0096 }
0097 };
0098
0099 template <class _IntType>
0100 template <class _URNG>
0101 _IntType negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {
0102 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0103 result_type __k = __pr.k();
0104 double __p = __pr.p();
0105
0106
0107 if (__k <= 21 * __p && sizeof(_IntType) > 1) {
0108 bernoulli_distribution __gen(__p);
0109 result_type __f = 0;
0110 result_type __s = 0;
0111 while (__s < __k) {
0112 if (__gen(__urng))
0113 ++__s;
0114 else
0115 ++__f;
0116 }
0117 _LIBCPP_ASSERT_INTERNAL(__f >= 0,
0118 "std::negative_binomial_distribution should never produce negative values. "
0119 "This is almost certainly a signed integer overflow issue on __f.");
0120 return __f;
0121 }
0122 return poisson_distribution<result_type>(gamma_distribution<double>(__k, (1 - __p) / __p)(__urng))(__urng);
0123 }
0124
0125 template <class _CharT, class _Traits, class _IntType>
0126 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0127 operator<<(basic_ostream<_CharT, _Traits>& __os, const negative_binomial_distribution<_IntType>& __x) {
0128 __save_flags<_CharT, _Traits> __lx(__os);
0129 typedef basic_ostream<_CharT, _Traits> _OStream;
0130 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0131 _CharT __sp = __os.widen(' ');
0132 __os.fill(__sp);
0133 return __os << __x.k() << __sp << __x.p();
0134 }
0135
0136 template <class _CharT, class _Traits, class _IntType>
0137 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0138 operator>>(basic_istream<_CharT, _Traits>& __is, negative_binomial_distribution<_IntType>& __x) {
0139 typedef negative_binomial_distribution<_IntType> _Eng;
0140 typedef typename _Eng::result_type result_type;
0141 typedef typename _Eng::param_type param_type;
0142 __save_flags<_CharT, _Traits> __lx(__is);
0143 typedef basic_istream<_CharT, _Traits> _Istream;
0144 __is.flags(_Istream::dec | _Istream::skipws);
0145 result_type __k;
0146 double __p;
0147 __is >> __k >> __p;
0148 if (!__is.fail())
0149 __x.param(param_type(__k, __p));
0150 return __is;
0151 }
0152
0153 _LIBCPP_END_NAMESPACE_STD
0154
0155 _LIBCPP_POP_MACROS
0156
0157 #endif