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___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
11 
12 #include <__config>
13 #include <__random/bernoulli_distribution.h>
14 #include <__random/gamma_distribution.h>
15 #include <__random/is_valid.h>
16 #include <__random/poisson_distribution.h>
17 #include <iosfwd>
18 #include <limits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template<class _IntType = int>
30 class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution
31 {
32     static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be an integer type larger than char");
33 public:
34     // types
35     typedef _IntType result_type;
36 
37     class _LIBCPP_TEMPLATE_VIS param_type
38     {
39         result_type __k_;
40         double __p_;
41     public:
42         typedef negative_binomial_distribution distribution_type;
43 
44         _LIBCPP_INLINE_VISIBILITY
45         explicit param_type(result_type __k = 1, double __p = 0.5)
46             : __k_(__k), __p_(__p) {}
47 
48         _LIBCPP_INLINE_VISIBILITY
49         result_type k() const {return __k_;}
50         _LIBCPP_INLINE_VISIBILITY
51         double p() const {return __p_;}
52 
53         friend _LIBCPP_INLINE_VISIBILITY
54             bool operator==(const param_type& __x, const param_type& __y)
55             {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
56         friend _LIBCPP_INLINE_VISIBILITY
57             bool operator!=(const param_type& __x, const param_type& __y)
58             {return !(__x == __y);}
59     };
60 
61 private:
62     param_type __p_;
63 
64 public:
65     // constructor and reset functions
66 #ifndef _LIBCPP_CXX03_LANG
67     _LIBCPP_INLINE_VISIBILITY
68     negative_binomial_distribution() : negative_binomial_distribution(1) {}
69     _LIBCPP_INLINE_VISIBILITY
70     explicit negative_binomial_distribution(result_type __k, double __p = 0.5)
71         : __p_(__k, __p) {}
72 #else
73     _LIBCPP_INLINE_VISIBILITY
74     explicit negative_binomial_distribution(result_type __k = 1,
75                                             double __p = 0.5)
76         : __p_(__k, __p) {}
77 #endif
78     _LIBCPP_INLINE_VISIBILITY
79     explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
80     _LIBCPP_INLINE_VISIBILITY
81     void reset() {}
82 
83     // generating functions
84     template<class _URNG>
85         _LIBCPP_INLINE_VISIBILITY
86         result_type operator()(_URNG& __g)
87         {return (*this)(__g, __p_);}
88     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
89 
90     // property functions
91     _LIBCPP_INLINE_VISIBILITY
92     result_type k() const {return __p_.k();}
93     _LIBCPP_INLINE_VISIBILITY
94     double p() const {return __p_.p();}
95 
96     _LIBCPP_INLINE_VISIBILITY
97     param_type param() const {return __p_;}
98     _LIBCPP_INLINE_VISIBILITY
99     void param(const param_type& __p) {__p_ = __p;}
100 
101     _LIBCPP_INLINE_VISIBILITY
102     result_type min() const {return 0;}
103     _LIBCPP_INLINE_VISIBILITY
104     result_type max() const {return numeric_limits<result_type>::max();}
105 
106     friend _LIBCPP_INLINE_VISIBILITY
107         bool operator==(const negative_binomial_distribution& __x,
108                         const negative_binomial_distribution& __y)
109         {return __x.__p_ == __y.__p_;}
110     friend _LIBCPP_INLINE_VISIBILITY
111         bool operator!=(const negative_binomial_distribution& __x,
112                         const negative_binomial_distribution& __y)
113         {return !(__x == __y);}
114 };
115 
116 template <class _IntType>
117 template<class _URNG>
118 _IntType
119 negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
120 {
121     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
122     result_type __k = __pr.k();
123     double __p = __pr.p();
124     if (__k <= 21 * __p)
125     {
126         bernoulli_distribution __gen(__p);
127         result_type __f = 0;
128         result_type __s = 0;
129         while (__s < __k)
130         {
131             if (__gen(__urng))
132                 ++__s;
133             else
134                 ++__f;
135         }
136         return __f;
137     }
138     return poisson_distribution<result_type>(gamma_distribution<double>
139                                             (__k, (1-__p)/__p)(__urng))(__urng);
140 }
141 
142 template <class _CharT, class _Traits, class _IntType>
143 basic_ostream<_CharT, _Traits>&
144 operator<<(basic_ostream<_CharT, _Traits>& __os,
145            const negative_binomial_distribution<_IntType>& __x)
146 {
147     __save_flags<_CharT, _Traits> __lx(__os);
148     typedef basic_ostream<_CharT, _Traits> _OStream;
149     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
150                _OStream::scientific);
151     _CharT __sp = __os.widen(' ');
152     __os.fill(__sp);
153     return __os << __x.k() << __sp << __x.p();
154 }
155 
156 template <class _CharT, class _Traits, class _IntType>
157 basic_istream<_CharT, _Traits>&
158 operator>>(basic_istream<_CharT, _Traits>& __is,
159            negative_binomial_distribution<_IntType>& __x)
160 {
161     typedef negative_binomial_distribution<_IntType> _Eng;
162     typedef typename _Eng::result_type result_type;
163     typedef typename _Eng::param_type param_type;
164     __save_flags<_CharT, _Traits> __lx(__is);
165     typedef basic_istream<_CharT, _Traits> _Istream;
166     __is.flags(_Istream::dec | _Istream::skipws);
167     result_type __k;
168     double __p;
169     __is >> __k >> __p;
170     if (!__is.fail())
171         __x.param(param_type(__k, __p));
172     return __is;
173 }
174 
175 _LIBCPP_END_NAMESPACE_STD
176 
177 _LIBCPP_POP_MACROS
178 
179 #endif // _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
180