From: Paolo Carlini Date: Fri, 19 Jun 2009 17:49:31 +0000 (+0000) Subject: random.h (_Adaptor): Simplify for _DInputType always a floating point type. X-Git-Tag: releases/gcc-4.5.0~5098 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9b88236b3440aacc4f3e0d1ef36bcf680fc04a4a;p=thirdparty%2Fgcc.git random.h (_Adaptor): Simplify for _DInputType always a floating point type. 2009-06-19 Paolo Carlini * include/bits/random.h (_Adaptor): Simplify for _DInputType always a floating point type. (uniform_int_distribution<>::uniform_int_distribution(_IntType, _IntType)): Fix second default argument. (uniform_int_distribution<>::_M_call): Remove. (uniform_int_distribution<>::operator()(_UniformRandomNumberGenerator&, const param_type&)): Only declare. * include/bits/random.tcc (uniform_int_distribution<>::_M_call( _UniformRandomNumberGenerator&, result_type, result_type, true_type): Remove. uniform_int_distribution<>::operator()(_UniformRandomNumberGenerator&, const param_type&): Define here. (geometric_distribution<>::operator()(_UniformRandomNumberGenerator&, const param_type&), discrete_distribution<>::operator() (_UniformRandomNumberGenerator&, const param_type&), piecewise_constant_distribution<>::operator() (_UniformRandomNumberGenerator&, const param_type&), piecewise_linear_distribution<>::operator() (_UniformRandomNumberGenerator&, const param_type&)): Use double as the second template argument of _Adaptor. * testsuite/26_numerics/random/uniform_int_distribution/cons/ default.cc: Adjust. From-SVN: r148720 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ded7f3e25e8f..516e783aa4a1 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,28 @@ +2009-06-19 Paolo Carlini + + * include/bits/random.h (_Adaptor): Simplify for _DInputType always + a floating point type. + (uniform_int_distribution<>::uniform_int_distribution(_IntType, + _IntType)): Fix second default argument. + (uniform_int_distribution<>::_M_call): Remove. + (uniform_int_distribution<>::operator()(_UniformRandomNumberGenerator&, + const param_type&)): Only declare. + * include/bits/random.tcc (uniform_int_distribution<>::_M_call( + _UniformRandomNumberGenerator&, result_type, result_type, true_type): + Remove. + uniform_int_distribution<>::operator()(_UniformRandomNumberGenerator&, + const param_type&): Define here. + (geometric_distribution<>::operator()(_UniformRandomNumberGenerator&, + const param_type&), discrete_distribution<>::operator() + (_UniformRandomNumberGenerator&, const param_type&), + piecewise_constant_distribution<>::operator() + (_UniformRandomNumberGenerator&, const param_type&), + piecewise_linear_distribution<>::operator() + (_UniformRandomNumberGenerator&, const param_type&)): Use double as + the second template argument of _Adaptor. + * testsuite/26_numerics/random/uniform_int_distribution/cons/ + default.cc: Adjust. + 2009-06-19 Paolo Carlini * include/bits/random.tcc (discrete_distribution<>::param_type:: diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h index ec3dd13cbca8..099c5fae1a91 100644 --- a/libstdc++-v3/include/bits/random.h +++ b/libstdc++-v3/include/bits/random.h @@ -95,40 +95,23 @@ namespace std _DInputType min() const - { - if (is_integral<_DInputType>::value) - return _M_g.min(); - else - return _DInputType(0); - } + { return _DInputType(0); } _DInputType max() const - { - if (is_integral<_DInputType>::value) - return _M_g.max(); - else - return _DInputType(1); - } + { return _DInputType(1); } /* * Converts a value generated by the adapted random number generator * into a value in the input domain for the dependent random number * distribution. - * - * Because the type traits are compile time constants only the - * appropriate clause of the if statements will actually be emitted - * by the compiler. */ _DInputType operator()() { - if (is_integral<_DInputType>::value) - return _M_g(); - else - return generate_canonical<_DInputType, - numeric_limits<_DInputType>::digits, - _Engine>(_M_g); + return std::generate_canonical<_DInputType, + std::numeric_limits<_DInputType>::digits, + _Engine>(_M_g); } private: @@ -380,7 +363,7 @@ namespace std static_assert(__w >= __l, "mersenne_twister_engine template arguments out of bounds"); static_assert(__w <= - static_cast(numeric_limits<_UIntType>::digits), + static_cast(std::numeric_limits<_UIntType>::digits), "mersenne_twister_engine template arguments out of bounds"); static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1), "mersenne_twister_engine template arguments out of bounds"); @@ -558,8 +541,9 @@ namespace std { __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept) static_assert(__s > 0U && __r > __s - && __w > 0U - && __w <= static_cast(numeric_limits<_UIntType>::digits), + && __w > 0U + && __w <= static_cast + (std::numeric_limits<_UIntType>::digits), "template arguments out of bounds" " in subtract_with_carry_engine"); @@ -922,7 +906,8 @@ namespace std { static_assert(__w > 0U && __w <= - static_cast(numeric_limits<_UIntType>::digits), + static_cast + (std::numeric_limits<_UIntType>::digits), "template arguments out of bounds " "in independent_bits_engine"); @@ -1507,7 +1492,8 @@ namespace std typedef uniform_int_distribution<_IntType> distribution_type; explicit - param_type(_IntType __a = 0, _IntType __b = 9) + param_type(_IntType __a = 0, + _IntType __b = std::numeric_limits<_IntType>::max()) : _M_a(__a), _M_b(__b) { _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b); @@ -1531,7 +1517,8 @@ namespace std * @brief Constructs a uniform distribution object. */ explicit - uniform_int_distribution(_IntType __a = 0, _IntType __b = 9) + uniform_int_distribution(_IntType __a = 0, + _IntType __b = std::numeric_limits<_IntType>::max()) : _M_param(__a, __b) { } @@ -1602,29 +1589,7 @@ namespace std template result_type operator()(_UniformRandomNumberGenerator& __urng, - const param_type& __p) - { - typedef typename _UniformRandomNumberGenerator::result_type - _UResult_type; - return _M_call(__urng, __p.a(), __p.b(), - typename is_integral<_UResult_type>::type()); - } - - private: - template - result_type - _M_call(_UniformRandomNumberGenerator& __urng, - result_type __min, result_type __max, true_type); - - template - result_type - _M_call(_UniformRandomNumberGenerator& __urng, - result_type __min, result_type __max, false_type) - { - return result_type((__urng() - __urng.min()) - / (__urng.max() - __urng.min()) - * (__max - __min + 1)) + __min; - } + const param_type& __p); param_type _M_param; }; diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index 846b6b057115..87c46186b57e 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -644,13 +644,13 @@ namespace std template typename uniform_int_distribution<_IntType>::result_type uniform_int_distribution<_IntType>:: - _M_call(_UniformRandomNumberGenerator& __urng, - result_type __min, result_type __max, true_type) + operator()(_UniformRandomNumberGenerator& __urng, + const param_type& __param) { // XXX Must be fixed to work well for *arbitrary* __urng.max(), - // __urng.min(), __max, __min. Currently works fine only in the - // most common case __urng.max() - __urng.min() >= __max - __min, - // with __urng.max() > __urng.min() >= 0. + // __urng.min(), __param.b(), __param.a(). Currently works fine only + // in the most common case __urng.max() - __urng.min() >= + // __param.b() - __param.a(), with __urng.max() > __urng.min() >= 0. typedef typename __gnu_cxx::__add_unsigned::__type __urntype; typedef typename __gnu_cxx::__add_unsigned::__type @@ -664,14 +664,14 @@ namespace std const __urntype __urnmin = __urng.min(); const __urntype __urnmax = __urng.max(); const __urntype __urnrange = __urnmax - __urnmin; - const __uctype __urange = __max - __min; + const __uctype __urange = __param.b() - __param.a(); const __uctype __udenom = (__urnrange <= __urange ? 1 : __urnrange / (__urange + 1)); do __ret = (__urntype(__urng()) - __urnmin) / __udenom; - while (__ret > __max - __min); + while (__ret > __param.b() - __param.a()); - return __ret + __min; + return __ret + __param.a(); } template @@ -799,7 +799,7 @@ namespace std // The largest _RealType convertible to _IntType. const double __thr = std::numeric_limits<_IntType>::max() + __naf; - __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __detail::_Adaptor<_UniformRandomNumberGenerator, double> __aurng(__urng); double __cand; @@ -2021,7 +2021,7 @@ namespace std operator()(_UniformRandomNumberGenerator& __urng, const param_type& __param) { - __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __detail::_Adaptor<_UniformRandomNumberGenerator, double> __aurng(__urng); const double __p = __aurng(); @@ -2193,7 +2193,7 @@ namespace std operator()(_UniformRandomNumberGenerator& __urng, const param_type& __param) { - __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __detail::_Adaptor<_UniformRandomNumberGenerator, double> __aurng(__urng); const double __p = __aurng(); @@ -2383,7 +2383,7 @@ namespace std operator()(_UniformRandomNumberGenerator& __urng, const param_type& __param) { - __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> + __detail::_Adaptor<_UniformRandomNumberGenerator, double> __aurng(__urng); const double __p = __aurng(); diff --git a/libstdc++-v3/testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc b/libstdc++-v3/testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc index c1bfc04b0e12..0e83565e0de2 100644 --- a/libstdc++-v3/testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc +++ b/libstdc++-v3/testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc @@ -23,6 +23,7 @@ // 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] #include +#include #include void @@ -32,9 +33,9 @@ test01() std::uniform_int_distribution u; VERIFY( u.a() == 0 ); - VERIFY( u.b() == 9 ); + VERIFY( u.b() == std::numeric_limits::max() ); VERIFY( u.min() == 0 ); - VERIFY( u.max() == 9 ); + VERIFY( u.max() == std::numeric_limits::max() ); } int main()