Path: blob/main/contrib/llvm-project/libcxx/include/__random/exponential_distribution.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H9#define _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H1011#include <__config>12#include <__random/generate_canonical.h>13#include <__random/is_valid.h>14#include <__random/uniform_real_distribution.h>15#include <cmath>16#include <iosfwd>17#include <limits>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_PUSH_MACROS24#include <__undef_macros>2526_LIBCPP_BEGIN_NAMESPACE_STD2728template <class _RealType = double>29class _LIBCPP_TEMPLATE_VIS exponential_distribution {30static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,31"RealType must be a supported floating-point type");3233public:34// types35typedef _RealType result_type;3637class _LIBCPP_TEMPLATE_VIS param_type {38result_type __lambda_;3940public:41typedef exponential_distribution distribution_type;4243_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}4445_LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __lambda_; }4647friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {48return __x.__lambda_ == __y.__lambda_;49}50friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }51};5253private:54param_type __p_;5556public:57// constructors and reset functions58#ifndef _LIBCPP_CXX03_LANG59_LIBCPP_HIDE_FROM_ABI exponential_distribution() : exponential_distribution(1) {}60_LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda) : __p_(param_type(__lambda)) {}61#else62_LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(result_type __lambda = 1) : __p_(param_type(__lambda)) {}63#endif64_LIBCPP_HIDE_FROM_ABI explicit exponential_distribution(const param_type& __p) : __p_(__p) {}65_LIBCPP_HIDE_FROM_ABI void reset() {}6667// generating functions68template <class _URNG>69_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {70return (*this)(__g, __p_);71}72template <class _URNG>73_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);7475// property functions76_LIBCPP_HIDE_FROM_ABI result_type lambda() const { return __p_.lambda(); }7778_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }79_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8081_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }82_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }8384friend _LIBCPP_HIDE_FROM_ABI bool85operator==(const exponential_distribution& __x, const exponential_distribution& __y) {86return __x.__p_ == __y.__p_;87}88friend _LIBCPP_HIDE_FROM_ABI bool89operator!=(const exponential_distribution& __x, const exponential_distribution& __y) {90return !(__x == __y);91}92};9394template <class _RealType>95template <class _URNG>96_RealType exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {97static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");98return -std::log(result_type(1) - std::generate_canonical<result_type, numeric_limits<result_type>::digits>(__g)) /99__p.lambda();100}101102template <class _CharT, class _Traits, class _RealType>103_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&104operator<<(basic_ostream<_CharT, _Traits>& __os, const exponential_distribution<_RealType>& __x) {105__save_flags<_CharT, _Traits> __lx(__os);106typedef basic_ostream<_CharT, _Traits> _OStream;107__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);108return __os << __x.lambda();109}110111template <class _CharT, class _Traits, class _RealType>112_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&113operator>>(basic_istream<_CharT, _Traits>& __is, exponential_distribution<_RealType>& __x) {114typedef exponential_distribution<_RealType> _Eng;115typedef typename _Eng::result_type result_type;116typedef typename _Eng::param_type param_type;117__save_flags<_CharT, _Traits> __lx(__is);118typedef basic_istream<_CharT, _Traits> _Istream;119__is.flags(_Istream::dec | _Istream::skipws);120result_type __lambda;121__is >> __lambda;122if (!__is.fail())123__x.param(param_type(__lambda));124return __is;125}126127_LIBCPP_END_NAMESPACE_STD128129_LIBCPP_POP_MACROS130131#endif // _LIBCPP___RANDOM_EXPONENTIAL_DISTRIBUTION_H132133134