xref: /freebsd/contrib/llvm-project/libcxx/include/__random/exponential_distribution.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
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 
9 #ifndef _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
11 
12 #include <__config>
13 #include <__random/generate_canonical.h>
14 #include <__random/is_valid.h>
15 #include <__random/uniform_real_distribution.h>
16 #include <cmath>
17 #include <iosfwd>
18 #include <limits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template <class _RealType = double>
30 class _LIBCPP_TEMPLATE_VIS exponential_distribution {
31   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32                 "RealType must be a supported floating-point type");
33 
34 public:
35   // types
36   typedef _RealType result_type;
37 
38   class _LIBCPP_TEMPLATE_VIS param_type {
39     result_type __lambda_;
40 
41   public:
42     typedef exponential_distribution distribution_type;
43 
44     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
45 
46     _LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __lambda_; }
47 
48     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
49       return __x.__lambda_ == __y.__lambda_;
50     }
51     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
52   };
53 
54 private:
55   param_type __p_;
56 
57 public:
58   // constructors and reset functions
59 #ifndef _LIBCPP_CXX03_LANG
60   _LIBCPP_HIDE_FROM_ABI exponential_distribution() : exponential_distribution(1) {}
61   _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda) : __p_(param_type(__lambda)) {}
62 #else
63   _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda = 1) : __p_(param_type(__lambda)) {}
64 #endif
65   _LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
66   _LIBCPP_HIDE_FROM_ABI void reset() {}
67 
68   // generating functions
69   template <class _URNG>
70   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
71     return (*this)(__g, __p_);
72   }
73   template <class _URNG>
74   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
75 
76   // property functions
77   _LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __p_.lambda(); }
78 
79   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
80   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
81 
82   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
83   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
84 
85   friend _LIBCPP_HIDE_FROM_ABI bool
86   operator==(const exponential_distribution& __x, const exponential_distribution& __y) {
87     return __x.__p_ == __y.__p_;
88   }
89   friend _LIBCPP_HIDE_FROM_ABI bool
90   operator!=(const exponential_distribution& __x, const exponential_distribution& __y) {
91     return !(__x == __y);
92   }
93 };
94 
95 template <class _RealType>
96 template <class _URNG>
97 _RealType exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
98   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
99   return -std::log(result_type(1) - std::generate_canonical<result_type, numeric_limits<result_type>::digits>(__g)) /
100          __p.lambda();
101 }
102 
103 template <class _CharT, class _Traits, class _RealType>
104 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
105 operator<<(basic_ostream<_CharT, _Traits>& __os, const exponential_distribution<_RealType>& __x) {
106   __save_flags<_CharT, _Traits> __lx(__os);
107   typedef basic_ostream<_CharT, _Traits> _OStream;
108   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
109   return __os << __x.lambda();
110 }
111 
112 template <class _CharT, class _Traits, class _RealType>
113 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
114 operator>>(basic_istream<_CharT, _Traits>& __is, exponential_distribution<_RealType>& __x) {
115   typedef exponential_distribution<_RealType> _Eng;
116   typedef typename _Eng::result_type result_type;
117   typedef typename _Eng::param_type param_type;
118   __save_flags<_CharT, _Traits> __lx(__is);
119   typedef basic_istream<_CharT, _Traits> _Istream;
120   __is.flags(_Istream::dec | _Istream::skipws);
121   result_type __lambda;
122   __is >> __lambda;
123   if (!__is.fail())
124     __x.param(param_type(__lambda));
125   return __is;
126 }
127 
128 _LIBCPP_END_NAMESPACE_STD
129 
130 _LIBCPP_POP_MACROS
131 
132 #endif // _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
133