Path: blob/main/contrib/llvm-project/libcxx/include/__random/negative_binomial_distribution.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H9#define _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H1011#include <__assert>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>1920#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif2324_LIBCPP_PUSH_MACROS25#include <__undef_macros>2627_LIBCPP_BEGIN_NAMESPACE_STD2829template <class _IntType = int>30class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution {31static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");3233public:34// types35typedef _IntType result_type;3637class _LIBCPP_TEMPLATE_VIS param_type {38result_type __k_;39double __p_;4041public:42typedef negative_binomial_distribution distribution_type;4344_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __k = 1, double __p = 0.5) : __k_(__k), __p_(__p) {}4546_LIBCPP_HIDE_FROM_ABI result_type k() const { return __k_; }47_LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }4849friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {50return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;51}52friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }53};5455private:56param_type __p_;5758public:59// constructor and reset functions60#ifndef _LIBCPP_CXX03_LANG61_LIBCPP_HIDE_FROM_ABI negative_binomial_distribution() : negative_binomial_distribution(1) {}62_LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k, double __p = 0.5) : __p_(__k, __p) {}63#else64_LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)65: __p_(__k, __p) {}66#endif67_LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}68_LIBCPP_HIDE_FROM_ABI void reset() {}6970// generating functions71template <class _URNG>72_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {73return (*this)(__g, __p_);74}75template <class _URNG>76_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);7778// property functions79_LIBCPP_HIDE_FROM_ABI result_type k() const { return __p_.k(); }80_LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }8182_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }83_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8485_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }86_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }8788friend _LIBCPP_HIDE_FROM_ABI bool89operator==(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {90return __x.__p_ == __y.__p_;91}92friend _LIBCPP_HIDE_FROM_ABI bool93operator!=(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {94return !(__x == __y);95}96};9798template <class _IntType>99template <class _URNG>100_IntType negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {101static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");102result_type __k = __pr.k();103double __p = __pr.p();104// When the number of bits in _IntType is small, we are too likely to105// overflow __f below to use this technique.106if (__k <= 21 * __p && sizeof(_IntType) > 1) {107bernoulli_distribution __gen(__p);108result_type __f = 0;109result_type __s = 0;110while (__s < __k) {111if (__gen(__urng))112++__s;113else114++__f;115}116_LIBCPP_ASSERT_INTERNAL(__f >= 0,117"std::negative_binomial_distribution should never produce negative values. "118"This is almost certainly a signed integer overflow issue on __f.");119return __f;120}121return poisson_distribution<result_type>(gamma_distribution<double>(__k, (1 - __p) / __p)(__urng))(__urng);122}123124template <class _CharT, class _Traits, class _IntType>125_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&126operator<<(basic_ostream<_CharT, _Traits>& __os, const negative_binomial_distribution<_IntType>& __x) {127__save_flags<_CharT, _Traits> __lx(__os);128typedef basic_ostream<_CharT, _Traits> _OStream;129__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);130_CharT __sp = __os.widen(' ');131__os.fill(__sp);132return __os << __x.k() << __sp << __x.p();133}134135template <class _CharT, class _Traits, class _IntType>136_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&137operator>>(basic_istream<_CharT, _Traits>& __is, negative_binomial_distribution<_IntType>& __x) {138typedef negative_binomial_distribution<_IntType> _Eng;139typedef typename _Eng::result_type result_type;140typedef typename _Eng::param_type param_type;141__save_flags<_CharT, _Traits> __lx(__is);142typedef basic_istream<_CharT, _Traits> _Istream;143__is.flags(_Istream::dec | _Istream::skipws);144result_type __k;145double __p;146__is >> __k >> __p;147if (!__is.fail())148__x.param(param_type(__k, __p));149return __is;150}151152_LIBCPP_END_NAMESPACE_STD153154_LIBCPP_POP_MACROS155156#endif // _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H157158159