File indexing completed on 2026-05-03 08:13:59
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
0011
0012 #include <__config>
0013 #include <__random/generate_canonical.h>
0014 #include <__random/is_valid.h>
0015 #include <__random/uniform_real_distribution.h>
0016 #include <cmath>
0017 #include <iosfwd>
0018 #include <limits>
0019
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 # pragma GCC system_header
0022 #endif
0023
0024 _LIBCPP_PUSH_MACROS
0025 #include <__undef_macros>
0026
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028
0029 template <class _RealType = double>
0030 class _LIBCPP_TEMPLATE_VIS exponential_distribution {
0031 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0032 "RealType must be a supported floating-point type");
0033
0034 public:
0035
0036 typedef _RealType result_type;
0037
0038 class _LIBCPP_TEMPLATE_VIS param_type {
0039 result_type __lambda_;
0040
0041 public:
0042 typedef exponential_distribution distribution_type;
0043
0044 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
0045
0046 _LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __lambda_; }
0047
0048 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0049 return __x.__lambda_ == __y.__lambda_;
0050 }
0051 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0052 };
0053
0054 private:
0055 param_type __p_;
0056
0057 public:
0058
0059 #ifndef _LIBCPP_CXX03_LANG
0060 _LIBCPP_HIDE_FROM_ABI exponential_distribution() : exponential_distribution(1) {}
0061 _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda) : __p_(param_type(__lambda)) {}
0062 #else
0063 _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda = 1) : __p_(param_type(__lambda)) {}
0064 #endif
0065 _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
0066 _LIBCPP_HIDE_FROM_ABI void reset() {}
0067
0068
0069 template <class _URNG>
0070 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0071 return (*this)(__g, __p_);
0072 }
0073 template <class _URNG>
0074 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0075
0076
0077 _LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __p_.lambda(); }
0078
0079 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0080 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0081
0082 _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
0083 _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
0084
0085 friend _LIBCPP_HIDE_FROM_ABI bool
0086 operator==(const exponential_distribution& __x, const exponential_distribution& __y) {
0087 return __x.__p_ == __y.__p_;
0088 }
0089 friend _LIBCPP_HIDE_FROM_ABI bool
0090 operator!=(const exponential_distribution& __x, const exponential_distribution& __y) {
0091 return !(__x == __y);
0092 }
0093 };
0094
0095 template <class _RealType>
0096 template <class _URNG>
0097 _RealType exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0098 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0099 return -std::log(result_type(1) - std::generate_canonical<result_type, numeric_limits<result_type>::digits>(__g)) /
0100 __p.lambda();
0101 }
0102
0103 template <class _CharT, class _Traits, class _RealType>
0104 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0105 operator<<(basic_ostream<_CharT, _Traits>& __os, const exponential_distribution<_RealType>& __x) {
0106 __save_flags<_CharT, _Traits> __lx(__os);
0107 typedef basic_ostream<_CharT, _Traits> _OStream;
0108 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0109 return __os << __x.lambda();
0110 }
0111
0112 template <class _CharT, class _Traits, class _RealType>
0113 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0114 operator>>(basic_istream<_CharT, _Traits>& __is, exponential_distribution<_RealType>& __x) {
0115 typedef exponential_distribution<_RealType> _Eng;
0116 typedef typename _Eng::result_type result_type;
0117 typedef typename _Eng::param_type param_type;
0118 __save_flags<_CharT, _Traits> __lx(__is);
0119 typedef basic_istream<_CharT, _Traits> _Istream;
0120 __is.flags(_Istream::dec | _Istream::skipws);
0121 result_type __lambda;
0122 __is >> __lambda;
0123 if (!__is.fail())
0124 __x.param(param_type(__lambda));
0125 return __is;
0126 }
0127
0128 _LIBCPP_END_NAMESPACE_STD
0129
0130 _LIBCPP_POP_MACROS
0131
0132 #endif