[857] | 1 | /* boost random/variate_generator.hpp header file
|
---|
| 2 | *
|
---|
| 3 | * Copyright Jens Maurer 2002
|
---|
| 4 | * Distributed under the Boost Software License, Version 1.0. (See
|
---|
| 5 | * accompanying file LICENSE_1_0.txt or copy at
|
---|
| 6 | * http://www.boost.org/LICENSE_1_0.txt)
|
---|
| 7 | *
|
---|
| 8 | * See http://www.boost.org for most recent version including documentation.
|
---|
| 9 | *
|
---|
| 10 | * $Id: variate_generator.hpp,v 1.8 2005/02/14 11:53:50 johnmaddock Exp $
|
---|
| 11 | *
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 | #ifndef BOOST_RANDOM_RANDOM_GENERATOR_HPP
|
---|
| 15 | #define BOOST_RANDOM_RANDOM_GENERATOR_HPP
|
---|
| 16 |
|
---|
| 17 | #include <boost/config.hpp>
|
---|
| 18 |
|
---|
| 19 | // implementation details
|
---|
| 20 | #include <boost/detail/workaround.hpp>
|
---|
| 21 | #include <boost/random/uniform_01.hpp>
|
---|
| 22 | #include <boost/random/detail/pass_through_engine.hpp>
|
---|
| 23 | #include <boost/random/detail/uniform_int_float.hpp>
|
---|
| 24 | #include <boost/random/detail/ptr_helper.hpp>
|
---|
| 25 |
|
---|
| 26 | // Borland C++ 5.6.0 has problems using its numeric_limits traits as
|
---|
| 27 | // template parameters
|
---|
| 28 | #if BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
|
---|
| 29 | #include <boost/type_traits/is_integral.hpp>
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | namespace boost {
|
---|
| 33 |
|
---|
| 34 | namespace random {
|
---|
| 35 | namespace detail {
|
---|
| 36 |
|
---|
| 37 | template<bool have_int, bool want_int>
|
---|
| 38 | struct engine_helper;
|
---|
| 39 |
|
---|
| 40 | // for consistency, always have two levels of decorations
|
---|
| 41 | template<>
|
---|
| 42 | struct engine_helper<true, true>
|
---|
| 43 | {
|
---|
| 44 | template<class Engine, class DistInputType>
|
---|
| 45 | struct impl
|
---|
| 46 | {
|
---|
| 47 | typedef pass_through_engine<Engine> type;
|
---|
| 48 | };
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | template<>
|
---|
| 52 | struct engine_helper<false, false>
|
---|
| 53 | {
|
---|
| 54 | template<class Engine, class DistInputType>
|
---|
| 55 | struct impl
|
---|
| 56 | {
|
---|
| 57 | typedef uniform_01<Engine, DistInputType> type;
|
---|
| 58 | };
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | template<>
|
---|
| 62 | struct engine_helper<true, false>
|
---|
| 63 | {
|
---|
| 64 | template<class Engine, class DistInputType>
|
---|
| 65 | struct impl
|
---|
| 66 | {
|
---|
| 67 | typedef uniform_01<Engine, DistInputType> type;
|
---|
| 68 | };
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | template<>
|
---|
| 72 | struct engine_helper<false, true>
|
---|
| 73 | {
|
---|
| 74 | template<class Engine, class DistInputType>
|
---|
| 75 | struct impl
|
---|
| 76 | {
|
---|
| 77 | typedef uniform_int_float<Engine, unsigned long> type;
|
---|
| 78 | };
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | } // namespace detail
|
---|
| 82 | } // namespace random
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | template<class Engine, class Distribution>
|
---|
| 86 | class variate_generator
|
---|
| 87 | {
|
---|
| 88 | private:
|
---|
| 89 | typedef random::detail::pass_through_engine<Engine> decorated_engine;
|
---|
| 90 |
|
---|
| 91 | public:
|
---|
| 92 | typedef typename decorated_engine::base_type engine_value_type;
|
---|
| 93 | typedef Engine engine_type;
|
---|
| 94 | typedef Distribution distribution_type;
|
---|
| 95 | typedef typename Distribution::result_type result_type;
|
---|
| 96 |
|
---|
| 97 | variate_generator(Engine e, Distribution d)
|
---|
| 98 | : _eng(decorated_engine(e)), _dist(d) { }
|
---|
| 99 |
|
---|
| 100 | result_type operator()() { return _dist(_eng); }
|
---|
| 101 | template<class T>
|
---|
| 102 | result_type operator()(T value) { return _dist(_eng, value); }
|
---|
| 103 |
|
---|
| 104 | engine_value_type& engine() { return _eng.base().base(); }
|
---|
| 105 | const engine_value_type& engine() const { return _eng.base().base(); }
|
---|
| 106 |
|
---|
| 107 | distribution_type& distribution() { return _dist; }
|
---|
| 108 | const distribution_type& distribution() const { return _dist; }
|
---|
| 109 |
|
---|
| 110 | result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().min)(); }
|
---|
| 111 | result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().max)(); }
|
---|
| 112 |
|
---|
| 113 | private:
|
---|
| 114 | #if BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
|
---|
| 115 | typedef typename random::detail::engine_helper<
|
---|
| 116 | boost::is_integral<typename decorated_engine::result_type>::value,
|
---|
| 117 | boost::is_integral<typename Distribution::input_type>::value
|
---|
| 118 | >::BOOST_NESTED_TEMPLATE impl<decorated_engine, typename Distribution::input_type>::type internal_engine_type;
|
---|
| 119 | #else
|
---|
| 120 | enum {
|
---|
| 121 | have_int = std::numeric_limits<typename decorated_engine::result_type>::is_integer,
|
---|
| 122 | want_int = std::numeric_limits<typename Distribution::input_type>::is_integer
|
---|
| 123 | };
|
---|
| 124 | typedef typename random::detail::engine_helper<have_int, want_int>::BOOST_NESTED_TEMPLATE impl<decorated_engine, typename Distribution::input_type>::type internal_engine_type;
|
---|
| 125 | #endif
|
---|
| 126 |
|
---|
| 127 | internal_engine_type _eng;
|
---|
| 128 | distribution_type _dist;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | } // namespace boost
|
---|
| 132 |
|
---|
| 133 | #endif // BOOST_RANDOM_RANDOM_GENERATOR_HPP
|
---|