geometric_distribution.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_GEOMETRIC_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/negative_binomial_distribution.h>
  12. #include <iosfwd>
  13. #include <limits>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_PUSH_MACROS
  18. #include <__undef_macros>
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. template<class _IntType = int>
  21. class _LIBCPP_TEMPLATE_VIS geometric_distribution
  22. {
  23. public:
  24. // types
  25. typedef _IntType result_type;
  26. class _LIBCPP_TEMPLATE_VIS param_type
  27. {
  28. double __p_;
  29. public:
  30. typedef geometric_distribution distribution_type;
  31. _LIBCPP_INLINE_VISIBILITY
  32. explicit param_type(double __p = 0.5) : __p_(__p) {}
  33. _LIBCPP_INLINE_VISIBILITY
  34. double p() const {return __p_;}
  35. friend _LIBCPP_INLINE_VISIBILITY
  36. bool operator==(const param_type& __x, const param_type& __y)
  37. {return __x.__p_ == __y.__p_;}
  38. friend _LIBCPP_INLINE_VISIBILITY
  39. bool operator!=(const param_type& __x, const param_type& __y)
  40. {return !(__x == __y);}
  41. };
  42. private:
  43. param_type __p_;
  44. public:
  45. // constructors and reset functions
  46. #ifndef _LIBCPP_CXX03_LANG
  47. _LIBCPP_INLINE_VISIBILITY
  48. geometric_distribution() : geometric_distribution(0.5) {}
  49. _LIBCPP_INLINE_VISIBILITY
  50. explicit geometric_distribution(double __p)
  51. : __p_(__p) {}
  52. #else
  53. _LIBCPP_INLINE_VISIBILITY
  54. explicit geometric_distribution(double __p = 0.5)
  55. : __p_(__p) {}
  56. #endif
  57. _LIBCPP_INLINE_VISIBILITY
  58. explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
  59. _LIBCPP_INLINE_VISIBILITY
  60. void reset() {}
  61. // generating functions
  62. template<class _URNG>
  63. _LIBCPP_INLINE_VISIBILITY
  64. result_type operator()(_URNG& __g)
  65. {return (*this)(__g, __p_);}
  66. template<class _URNG>
  67. _LIBCPP_INLINE_VISIBILITY
  68. result_type operator()(_URNG& __g, const param_type& __p)
  69. {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
  70. // property functions
  71. _LIBCPP_INLINE_VISIBILITY
  72. double p() const {return __p_.p();}
  73. _LIBCPP_INLINE_VISIBILITY
  74. param_type param() const {return __p_;}
  75. _LIBCPP_INLINE_VISIBILITY
  76. void param(const param_type& __p) {__p_ = __p;}
  77. _LIBCPP_INLINE_VISIBILITY
  78. result_type min() const {return 0;}
  79. _LIBCPP_INLINE_VISIBILITY
  80. result_type max() const {return numeric_limits<result_type>::max();}
  81. friend _LIBCPP_INLINE_VISIBILITY
  82. bool operator==(const geometric_distribution& __x,
  83. const geometric_distribution& __y)
  84. {return __x.__p_ == __y.__p_;}
  85. friend _LIBCPP_INLINE_VISIBILITY
  86. bool operator!=(const geometric_distribution& __x,
  87. const geometric_distribution& __y)
  88. {return !(__x == __y);}
  89. };
  90. template <class _CharT, class _Traits, class _IntType>
  91. basic_ostream<_CharT, _Traits>&
  92. operator<<(basic_ostream<_CharT, _Traits>& __os,
  93. const geometric_distribution<_IntType>& __x)
  94. {
  95. __save_flags<_CharT, _Traits> __lx(__os);
  96. typedef basic_ostream<_CharT, _Traits> _OStream;
  97. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  98. _OStream::scientific);
  99. return __os << __x.p();
  100. }
  101. template <class _CharT, class _Traits, class _IntType>
  102. basic_istream<_CharT, _Traits>&
  103. operator>>(basic_istream<_CharT, _Traits>& __is,
  104. geometric_distribution<_IntType>& __x)
  105. {
  106. typedef geometric_distribution<_IntType> _Eng;
  107. typedef typename _Eng::param_type param_type;
  108. __save_flags<_CharT, _Traits> __lx(__is);
  109. typedef basic_istream<_CharT, _Traits> _Istream;
  110. __is.flags(_Istream::dec | _Istream::skipws);
  111. double __p;
  112. __is >> __p;
  113. if (!__is.fail())
  114. __x.param(param_type(__p));
  115. return __is;
  116. }
  117. _LIBCPP_END_NAMESPACE_STD
  118. _LIBCPP_POP_MACROS
  119. #endif // _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H