This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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] Follow-up tweaks to tr1::poisson_distribution


Hi,

tested x86-linux, committed.

Paolo.

/////////////////////
2006-08-15  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/random (poisson_distribution<>::_M_initialize): Add.
	(poisson_distribution<>::poisson_distribution(const _RealType&):
	Use it.
	(operator>>(std::basic_istream<>&, poisson_distribution<>&)):
	Likewise.
	(poisson_distribution<>::_M_large): Remove.
	* include/tr1/random.tcc (poisson_distribution<>::_M_initialize):
	Define.
	(operator<<(std::basic_ostream<>&, const poisson_distribution<>&)):
	Do not output the constants.

	* include/tr1/random (operator>>(std::basic_istream<>&,
	gamma_distribution&)): Minor tweak.
	
	* include/tr1/random.tcc (poisson_distribution<>::operator()):
	Minor tweak.

	* include/tr1/random: Consistently, all data members private.
Index: include/tr1/random
===================================================================
--- include/tr1/random	(revision 116152)
+++ include/tr1/random	(working copy)
@@ -1556,7 +1556,7 @@
 		 bernoulli_distribution& __x)
       { return __is >> __x._M_p; }
 
-  protected:
+  private:
     double _M_p;
   };
 
@@ -1643,7 +1643,7 @@
 	  return __is;
 	}
 
-    protected:
+    private:
       _RealType _M_p;
       _RealType _M_log_p;
     };
@@ -1665,12 +1665,6 @@
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 	       const poisson_distribution<_IntType, _RealType>& __x);
 
-  template<typename _IntType, typename _RealType,
-	   typename _CharT, typename _Traits>
-    std::basic_istream<_CharT, _Traits>&
-    operator>>(std::basic_istream<_CharT, _Traits>& __is,
-	       poisson_distribution<_IntType, _RealType>& __x);
-
   template<typename _IntType, typename _RealType>
     class poisson_distribution
     {
@@ -1681,7 +1675,12 @@
 
       // constructors and member function
       explicit
-      poisson_distribution(const _RealType& __mean = _RealType(1));
+      poisson_distribution(const _RealType& __mean = _RealType(1))
+      : _M_mean(__mean)
+      {
+	_GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
+	_M_initialize();
+      }
 
       /**
        * Gets the distribution parameter @p mean.
@@ -1722,20 +1721,27 @@
        *
        * @returns The input stream with @p __x extracted or in an error state.
        */
-      template<typename _IntType1, typename _RealType1,
-	       typename _CharT, typename _Traits>
+      template<typename _CharT, typename _Traits>
         friend std::basic_istream<_CharT, _Traits>&
         operator>>(std::basic_istream<_CharT, _Traits>& __is,
-		   poisson_distribution<_IntType1, _RealType1>& __x);
+		   poisson_distribution& __x)
+        {
+	  __is >> __x._M_mean;
+	  __x._M_initialize();
+	  return __is;
+	}
 
-    protected:
+    private:
+      void
+      _M_initialize();
+
       _RealType _M_mean;
-
+      // _M_lm_thr hosts either log(mean) or the threshold of the simple
+      // method.
       _RealType _M_lm_thr;
 #if _GLIBCXX_USE_C99_MATH_TR1
       _RealType _M_lfm, _M_sm, _M_d, _M_scx4, _M_2cx, _M_c2b, _M_cb;
 #endif
-      bool _M_large;
     };
 
   /* @} */ // group tr1_random_distributions_discrete
@@ -1834,7 +1840,7 @@
         operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   uniform_real<_RealType1>& __x);
 
-    protected:
+    private:
       _RealType _M_min;
       _RealType _M_max;
     };
@@ -2115,10 +2121,10 @@
        *
        * @returns The input stream with @p __x extracted or in an error state.
        */
-      template<typename _RealType1, typename _CharT, typename _Traits>
+      template<typename _CharT, typename _Traits>
         friend std::basic_istream<_CharT, _Traits>&
         operator>>(std::basic_istream<_CharT, _Traits>& __is,
-		   gamma_distribution<_RealType1>& __x)
+		   gamma_distribution& __x)
         { return __is >> __x._M_alpha; }
 
     private:
