Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r51337 - in trunk: boost/random libs/random
From: steven_at_[hidden]
Date: 2009-02-19 18:27:41


Author: steven_watanabe
Date: 2009-02-19 18:27:40 EST (Thu, 19 Feb 2009)
New Revision: 51337
URL: http://svn.boost.org/trac/boost/changeset/51337

Log:
Convert uniform_01 to the current distribution interface, dispatching on the first template paramter to retain backwards compatibility. Fixes #599. Fixes #1544. Fixes #2679
Text files modified:
   trunk/boost/random/uniform_01.hpp | 150 ++++++++++++++++++++++++++++++++++++---
   trunk/libs/random/instantiate.cpp | 2
   2 files changed, 138 insertions(+), 14 deletions(-)

Modified: trunk/boost/random/uniform_01.hpp
==============================================================================
--- trunk/boost/random/uniform_01.hpp (original)
+++ trunk/boost/random/uniform_01.hpp 2009-02-19 18:27:40 EST (Thu, 19 Feb 2009)
@@ -20,16 +20,59 @@
 #include <boost/config.hpp>
 #include <boost/limits.hpp>
 #include <boost/static_assert.hpp>
+#include <boost/random/detail/pass_through_engine.hpp>
 
 namespace boost {
 
-// Because it is so commonly used: uniform distribution on the real [0..1)
-// range. This allows for specializations to avoid a costly int -> float
-// conversion plus float multiplication
-template<class UniformRandomNumberGenerator, class RealType = double>
-class uniform_01
+namespace detail {
+
+template<class RealType>
+class new_uniform_01
 {
 public:
+ typedef RealType input_type;
+ typedef RealType result_type;
+ // compiler-generated copy ctor and copy assignment are fine
+ result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
+ result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
+ void reset() { }
+
+ template<class Engine>
+ result_type operator()(Engine& eng) {
+ for (;;) {
+ typedef typename Engine::result_type base_result;
+ result_type factor = result_type(1) /
+ (result_type((eng.max)()-(eng.min)()) +
+ result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
+ result_type result = result_type(eng() - (eng.min)()) * factor;
+ if (result < result_type(1))
+ return result;
+ }
+ }
+
+#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
+ {
+ return os;
+ }
+
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
+ {
+ return is;
+ }
+#endif
+};
+
+template<class UniformRandomNumberGenerator, class RealType>
+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;
 
@@ -39,7 +82,7 @@
   BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
 #endif
 
- explicit uniform_01(base_type rng)
+ explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
     : _rng(rng),
       _factor(result_type(1) /
               (result_type((_rng.max)()-(_rng.min)()) +
@@ -50,22 +93,22 @@
 
   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
- base_type& base() { return _rng; }
- const base_type& base() const { return _rng; }
+ typename traits::value_type& base() { return _rng.base(); }
+ const typename traits::value_type& base() const { return _rng.base(); }
   void reset() { }
 
   result_type operator()() {
     for (;;) {
       result_type result = result_type(_rng() - (_rng.min)()) * _factor;
       if (result < result_type(1))
- return result;
+ return result;
     }
   }
 
 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
   template<class CharT, class Traits>
   friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
+ operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)
   {
     os << u._rng;
     return os;
@@ -73,7 +116,7 @@
 
   template<class CharT, class Traits>
   friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
+ operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)
   {
     is >> u._rng;
     return is;
@@ -81,17 +124,96 @@
 #endif
 
 private:
- typedef typename base_type::result_type base_result;
- base_type _rng;
+ typedef typename internal_engine_type::result_type base_result;
+ internal_engine_type _rng;
   result_type _factor;
 };
 
 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
 // A definition is required even for integral static constants
 template<class UniformRandomNumberGenerator, class RealType>
-const bool uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
+const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
 #endif
 
+template<class UniformRandomNumberGenerator>
+struct select_uniform_01
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;
+ };
+};
+
+template<>
+struct select_uniform_01<float>
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef new_uniform_01<float> type;
+ };
+};
+
+template<>
+struct select_uniform_01<double>
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef new_uniform_01<double> type;
+ };
+};
+
+template<>
+struct select_uniform_01<long double>
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef new_uniform_01<long double> type;
+ };
+};
+
+}
+
+// Because it is so commonly used: uniform distribution on the real [0..1)
+// range. This allows for specializations to avoid a costly int -> float
+// conversion plus float multiplication
+template<class UniformRandomNumberGenerator = double, class RealType = double>
+class uniform_01
+ : public detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type
+{
+ typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
+ typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
+public:
+
+ uniform_01() {}
+
+ explicit uniform_01(typename traits::rvalue_type rng)
+ : impl_type(rng)
+ {
+ }
+
+#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
+ {
+ os << static_cast<const impl_type&>(u);
+ return os;
+ }
+
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
+ {
+ is >> static_cast<impl_type&>(u);
+ return is;
+ }
+#endif
+};
+
 } // namespace boost
 
 #endif // BOOST_RANDOM_UNIFORM_01_HPP

Modified: trunk/libs/random/instantiate.cpp
==============================================================================
--- trunk/libs/random/instantiate.cpp (original)
+++ trunk/libs/random/instantiate.cpp 2009-02-19 18:27:40 EST (Thu, 19 Feb 2009)
@@ -111,6 +111,8 @@
 template<class URNG, class RealType>
 void instantiate_real_dist(URNG& urng, RealType /* ignored */)
 {
+ instantiate_dist(urng, "uniform_01",
+ boost::uniform_01<RealType>());
   instantiate_dist(urng, "uniform_real",
                    boost::uniform_real<RealType>(0, 2.1));
   instantiate_dist(urng, "triangle_distribution",


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