Path: blob/main/contrib/llvm-project/libcxx/include/__random/weibull_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_WEIBULL_DISTRIBUTION_H9#define _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H1011#include <__config>12#include <__random/exponential_distribution.h>13#include <__random/is_valid.h>14#include <cmath>15#include <iosfwd>16#include <limits>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_PUSH_MACROS23#include <__undef_macros>2425_LIBCPP_BEGIN_NAMESPACE_STD2627template <class _RealType = double>28class _LIBCPP_TEMPLATE_VIS weibull_distribution {29static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,30"RealType must be a supported floating-point type");3132public:33// types34typedef _RealType result_type;3536class _LIBCPP_TEMPLATE_VIS param_type {37result_type __a_;38result_type __b_;3940public:41typedef weibull_distribution distribution_type;4243_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 1, result_type __b = 1) : __a_(__a), __b_(__b) {}4445_LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }46_LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }4748friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {49return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;50}51friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }52};5354private:55param_type __p_;5657public:58// constructor and reset functions59#ifndef _LIBCPP_CXX03_LANG60_LIBCPP_HIDE_FROM_ABI weibull_distribution() : weibull_distribution(1) {}61_LIBCPP_HIDE_FROM_ABI explicit weibull_distribution(result_type __a, result_type __b = 1)62: __p_(param_type(__a, __b)) {}63#else64_LIBCPP_HIDE_FROM_ABI explicit weibull_distribution(result_type __a = 1, result_type __b = 1)65: __p_(param_type(__a, __b)) {}66#endif67_LIBCPP_HIDE_FROM_ABI explicit weibull_distribution(const param_type& __p) : __p_(__p) {}68_LIBCPP_HIDE_FROM_ABI void reset() {}6970// generating functions71template <class _URNG>72_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {73return (*this)(__g, __p_);74}75template <class _URNG>76_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {77return __p.b() * std::pow(exponential_distribution<result_type>()(__g), 1 / __p.a());78}7980// property functions81_LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }82_LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }8384_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }85_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8687_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }88_LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }8990friend _LIBCPP_HIDE_FROM_ABI bool operator==(const weibull_distribution& __x, const weibull_distribution& __y) {91return __x.__p_ == __y.__p_;92}93friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const weibull_distribution& __x, const weibull_distribution& __y) {94return !(__x == __y);95}96};9798template <class _CharT, class _Traits, class _RT>99_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&100operator<<(basic_ostream<_CharT, _Traits>& __os, const weibull_distribution<_RT>& __x) {101__save_flags<_CharT, _Traits> __lx(__os);102typedef basic_ostream<_CharT, _Traits> _OStream;103__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);104_CharT __sp = __os.widen(' ');105__os.fill(__sp);106__os << __x.a() << __sp << __x.b();107return __os;108}109110template <class _CharT, class _Traits, class _RT>111_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&112operator>>(basic_istream<_CharT, _Traits>& __is, weibull_distribution<_RT>& __x) {113typedef weibull_distribution<_RT> _Eng;114typedef typename _Eng::result_type result_type;115typedef typename _Eng::param_type param_type;116__save_flags<_CharT, _Traits> __lx(__is);117typedef basic_istream<_CharT, _Traits> _Istream;118__is.flags(_Istream::dec | _Istream::skipws);119result_type __a;120result_type __b;121__is >> __a >> __b;122if (!__is.fail())123__x.param(param_type(__a, __b));124return __is;125}126127_LIBCPP_END_NAMESPACE_STD128129_LIBCPP_POP_MACROS130131#endif // _LIBCPP___RANDOM_WEIBULL_DISTRIBUTION_H132133134