source: NonGTP/Boost/boost/random/variate_generator.hpp @ 857

Revision 857, 3.8 KB checked in by igarcia, 19 years ago (diff)
Line 
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
32namespace boost {
33
34namespace random {
35namespace detail {
36
37template<bool have_int, bool want_int>
38struct engine_helper;
39
40// for consistency, always have two levels of decorations
41template<>
42struct 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
51template<>
52struct 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
61template<>
62struct 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
71template<>
72struct 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
85template<class Engine, class Distribution>
86class variate_generator
87{
88private:
89  typedef random::detail::pass_through_engine<Engine> decorated_engine;
90
91public:
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
113private:
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
Note: See TracBrowser for help on using the repository browser.