133 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___RANDOM_UNIFORM_REAL_DISTRIBUTION_H10#define _LIBCPP___CXX03___RANDOM_UNIFORM_REAL_DISTRIBUTION_H11 12#include <__cxx03/__config>13#include <__cxx03/__random/generate_canonical.h>14#include <__cxx03/__random/is_valid.h>15#include <__cxx03/iosfwd>16#include <__cxx03/limits>17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif21 22_LIBCPP_PUSH_MACROS23#include <__cxx03/__undef_macros>24 25_LIBCPP_BEGIN_NAMESPACE_STD26 27template <class _RealType = double>28class _LIBCPP_TEMPLATE_VIS uniform_real_distribution {29 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,30 "RealType must be a supported floating-point type");31 32public:33 // types34 typedef _RealType result_type;35 36 class _LIBCPP_TEMPLATE_VIS param_type {37 result_type __a_;38 result_type __b_;39 40 public:41 typedef uniform_real_distribution distribution_type;42 43 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}44 45 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }46 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }47 48 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {49 return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;50 }51 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }52 };53 54private:55 param_type __p_;56 57public:58 // constructors and reset functions59 _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)60 : __p_(param_type(__a, __b)) {}61 _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}62 _LIBCPP_HIDE_FROM_ABI void reset() {}63 64 // generating functions65 template <class _URNG>66 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {67 return (*this)(__g, __p_);68 }69 template <class _URNG>70 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);71 72 // property functions73 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }74 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }75 76 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }77 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }78 79 _LIBCPP_HIDE_FROM_ABI result_type min() const { return a(); }80 _LIBCPP_HIDE_FROM_ABI result_type max() const { return b(); }81 82 friend _LIBCPP_HIDE_FROM_ABI bool83 operator==(const uniform_real_distribution& __x, const uniform_real_distribution& __y) {84 return __x.__p_ == __y.__p_;85 }86 friend _LIBCPP_HIDE_FROM_ABI bool87 operator!=(const uniform_real_distribution& __x, const uniform_real_distribution& __y) {88 return !(__x == __y);89 }90};91 92template <class _RealType>93template <class _URNG>94inline typename uniform_real_distribution<_RealType>::result_type95uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {96 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");97 return (__p.b() - __p.a()) * std::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) + __p.a();98}99 100template <class _CharT, class _Traits, class _RT>101_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&102operator<<(basic_ostream<_CharT, _Traits>& __os, const uniform_real_distribution<_RT>& __x) {103 __save_flags<_CharT, _Traits> __lx(__os);104 typedef basic_ostream<_CharT, _Traits> _OStream;105 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);106 _CharT __sp = __os.widen(' ');107 __os.fill(__sp);108 return __os << __x.a() << __sp << __x.b();109}110 111template <class _CharT, class _Traits, class _RT>112_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&113operator>>(basic_istream<_CharT, _Traits>& __is, uniform_real_distribution<_RT>& __x) {114 typedef uniform_real_distribution<_RT> _Eng;115 typedef typename _Eng::result_type result_type;116 typedef typename _Eng::param_type param_type;117 __save_flags<_CharT, _Traits> __lx(__is);118 typedef basic_istream<_CharT, _Traits> _Istream;119 __is.flags(_Istream::dec | _Istream::skipws);120 result_type __a;121 result_type __b;122 __is >> __a >> __b;123 if (!__is.fail())124 __x.param(param_type(__a, __b));125 return __is;126}127 128_LIBCPP_END_NAMESPACE_STD129 130_LIBCPP_POP_MACROS131 132#endif // _LIBCPP___CXX03___RANDOM_UNIFORM_REAL_DISTRIBUTION_H133