bernoulli_distribution.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. public:
  22. // types
  23. typedef bool result_type;
  24. class _LIBCPP_TEMPLATE_VIS param_type {
  25. double __p_;
  26. public:
  27. typedef bernoulli_distribution distribution_type;
  28. _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
  29. _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
  30. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
  31. return __x.__p_ == __y.__p_;
  32. }
  33. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
  34. };
  35. private:
  36. param_type __p_;
  37. public:
  38. // constructors and reset functions
  39. #ifndef _LIBCPP_CXX03_LANG
  40. _LIBCPP_HIDE_FROM_ABI bernoulli_distribution() : bernoulli_distribution(0.5) {}
  41. _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
  42. #else
  43. _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
  44. #endif
  45. _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
  46. _LIBCPP_HIDE_FROM_ABI void reset() {}
  47. // generating functions
  48. template <class _URNG>
  49. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
  50. return (*this)(__g, __p_);
  51. }
  52. template <class _URNG>
  53. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  54. // property functions
  55. _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
  56. _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
  57. _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
  58. _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }
  59. _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }
  60. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
  61. return __x.__p_ == __y.__p_;
  62. }
  63. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
  64. return !(__x == __y);
  65. }
  66. };
  67. template <class _URNG>
  68. inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) {
  69. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  70. uniform_real_distribution<double> __gen;
  71. return __gen(__g) < __p.p();
  72. }
  73. template <class _CharT, class _Traits>
  74. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  75. operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) {
  76. __save_flags<_CharT, _Traits> __lx(__os);
  77. typedef basic_ostream<_CharT, _Traits> _OStream;
  78. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
  79. _CharT __sp = __os.widen(' ');
  80. __os.fill(__sp);
  81. return __os << __x.p();
  82. }
  83. template <class _CharT, class _Traits>
  84. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  85. operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) {
  86. typedef bernoulli_distribution _Eng;
  87. typedef typename _Eng::param_type param_type;
  88. __save_flags<_CharT, _Traits> __lx(__is);
  89. typedef basic_istream<_CharT, _Traits> _Istream;
  90. __is.flags(_Istream::dec | _Istream::skipws);
  91. double __p;
  92. __is >> __p;
  93. if (!__is.fail())
  94. __x.param(param_type(__p));
  95. return __is;
  96. }
  97. _LIBCPP_END_NAMESPACE_STD
  98. _LIBCPP_POP_MACROS
  99. #endif // _LIBCPP___RANDOM_BERNOULLI_DISTRIBUTION_H