xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/exponential_distribution.h (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
14824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
24824e7fdSDimitry Andric //
34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64824e7fdSDimitry Andric //
74824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
84824e7fdSDimitry Andric 
94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
114824e7fdSDimitry Andric 
124824e7fdSDimitry Andric #include <__config>
134824e7fdSDimitry Andric #include <__random/generate_canonical.h>
14*81ad6265SDimitry Andric #include <__random/is_valid.h>
154824e7fdSDimitry Andric #include <__random/uniform_real_distribution.h>
164824e7fdSDimitry Andric #include <cmath>
174824e7fdSDimitry Andric #include <iosfwd>
184824e7fdSDimitry Andric #include <limits>
194824e7fdSDimitry Andric 
204824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
214824e7fdSDimitry Andric #  pragma GCC system_header
224824e7fdSDimitry Andric #endif
234824e7fdSDimitry Andric 
244824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
254824e7fdSDimitry Andric #include <__undef_macros>
264824e7fdSDimitry Andric 
274824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
284824e7fdSDimitry Andric 
294824e7fdSDimitry Andric template<class _RealType = double>
304824e7fdSDimitry Andric class _LIBCPP_TEMPLATE_VIS exponential_distribution
314824e7fdSDimitry Andric {
324824e7fdSDimitry Andric public:
334824e7fdSDimitry Andric     // types
344824e7fdSDimitry Andric     typedef _RealType result_type;
354824e7fdSDimitry Andric 
364824e7fdSDimitry Andric     class _LIBCPP_TEMPLATE_VIS param_type
374824e7fdSDimitry Andric     {
384824e7fdSDimitry Andric         result_type __lambda_;
394824e7fdSDimitry Andric     public:
404824e7fdSDimitry Andric         typedef exponential_distribution distribution_type;
414824e7fdSDimitry Andric 
424824e7fdSDimitry Andric         _LIBCPP_INLINE_VISIBILITY
434824e7fdSDimitry Andric         explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
444824e7fdSDimitry Andric 
454824e7fdSDimitry Andric         _LIBCPP_INLINE_VISIBILITY
464824e7fdSDimitry Andric         result_type lambda() const {return __lambda_;}
474824e7fdSDimitry Andric 
484824e7fdSDimitry Andric         friend _LIBCPP_INLINE_VISIBILITY
494824e7fdSDimitry Andric             bool operator==(const param_type& __x, const param_type& __y)
504824e7fdSDimitry Andric             {return __x.__lambda_ == __y.__lambda_;}
514824e7fdSDimitry Andric         friend _LIBCPP_INLINE_VISIBILITY
524824e7fdSDimitry Andric             bool operator!=(const param_type& __x, const param_type& __y)
534824e7fdSDimitry Andric             {return !(__x == __y);}
544824e7fdSDimitry Andric     };
554824e7fdSDimitry Andric 
564824e7fdSDimitry Andric private:
574824e7fdSDimitry Andric     param_type __p_;
584824e7fdSDimitry Andric 
594824e7fdSDimitry Andric public:
604824e7fdSDimitry Andric     // constructors and reset functions
614824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG
624824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
634824e7fdSDimitry Andric     exponential_distribution() : exponential_distribution(1) {}
644824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
654824e7fdSDimitry Andric     explicit exponential_distribution(result_type __lambda)
664824e7fdSDimitry Andric         : __p_(param_type(__lambda)) {}
674824e7fdSDimitry Andric #else
684824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
694824e7fdSDimitry Andric     explicit exponential_distribution(result_type __lambda = 1)
704824e7fdSDimitry Andric         : __p_(param_type(__lambda)) {}
714824e7fdSDimitry Andric #endif
724824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
734824e7fdSDimitry Andric     explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
744824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
754824e7fdSDimitry Andric     void reset() {}
764824e7fdSDimitry Andric 
774824e7fdSDimitry Andric     // generating functions
784824e7fdSDimitry Andric     template<class _URNG>
794824e7fdSDimitry Andric         _LIBCPP_INLINE_VISIBILITY
804824e7fdSDimitry Andric         result_type operator()(_URNG& __g)
814824e7fdSDimitry Andric         {return (*this)(__g, __p_);}
824824e7fdSDimitry Andric     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
834824e7fdSDimitry Andric 
844824e7fdSDimitry Andric     // property functions
854824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
864824e7fdSDimitry Andric     result_type lambda() const {return __p_.lambda();}
874824e7fdSDimitry Andric 
884824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
894824e7fdSDimitry Andric     param_type param() const {return __p_;}
904824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
914824e7fdSDimitry Andric     void param(const param_type& __p) {__p_ = __p;}
924824e7fdSDimitry Andric 
934824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
944824e7fdSDimitry Andric     result_type min() const {return 0;}
954824e7fdSDimitry Andric     _LIBCPP_INLINE_VISIBILITY
964824e7fdSDimitry Andric     result_type max() const {return numeric_limits<result_type>::infinity();}
974824e7fdSDimitry Andric 
984824e7fdSDimitry Andric     friend _LIBCPP_INLINE_VISIBILITY
994824e7fdSDimitry Andric         bool operator==(const exponential_distribution& __x,
1004824e7fdSDimitry Andric                         const exponential_distribution& __y)
1014824e7fdSDimitry Andric         {return __x.__p_ == __y.__p_;}
1024824e7fdSDimitry Andric     friend _LIBCPP_INLINE_VISIBILITY
1034824e7fdSDimitry Andric         bool operator!=(const exponential_distribution& __x,
1044824e7fdSDimitry Andric                         const exponential_distribution& __y)
1054824e7fdSDimitry Andric         {return !(__x == __y);}
1064824e7fdSDimitry Andric };
1074824e7fdSDimitry Andric 
1084824e7fdSDimitry Andric template <class _RealType>
1094824e7fdSDimitry Andric template<class _URNG>
1104824e7fdSDimitry Andric _RealType
1114824e7fdSDimitry Andric exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
1124824e7fdSDimitry Andric {
113*81ad6265SDimitry Andric     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
1144824e7fdSDimitry Andric     return -_VSTD::log
1154824e7fdSDimitry Andric                   (
1164824e7fdSDimitry Andric                       result_type(1) -
1174824e7fdSDimitry Andric                       _VSTD::generate_canonical<result_type,
1184824e7fdSDimitry Andric                                        numeric_limits<result_type>::digits>(__g)
1194824e7fdSDimitry Andric                   )
1204824e7fdSDimitry Andric                   / __p.lambda();
1214824e7fdSDimitry Andric }
1224824e7fdSDimitry Andric 
1234824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RealType>
1244824e7fdSDimitry Andric basic_ostream<_CharT, _Traits>&
1254824e7fdSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os,
1264824e7fdSDimitry Andric            const exponential_distribution<_RealType>& __x)
1274824e7fdSDimitry Andric {
1284824e7fdSDimitry Andric     __save_flags<_CharT, _Traits> __lx(__os);
1294824e7fdSDimitry Andric     typedef basic_ostream<_CharT, _Traits> _OStream;
1304824e7fdSDimitry Andric     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
1314824e7fdSDimitry Andric                _OStream::scientific);
1324824e7fdSDimitry Andric     return __os << __x.lambda();
1334824e7fdSDimitry Andric }
1344824e7fdSDimitry Andric 
1354824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RealType>
1364824e7fdSDimitry Andric basic_istream<_CharT, _Traits>&
1374824e7fdSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is,
1384824e7fdSDimitry Andric            exponential_distribution<_RealType>& __x)
1394824e7fdSDimitry Andric {
1404824e7fdSDimitry Andric     typedef exponential_distribution<_RealType> _Eng;
1414824e7fdSDimitry Andric     typedef typename _Eng::result_type result_type;
1424824e7fdSDimitry Andric     typedef typename _Eng::param_type param_type;
1434824e7fdSDimitry Andric     __save_flags<_CharT, _Traits> __lx(__is);
1444824e7fdSDimitry Andric     typedef basic_istream<_CharT, _Traits> _Istream;
1454824e7fdSDimitry Andric     __is.flags(_Istream::dec | _Istream::skipws);
1464824e7fdSDimitry Andric     result_type __lambda;
1474824e7fdSDimitry Andric     __is >> __lambda;
1484824e7fdSDimitry Andric     if (!__is.fail())
1494824e7fdSDimitry Andric         __x.param(param_type(__lambda));
1504824e7fdSDimitry Andric     return __is;
1514824e7fdSDimitry Andric }
1524824e7fdSDimitry Andric 
1534824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1544824e7fdSDimitry Andric 
1554824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1564824e7fdSDimitry Andric 
1574824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H
158