Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69975 - in trunk/boost/random: . detail
From: steven_at_[hidden]
Date: 2011-03-14 15:42:37


Author: steven_watanabe
Date: 2011-03-14 15:42:32 EDT (Mon, 14 Mar 2011)
New Revision: 69975
URL: http://svn.boost.org/trac/boost/changeset/69975

Log:
Remove pass_through_engine. Make uniform_int_float more correct.
Removed:
   trunk/boost/random/detail/pass_through_engine.hpp
Text files modified:
   trunk/boost/random/detail/uniform_int_float.hpp | 44 ++++++++++++++++++++++++---------------
   trunk/boost/random/uniform_01.hpp | 15 ++++++-------
   trunk/boost/random/uniform_int_distribution.hpp | 4 --
   trunk/boost/random/variate_generator.hpp | 17 +++++++--------
   4 files changed, 43 insertions(+), 37 deletions(-)

Deleted: trunk/boost/random/detail/pass_through_engine.hpp
==============================================================================
--- trunk/boost/random/detail/pass_through_engine.hpp 2011-03-14 15:42:32 EDT (Mon, 14 Mar 2011)
+++ (empty file)
@@ -1,100 +0,0 @@
-/* boost random/detail/uniform_int_float.hpp header file
- *
- * Copyright Jens Maurer 2000-2001
- * 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$
- *
- */
-
-#ifndef BOOST_RANDOM_DETAIL_PASS_THROUGH_ENGINE_HPP
-#define BOOST_RANDOM_DETAIL_PASS_THROUGH_ENGINE_HPP
-
-#include <boost/config.hpp>
-#include <boost/random/detail/ptr_helper.hpp>
-#include <boost/random/detail/disable_warnings.hpp>
-
-namespace boost {
-namespace random {
-namespace detail {
-
-template<class UniformRandomNumberGenerator>
-class pass_through_engine
-{
-private:
- typedef ptr_helper<UniformRandomNumberGenerator> helper_type;
-
-public:
- typedef typename helper_type::value_type base_type;
- typedef typename base_type::result_type result_type;
-
- explicit pass_through_engine(UniformRandomNumberGenerator rng)
- // make argument an rvalue to avoid matching Generator& constructor
- : _rng(static_cast<typename helper_type::rvalue_type>(rng))
- { }
-
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (base().min)(); }
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (base().max)(); }
- base_type& base() { return helper_type::ref(_rng); }
- const base_type& base() const { return helper_type::ref(_rng); }
-
- result_type operator()() { return base()(); }
-
-private:
- UniformRandomNumberGenerator _rng;
-};
-
-#ifndef BOOST_NO_STD_LOCALE
-
-template<class UniformRandomNumberGenerator, class CharT, class Traits>
-std::basic_ostream<CharT,Traits>&
-operator<<(
- std::basic_ostream<CharT,Traits>& os
- , const pass_through_engine<UniformRandomNumberGenerator>& ud
- )
-{
- return os << ud.base();
-}
-
-template<class UniformRandomNumberGenerator, class CharT, class Traits>
-std::basic_istream<CharT,Traits>&
-operator>>(
- std::basic_istream<CharT,Traits>& is
- , const pass_through_engine<UniformRandomNumberGenerator>& ud
- )
-{
- return is >> ud.base();
-}
-
-#else // no new streams
-
-template<class UniformRandomNumberGenerator>
-inline std::ostream&
-operator<<(std::ostream& os,
- const pass_through_engine<UniformRandomNumberGenerator>& ud)
-{
- return os << ud.base();
-}
-
-template<class UniformRandomNumberGenerator>
-inline std::istream&
-operator>>(std::istream& is,
- const pass_through_engine<UniformRandomNumberGenerator>& ud)
-{
- return is >> ud.base();
-}
-
-#endif
-
-} // namespace detail
-} // namespace random
-} // namespace boost
-
-#include <boost/random/detail/enable_warnings.hpp>
-
-#endif // BOOST_RANDOM_DETAIL_PASS_THROUGH_ENGINE_HPP
-

