X-Git-Url: https://git.donarmstrong.com/?p=rsem.git;a=blobdiff_plain;f=boost%2Frandom%2Fdiscard_block.hpp;h=aba62126710726bd23d4b495fe32cb791bc19236;hp=27abd66e712ccd8b1e2272597b131f8422bb0df6;hb=2d71eb92104693ca9baa5a2e1c23eeca776d8fd3;hpb=a95154919f950f86de9104b2b9dcf1f0c7e83387 diff --git a/boost/random/discard_block.hpp b/boost/random/discard_block.hpp index 27abd66..aba6212 100644 --- a/boost/random/discard_block.hpp +++ b/boost/random/discard_block.hpp @@ -1,13 +1,14 @@ /* 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) * * See http://www.boost.org for most recent version including documentation. * - * $Id: discard_block.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $ + * $Id: discard_block.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $ * * Revision history * 2001-03-02 created @@ -18,113 +19,220 @@ #include #include +#include #include #include #include +#include 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 discard_block +template +class discard_block_engine { + typedef typename detail::seed_type< + typename UniformRandomNumberGenerator::result_type>::type seed_type; 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 explicit discard_block(T s) : _rng(s), _n(0) {} - template discard_block(It& first, It last) - : _rng(first, last), _n(0) { } - void seed() { _rng.seed(); _n = 0; } - template void seed(T s) { _rng.seed(s); _n = 0; } - template 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, + seed_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 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, seed_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 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 + // Don't use discard, since we still need to + // be somewhat compatible with TR1. + // _rng.discard(total_block - _n); + for(std::size_t i = 0; i < total_block - _n; ++i) { + _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 + void discard(boost::uintmax_t z) + { + for(boost::uintmax_t j = 0; j < z; ++j) { + (*this)(); + } + } -#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE + template + void generate(It first, It last) + { detail::generate(*this, first, last); } + + /** + * 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 - friend std::basic_ostream& - operator<<(std::basic_ostream& os, const discard_block& s) - { - os << s._rng << " " << s._n << " "; - return os; - } - - template - friend std::basic_istream& - operator>>(std::basic_istream& 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 + friend std::basic_ostream& + operator<<(std::basic_ostream& 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 + friend std::basic_istream& + operator>>(std::basic_istream& 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 -const bool discard_block::has_fixed_range; -template -const unsigned int discard_block::total_block; -template -const unsigned int discard_block::returned_block; +template +const bool discard_block_engine::has_fixed_range; +template +const std::size_t discard_block_engine::total_block; +template +const std::size_t discard_block_engine::returned_block; +template +const std::size_t discard_block_engine::block_size; +template +const std::size_t discard_block_engine::used_block; #endif +/// \cond \show_deprecated + +template +class discard_block : public discard_block_engine +{ + typedef discard_block_engine base_t; +public: + typedef typename base_t::result_type result_type; + discard_block() {} + template + discard_block(T& arg) : base_t(arg) {} + template + discard_block(const T& arg) : base_t(arg) {} + template + discard_block(It& first, It last) : base_t(first, last) {} + result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () + { return (this->base().min)(); } + result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () + { return (this->base().max)(); } +}; + +/// \endcond + +namespace detail { + + template + struct generator_bits; + + template + struct generator_bits > { + static std::size_t value() { return generator_bits::value(); } + }; + + template + struct generator_bits > { + static std::size_t value() { return generator_bits::value(); } + }; + +} + } // namespace random } // namespace boost