geometric_distribution.h 4.4 KB

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