bernoulli_distribution.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_BERNOULLI_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/is_valid.h>
  12. #include <__random/uniform_real_distribution.h>
  13. #include <iosfwd>
  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. class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
  21. {
  22. public:
  23. // types
  24. typedef bool result_type;
  25. class _LIBCPP_TEMPLATE_VIS param_type
  26. {
  27. double __p_;
  28. public:
  29. typedef bernoulli_distribution distribution_type;
  30. _LIBCPP_INLINE_VISIBILITY
  31. explicit param_type(double __p = 0.5) : __p_(__p) {}
  32. _LIBCPP_INLINE_VISIBILITY
  33. double p() const {return __p_;}
  34. friend _LIBCPP_INLINE_VISIBILITY
  35. bool operator==(const param_type& __x, const param_type& __y)
  36. {return __x.__p_ == __y.__p_;}
  37. friend _LIBCPP_INLINE_VISIBILITY
  38. bool operator!=(const param_type& __x, const param_type& __y)
  39. {return !(__x == __y);}
  40. };
  41. private:
  42. param_type __p_;
  43. public:
  44. // constructors and reset functions
  45. #ifndef _LIBCPP_CXX03_LANG
  46. _LIBCPP_INLINE_VISIBILITY
  47. bernoulli_distribution() : bernoulli_distribution(0.5) {}
  48. _LIBCPP_INLINE_VISIBILITY
  49. explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
  50. #else
  51. _LIBCPP_INLINE_VISIBILITY
  52. explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
  53. #endif
  54. _LIBCPP_INLINE_VISIBILITY
  55. explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
  56. _LIBCPP_INLINE_VISIBILITY
  57. void reset() {}
  58. // generating functions
  59. template<class _URNG>
  60. _LIBCPP_INLINE_VISIBILITY
  61. result_type operator()(_URNG& __g)
  62. {return (*this)(__g, __p_);}
  63. template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
  64. // property functions
  65. _LIBCPP_INLINE_VISIBILITY
  66. double p() const {return __p_.p();}
  67. _LIBCPP_INLINE_VISIBILITY
  68. param_type param() const {return __p_;}
  69. _LIBCPP_INLINE_VISIBILITY
  70. void param(const param_type& __p) {__p_ = __p;}
  71. _LIBCPP_INLINE_VISIBILITY
  72. result_type min() const {return false;}
  73. _LIBCPP_INLINE_VISIBILITY
  74. result_type max() const {return true;}
  75. friend _LIBCPP_INLINE_VISIBILITY
  76. bool operator==(const bernoulli_distribution& __x,
  77. const bernoulli_distribution& __y)
  78. {return __x.__p_ == __y.__p_;}
  79. friend _LIBCPP_INLINE_VISIBILITY
  80. bool operator!=(const bernoulli_distribution& __x,
  81. const bernoulli_distribution& __y)
  82. {return !(__x == __y);}
  83. };
  84. template<class _URNG>
  85. inline
  86. bernoulli_distribution::result_type
  87. bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
  88. {
  89. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  90. uniform_real_distribution<double> __gen;
  91. return __gen(__g) < __p.p();
  92. }
  93. template <class _CharT, class _Traits>
  94. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  95. operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __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. _CharT __sp = __os.widen(' ');
  102. __os.fill(__sp);
  103. return __os << __x.p();
  104. }
  105. template <class _CharT, class _Traits>
  106. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  107. operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
  108. {
  109. typedef bernoulli_distribution _Eng;
  110. typedef typename _Eng::param_type param_type;
  111. __save_flags<_CharT, _Traits> __lx(__is);
  112. typedef basic_istream<_CharT, _Traits> _Istream;
  113. __is.flags(_Istream::dec | _Istream::skipws);
  114. double __p;
  115. __is >> __p;
  116. if (!__is.fail())
  117. __x.param(param_type(__p));
  118. return __is;
  119. }
  120. _LIBCPP_END_NAMESPACE_STD
  121. _LIBCPP_POP_MACROS
  122. #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H