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] tr1/random/random_device follow-up


Hi,

tested x86-linux, committed to mainline.

Paolo.

/////////////////
2006-06-09  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/random (random_device::random_device(const
	std::string& = "/dev/urandom")): Open in binary mode.

	* include/tr1/random (random_device::random_device(const
	std::string& = "rand")): Use mersenne_twister.
	(random_device::_M_strtoul): New.
	(random_device::operator()()): Update.

	* include/tr1/random: Minor stylistic changes, consistently
	qualify with std::.
Index: include/tr1/random
===================================================================
--- include/tr1/random	(revision 114529)
+++ include/tr1/random	(working copy)
@@ -44,7 +44,6 @@
 #include <iosfwd>
 #include <limits>
 #include <tr1/type_traits>
-#include <sstream>
 #include <fstream>
 
 namespace std
@@ -560,8 +559,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const mersenne_twister& __x)
         {
 	  std::copy(__x._M_x, __x._M_x + state_size,
@@ -580,8 +579,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   mersenne_twister& __x)
         {
 	  for (int __i = 0; __i < state_size; ++__i)
@@ -769,8 +768,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const subtract_with_carry& __x)
         {
 	  std::copy(__x._M_x, __x._M_x + long_lag,
@@ -789,8 +788,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   subtract_with_carry& __x)
         {
 	  for (int __i = 0; __i < long_lag; ++__i)
@@ -961,8 +960,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const discard_block& __x)
         { return __os << __x._M_b << " " << __x._M_n; }
 
@@ -977,8 +976,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   discard_block& __x)
         { return __is >> __x._M_b >> __x._M_n; }
 
@@ -1129,8 +1128,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const xor_combine& __x)
         { return __os << __x.base1() << " " << __x.base1(); }
 
@@ -1145,8 +1144,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   xor_combine& __x)
         { return __is >> __x._M_b1 >> __x._M_b2; }
 
@@ -1165,15 +1164,17 @@
   public:
     // types
     typedef unsigned int result_type;
-    
+
     // constructors, destructors and member functions
 
 #ifdef _GLIBCXX_USE_RANDOM_TR1
+
     explicit
     random_device(const std::string& __token = "/dev/urandom")
     {
       if ((__token != "/dev/urandom" && __token != "/dev/random")
-	  || !_M_filebuf.open(__token.c_str(), std::ios_base::in))
+	  || !_M_filebuf.open(__token.c_str(),
+			      std::ios_base::in | std::ios_base::binary))
 	std::__throw_runtime_error(__N("random_device::"
 				       "random_device(const std::string&)"));
     }
@@ -1182,21 +1183,30 @@
     { _M_filebuf.close(); }
 
 #else
+
     explicit
     random_device(const std::string& __token = "rand")
+    : _M_mt(_M_strtoul(__token)) { }
+
+  private:
+    static unsigned long
+    _M_strtoul(const std::string& __str)
     {
-      if (__token != "rand")
+      unsigned long __ret = 5489UL;
+      if (__str != "rand")
 	{
-	  std::stringstream __ss(__token);
-	  unsigned int __seed;
-	  __ss >> __seed;
-	  if (__ss.fail())
-	    std::__throw_runtime_error(__N("random_device::random_device"
+	  const char* __nptr = __str.c_str();
+	  char* __endptr;
+	  __ret = std::strtoul(__nptr, &__endptr, 0);
+	  if (*__nptr == '\0' || *__endptr != '\0')
+	    std::__throw_runtime_error(__N("random_device::_M_strtoul"
 					   "(const std::string&)"));
-	  else
-	    std::srand(__seed);
 	}
+      return __ret;
     }
+
+  public:
+
 #endif
 
     result_type
@@ -1219,7 +1229,7 @@
       _M_filebuf.sgetn(reinterpret_cast<char*>(&__ret), sizeof(result_type));
       return __ret;
 #else
-      return max() * (std::rand() / double(RAND_MAX));
+      return _M_mt();
 #endif
     }
 
@@ -1229,7 +1239,9 @@
 
 #ifdef _GLIBCXX_USE_RANDOM_TR1
     std::filebuf _M_filebuf;
-#endif    
+#else
+    mt19937      _M_mt;
+#endif
   };
 
   /* @} */ // group tr1_random_generators
@@ -1325,8 +1337,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const uniform_int& __x)
         { return __os << __x._M_min << " " << __x._M_max; }
 
@@ -1340,8 +1352,8 @@
        * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is, uniform_int& __u)
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is, uniform_int& __u)
         { return __is >> __u._M_min >> __u._M_max; }
 
     private:
@@ -1428,8 +1440,8 @@
      * an error state.
      */
     template<typename _CharT, typename _Traits>
-      friend basic_ostream<_CharT, _Traits>&
-      operator<<(basic_ostream<_CharT, _Traits>& __os,
+      friend std::basic_ostream<_CharT, _Traits>&
+      operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		 const bernoulli_distribution& __x)
       { return __os << __x.p(); }
 
@@ -1443,8 +1455,8 @@
      * @returns The input stream with @p __u extracted or in an error state.
      */
     template<typename _CharT, typename _Traits>
-      friend basic_istream<_CharT, _Traits>&
-      operator>>(basic_istream<_CharT, _Traits>& __is,
+      friend std::basic_istream<_CharT, _Traits>&
+      operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		 bernoulli_distribution& __u)
       { return __is >> __u._M_p; }
 
@@ -1518,8 +1530,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const geometric_distribution& __x)
         { return __os << __x.p(); }
 
@@ -1533,8 +1545,8 @@
        * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   geometric_distribution& __u)
         {
 	  __is >> __u._M_p;
@@ -1605,8 +1617,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const uniform_real& __x)
         { return __os << __x.min() << " " << __x.max(); }
 
@@ -1620,9 +1632,9 @@
        * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
-		 uniform_real& __u)
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
+		   uniform_real& __u)
         { return __is >> __u._M_min >> __u._M_max; }
 
     protected:
@@ -1694,8 +1706,8 @@
        * an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_ostream<_CharT, _Traits>&
-        operator<<(basic_ostream<_CharT, _Traits>& __os,
+        friend std::basic_ostream<_CharT, _Traits>&
+        operator<<(std::basic_ostream<_CharT, _Traits>& __os,
 		   const exponential_distribution& __x)
         { return __os << __x.lambda(); }
 
@@ -1709,8 +1721,8 @@
        * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
-        friend basic_istream<_CharT, _Traits>&
-        operator>>(basic_istream<_CharT, _Traits>& __is,
+        friend std::basic_istream<_CharT, _Traits>&
+        operator>>(std::basic_istream<_CharT, _Traits>& __is,
 		   exponential_distribution& __u)
         { return __is >> __u._M_lambda; }
 

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