Commit 9b88236b by Paolo Carlini Committed by Paolo Carlini

random.h (_Adaptor): Simplify for _DInputType always a floating point type.

2009-06-19  Paolo Carlini  <paolo.carlini@oracle.com>

	* 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
parent f8dd9e0d
2009-06-19 Paolo Carlini <paolo.carlini@oracle.com> 2009-06-19 Paolo Carlini <paolo.carlini@oracle.com>
* 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 <paolo.carlini@oracle.com>
* include/bits/random.tcc (discrete_distribution<>::param_type:: * include/bits/random.tcc (discrete_distribution<>::param_type::
param_type(size_t, double, double, _Func), param_type(size_t, double, double, _Func),
discrete_distribution<>::operator()(_UniformRandomNumberGenerator&, discrete_distribution<>::operator()(_UniformRandomNumberGenerator&,
......
...@@ -95,39 +95,22 @@ namespace std ...@@ -95,39 +95,22 @@ namespace std
_DInputType _DInputType
min() const min() const
{ { return _DInputType(0); }
if (is_integral<_DInputType>::value)
return _M_g.min();
else
return _DInputType(0);
}
_DInputType _DInputType
max() const max() const
{ { return _DInputType(1); }
if (is_integral<_DInputType>::value)
return _M_g.max();
else
return _DInputType(1);
}
/* /*
* Converts a value generated by the adapted random number generator * Converts a value generated by the adapted random number generator
* into a value in the input domain for the dependent random number * into a value in the input domain for the dependent random number
* distribution. * 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 _DInputType
operator()() operator()()
{ {
if (is_integral<_DInputType>::value) return std::generate_canonical<_DInputType,
return _M_g(); std::numeric_limits<_DInputType>::digits,
else
return generate_canonical<_DInputType,
numeric_limits<_DInputType>::digits,
_Engine>(_M_g); _Engine>(_M_g);
} }
...@@ -380,7 +363,7 @@ namespace std ...@@ -380,7 +363,7 @@ namespace std
static_assert(__w >= __l, static_assert(__w >= __l,
"mersenne_twister_engine template arguments out of bounds"); "mersenne_twister_engine template arguments out of bounds");
static_assert(__w <= static_assert(__w <=
static_cast<size_t>(numeric_limits<_UIntType>::digits), static_cast<size_t>(std::numeric_limits<_UIntType>::digits),
"mersenne_twister_engine template arguments out of bounds"); "mersenne_twister_engine template arguments out of bounds");
static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1), static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"mersenne_twister_engine template arguments out of bounds"); "mersenne_twister_engine template arguments out of bounds");
...@@ -559,7 +542,8 @@ namespace std ...@@ -559,7 +542,8 @@ namespace std
__glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept) __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
static_assert(__s > 0U && __r > __s static_assert(__s > 0U && __r > __s
&& __w > 0U && __w > 0U
&& __w <= static_cast<size_t>(numeric_limits<_UIntType>::digits), && __w <= static_cast<size_t>
(std::numeric_limits<_UIntType>::digits),
"template arguments out of bounds" "template arguments out of bounds"
" in subtract_with_carry_engine"); " in subtract_with_carry_engine");
...@@ -922,7 +906,8 @@ namespace std ...@@ -922,7 +906,8 @@ namespace std
{ {
static_assert(__w > 0U static_assert(__w > 0U
&& __w <= && __w <=
static_cast<size_t>(numeric_limits<_UIntType>::digits), static_cast<size_t>
(std::numeric_limits<_UIntType>::digits),
"template arguments out of bounds " "template arguments out of bounds "
"in independent_bits_engine"); "in independent_bits_engine");
...@@ -1507,7 +1492,8 @@ namespace std ...@@ -1507,7 +1492,8 @@ namespace std
typedef uniform_int_distribution<_IntType> distribution_type; typedef uniform_int_distribution<_IntType> distribution_type;
explicit 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) : _M_a(__a), _M_b(__b)
{ {
_GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b); _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
...@@ -1531,7 +1517,8 @@ namespace std ...@@ -1531,7 +1517,8 @@ namespace std
* @brief Constructs a uniform distribution object. * @brief Constructs a uniform distribution object.
*/ */
explicit 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) : _M_param(__a, __b)
{ } { }
...@@ -1602,29 +1589,7 @@ namespace std ...@@ -1602,29 +1589,7 @@ namespace std
template<typename _UniformRandomNumberGenerator> template<typename _UniformRandomNumberGenerator>
result_type result_type
operator()(_UniformRandomNumberGenerator& __urng, operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __p) 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<typename _UniformRandomNumberGenerator>
result_type
_M_call(_UniformRandomNumberGenerator& __urng,
result_type __min, result_type __max, true_type);
template<typename _UniformRandomNumberGenerator>
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;
}
param_type _M_param; param_type _M_param;
}; };
......
...@@ -644,13 +644,13 @@ namespace std ...@@ -644,13 +644,13 @@ namespace std
template<typename _UniformRandomNumberGenerator> template<typename _UniformRandomNumberGenerator>
typename uniform_int_distribution<_IntType>::result_type typename uniform_int_distribution<_IntType>::result_type
uniform_int_distribution<_IntType>:: uniform_int_distribution<_IntType>::
_M_call(_UniformRandomNumberGenerator& __urng, operator()(_UniformRandomNumberGenerator& __urng,
result_type __min, result_type __max, true_type) const param_type& __param)
{ {
// XXX Must be fixed to work well for *arbitrary* __urng.max(), // XXX Must be fixed to work well for *arbitrary* __urng.max(),
// __urng.min(), __max, __min. Currently works fine only in the // __urng.min(), __param.b(), __param.a(). Currently works fine only
// most common case __urng.max() - __urng.min() >= __max - __min, // in the most common case __urng.max() - __urng.min() >=
// with __urng.max() > __urng.min() >= 0. // __param.b() - __param.a(), with __urng.max() > __urng.min() >= 0.
typedef typename __gnu_cxx::__add_unsigned<typename typedef typename __gnu_cxx::__add_unsigned<typename
_UniformRandomNumberGenerator::result_type>::__type __urntype; _UniformRandomNumberGenerator::result_type>::__type __urntype;
typedef typename __gnu_cxx::__add_unsigned<result_type>::__type typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
...@@ -664,14 +664,14 @@ namespace std ...@@ -664,14 +664,14 @@ namespace std
const __urntype __urnmin = __urng.min(); const __urntype __urnmin = __urng.min();
const __urntype __urnmax = __urng.max(); const __urntype __urnmax = __urng.max();
const __urntype __urnrange = __urnmax - __urnmin; const __urntype __urnrange = __urnmax - __urnmin;
const __uctype __urange = __max - __min; const __uctype __urange = __param.b() - __param.a();
const __uctype __udenom = (__urnrange <= __urange const __uctype __udenom = (__urnrange <= __urange
? 1 : __urnrange / (__urange + 1)); ? 1 : __urnrange / (__urange + 1));
do do
__ret = (__urntype(__urng()) - __urnmin) / __udenom; __ret = (__urntype(__urng()) - __urnmin) / __udenom;
while (__ret > __max - __min); while (__ret > __param.b() - __param.a());
return __ret + __min; return __ret + __param.a();
} }
template<typename _IntType, typename _CharT, typename _Traits> template<typename _IntType, typename _CharT, typename _Traits>
...@@ -799,7 +799,7 @@ namespace std ...@@ -799,7 +799,7 @@ namespace std
// The largest _RealType convertible to _IntType. // The largest _RealType convertible to _IntType.
const double __thr = const double __thr =
std::numeric_limits<_IntType>::max() + __naf; std::numeric_limits<_IntType>::max() + __naf;
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, double>
__aurng(__urng); __aurng(__urng);
double __cand; double __cand;
...@@ -2021,7 +2021,7 @@ namespace std ...@@ -2021,7 +2021,7 @@ namespace std
operator()(_UniformRandomNumberGenerator& __urng, operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __param) const param_type& __param)
{ {
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, double>
__aurng(__urng); __aurng(__urng);
const double __p = __aurng(); const double __p = __aurng();
...@@ -2193,7 +2193,7 @@ namespace std ...@@ -2193,7 +2193,7 @@ namespace std
operator()(_UniformRandomNumberGenerator& __urng, operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __param) const param_type& __param)
{ {
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, double>
__aurng(__urng); __aurng(__urng);
const double __p = __aurng(); const double __p = __aurng();
...@@ -2383,7 +2383,7 @@ namespace std ...@@ -2383,7 +2383,7 @@ namespace std
operator()(_UniformRandomNumberGenerator& __urng, operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __param) const param_type& __param)
{ {
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, double>
__aurng(__urng); __aurng(__urng);
const double __p = __aurng(); const double __p = __aurng();
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
// 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist] // 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist]
#include <random> #include <random>
#include <limits>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
void void
...@@ -32,9 +33,9 @@ test01() ...@@ -32,9 +33,9 @@ test01()
std::uniform_int_distribution<int> u; std::uniform_int_distribution<int> u;
VERIFY( u.a() == 0 ); VERIFY( u.a() == 0 );
VERIFY( u.b() == 9 ); VERIFY( u.b() == std::numeric_limits<int>::max() );
VERIFY( u.min() == 0 ); VERIFY( u.min() == 0 );
VERIFY( u.max() == 9 ); VERIFY( u.max() == std::numeric_limits<int>::max() );
} }
int main() int main()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment