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