Modified: trunk/boost/random/detail/uniform_int_float.hpp
==============================================================================
--- trunk/boost/random/detail/uniform_int_float.hpp (original)
+++ trunk/boost/random/detail/uniform_int_float.hpp 2011-03-14 15:42:32 EDT (Mon, 14 Mar 2011)
@@ -1,6 +1,7 @@
 /* boost random/detail/uniform_int_float.hpp header file
  *
  * Copyright Jens Maurer 2000-2001
+ * Copyright Steven Watanabe 2011
  * 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)
@@ -14,44 +15,53 @@
 #ifndef BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
 #define BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
 
+#include <boost/limits.hpp>
 #include <boost/config.hpp>
+#include <boost/integer.hpp>
 #include <boost/random/detail/config.hpp>
-#include <boost/random/uniform_01.hpp>
-
 
 namespace boost {
 namespace random {
 namespace detail {
 
-template<class UniformRandomNumberGenerator, class IntType = unsigned long>
+template<class URNG>
 class uniform_int_float
 {
 public:
- typedef UniformRandomNumberGenerator base_type;
- typedef IntType result_type;
-
- uniform_int_float(base_type rng, IntType min_arg = 0, IntType max_arg = 0xffffffff)
- : _rng(rng), _min(min_arg), _max(max_arg) {}
+ typedef URNG base_type;
+ typedef typename base_type::result_type base_result;
 
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
+ typedef typename boost::uint_t<
+ (std::numeric_limits<boost::uintmax_t>::digits <
+ std::numeric_limits<base_result>::digits)?
+ std::numeric_limits<boost::uintmax_t>::digits :
+ std::numeric_limits<base_result>::digits
+ >::fast result_type;
+
+ uniform_int_float(base_type& rng)
+ : _rng(rng) {}
+
+ static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return 0; }
+ static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ {
+ std::size_t digits = std::numeric_limits<result_type>::digits;
+ if(URNG::precision() < digits) digits = URNG::precision();
+ return (result_type(2) << (digits - 1)) - 1;
+ }
     base_type& base() { return _rng; }
     const base_type& base() const { return _rng; }
 
     result_type operator()()
     {
- typedef typename base_type::result_type base_result;
- base_result range = static_cast<base_result>(_max-_min)+1;
- uniform_01<typename base_type::result_type> dist;
- return static_cast<IntType>(dist(_rng) * range) + _min;
+ base_result range = static_cast<base_result>(max())+1;
+ return static_cast<result_type>(_rng() * range);
     }
 
 private:
- base_type _rng;
- result_type _min, _max;
+ base_type& _rng;
 };
 
-
 } // namespace detail
 } // namespace random
 } // namespace boost

Modified: trunk/boost/random/uniform_01.hpp
==============================================================================
--- trunk/boost/random/uniform_01.hpp (original)
+++ trunk/boost/random/uniform_01.hpp 2011-03-14 15:42:32 EDT (Mon, 14 Mar 2011)
@@ -21,7 +21,7 @@
 #include <boost/limits.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/random/detail/config.hpp>
-#include <boost/random/detail/pass_through_engine.hpp>
+#include <boost/random/detail/ptr_helper.hpp>
 
 #include <boost/random/detail/disable_warnings.hpp>
 
@@ -122,7 +122,6 @@
 class backward_compatible_uniform_01
 {
   typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
- typedef boost::random::detail::pass_through_engine<UniformRandomNumberGenerator> internal_engine_type;
 public:
   typedef UniformRandomNumberGenerator base_type;
   typedef RealType result_type;
@@ -136,7 +135,7 @@
   explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
     : _rng(rng),
       _factor(result_type(1) /
- (result_type((_rng.max)()-(_rng.min)()) +
+ (result_type((base().max)()-(base().min)()) +
                result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
   {
   }
@@ -144,13 +143,13 @@
 
   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
- typename traits::value_type& base() { return _rng.base(); }
- const typename traits::value_type& base() const { return _rng.base(); }
+ typename traits::value_type& base() { return traits::ref(_rng); }
+ const typename traits::value_type& base() const { return traits::ref(_rng); }
   void reset() { }
 
   result_type operator()() {
     for (;;) {
- result_type result = result_type(_rng() - (_rng.min)()) * _factor;
+ result_type result = result_type(base()() - (base().min)()) * _factor;
       if (result < result_type(1))
         return result;
     }
@@ -175,8 +174,8 @@
 #endif
 
 private:
- typedef typename internal_engine_type::result_type base_result;
- internal_engine_type _rng;
+ typedef typename traits::value_type::result_type base_result;
+ UniformRandomNumberGenerator _rng;
   result_type _factor;
 };
 

Modified: trunk/boost/random/uniform_int_distribution.hpp
==============================================================================
--- trunk/boost/random/uniform_int_distribution.hpp (original)
+++ trunk/boost/random/uniform_int_distribution.hpp 2011-03-14 15:42:32 EDT (Mon, 14 Mar 2011)
@@ -27,7 +27,6 @@
 #include <boost/random/detail/config.hpp>
 #include <boost/random/detail/operators.hpp>
 #include <boost/random/detail/uniform_int_float.hpp>
-#include <boost/random/detail/pass_through_engine.hpp>
 #include <boost/random/detail/signed_unsigned_tools.hpp>
 #include <boost/type_traits/make_unsigned.hpp>
 #include <boost/type_traits/is_integral.hpp>
@@ -218,8 +217,7 @@
     Engine& eng, T min_value, T max_value,
     boost::mpl::false_ /** is_integral<Engine::result_type> */)
 {
- pass_through_engine<Engine&> ref_wrapper(eng);
- uniform_int_float<pass_through_engine<Engine&> > wrapper(ref_wrapper);
+ uniform_int_float<Engine> wrapper(eng);
     return generate_uniform_int(wrapper, min_value, max_value, boost::mpl::true_());
 }
 

