File indexing completed on 2025-01-30 09:31:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_
0029 #define ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_
0030
0031 #include <cassert>
0032 #include <istream>
0033 #include <limits>
0034 #include <type_traits>
0035
0036 #include "absl/base/optimization.h"
0037 #include "absl/random/internal/fast_uniform_bits.h"
0038 #include "absl/random/internal/iostream_state_saver.h"
0039 #include "absl/random/internal/traits.h"
0040 #include "absl/random/internal/wide_multiply.h"
0041
0042 namespace absl {
0043 ABSL_NAMESPACE_BEGIN
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 template <typename IntType = int>
0058 class uniform_int_distribution {
0059 private:
0060 using unsigned_type =
0061 typename random_internal::make_unsigned_bits<IntType>::type;
0062
0063 public:
0064 using result_type = IntType;
0065
0066 class param_type {
0067 public:
0068 using distribution_type = uniform_int_distribution;
0069
0070 explicit param_type(
0071 result_type lo = 0,
0072 result_type hi = (std::numeric_limits<result_type>::max)())
0073 : lo_(lo),
0074 range_(static_cast<unsigned_type>(hi) -
0075 static_cast<unsigned_type>(lo)) {
0076
0077 assert(lo <= hi);
0078 }
0079
0080 result_type a() const { return lo_; }
0081 result_type b() const {
0082 return static_cast<result_type>(static_cast<unsigned_type>(lo_) + range_);
0083 }
0084
0085 friend bool operator==(const param_type& a, const param_type& b) {
0086 return a.lo_ == b.lo_ && a.range_ == b.range_;
0087 }
0088
0089 friend bool operator!=(const param_type& a, const param_type& b) {
0090 return !(a == b);
0091 }
0092
0093 private:
0094 friend class uniform_int_distribution;
0095 unsigned_type range() const { return range_; }
0096
0097 result_type lo_;
0098 unsigned_type range_;
0099
0100 static_assert(random_internal::IsIntegral<result_type>::value,
0101 "Class-template absl::uniform_int_distribution<> must be "
0102 "parameterized using an integral type.");
0103 };
0104
0105 uniform_int_distribution() : uniform_int_distribution(0) {}
0106
0107 explicit uniform_int_distribution(
0108 result_type lo,
0109 result_type hi = (std::numeric_limits<result_type>::max)())
0110 : param_(lo, hi) {}
0111
0112 explicit uniform_int_distribution(const param_type& param) : param_(param) {}
0113
0114
0115
0116
0117
0118 void reset() {}
0119
0120 template <typename URBG>
0121 result_type operator()(URBG& gen) {
0122 return (*this)(gen, param());
0123 }
0124
0125 template <typename URBG>
0126 result_type operator()(
0127 URBG& gen, const param_type& param) {
0128 return static_cast<result_type>(param.a() + Generate(gen, param.range()));
0129 }
0130
0131 result_type a() const { return param_.a(); }
0132 result_type b() const { return param_.b(); }
0133
0134 param_type param() const { return param_; }
0135 void param(const param_type& params) { param_ = params; }
0136
0137 result_type(min)() const { return a(); }
0138 result_type(max)() const { return b(); }
0139
0140 friend bool operator==(const uniform_int_distribution& a,
0141 const uniform_int_distribution& b) {
0142 return a.param_ == b.param_;
0143 }
0144 friend bool operator!=(const uniform_int_distribution& a,
0145 const uniform_int_distribution& b) {
0146 return !(a == b);
0147 }
0148
0149 private:
0150
0151 template <typename URBG>
0152 unsigned_type Generate(URBG& g,
0153 unsigned_type R);
0154 param_type param_;
0155 };
0156
0157
0158
0159
0160 template <typename CharT, typename Traits, typename IntType>
0161 std::basic_ostream<CharT, Traits>& operator<<(
0162 std::basic_ostream<CharT, Traits>& os,
0163 const uniform_int_distribution<IntType>& x) {
0164 using stream_type =
0165 typename random_internal::stream_format_type<IntType>::type;
0166 auto saver = random_internal::make_ostream_state_saver(os);
0167 os << static_cast<stream_type>(x.a()) << os.fill()
0168 << static_cast<stream_type>(x.b());
0169 return os;
0170 }
0171
0172 template <typename CharT, typename Traits, typename IntType>
0173 std::basic_istream<CharT, Traits>& operator>>(
0174 std::basic_istream<CharT, Traits>& is,
0175 uniform_int_distribution<IntType>& x) {
0176 using param_type = typename uniform_int_distribution<IntType>::param_type;
0177 using result_type = typename uniform_int_distribution<IntType>::result_type;
0178 using stream_type =
0179 typename random_internal::stream_format_type<IntType>::type;
0180
0181 stream_type a;
0182 stream_type b;
0183
0184 auto saver = random_internal::make_istream_state_saver(is);
0185 is >> a >> b;
0186 if (!is.fail()) {
0187 x.param(
0188 param_type(static_cast<result_type>(a), static_cast<result_type>(b)));
0189 }
0190 return is;
0191 }
0192
0193 template <typename IntType>
0194 template <typename URBG>
0195 typename random_internal::make_unsigned_bits<IntType>::type
0196 uniform_int_distribution<IntType>::Generate(
0197 URBG& g,
0198 typename random_internal::make_unsigned_bits<IntType>::type R) {
0199 random_internal::FastUniformBits<unsigned_type> fast_bits;
0200 unsigned_type bits = fast_bits(g);
0201 const unsigned_type Lim = R + 1;
0202 if ((R & Lim) == 0) {
0203
0204 return bits & R;
0205 }
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247 using helper = random_internal::wide_multiply<unsigned_type>;
0248 auto product = helper::multiply(bits, Lim);
0249
0250
0251
0252
0253
0254
0255 if (ABSL_PREDICT_FALSE(helper::lo(product) < Lim)) {
0256
0257
0258
0259
0260
0261 const unsigned_type threshold =
0262 ((std::numeric_limits<unsigned_type>::max)() - Lim + 1) % Lim;
0263 while (helper::lo(product) < threshold) {
0264 bits = fast_bits(g);
0265 product = helper::multiply(bits, Lim);
0266 }
0267 }
0268
0269 return helper::hi(product);
0270 }
0271
0272 ABSL_NAMESPACE_END
0273 }
0274
0275 #endif