negative_binomial_distribution.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H
  10. #include <__assert>
  11. #include <__config>
  12. #include <__random/bernoulli_distribution.h>
  13. #include <__random/gamma_distribution.h>
  14. #include <__random/is_valid.h>
  15. #include <__random/poisson_distribution.h>
  16. #include <iosfwd>
  17. #include <limits>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_PUSH_MACROS
  22. #include <__undef_macros>
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class _IntType = int>
  25. class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution {
  26. static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
  27. public:
  28. // types
  29. typedef _IntType result_type;
  30. class _LIBCPP_TEMPLATE_VIS param_type {
  31. result_type __k_;
  32. double __p_;
  33. public:
  34. typedef negative_binomial_distribution distribution_type;
  35. _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __k = 1, double __p = 0.5) : __k_(__k), __p_(__p) {}
  36. _LIBCPP_HIDE_FROM_ABI result_type k() const { return __k_; }
  37. _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
  38. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
  39. return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;
  40. }
  41. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
  42. };
  43. private:
  44. param_type __p_;
  45. public:
  46. // constructor and reset functions
  47. #ifndef _LIBCPP_CXX03_LANG
  48. _LIBCPP_HIDE_FROM_ABI negative_binomial_distribution() : negative_binomial_distribution(1) {}
  49. _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k, double __p = 0.5) : __p_(__k, __p) {}
  50. #else
  51. _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
  52. : __p_(__k, __p) {}
  53. #endif
  54. _LIBCPP_HIDE_FROM_ABI explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
  55. _LIBCPP_HIDE_FROM_ABI void reset() {}
  56. // generating functions
  57. template <class _URNG>
  58. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
  59. return (*this)(__g, __p_);
  60. }
  61. template <class _URNG>
  62. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  63. // property functions
  64. _LIBCPP_HIDE_FROM_ABI result_type k() const { return __p_.k(); }
  65. _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
  66. _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
  67. _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
  68. _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
  69. _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
  70. friend _LIBCPP_HIDE_FROM_ABI bool
  71. operator==(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
  72. return __x.__p_ == __y.__p_;
  73. }
  74. friend _LIBCPP_HIDE_FROM_ABI bool
  75. operator!=(const negative_binomial_distribution& __x, const negative_binomial_distribution& __y) {
  76. return !(__x == __y);
  77. }
  78. };
  79. template <class _IntType>
  80. template <class _URNG>
  81. _IntType negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) {
  82. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  83. result_type __k = __pr.k();
  84. double __p = __pr.p();
  85. // When the number of bits in _IntType is small, we are too likely to
  86. // overflow __f below to use this technique.
  87. if (__k <= 21 * __p && sizeof(_IntType) > 1) {
  88. bernoulli_distribution __gen(__p);
  89. result_type __f = 0;
  90. result_type __s = 0;
  91. while (__s < __k) {
  92. if (__gen(__urng))
  93. ++__s;
  94. else
  95. ++__f;
  96. }
  97. _LIBCPP_ASSERT_INTERNAL(__f >= 0,
  98. "std::negative_binomial_distribution should never produce negative values. "
  99. "This is almost certainly a signed integer overflow issue on __f.");
  100. return __f;
  101. }
  102. return poisson_distribution<result_type>(gamma_distribution<double>(__k, (1 - __p) / __p)(__urng))(__urng);
  103. }
  104. template <class _CharT, class _Traits, class _IntType>
  105. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  106. operator<<(basic_ostream<_CharT, _Traits>& __os, const negative_binomial_distribution<_IntType>& __x) {
  107. __save_flags<_CharT, _Traits> __lx(__os);
  108. typedef basic_ostream<_CharT, _Traits> _OStream;
  109. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
  110. _CharT __sp = __os.widen(' ');
  111. __os.fill(__sp);
  112. return __os << __x.k() << __sp << __x.p();
  113. }
  114. template <class _CharT, class _Traits, class _IntType>
  115. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  116. operator>>(basic_istream<_CharT, _Traits>& __is, negative_binomial_distribution<_IntType>& __x) {
  117. typedef negative_binomial_distribution<_IntType> _Eng;
  118. typedef typename _Eng::result_type result_type;
  119. typedef typename _Eng::param_type param_type;
  120. __save_flags<_CharT, _Traits> __lx(__is);
  121. typedef basic_istream<_CharT, _Traits> _Istream;
  122. __is.flags(_Istream::dec | _Istream::skipws);
  123. result_type __k;
  124. double __p;
  125. __is >> __k >> __p;
  126. if (!__is.fail())
  127. __x.param(param_type(__k, __p));
  128. return __is;
  129. }
  130. _LIBCPP_END_NAMESPACE_STD
  131. _LIBCPP_POP_MACROS
  132. #endif // _LIBCPP___RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_H