This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] More work on <random>


Hi,

tested x86_64-linux, committed to mainline.

Paolo.

//////////////////////
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.
Index: include/bits/random.tcc
===================================================================
--- include/bits/random.tcc	(revision 148719)
+++ include/bits/random.tcc	(working copy)
@@ -644,13 +644,13 @@
     template<typename _UniformRandomNumberGenerator>
       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<typename
 	  _UniformRandomNumberGenerator::result_type>::__type __urntype;
 	typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
@@ -664,14 +664,14 @@
 	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<typename _IntType, typename _CharT, typename _Traits>
@@ -799,7 +799,7 @@
 	// 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 @@
       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 @@
       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 @@
       operator()(_UniformRandomNumberGenerator& __urng,
 		 const param_type& __param)
       {
-	__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
+	__detail::_Adaptor<_UniformRandomNumberGenerator, double>
 	  __aurng(__urng);
 
 	const double __p = __aurng();
Index: include/bits/random.h
===================================================================
--- include/bits/random.h	(revision 148719)
+++ include/bits/random.h	(working copy)
@@ -95,40 +95,23 @@
 
 	_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 @@
       static_assert(__w >= __l,
 		    "mersenne_twister_engine template arguments out of bounds");
       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");
       static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
 		    "mersenne_twister_engine template arguments out of bounds");
@@ -558,8 +541,9 @@
     {
       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
       static_assert(__s > 0U && __r > __s
-		 && __w > 0U
-		 && __w <= static_cast<size_t>(numeric_limits<_UIntType>::digits),
+		    && __w > 0U
+		    && __w <= static_cast<size_t>
+		    (std::numeric_limits<_UIntType>::digits),
 		    "template arguments out of bounds"
 		    " in subtract_with_carry_engine");
 
@@ -922,7 +906,8 @@
     {
       static_assert(__w > 0U
 		    && __w <=
-		    static_cast<size_t>(numeric_limits<_UIntType>::digits),
+		    static_cast<size_t>
+		    (std::numeric_limits<_UIntType>::digits),
 		    "template arguments out of bounds "
 		    "in independent_bits_engine");
 
@@ -1507,7 +1492,8 @@
 	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 @@
        * @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,30 +1589,8 @@
       template<typename _UniformRandomNumberGenerator>
 	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());
-	}
+		   const param_type& __p);
 
-    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;
     };
 
Index: testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc
===================================================================
--- testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc	(revision 148718)
+++ testsuite/26_numerics/random/uniform_int_distribution/cons/default.cc	(working copy)
@@ -23,6 +23,7 @@
 // 26.4.2.4 Concept RandomNumberDistribution [rand.concept.dist]
 
 #include <random>
+#include <limits>
 #include <testsuite_hooks.h>
 
 void
@@ -32,9 +33,9 @@
 
   std::uniform_int_distribution<int> u;
   VERIFY( u.a() == 0 );
-  VERIFY( u.b() == 9 );
+  VERIFY( u.b() == std::numeric_limits<int>::max() );
   VERIFY( u.min() == 0 );
-  VERIFY( u.max() == 9 );
+  VERIFY( u.max() == std::numeric_limits<int>::max() );
 }
 
 int main()

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]