]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/random/uniform_01.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
07d6a3ea1c1d17d41eff176d585e0b5998ee81e7
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / random / uniform_01.hpp
1 /* boost random/uniform_01.hpp header file\r
2  *\r
3  * Copyright Jens Maurer 2000-2001\r
4  * Distributed under the Boost Software License, Version 1.0. (See\r
5  * accompanying file LICENSE_1_0.txt or copy at\r
6  * http://www.boost.org/LICENSE_1_0.txt)\r
7  *\r
8  * See http://www.boost.org for most recent version including documentation.\r
9  *\r
10  * $Id: uniform_01.hpp 58649 2010-01-02 21:23:17Z steven_watanabe $\r
11  *\r
12  * Revision history\r
13  *  2001-02-18  moved to individual header files\r
14  */\r
15 \r
16 #ifndef BOOST_RANDOM_UNIFORM_01_HPP\r
17 #define BOOST_RANDOM_UNIFORM_01_HPP\r
18 \r
19 #include <iostream>\r
20 #include <boost/config.hpp>\r
21 #include <boost/limits.hpp>\r
22 #include <boost/static_assert.hpp>\r
23 #include <boost/random/detail/config.hpp>\r
24 #include <boost/random/detail/pass_through_engine.hpp>\r
25 \r
26 #include <boost/random/detail/disable_warnings.hpp>\r
27 \r
28 namespace boost {\r
29 \r
30 namespace detail {\r
31 \r
32 template<class RealType>\r
33 class new_uniform_01\r
34 {\r
35 public:\r
36   typedef RealType input_type;\r
37   typedef RealType result_type;\r
38   // compiler-generated copy ctor and copy assignment are fine\r
39   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }\r
40   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }\r
41   void reset() { }\r
42 \r
43   template<class Engine>\r
44   result_type operator()(Engine& eng) {\r
45     for (;;) {\r
46       typedef typename Engine::result_type base_result;\r
47       result_type factor = result_type(1) /\r
48               (result_type((eng.max)()-(eng.min)()) +\r
49                result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));\r
50       result_type result = result_type(eng() - (eng.min)()) * factor;\r
51       if (result < result_type(1))\r
52         return result;\r
53     }\r
54   }\r
55 \r
56 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS\r
57   template<class CharT, class Traits>\r
58   friend std::basic_ostream<CharT,Traits>&\r
59   operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)\r
60   {\r
61     return os;\r
62   }\r
63 \r
64   template<class CharT, class Traits>\r
65   friend std::basic_istream<CharT,Traits>&\r
66   operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)\r
67   {\r
68     return is;\r
69   }\r
70 #endif\r
71 };\r
72 \r
73 template<class UniformRandomNumberGenerator, class RealType>\r
74 class backward_compatible_uniform_01\r
75 {\r
76   typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;\r
77   typedef boost::random::detail::pass_through_engine<UniformRandomNumberGenerator> internal_engine_type;\r
78 public:\r
79   typedef UniformRandomNumberGenerator base_type;\r
80   typedef RealType result_type;\r
81 \r
82   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);\r
83 \r
84 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)\r
85   BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);\r
86 #endif\r
87 \r
88   explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)\r
89     : _rng(rng),\r
90       _factor(result_type(1) /\r
91               (result_type((_rng.max)()-(_rng.min)()) +\r
92                result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))\r
93   {\r
94   }\r
95   // compiler-generated copy ctor and copy assignment are fine\r
96 \r
97   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }\r
98   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }\r
99   typename traits::value_type& base() { return _rng.base(); }\r
100   const typename traits::value_type& base() const { return _rng.base(); }\r
101   void reset() { }\r
102 \r
103   result_type operator()() {\r
104     for (;;) {\r
105       result_type result = result_type(_rng() - (_rng.min)()) * _factor;\r
106       if (result < result_type(1))\r
107         return result;\r
108     }\r
109   }\r
110 \r
111 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)\r
112   template<class CharT, class Traits>\r
113   friend std::basic_ostream<CharT,Traits>&\r
114   operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)\r
115   {\r
116     os << u._rng;\r
117     return os;\r
118   }\r
119 \r
120   template<class CharT, class Traits>\r
121   friend std::basic_istream<CharT,Traits>&\r
122   operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)\r
123   {\r
124     is >> u._rng;\r
125     return is;\r
126   }\r
127 #endif\r
128 \r
129 private:\r
130   typedef typename internal_engine_type::result_type base_result;\r
131   internal_engine_type _rng;\r
132   result_type _factor;\r
133 };\r
134 \r
135 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION\r
136 //  A definition is required even for integral static constants\r
137 template<class UniformRandomNumberGenerator, class RealType>\r
138 const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;\r
139 #endif\r
140 \r
141 template<class UniformRandomNumberGenerator>\r
142 struct select_uniform_01\r
143 {\r
144   template<class RealType>\r
145   struct apply\r
146   {\r
147     typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;\r
148   };\r
149 };\r
150 \r
151 template<>\r
152 struct select_uniform_01<float>\r
153 {\r
154   template<class RealType>\r
155   struct apply\r
156   {\r
157     typedef new_uniform_01<float> type;\r
158   };\r
159 };\r
160 \r
161 template<>\r
162 struct select_uniform_01<double>\r
163 {\r
164   template<class RealType>\r
165   struct apply\r
166   {\r
167     typedef new_uniform_01<double> type;\r
168   };\r
169 };\r
170 \r
171 template<>\r
172 struct select_uniform_01<long double>\r
173 {\r
174   template<class RealType>\r
175   struct apply\r
176   {\r
177     typedef new_uniform_01<long double> type;\r
178   };\r
179 };\r
180 \r
181 }\r
182 \r
183 // Because it is so commonly used: uniform distribution on the real [0..1)\r
184 // range.  This allows for specializations to avoid a costly int -> float\r
185 // conversion plus float multiplication\r
186 template<class UniformRandomNumberGenerator = double, class RealType = double>\r
187 class uniform_01\r
188   : public detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type\r
189 {\r
190   typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;\r
191   typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;\r
192 public:\r
193 \r
194   uniform_01() {}\r
195 \r
196   explicit uniform_01(typename traits::rvalue_type rng)\r
197     : impl_type(rng)\r
198   {\r
199   }\r
200 \r
201 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)\r
202   template<class CharT, class Traits>\r
203   friend std::basic_ostream<CharT,Traits>&\r
204   operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)\r
205   {\r
206     os << static_cast<const impl_type&>(u);\r
207     return os;\r
208   }\r
209 \r
210   template<class CharT, class Traits>\r
211   friend std::basic_istream<CharT,Traits>&\r
212   operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)\r
213   {\r
214     is >> static_cast<impl_type&>(u);\r
215     return is;\r
216   }\r
217 #endif\r
218 };\r
219 \r
220 } // namespace boost\r
221 \r
222 #include <boost/random/detail/enable_warnings.hpp>\r
223 \r
224 #endif // BOOST_RANDOM_UNIFORM_01_HPP\r