xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__random/negative_binomial_distribution.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
11 
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_header
23 #endif
24 
25 _LIBCPP_PUSH_MACROS
26 #include <__cxx03/__undef_macros>
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 template <class _IntType = int>
31 class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution {
32   static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
33 
34 public:
35   // types
36   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 
__k_(__k)45     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __k = 1, double __p = 0.5) : __k_(__k), __p_(__p) {}
46 
k()47     _LIBCPP_HIDE_FROM_ABI result_type k() const { return __k_; }
p()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 
56 private:
57   param_type __p_;
58 
59 public:
60   // constructor and reset functions
61   _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
__p_(__k,__p)62       : __p_(__k, __p) {}
negative_binomial_distribution(const param_type & __p)63   _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
reset()64   _LIBCPP_HIDE_FROM_ABI void reset() {}
65 
66   // generating functions
67   template <class _URNG>
operator()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 functions
k()75   _LIBCPP_HIDE_FROM_ABI result_type k() const { return __p_.k(); }
p()76   _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
77 
param()78   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)79   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
80 
min()81   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()82   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
83 
84   friend _LIBCPP_HIDE_FROM_ABI bool
85   operator==(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
86     return __x.__p_ == __y.__p_;
87   }
88   friend _LIBCPP_HIDE_FROM_ABI bool
89   operator!=(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
90     return !(__x == __y);
91   }
92 };
93 
94 template <class _IntType>
95 template <class _URNG>
operator()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 to
101   // 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       else
110         ++__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 
120 template <class _CharT, class _Traits, class _IntType>
121 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
122 operator<<(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 
131 template <class _CharT, class _Traits, class _IntType>
132 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
133 operator>>(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_STD
149 
150 _LIBCPP_POP_MACROS
151 
152 #endif // _LIBCPP___CXX03___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
153