P0935R0 Eradicating unnecessarily explicit default constructors
[official-gcc.git] / libstdc++-v3 / include / bits / uniform_int_dist.h
blob3b6d504466df9a7bf4b68fd434d24626e3f1aa4b
1 // Class template uniform_int_distribution -*- C++ -*-
3 // Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /**
26 * @file bits/uniform_int_dist.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
31 #ifndef _GLIBCXX_BITS_UNIFORM_INT_DIST_H
32 #define _GLIBCXX_BITS_UNIFORM_INT_DIST_H
34 #include <type_traits>
35 #include <limits>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 namespace __detail
43 /* Determine whether number is a power of 2. */
44 template<typename _Tp>
45 inline bool
46 _Power_of_2(_Tp __x)
48 return ((__x - 1) & __x) == 0;
52 /**
53 * @brief Uniform discrete distribution for random numbers.
54 * A discrete random distribution on the range @f$[min, max]@f$ with equal
55 * probability throughout the range.
57 template<typename _IntType = int>
58 class uniform_int_distribution
60 static_assert(std::is_integral<_IntType>::value,
61 "template argument must be an integral type");
63 public:
64 /** The type of the range of the distribution. */
65 typedef _IntType result_type;
66 /** Parameter type. */
67 struct param_type
69 typedef uniform_int_distribution<_IntType> distribution_type;
71 explicit
72 param_type(_IntType __a = 0,
73 _IntType __b = std::numeric_limits<_IntType>::max())
74 : _M_a(__a), _M_b(__b)
76 __glibcxx_assert(_M_a <= _M_b);
79 result_type
80 a() const
81 { return _M_a; }
83 result_type
84 b() const
85 { return _M_b; }
87 friend bool
88 operator==(const param_type& __p1, const param_type& __p2)
89 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
91 friend bool
92 operator!=(const param_type& __p1, const param_type& __p2)
93 { return !(__p1 == __p2); }
95 private:
96 _IntType _M_a;
97 _IntType _M_b;
100 public:
102 * @brief Constructs a uniform distribution object.
104 uniform_int_distribution() : uniform_int_distribution(0) { }
107 * @brief Constructs a uniform distribution object.
109 explicit
110 uniform_int_distribution(_IntType __a,
111 _IntType __b = std::numeric_limits<_IntType>::max())
112 : _M_param(__a, __b)
115 explicit
116 uniform_int_distribution(const param_type& __p)
117 : _M_param(__p)
121 * @brief Resets the distribution state.
123 * Does nothing for the uniform integer distribution.
125 void
126 reset() { }
128 result_type
129 a() const
130 { return _M_param.a(); }
132 result_type
133 b() const
134 { return _M_param.b(); }
137 * @brief Returns the parameter set of the distribution.
139 param_type
140 param() const
141 { return _M_param; }
144 * @brief Sets the parameter set of the distribution.
145 * @param __param The new parameter set of the distribution.
147 void
148 param(const param_type& __param)
149 { _M_param = __param; }
152 * @brief Returns the inclusive lower bound of the distribution range.
154 result_type
155 min() const
156 { return this->a(); }
159 * @brief Returns the inclusive upper bound of the distribution range.
161 result_type
162 max() const
163 { return this->b(); }
166 * @brief Generating functions.
168 template<typename _UniformRandomNumberGenerator>
169 result_type
170 operator()(_UniformRandomNumberGenerator& __urng)
171 { return this->operator()(__urng, _M_param); }
173 template<typename _UniformRandomNumberGenerator>
174 result_type
175 operator()(_UniformRandomNumberGenerator& __urng,
176 const param_type& __p);
178 template<typename _ForwardIterator,
179 typename _UniformRandomNumberGenerator>
180 void
181 __generate(_ForwardIterator __f, _ForwardIterator __t,
182 _UniformRandomNumberGenerator& __urng)
183 { this->__generate(__f, __t, __urng, _M_param); }
185 template<typename _ForwardIterator,
186 typename _UniformRandomNumberGenerator>
187 void
188 __generate(_ForwardIterator __f, _ForwardIterator __t,
189 _UniformRandomNumberGenerator& __urng,
190 const param_type& __p)
191 { this->__generate_impl(__f, __t, __urng, __p); }
193 template<typename _UniformRandomNumberGenerator>
194 void
195 __generate(result_type* __f, result_type* __t,
196 _UniformRandomNumberGenerator& __urng,
197 const param_type& __p)
198 { this->__generate_impl(__f, __t, __urng, __p); }
201 * @brief Return true if two uniform integer distributions have
202 * the same parameters.
204 friend bool
205 operator==(const uniform_int_distribution& __d1,
206 const uniform_int_distribution& __d2)
207 { return __d1._M_param == __d2._M_param; }
209 private:
210 template<typename _ForwardIterator,
211 typename _UniformRandomNumberGenerator>
212 void
213 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
214 _UniformRandomNumberGenerator& __urng,
215 const param_type& __p);
217 param_type _M_param;
220 template<typename _IntType>
221 template<typename _UniformRandomNumberGenerator>
222 typename uniform_int_distribution<_IntType>::result_type
223 uniform_int_distribution<_IntType>::
224 operator()(_UniformRandomNumberGenerator& __urng,
225 const param_type& __param)
227 typedef typename _UniformRandomNumberGenerator::result_type
228 _Gresult_type;
229 typedef typename std::make_unsigned<result_type>::type __utype;
230 typedef typename std::common_type<_Gresult_type, __utype>::type
231 __uctype;
233 const __uctype __urngmin = __urng.min();
234 const __uctype __urngmax = __urng.max();
235 const __uctype __urngrange = __urngmax - __urngmin;
236 const __uctype __urange
237 = __uctype(__param.b()) - __uctype(__param.a());
239 __uctype __ret;
241 if (__urngrange > __urange)
243 // downscaling
244 const __uctype __uerange = __urange + 1; // __urange can be zero
245 const __uctype __scaling = __urngrange / __uerange;
246 const __uctype __past = __uerange * __scaling;
248 __ret = __uctype(__urng()) - __urngmin;
249 while (__ret >= __past);
250 __ret /= __scaling;
252 else if (__urngrange < __urange)
254 // upscaling
256 Note that every value in [0, urange]
257 can be written uniquely as
259 (urngrange + 1) * high + low
261 where
263 high in [0, urange / (urngrange + 1)]
267 low in [0, urngrange].
269 __uctype __tmp; // wraparound control
272 const __uctype __uerngrange = __urngrange + 1;
273 __tmp = (__uerngrange * operator()
274 (__urng, param_type(0, __urange / __uerngrange)));
275 __ret = __tmp + (__uctype(__urng()) - __urngmin);
277 while (__ret > __urange || __ret < __tmp);
279 else
280 __ret = __uctype(__urng()) - __urngmin;
282 return __ret + __param.a();
286 template<typename _IntType>
287 template<typename _ForwardIterator,
288 typename _UniformRandomNumberGenerator>
289 void
290 uniform_int_distribution<_IntType>::
291 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
292 _UniformRandomNumberGenerator& __urng,
293 const param_type& __param)
295 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
296 typedef typename _UniformRandomNumberGenerator::result_type
297 _Gresult_type;
298 typedef typename std::make_unsigned<result_type>::type __utype;
299 typedef typename std::common_type<_Gresult_type, __utype>::type
300 __uctype;
302 const __uctype __urngmin = __urng.min();
303 const __uctype __urngmax = __urng.max();
304 const __uctype __urngrange = __urngmax - __urngmin;
305 const __uctype __urange
306 = __uctype(__param.b()) - __uctype(__param.a());
308 __uctype __ret;
310 if (__urngrange > __urange)
312 if (__detail::_Power_of_2(__urngrange + 1)
313 && __detail::_Power_of_2(__urange + 1))
315 while (__f != __t)
317 __ret = __uctype(__urng()) - __urngmin;
318 *__f++ = (__ret & __urange) + __param.a();
321 else
323 // downscaling
324 const __uctype __uerange = __urange + 1; // __urange can be zero
325 const __uctype __scaling = __urngrange / __uerange;
326 const __uctype __past = __uerange * __scaling;
327 while (__f != __t)
330 __ret = __uctype(__urng()) - __urngmin;
331 while (__ret >= __past);
332 *__f++ = __ret / __scaling + __param.a();
336 else if (__urngrange < __urange)
338 // upscaling
340 Note that every value in [0, urange]
341 can be written uniquely as
343 (urngrange + 1) * high + low
345 where
347 high in [0, urange / (urngrange + 1)]
351 low in [0, urngrange].
353 __uctype __tmp; // wraparound control
354 while (__f != __t)
358 const __uctype __uerngrange = __urngrange + 1;
359 __tmp = (__uerngrange * operator()
360 (__urng, param_type(0, __urange / __uerngrange)));
361 __ret = __tmp + (__uctype(__urng()) - __urngmin);
363 while (__ret > __urange || __ret < __tmp);
364 *__f++ = __ret;
367 else
368 while (__f != __t)
369 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
372 // operator!= and operator<< and operator>> are defined in <bits/random.h>
374 _GLIBCXX_END_NAMESPACE_VERSION
375 } // namespace std
377 #endif