Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63278 - in trunk: boost/random libs/random/doc libs/random/test
From: steven_at_[hidden]
Date: 2010-06-23 23:05:47


Author: steven_watanabe
Date: 2010-06-23 23:05:45 EDT (Wed, 23 Jun 2010)
New Revision: 63278
URL: http://svn.boost.org/trac/boost/changeset/63278

Log:
Sync discard_block with C++0x.
Text files modified:
   trunk/boost/random/discard_block.hpp | 211 ++++++++++++++++++++++++++-------------
   trunk/boost/random/ranlux.hpp | 16 +-
   trunk/boost/random/subtract_with_carry.hpp | 2
   trunk/libs/random/doc/Jamfile.v2 | 2
   trunk/libs/random/test/instantiate.cpp | 3
   5 files changed, 151 insertions(+), 83 deletions(-)

Modified: trunk/boost/random/discard_block.hpp
==============================================================================
--- trunk/boost/random/discard_block.hpp (original)
+++ trunk/boost/random/discard_block.hpp 2010-06-23 23:05:45 EDT (Wed, 23 Jun 2010)
@@ -1,6 +1,7 @@
 /* boost random/discard_block.hpp header file
  *
  * Copyright Jens Maurer 2002
+ * Copyright Steven Watanabe 2010
  * Distributed under the Boost Software License, Version 1.0. (See
  * accompanying file LICENSE_1_0.txt or copy at
  * http://www.boost.org/LICENSE_1_0.txt)
@@ -21,108 +22,174 @@
 #include <boost/limits.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/random/detail/config.hpp>
+#include <boost/random/detail/seed.hpp>
 
 
 namespace boost {
 namespace random {
 
 /**
- * The class template \discard_block is a model of
+ * The class template \discard_block_engine is a model of
  * \pseudo_random_number_generator. It modifies
  * another generator by discarding parts of its output.
- * Out of every block of @c r results, the first @c p
+ * Out of every block of @c p results, the first @c r
  * will be returned and the rest discarded.
  *
  * Requires: 0 < p <= r
  */
-template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
-class discard_block
+template<class UniformRandomNumberGenerator, std::size_t p, std::size_t r>
+class discard_block_engine
 {
 public:
- typedef UniformRandomNumberGenerator base_type;
- typedef typename base_type::result_type result_type;
+ typedef UniformRandomNumberGenerator base_type;
+ typedef typename base_type::result_type result_type;
 
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
- BOOST_STATIC_CONSTANT(unsigned int, total_block = p);
- BOOST_STATIC_CONSTANT(unsigned int, returned_block = r);
+ BOOST_STATIC_CONSTANT(std::size_t, block_size = p);
+ BOOST_STATIC_CONSTANT(std::size_t, used_block = r);
 
-#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
- BOOST_STATIC_ASSERT(total_block >= returned_block);
+ BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
+ BOOST_STATIC_CONSTANT(std::size_t, total_block = p);
+ BOOST_STATIC_CONSTANT(std::size_t, returned_block = r);
+
+ BOOST_STATIC_ASSERT(total_block >= returned_block);
+
+ /** Uses the default seed for the base generator. */
+ discard_block_engine() : _rng(), _n(0) { }
+ /** Constructs a new \discard_block_engine with a copy of rng. */
+ explicit discard_block_engine(const base_type & rng) : _rng(rng), _n(0) { }
+
+#ifndef BOOST_NO_RVALUE_REFERENCES
+ /** Constructs a new \discard_block_engine with rng. */
+ explicit discard_block_engine(base_type && rng) : _rng(rng), _n(0) { }
 #endif
 
- discard_block() : _rng(), _n(0) { }
- explicit discard_block(const base_type & rng) : _rng(rng), _n(0) { }
- template<class T> explicit discard_block(T s) : _rng(s), _n(0) {}
- template<class It> discard_block(It& first, It last)
- : _rng(first, last), _n(0) { }
- void seed() { _rng.seed(); _n = 0; }
- template<class T> void seed(T s) { _rng.seed(s); _n = 0; }
- template<class It> void seed(It& first, It last)
- { _n = 0; _rng.seed(first, last); }
-
- const base_type& base() const { return _rng; }
-
- result_type operator()()
- {
- if(_n >= returned_block) {
- // discard values of random number generator
- for( ; _n < total_block; ++_n)
- _rng();
- _n = 0;
+ /**
+ * Creates a new \discard_block_engine and seeds the underlying
+ * generator with @c value
+ */
+ BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(discard_block_engine,
+ result_type, value)
+ { _rng.seed(value); _n = 0; }
+
+ /**
+ * Creates a new \discard_block_engine and seeds the underlying
+ * generator with @c seq
+ */
+ BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(discard_block_engine, SeedSeq, seq)
+ { _rng.seed(seq); _n = 0; }
+
+ /**
+ * Creates a new \discard_block_engine and seeds the underlying
+ * generator with first and last.
+ */
+ template<class It> discard_block_engine(It& first, It last)
+ : _rng(first, last), _n(0) { }
+
+ /** default seeds the underlying generator. */
+ void seed() { _rng.seed(); _n = 0; }
+ /** Seeds the underlying generator with s. */
+ BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(discard_block_engine, result_type, s)
+ { _rng.seed(s); _n = 0; }
+ /** Seeds the underlying generator with seq. */
+ BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(discard_block_engine, SeedSeq, seq)
+ { _rng.seed(seq); _n = 0; }
+ /** Seeds the underlying generator with first and last. */
+ template<class It> void seed(It& first, It last)
+ { _rng.seed(first, last); _n = 0; }
+
+ /** Returns the underlying engine. */
+ const base_type& base() const { return _rng; }
+
+ /** Returns the next value of the generator. */
+ result_type operator()()
+ {
+ if(_n >= returned_block) {
+ // discard values of random number generator
+ for( ; _n < total_block; ++_n)
+ _rng();
+ _n = 0;
+ }
+ ++_n;
+ return _rng();
     }
- ++_n;
- return _rng();
- }
-
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
- static bool validation(result_type x) { return true; } // dummy
 
-#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
+#ifndef BOOST_NO_LONG_LONG
+ void discard(boost::ulong_long_type z)
+ {
+ for(boost::ulong_long_type j = 0; j < z; ++j) {
+ (*this)();
+ }
+ }
+#endif
+
+ template<class It>
+ void generate(It first, It last)
+ {
+ for(; first != last; ++first) {
+ *first = (*this)();
+ }
+ }
+
+ /**
+ * Returns the smallest value that the generator can produce.
+ * This is the same as the minimum of the underlying generator.
+ */
+ static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return (base_type::min)(); }
+ /**
+ * Returns the largest value that the generator can produce.
+ * This is the same as the maximum of the underlying generator.
+ */
+ static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return (base_type::max)(); }
 
 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const discard_block& s)
