[PATCH] libstdc++: Simplify intervals() and densities() for piecewise distributions

Jonathan Wakely jwakely@redhat.com
Sun Nov 17 00:17:13 GMT 2024


Make the members of the distributions call the equivalent member of the
param_type, instead of duplicating the logic.

Also add FIXME comments about the vector<double> members of the param
types, which were changed by LWG 1439 in 2010 but never fixed in
libstdc++.

libstdc++-v3/ChangeLog:

	* include/bits/random.h (piecewise_constant_distribution):
	Simplify member functions and add comments about
	non-conformance.
	(piecewise_linear_distribution): Likewise.
---

Tested x86_64-linux.

Fixing the incorrect types would be an ABI break, so isn't easy.

 libstdc++-v3/include/bits/random.h | 83 +++++++++---------------------
 1 file changed, 25 insertions(+), 58 deletions(-)

diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h
index 1f39f0a90be..a90e52704a9 100644
--- a/libstdc++-v3/include/bits/random.h
+++ b/libstdc++-v3/include/bits/random.h
@@ -5826,15 +5826,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	param_type(const param_type&) = default;
 	param_type& operator=(const param_type&) = default;
 
-	std::vector<_RealType>
+	std::vector<result_type>
 	intervals() const
 	{
 	  if (_M_int.empty())
-	    {
-	      std::vector<_RealType> __tmp(2);
-	      __tmp[1] = _RealType(1);
-	      return __tmp;
-	    }
+	    return std::vector<result_type>{_RealType(0), _RealType(1)};
 	  else
 	    return _M_int;
 	}
@@ -5857,9 +5853,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	void
 	_M_initialize();
 
-	std::vector<_RealType> _M_int;
-	std::vector<double> _M_den;
-	std::vector<double> _M_cp;
+	std::vector<result_type> _M_int;
+	std::vector<double> _M_den; // FIXME: should be vector<result_type>
+	std::vector<double> _M_cp;  // FIXME: should be vector<result_type>
       };
 
       piecewise_constant_distribution()
@@ -5901,28 +5897,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        * @brief Returns a vector of the intervals.
        */
-      std::vector<_RealType>
+      std::vector<result_type>
       intervals() const
-      {
-	if (_M_param._M_int.empty())
-	  {
-	    std::vector<_RealType> __tmp(2);
-	    __tmp[1] = _RealType(1);
-	    return __tmp;
-	  }
-	else
-	  return _M_param._M_int;
-      }
+      { return _M_param.intervals(); }
 
       /**
        * @brief Returns a vector of the probability densities.
        */
       std::vector<double>
       densities() const
-      {
-	return _M_param._M_den.empty()
-	  ? std::vector<double>(1, 1.0) : _M_param._M_den;
-      }
+      { return _M_param.densities(); }
 
       /**
        * @brief Returns the parameter set of the distribution.
@@ -5945,8 +5929,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       result_type
       min() const
       {
-	return _M_param._M_int.empty()
-	  ? result_type(0) : _M_param._M_int.front();
+	return _M_param._M_int.empty() ? result_type(0)
+				       : _M_param._M_int.front();
       }
 
       /**
@@ -5955,8 +5939,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       result_type
       max() const
       {
-	return _M_param._M_int.empty()
-	  ? result_type(1) : _M_param._M_int.back();
+	return _M_param._M_int.empty() ? result_type(1)
+				       : _M_param._M_int.back();
       }
 
       /**
@@ -6102,19 +6086,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
 		     _Func __fw);
 
-	// See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
 	param_type(const param_type&) = default;
 	param_type& operator=(const param_type&) = default;
 
-	std::vector<_RealType>
+	std::vector<result_type>
 	intervals() const
 	{
 	  if (_M_int.empty())
-	    {
-	      std::vector<_RealType> __tmp(2);
-	      __tmp[1] = _RealType(1);
-	      return __tmp;
-	    }
+	    return std::vector<result_type>{_RealType(0), _RealType(1)};
 	  else
 	    return _M_int;
 	}
@@ -6137,10 +6116,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	void
 	_M_initialize();
 
-	std::vector<_RealType> _M_int;
-	std::vector<double> _M_den;
-	std::vector<double> _M_cp;
-	std::vector<double> _M_m;
+	std::vector<result_type> _M_int;
+	std::vector<double> _M_den; // FIXME: should be vector<result_type>
+	std::vector<double> _M_cp;  // FIXME: should be vector<result_type>
+	std::vector<double> _M_m;   // FIXME: should be vector<result_type>
       };
 
       piecewise_linear_distribution()
@@ -6182,18 +6161,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        * @brief Return the intervals of the distribution.
        */
-      std::vector<_RealType>
+      std::vector<result_type>
       intervals() const
-      {
-	if (_M_param._M_int.empty())
-	  {
-	    std::vector<_RealType> __tmp(2);
-	    __tmp[1] = _RealType(1);
-	    return __tmp;
-	  }
-	else
-	  return _M_param._M_int;
-      }
+      { return _M_param.intervals(); }
 
       /**
        * @brief Return a vector of the probability densities of the
@@ -6201,10 +6171,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        */
       std::vector<double>
       densities() const
-      {
-	return _M_param._M_den.empty()
-	  ? std::vector<double>(2, 1.0) : _M_param._M_den;
-      }
+      { return _M_param.densities(); }
 
       /**
        * @brief Returns the parameter set of the distribution.
@@ -6227,8 +6194,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       result_type
       min() const
       {
-	return _M_param._M_int.empty()
-	  ? result_type(0) : _M_param._M_int.front();
+	return _M_param._M_int.empty() ? result_type(0)
+				       : _M_param._M_int.front();
       }
 
       /**
@@ -6237,8 +6204,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       result_type
       max() const
       {
-	return _M_param._M_int.empty()
-	  ? result_type(1) : _M_param._M_int.back();
+	return _M_param._M_int.empty() ? result_type(1)
+				       : _M_param._M_int.back();
       }
 
       /**
-- 
2.47.0



More information about the Libstdc++ mailing list