Modified: trunk/boost/random/variate_generator.hpp
==============================================================================
--- trunk/boost/random/variate_generator.hpp (original)
+++ trunk/boost/random/variate_generator.hpp 2011-03-14 15:42:32 EDT (Mon, 14 Mar 2011)
@@ -15,7 +15,7 @@
 #ifndef BOOST_RANDOM_RANDOM_GENERATOR_HPP
 #define BOOST_RANDOM_RANDOM_GENERATOR_HPP
 
-#include <boost/random/detail/pass_through_engine.hpp>
+#include <boost/random/detail/ptr_helper.hpp>
 
 #include <boost/random/detail/disable_warnings.hpp>
 
@@ -51,10 +51,9 @@
 class variate_generator
 {
 private:
- typedef random::detail::pass_through_engine<Engine> decorated_engine;
-
+ typedef boost::random::detail::ptr_helper<Engine> helper_type;
 public:
- typedef typename decorated_engine::base_type engine_value_type;
+ typedef typename helper_type::value_type engine_value_type;
     typedef Engine engine_type;
     typedef Distribution distribution_type;
     typedef typename Distribution::result_type result_type;
@@ -71,21 +70,21 @@
       : _eng(e), _dist(d) { }
 
     /** Returns: distribution()(engine()) */
- result_type operator()() { return _dist(_eng.base()); }
+ result_type operator()() { return _dist(engine()); }
     /**
      * Returns: distribution()(engine(), value).
      */
     template<class T>
- result_type operator()(const T& value) { return _dist(_eng, value); }
+ result_type operator()(const T& value) { return _dist(engine(), value); }
 
     /**
      * Returns: A reference to the associated uniform random number generator.
      */
- engine_value_type& engine() { return _eng.base(); }
+ engine_value_type& engine() { return helper_type::ref(_eng); }
     /**
      * Returns: A reference to the associated uniform random number generator.
      */
- const engine_value_type& engine() const { return _eng.base(); }
+ const engine_value_type& engine() const { return helper_type::ref(_eng); }
 
     /** Returns: A reference to the associated \random_distribution. */
     distribution_type& distribution() { return _dist; }
@@ -108,7 +107,7 @@
     result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().max)(); }
 
 private:
- decorated_engine _eng;
+ Engine _eng;
     distribution_type _dist;
 };
 


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