- {
- os << s._rng << " " << s._n << " ";
- return os;
- }
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, discard_block& s)
- {
- is >> s._rng >> std::ws >> s._n >> std::ws;
- return is;
- }
-#endif
+ /** Writes a \discard_block_engine to a @c std::ostream. */
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os,
+ const discard_block_engine& s)
+ {
+ os << s._rng << " " << s._n;
+ return os;
+ }
 
- friend bool operator==(const discard_block& x, const discard_block& y)
- { return x._rng == y._rng && x._n == y._n; }
- friend bool operator!=(const discard_block& x, const discard_block& y)
- { return !(x == y); }
-#else
- // Use a member function; Streamable concept not supported.
- bool operator==(const discard_block& rhs) const
- { return _rng == rhs._rng && _n == rhs._n; }
- bool operator!=(const discard_block& rhs) const
- { return !(*this == rhs); }
+ /** Reads a \discard_block_engine from a @c std::istream. */
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, discard_block_engine& s)
+ {
+ is >> s._rng >> std::ws >> s._n;
+ return is;
+ }
 #endif
 
+ /** Returns true if the two generators will produce identical sequences. */
+ friend bool operator==(const discard_block_engine& x,
+ const discard_block_engine& y)
+ { return x._rng == y._rng && x._n == y._n; }
+ /** Returns true if the two generators will produce different sequences. */
+ friend bool operator!=(const discard_block_engine& x,
+ const discard_block_engine& y)
+ { return !(x == y); }
+
 private:
- base_type _rng;
- unsigned int _n;
+ base_type _rng;
+ std::size_t _n;
 };
 
 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
 // A definition is required even for integral static constants
-template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
-const bool discard_block<UniformRandomNumberGenerator, p, r>::has_fixed_range;
-template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
-const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::total_block;
-template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
-const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::returned_block;
+template<class URNG, std::size_t p, std::size_t r>
+const bool discard_block_engine<URNG, p, r>::has_fixed_range;
+template<class URNG, std::size_t p, std::size_t r>
+const std::size_t discard_block_engine<URNG, p, r>::total_block;
+template<class URNG, std::size_t p, std::size_t r>
+const std::size_t discard_block_engine<URNG, p, r>::returned_block;
+template<class URNG, std::size_t p, std::size_t r>
+const std::size_t discard_block_engine<URNG, p, r>::block_size;
+template<class URNG, std::size_t p, std::size_t r>
+const std::size_t discard_block_engine<URNG, p, r>::used_block;
 #endif
 
 } // namespace random

