1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
11344cef66SArthur O'Dwyer
12*37dca605SLouis Dionne #include <__assert>
13344cef66SArthur O'Dwyer #include <__config>
14344cef66SArthur O'Dwyer #include <__random/bernoulli_distribution.h>
15344cef66SArthur O'Dwyer #include <__random/gamma_distribution.h>
16a3255f21SArthur O'Dwyer #include <__random/is_valid.h>
17344cef66SArthur O'Dwyer #include <__random/poisson_distribution.h>
18344cef66SArthur O'Dwyer #include <iosfwd>
19344cef66SArthur O'Dwyer #include <limits>
20344cef66SArthur O'Dwyer
21344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22344cef66SArthur O'Dwyer # pragma GCC system_header
23344cef66SArthur O'Dwyer #endif
24344cef66SArthur O'Dwyer
25344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
26344cef66SArthur O'Dwyer #include <__undef_macros>
27344cef66SArthur O'Dwyer
28344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
29344cef66SArthur O'Dwyer
30344cef66SArthur O'Dwyer template <class _IntType = int>
319783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution {
3207e984bcSLouis Dionne static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
339783f28cSLouis Dionne
34344cef66SArthur O'Dwyer public:
35344cef66SArthur O'Dwyer // types
36344cef66SArthur O'Dwyer typedef _IntType result_type;
37344cef66SArthur O'Dwyer
389783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS param_type {
39344cef66SArthur O'Dwyer result_type __k_;
40344cef66SArthur O'Dwyer double __p_;
419783f28cSLouis Dionne
42344cef66SArthur O'Dwyer public:
43344cef66SArthur O'Dwyer typedef negative_binomial_distribution distribution_type;
44344cef66SArthur O'Dwyer
__k_(__k)459783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __k = 1, double __p = 0.5) : __k_(__k), __p_(__p) {}
46344cef66SArthur O'Dwyer
k()479783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type k() const { return __k_; }
p()489783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
49344cef66SArthur O'Dwyer
509783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
519783f28cSLouis Dionne return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;
529783f28cSLouis Dionne }
539783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
54344cef66SArthur O'Dwyer };
55344cef66SArthur O'Dwyer
56344cef66SArthur O'Dwyer private:
57344cef66SArthur O'Dwyer param_type __p_;
58344cef66SArthur O'Dwyer
59344cef66SArthur O'Dwyer public:
60344cef66SArthur O'Dwyer // constructor and reset functions
61344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
negative_binomial_distribution()629783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI negative_binomial_distribution() : negative_binomial_distribution(1) {}
__p_(__k,__p)639783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k, double __p = 0.5) : __p_(__k, __p) {}
64344cef66SArthur O'Dwyer #else
659783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
66344cef66SArthur O'Dwyer : __p_(__k, __p) {}
67344cef66SArthur O'Dwyer #endif
negative_binomial_distribution(const param_type & __p)689783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
reset()699783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void reset() {}
70344cef66SArthur O'Dwyer
71344cef66SArthur O'Dwyer // generating functions
72344cef66SArthur O'Dwyer template <class _URNG>
operator()739783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
749783f28cSLouis Dionne return (*this)(__g, __p_);
759783f28cSLouis Dionne }
7683ce1397SNikolas Klauser template <class _URNG>
7783ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
78344cef66SArthur O'Dwyer
79344cef66SArthur O'Dwyer // property functions
k()809783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type k() const { return __p_.k(); }
p()819783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
82344cef66SArthur O'Dwyer
param()839783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)849783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
85344cef66SArthur O'Dwyer
min()869783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()879783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
88344cef66SArthur O'Dwyer
899783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool
909783f28cSLouis Dionne operator==(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
919783f28cSLouis Dionne return __x.__p_ == __y.__p_;
929783f28cSLouis Dionne }
939783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool
949783f28cSLouis Dionne operator!=(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
959783f28cSLouis Dionne return !(__x == __y);
969783f28cSLouis Dionne }
97344cef66SArthur O'Dwyer };
98344cef66SArthur O'Dwyer
99344cef66SArthur O'Dwyer template <class _IntType>
100344cef66SArthur O'Dwyer template <class _URNG>
operator()1019783f28cSLouis Dionne _IntType negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {
1027624552eSArthur O'Dwyer static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
103344cef66SArthur O'Dwyer result_type __k = __pr.k();
104344cef66SArthur O'Dwyer double __p = __pr.p();
10507e984bcSLouis Dionne // When the number of bits in _IntType is small, we are too likely to
10607e984bcSLouis Dionne // overflow __f below to use this technique.
1079783f28cSLouis Dionne if (__k <= 21 * __p && sizeof(_IntType) > 1) {
108344cef66SArthur O'Dwyer bernoulli_distribution __gen(__p);
109344cef66SArthur O'Dwyer result_type __f = 0;
110344cef66SArthur O'Dwyer result_type __s = 0;
1119783f28cSLouis Dionne while (__s < __k) {
112344cef66SArthur O'Dwyer if (__gen(__urng))
113344cef66SArthur O'Dwyer ++__s;
114344cef66SArthur O'Dwyer else
115344cef66SArthur O'Dwyer ++__f;
116344cef66SArthur O'Dwyer }
1174f215fddSKonstantin Varlamov _LIBCPP_ASSERT_INTERNAL(__f >= 0,
118cd0ad421Svarconst "std::negative_binomial_distribution should never produce negative values. "
11907e984bcSLouis Dionne "This is almost certainly a signed integer overflow issue on __f.");
120344cef66SArthur O'Dwyer return __f;
121344cef66SArthur O'Dwyer }
1229783f28cSLouis Dionne return poisson_distribution<result_type>(gamma_distribution<double>(__k, (1 - __p) / __p)(__urng))(__urng);
123344cef66SArthur O'Dwyer }
124344cef66SArthur O'Dwyer
125344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IntType>
12680c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1279783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const negative_binomial_distribution<_IntType>& __x) {
128344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__os);
129344cef66SArthur O'Dwyer typedef basic_ostream<_CharT, _Traits> _OStream;
1309783f28cSLouis Dionne __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
131344cef66SArthur O'Dwyer _CharT __sp = __os.widen(' ');
132344cef66SArthur O'Dwyer __os.fill(__sp);
133344cef66SArthur O'Dwyer return __os << __x.k() << __sp << __x.p();
134344cef66SArthur O'Dwyer }
135344cef66SArthur O'Dwyer
136344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IntType>
13780c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1389783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, negative_binomial_distribution<_IntType>& __x) {
139344cef66SArthur O'Dwyer typedef negative_binomial_distribution<_IntType> _Eng;
140344cef66SArthur O'Dwyer typedef typename _Eng::result_type result_type;
141344cef66SArthur O'Dwyer typedef typename _Eng::param_type param_type;
142344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__is);
143344cef66SArthur O'Dwyer typedef basic_istream<_CharT, _Traits> _Istream;
144344cef66SArthur O'Dwyer __is.flags(_Istream::dec | _Istream::skipws);
145344cef66SArthur O'Dwyer result_type __k;
146344cef66SArthur O'Dwyer double __p;
147344cef66SArthur O'Dwyer __is >> __k >> __p;
148344cef66SArthur O'Dwyer if (!__is.fail())
149344cef66SArthur O'Dwyer __x.param(param_type(__k, __p));
150344cef66SArthur O'Dwyer return __is;
151344cef66SArthur O'Dwyer }
152344cef66SArthur O'Dwyer
153344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
154344cef66SArthur O'Dwyer
155344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
156344cef66SArthur O'Dwyer
157344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
158