Index: include/tr1/random.tcc
===================================================================
--- include/tr1/random.tcc	(revision 116152)
+++ include/tr1/random.tcc	(working copy)
@@ -656,16 +656,13 @@
 
 
   template<typename _IntType, typename _RealType>
+    void
     poisson_distribution<_IntType, _RealType>::
-    poisson_distribution(const _RealType& __mean)
-    : _M_mean(__mean), _M_large(false)
+    _M_initialize()
     {
-      _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
-
 #if _GLIBCXX_USE_C99_MATH_TR1
       if (_M_mean >= 12)
 	{
-	  _M_large = true;
 	  const _RealType __m = std::floor(_M_mean);
 	  _M_lm_thr = std::log(_M_mean);
 	  _M_lfm = std::tr1::lgamma(__m + 1);
@@ -708,20 +705,20 @@
       operator()(_UniformRandomNumberGenerator& __urng)
       {
 #if _GLIBCXX_USE_C99_MATH_TR1
-	if (_M_large)
+	if (_M_mean >= 12)
 	  {
 	    _RealType __x;
 
 	    const _RealType __m = std::floor(_M_mean);
-	    // sqrt(mu * pi / 2)
-	    const _RealType __c1 = (_M_sm
-				    * 1.2533141373155002512078826424055226L);
+	    // sqrt(pi / 2)
+	    const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
+	    const _RealType __c1 = _M_sm * __spi_2;
 	    const _RealType __c2 = _M_c2b + __c1; 
 	    const _RealType __c3 = __c2 + 1;
 	    const _RealType __c4 = __c3 + 1;
-	    // c4 + e^(1 / 78)
-	    const _RealType __c5 = (__c4
-				    + 1.0129030479320018583185514777512983L);
+	    // e^(1 / 78)
+	    const _RealType __e178 = 1.0129030479320018583185514777512983L;
+	    const _RealType __c5 = __c4 + __e178;
 	    const _RealType __c = _M_cb + __c5;
 	    const _RealType __cx = 2 * (2 * __m + _M_d);
 
@@ -801,20 +798,11 @@
       const std::ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
       const std::streamsize __precision = __os.precision();
-      const _CharT __space = __os.widen(' ');
       __os.flags(std::ios_base::scientific | std::ios_base::left);
-      __os.fill(__space);
+      __os.fill(__os.widen(' '));
       __os.precision(_Max_digits10<_RealType>::__value);
 
-      __os << __x._M_large << __space << __x.mean()
-	   << __space << __x._M_lm_thr;
-#if _GLIBCXX_USE_C99_MATH_TR1
-      if (__x._M_large)
-	__os << __space << __x._M_lfm << __space << __x._M_sm
-	     << __space << __x._M_d << __space << __x._M_scx4
-	     << __space << __x._M_2cx << __space << __x._M_c2b
-	     << __space << __x._M_cb;
-#endif
+      __os << __x.mean();
 
       __os.flags(__flags);
       __os.fill(__fill);
@@ -822,27 +810,7 @@
       return __os;
     }
 
-  template<typename _IntType, typename _RealType,
-	   typename _CharT, typename _Traits>
-    std::basic_istream<_CharT, _Traits>&
-    operator>>(std::basic_istream<_CharT, _Traits>& __is,
-	       poisson_distribution<_IntType, _RealType>& __x)
-    {
-      const std::ios_base::fmtflags __flags = __is.flags();
-      __is.flags(std::ios_base::skipws);
 
-      __is >> __x._M_large >> __x._M_mean >> __x._M_lm_thr;
-#if _GLIBCXX_USE_C99_MATH_TR1
-      if (__x._M_large)
-	__is >> __x._M_lfm >> __x._M_sm >> __x._M_d >> __x._M_scx4
-	     >> __x._M_2cx >> __x._M_c2b >> __x._M_cb;
-#endif
-
-      __is.flags(__flags);
-      return __is;
-    }
-
-
   template<typename _RealType, typename _CharT, typename _Traits>
     std::basic_ostream<_CharT, _Traits>&
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,

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