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: implement mersenne operators, etc.


Hi,

tested x86/ia64-linux, committed to mainline.

Paolo.

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

	* include/tr1/random (mersenne_twister<>::operator==,
	operator!=, operator<<, operator>>): Implement.
	* testsuite/tr1/5_numerical_facilities/random/mersenne_twister/
	operators/equal.cc: New.	
	* testsuite/tr1/5_numerical_facilities/random/mersenne_twister/
	operators/not_equal.cc: Likewise.
	* testsuite/tr1/5_numerical_facilities/random/mersenne_twister/
	operators/serialize.cc: Likewise.

	* include/tr1/random (subtract_with_carry<>::operator==): Fix,
	compare the whole state; use std::equal.

	* include/tr1/random: Minor formatting and style changes.
Index: include/tr1/random
===================================================================
--- include/tr1/random	(revision 114452)
+++ include/tr1/random	(working copy)
@@ -517,6 +517,76 @@
       result_type
       operator()();
 
+      /**
+       * Compares two % mersenne_twister random number generator objects of
+       * the same type for equality.
+       *
+       * @param __lhs A % mersenne_twister random number generator object.
+       * @param __rhs Another % mersenne_twister random number generator
+       *              object.
+       *
+       * @returns true if the two objects are equal, false otherwise.
+       */
+      friend bool
+      operator==(const mersenne_twister& __lhs,
+		 const mersenne_twister& __rhs)
+      { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
+
+      /**
+       * Compares two % mersenne_twister random number generator objects of
+       * the same type for inequality.
+       *
+       * @param __lhs A % mersenne_twister random number generator object.
+       * @param __rhs Another % mersenne_twister random number generator
+       *              object.
+       *
+       * @returns true if the two objects are not equal, false otherwise.
+       */
+      friend bool
+      operator!=(const mersenne_twister& __lhs,
+		 const mersenne_twister& __rhs)
+      { return !(__lhs == __rhs); }
+
+      /**
+       * Inserts the current state of a % mersenne_twister random number
+       * generator engine @p __x into the output stream @p __os.
+       *
+       * @param __os An output stream.
+       * @param __x  A % mersenne_twister random number generator engine.
+       *
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
+       */
+      template<typename _CharT, typename _Traits>
+        friend basic_ostream<_CharT, _Traits>&
+        operator<<(basic_ostream<_CharT, _Traits>& __os,
+		   const mersenne_twister& __x)
+        {
+	  std::copy(__x._M_x, __x._M_x + state_size,
+		    std::ostream_iterator<_UIntType>(__os, " "));
+	  return __os;
+	}
+
+      /**
+       * Extracts the current state of a % mersenne_twister random number
+       * generator engine @p __x from the input stream @p __is.
+       *
+       * @param __is An input stream.
+       * @param __x  A % mersenne_twister random number generator engine.
+       *
+       * @returns The input stream with the state of @p __x extracted or in
+       * an error state.
+       */
+      template<typename _CharT, typename _Traits>
+        friend basic_istream<_CharT, _Traits>&
+        operator>>(basic_istream<_CharT, _Traits>& __is,
+		   mersenne_twister& __x)
+        {
+	  for (int __i = 0; __i < state_size; ++__i)
+	    __is >> __x._M_x[__i];
+	  return __is;
+	}
+
     private:
       template<class _Gen>
         void
@@ -669,10 +739,7 @@
       friend bool
       operator==(const subtract_with_carry& __lhs,
 		 const subtract_with_carry& __rhs)
-      { 
-	return ((__lhs._M_x[0] == __rhs._M_x[0])
-		&& (__lhs._M_x[__r - 1] == __rhs._M_x[__r - 1]));
-      }
+      { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
 
       /**
        * Compares two % subtract_with_carry random number generator objects of
@@ -691,40 +758,40 @@
 
       /**
        * Inserts the current state of a % subtract_with_carry random number
-       * genator engine @p x into the output stream @p __os.
+       * generator engine @p __x into the output stream @p __os.
        *
        * @param __os An output stream.
        * @param __x  A % subtract_with_carry random number generator engine.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
         operator<<(basic_ostream<_CharT, _Traits>& __os,
 		   const subtract_with_carry& __x)
         {
-	  std::copy(__x._M_x, __x._M_x + __r,
+	  std::copy(__x._M_x, __x._M_x + long_lag,
 		    std::ostream_iterator<_IntType>(__os, " "));
 	  return __os << __x._M_carry;
 	}
 
       /**
        * Extracts the current state of a % subtract_with_carry random number
-       * gerator engine @p x from the input stream @p __is.
+       * generator engine @p __x from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __x  A % subtract_with_carry random number generator engine.
        *
-       * @returns The input stream with the state of @p x extracted or in an
-       * error state.
+       * @returns The input stream with the state of @p __x extracted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
         operator>>(basic_istream<_CharT, _Traits>& __is,
 		   subtract_with_carry& __x)
         {
-	  for (int __i = 0; __i < __r; ++__i)
+	  for (int __i = 0; __i < long_lag; ++__i)
 	    __is >> __x._M_x[__i];
 	  __is >> __x._M_carry;
 	  return __is;
@@ -865,10 +932,7 @@
        */
       friend bool
       operator==(const discard_block& __lhs, const discard_block& __rhs)
-      { 
-	return ((__lhs._M_b == __rhs._M_b)
-		&& (__lhs._M_n == __rhs._M_n));
-      }
+      { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
 
       /**
        * Compares two %discard_block random number generator objects of
@@ -886,13 +950,13 @@
 
       /**
        * Inserts the current state of a %discard_block random number
-       * genator engine @p x into the output stream @p __os.
+       * generator engine @p __x into the output stream @p __os.
        *
        * @param __os An output stream.
        * @param __x  A %discard_block random number generator engine.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
@@ -902,13 +966,13 @@
 
       /**
        * Extracts the current state of a % subtract_with_carry random number
-       * gerator engine @p x from the input stream @p __is.
+       * generator engine @p __x from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __x  A %discard_block random number generator engine.
        *
-       * @returns The input stream with the state of @p x extracted or in an
-       * error state.
+       * @returns The input stream with the state of @p __x extracted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
@@ -1035,7 +1099,7 @@
       operator==(const xor_combine& __lhs, const xor_combine& __rhs)
       {
 	return (__lhs.base1() == __rhs.base1())
-	  && (__lhs.base2() == __rhs.base2());
+	        && (__lhs.base2() == __rhs.base2());
       }
 
       /**
@@ -1054,13 +1118,13 @@
 
       /**
        * Inserts the current state of a %xor_combine random number
-       * genator engine @p x into the output stream @p __os.
+       * generator engine @p __x into the output stream @p __os.
        *
        * @param __os An output stream.
        * @param __x  A %xor_combine random number generator engine.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
@@ -1070,13 +1134,13 @@
 
       /**
        * Extracts the current state of a %xor_combine random number
-       * gerator engine @p x from the input stream @p __is.
+       * generator engine @p __x from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __x  A %xor_combine random number generator engine.
        *
-       * @returns The input stream with the state of @p x extracted or in an
-       * error state.
+       * @returns The input stream with the state of @p __x extracted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
@@ -1199,14 +1263,14 @@
         { return __urng() % __n; }
 
       /**
-       * Inserts a %uniform_int random number distribution @p x into the
+       * Inserts a %uniform_int random number distribution @p __x into the
        * output stream @p os.
        *
        * @param __os An output stream.
        * @param __x  A %uniform_int random number distribution.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
@@ -1216,12 +1280,12 @@
 
       /**
        * Extracts a %unform_int random number distribution
-       * @p u from the input stream @p __is.
+       * @p __u from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __u  A %uniform_int random number generator engine.
        *
-       * @returns The input stream with @p u extracted or in an error state.
+       * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
@@ -1303,13 +1367,13 @@
 
     /**
      * Inserts a %bernoulli_distribution random number distribution
-     * @p x into the output stream @p __os.
+     * @p __x into the output stream @p __os.
      *
      * @param __os An output stream.
      * @param __x  A %bernoulli_distribution random number distribution.
      *
-     * @returns The output stream with the state of @p x inserted or in an
-     * error state.
+     * @returns The output stream with the state of @p __x inserted or in
+     * an error state.
      */
     template<typename _CharT, typename _Traits>
       friend basic_ostream<_CharT, _Traits>&
@@ -1319,12 +1383,12 @@
 
     /**
      * Extracts a %bernoulli_distribution random number distribution
-     * @p u from the input stream @p __is.
+     * @p __u from the input stream @p __is.
      *
      * @param __is An input stream.
      * @param __u  A %bernoulli_distribution random number generator engine.
      *
-     * @returns The input stream with @p u extracted or in an error state.
+     * @returns The input stream with @p __u extracted or in an error state.
      */
     template<typename _CharT, typename _Traits>
       friend basic_istream<_CharT, _Traits>&
@@ -1393,13 +1457,13 @@
 
       /**
        * Inserts a %geometric_distribution random number distribution
-       * @p x into the output stream @p __os.
+       * @p __x into the output stream @p __os.
        *
        * @param __os An output stream.
        * @param __x  A %geometric_distribution random number distribution.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
@@ -1409,12 +1473,12 @@
 
       /**
        * Extracts a %geometric_distribution random number distribution
-       * @p u from the input stream @p __is.
+       * @p __u from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __u  A %geometric_distribution random number generator engine.
        *
-       * @returns The input stream with @p u extracted or in an error state.
+       * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
@@ -1479,14 +1543,14 @@
         { return (__urng() * (max() - min())) + min(); }
 
       /**
-       * Inserts a %uniform_real random number distribution @p x into the
+       * Inserts a %uniform_real random number distribution @p __x into the
        * output stream @p __os.
        *
        * @param __os An output stream.
        * @param __x  A %uniform_real random number distribution.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
@@ -1496,12 +1560,12 @@
 
       /**
        * Extracts a %unform_real random number distribution
-       * @p u from the input stream @p __is.
+       * @p __u from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __u  A %uniform_real random number generator engine.
        *
-       * @returns The input stream with @p u extracted or in an error state.
+       * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
@@ -1569,13 +1633,13 @@
 
       /**
        * Inserts a %exponential_distribution random number distribution
-       * @p x into the output stream @p __os.
+       * @p __x into the output stream @p __os.
        *
        * @param __os An output stream.
        * @param __x  A %exponential_distribution random number distribution.
        *
-       * @returns The output stream with the state of @p x inserted or in an
-       * error state.
+       * @returns The output stream with the state of @p __x inserted or in
+       * an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_ostream<_CharT, _Traits>&
@@ -1585,12 +1649,12 @@
 
       /**
        * Extracts a %exponential_distribution random number distribution
-       * @p u from the input stream @p __is.
+       * @p __u from the input stream @p __is.
        *
        * @param __is An input stream.
        * @param __u  A %exponential_distribution random number generator engine.
        *
-       * @returns The input stream with @p u extracted or in an error state.
+       * @returns The input stream with @p __u extracted or in an error state.
        */
       template<typename _CharT, typename _Traits>
         friend basic_istream<_CharT, _Traits>&
Index: testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/not_equal.cc
===================================================================
--- testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/not_equal.cc	(revision 0)
+++ testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/not_equal.cc	(revision 0)
@@ -0,0 +1,52 @@
+// 2006-06-06  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2006 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// 5.1.4.2 class template mersenne_twister [tr.rand.eng.mers]
+// 5.1.1 Table 16
+
+#include <tr1/random>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std::tr1;
+
+  mersenne_twister<
+    unsigned long, 32, 624, 397, 31,
+    0x9908b0dful, 11, 7,
+    0x9d2c5680ul, 15,
+    0xefc60000ul, 18> u(1);
+
+  mersenne_twister<
+    unsigned long, 32, 624, 397, 31,
+    0x9908b0dful, 11, 7,
+    0x9d2c5680ul, 15,
+    0xefc60000ul, 18> v(2);
+  
+  VERIFY( u != v );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/equal.cc
===================================================================
--- testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/equal.cc	(revision 0)
+++ testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/equal.cc	(revision 0)
@@ -0,0 +1,53 @@
+// 2006-06-06  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2006 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// 5.1.4.2 class template mersenne_twister [tr.rand.eng.mers]
+// 5.1.1 Table 16
+
+#include <tr1/random>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std::tr1;
+
+  mersenne_twister<
+    unsigned long, 32, 624, 397, 31,
+    0x9908b0dful, 11, 7,
+    0x9d2c5680ul, 15,
+    0xefc60000ul, 18> u, v;
+
+  VERIFY( u == v );
+  
+  for (int i = 0; i < 100; ++i)
+    {
+      u();
+      v();
+    }
+  VERIFY( u == v );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/serialize.cc
===================================================================
--- testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/serialize.cc	(revision 0)
+++ testsuite/tr1/5_numerical_facilities/random/mersenne_twister/operators/serialize.cc	(revision 0)
@@ -0,0 +1,54 @@
+// 2006-06-06  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2006 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// 5.1.4.2 class template mersenne_twister [tr.rand.eng.mers]
+// 5.1.1 Table 16
+
+#include <sstream>
+#include <tr1/random>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::mersenne_twister;
+
+  std::stringstream str;
+  mersenne_twister<
+    unsigned long, 32, 624, 397, 31,
+    0x9908b0dful, 11, 7,
+    0x9d2c5680ul, 15,
+    0xefc60000ul, 18> u, v;
+  
+  u(); // advance
+  str << u;
+  
+  VERIFY( u != v );
+  
+  str >> v;
+  VERIFY( u == v );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

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