OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / random / detail / uniform_real_distribution.inl
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 #include <thrust/random/uniform_real_distribution.h>
18
19 namespace thrust
20 {
21
22 namespace random
23 {
24
25
26 template<typename RealType>
27   uniform_real_distribution<RealType>
28     ::uniform_real_distribution(RealType a, RealType b)
29       :m_param(a,b)
30 {
31 } // end uniform_real_distribution::uniform_real_distribution()
32
33 template<typename RealType>
34   uniform_real_distribution<RealType>
35     ::uniform_real_distribution(const param_type &parm)
36       :m_param(parm)
37 {
38 } // end uniform_real_distribution::uniform_real_distribution()
39
40 template<typename RealType>
41   void uniform_real_distribution<RealType>
42     ::reset(void)
43 {
44 } // end uniform_real_distribution::reset()
45
46 template<typename RealType>
47   template<typename UniformRandomNumberGenerator>
48     typename uniform_real_distribution<RealType>::result_type
49       uniform_real_distribution<RealType>
50         ::operator()(UniformRandomNumberGenerator &urng)
51 {
52   return operator()(urng, m_param);
53 } // end uniform_real::operator()()
54
55 template<typename RealType>
56   template<typename UniformRandomNumberGenerator>
57     typename uniform_real_distribution<RealType>::result_type
58       uniform_real_distribution<RealType>
59         ::operator()(UniformRandomNumberGenerator &urng,
60                      const param_type &parm)
61 {
62   // call the urng & map its result to [0,1)
63   result_type result = static_cast<result_type>(urng() - UniformRandomNumberGenerator::min);
64
65   // adding one to the denominator ensures that the interval is half-open at 1.0
66   // XXX adding 1.0 to a potentially large floating point number seems like a bad idea
67   // XXX OTOH adding 1 to what is potentially UINT_MAX also seems like a bad idea
68   // XXX we could statically check if 1u + (max - min) is representable and do that, otherwise use the current implementation
69   result /= (result_type(1) + static_cast<result_type>(UniformRandomNumberGenerator::max - UniformRandomNumberGenerator::min));
70
71   return (result * (parm.second - parm.first)) + parm.first;
72 } // end uniform_real::operator()()
73
74 template<typename RealType>
75   typename uniform_real_distribution<RealType>::result_type
76     uniform_real_distribution<RealType>
77       ::a(void) const
78 {
79   return m_param.first;
80 } // end uniform_real::a()
81
82 template<typename RealType>
83   typename uniform_real_distribution<RealType>::result_type
84     uniform_real_distribution<RealType>
85       ::b(void) const
86 {
87   return m_param.second;
88 } // end uniform_real_distribution::b()
89
90 template<typename RealType>
91   typename uniform_real_distribution<RealType>::param_type
92     uniform_real_distribution<RealType>
93       ::param(void) const
94 {
95   return m_param;;
96 } // end uniform_real_distribution::param()
97
98 template<typename RealType>
99   void uniform_real_distribution<RealType>
100     ::param(const param_type &parm)
101 {
102   m_param = parm;
103 } // end uniform_real_distribution::param()
104
105 template<typename RealType>
106   typename uniform_real_distribution<RealType>::result_type
107     uniform_real_distribution<RealType>
108       ::min THRUST_PREVENT_MACRO_SUBSTITUTION (void) const
109 {
110   return a();
111 } // end uniform_real_distribution::min()
112
113 template<typename RealType>
114   typename uniform_real_distribution<RealType>::result_type
115     uniform_real_distribution<RealType>
116       ::max THRUST_PREVENT_MACRO_SUBSTITUTION (void) const
117 {
118   return b();
119 } // end uniform_real_distribution::max()
120
121
122 template<typename RealType>
123   bool uniform_real_distribution<RealType>
124     ::equal(const uniform_real_distribution &rhs) const
125 {
126   return m_param == rhs.param();
127 }
128
129
130 template<typename RealType>
131   template<typename CharT, typename Traits>
132     std::basic_ostream<CharT,Traits>&
133       uniform_real_distribution<RealType>
134         ::stream_out(std::basic_ostream<CharT,Traits> &os) const
135 {
136   typedef std::basic_ostream<CharT,Traits> ostream_type;
137   typedef typename ostream_type::ios_base  ios_base;
138
139   // save old flags and fill character
140   const typename ios_base::fmtflags flags = os.flags();
141   const CharT fill = os.fill();
142
143   const CharT space = os.widen(' ');
144   os.flags(ios_base::dec | ios_base::fixed | ios_base::left);
145   os.fill(space);
146
147   os << a() << space << b();
148
149   // restore old flags and fill character
150   os.flags(flags);
151   os.fill(fill);
152   return os;
153 }
154
155
156 template<typename RealType>
157   template<typename CharT, typename Traits>
158     std::basic_istream<CharT,Traits>&
159       uniform_real_distribution<RealType>
160         ::stream_in(std::basic_istream<CharT,Traits> &is)
161 {
162   typedef std::basic_istream<CharT,Traits> istream_type;
163   typedef typename istream_type::ios_base  ios_base;
164
165   // save old flags
166   const typename ios_base::fmtflags flags = is.flags();
167
168   is.flags(ios_base::skipws);
169
170   is >> m_param.first >> m_param.second;
171
172   // restore old flags
173   is.flags(flags);
174   return is;
175 }
176
177
178 template<typename RealType>
179 bool operator==(const uniform_real_distribution<RealType> &lhs,
180                 const uniform_real_distribution<RealType> &rhs)
181 {
182   return thrust::random::detail::random_core_access::equal(lhs,rhs);
183 }
184
185
186 template<typename RealType>
187 bool operator!=(const uniform_real_distribution<RealType> &lhs,
188                 const uniform_real_distribution<RealType> &rhs)
189 {
190   return !(lhs == rhs);
191 }
192
193
194 template<typename RealType,
195          typename CharT, typename Traits>
196 std::basic_ostream<CharT,Traits>&
197 operator<<(std::basic_ostream<CharT,Traits> &os,
198            const uniform_real_distribution<RealType> &d)
199 {
200   return thrust::random::detail::random_core_access::stream_out(os,d);
201 }
202
203
204 template<typename RealType,
205          typename CharT, typename Traits>
206 std::basic_istream<CharT,Traits>&
207 operator>>(std::basic_istream<CharT,Traits> &is,
208            uniform_real_distribution<RealType> &d)
209 {
210   return thrust::random::detail::random_core_access::stream_in(is,d);
211 }
212
213
214 } // end random
215
216 } // end thrust
217