Modified: trunk/boost/random/ranlux.hpp
==============================================================================
--- trunk/boost/random/ranlux.hpp (original)
+++ trunk/boost/random/ranlux.hpp 2010-06-23 23:05:45 EDT (Wed, 23 Jun 2010)
@@ -52,28 +52,28 @@
 }
 
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux_base, 223, 24> ranlux3;
+typedef random::discard_block_engine<random::ranlux_base, 223, 24> ranlux3;
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux_base, 389, 24> ranlux4;
+typedef random::discard_block_engine<random::ranlux_base, 389, 24> ranlux4;
 
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux_base_01, 223, 24> ranlux3_01;
+typedef random::discard_block_engine<random::ranlux_base_01, 223, 24> ranlux3_01;
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux_base_01, 389, 24> ranlux4_01;
+typedef random::discard_block_engine<random::ranlux_base_01, 389, 24> ranlux4_01;
 
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux64_base_01, 223, 24> ranlux64_3_01;
+typedef random::discard_block_engine<random::ranlux64_base_01, 223, 24> ranlux64_3_01;
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux64_base_01, 389, 24> ranlux64_4_01;
+typedef random::discard_block_engine<random::ranlux64_base_01, 389, 24> ranlux64_4_01;
 
 #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
 namespace random {
   typedef random::subtract_with_carry_engine<int64_t, 48, 10, 24> ranlux64_base;
 }
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux64_base, 223, 24> ranlux64_3;
+typedef random::discard_block_engine<random::ranlux64_base, 223, 24> ranlux64_3;
 /** @copydoc boost::random::detail::ranlux_documentation */
-typedef random::discard_block<random::ranlux64_base, 389, 24> ranlux64_4;
+typedef random::discard_block_engine<random::ranlux64_base, 389, 24> ranlux64_4;
 #endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
 
 } // namespace boost

Modified: trunk/boost/random/subtract_with_carry.hpp
==============================================================================
--- trunk/boost/random/subtract_with_carry.hpp (original)
+++ trunk/boost/random/subtract_with_carry.hpp 2010-06-23 23:05:45 EDT (Wed, 23 Jun 2010)
@@ -155,8 +155,6 @@
             (k < short_lag)?
                 (k + long_lag - short_lag) :
                 (k - short_lag);
- if(short_index < 0)
- short_index += long_lag;
         IntType delta;
         if (x[short_index] >= x[k] + carry) {
             // x(n) >= 0

Modified: trunk/libs/random/doc/Jamfile.v2
==============================================================================
--- trunk/libs/random/doc/Jamfile.v2 (original)
+++ trunk/libs/random/doc/Jamfile.v2 2010-06-23 23:05:45 EDT (Wed, 23 Jun 2010)
@@ -77,7 +77,7 @@
         generators=\"@xmlonly <link linkend=\\\"boost_random.reference.generators\\\">generators</link> @endxmlonly\" \\
         distributions=\"@xmlonly <link linkend=\\\"boost_random.reference.distributions\\\">distributions</link> @endxmlonly\" \\
         additive_combine=\"@xmlonly <classname alt=\\\"boost::random::additive_combine\\\">additive_combine</classname> @endxmlonly\" \\
- discard_block=\"@xmlonly <classname alt=\\\"boost::random::discard_block\\\">discard_block</classname> @endxmlonly\" \\
+ discard_block_engine=\"@xmlonly <classname alt=\\\"boost::random::discard_block_engine\\\">discard_block_engine</classname> @endxmlonly\" \\
         lagged_fibonacci=\"@xmlonly<classname alt=\\\"boost::random::lagged_fibonacci\\\">lagged_fibonacci</classname>@endxmlonly\" \\
         linear_congruential_engine=\"@xmlonly<classname alt=\\\"boost::random::linear_congruential_engine\\\">linear_congruential_engine</classname>@endxmlonly\" \\
         minstd_rand=\"@xmlonly <classname alt=\\\"boost::random::minstd_rand\\\">minstd_rand</classname> @endxmlonly\" \\

Modified: trunk/libs/random/test/instantiate.cpp
==============================================================================
--- trunk/libs/random/test/instantiate.cpp (original)
+++ trunk/libs/random/test/instantiate.cpp 2010-06-23 23:05:45 EDT (Wed, 23 Jun 2010)
@@ -203,6 +203,8 @@
     test_seed(urng, static_cast<ResultType>(~0u));
 }
 
+#if 0
+
 // ranlux uses int32_t for seeding instead of result_type
 template<class ResultType>
 void instantiate_seed(const boost::ranlux3 & urng, const ResultType &) {
@@ -239,6 +241,7 @@
     instantiate_seed<boost::ranlux64_4_01, boost::uint32_t>(urng, ResultType());
 }
 
+#endif
 
 template<class URNG, class ResultType>
 void instantiate_urng(const std::string & s, const URNG & u, const ResultType & r)


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk