Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:31:49

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: uniform_int_distribution.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header defines a class for representing a uniform integer distribution
0020 // over the closed (inclusive) interval [a,b]. You use this distribution in
0021 // combination with an Abseil random bit generator to produce random values
0022 // according to the rules of the distribution.
0023 //
0024 // `absl::uniform_int_distribution` is a drop-in replacement for the C++11
0025 // `std::uniform_int_distribution` [rand.dist.uni.int] but is considerably
0026 // faster than the libstdc++ implementation.
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 // absl::uniform_int_distribution<T>
0046 //
0047 // This distribution produces random integer values uniformly distributed in the
0048 // closed (inclusive) interval [a, b].
0049 //
0050 // Example:
0051 //
0052 //   absl::BitGen gen;
0053 //
0054 //   // Use the distribution to produce a value between 1 and 6, inclusive.
0055 //   int die_roll = absl::uniform_int_distribution<int>(1, 6)(gen);
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       // [rand.dist.uni.int] precondition 2
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   };  // param_type
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   // uniform_int_distribution<T>::reset()
0115   //
0116   // Resets the uniform int distribution. Note that this function has no effect
0117   // because the distribution already produces independent values.
0118   void reset() {}
0119 
0120   template <typename URBG>
0121   result_type operator()(URBG& gen) {  // NOLINT(runtime/references)
0122     return (*this)(gen, param());
0123   }
0124 
0125   template <typename URBG>
0126   result_type operator()(
0127       URBG& gen, const param_type& param) {  // NOLINT(runtime/references)
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   // Generates a value in the *closed* interval [0, R]
0151   template <typename URBG>
0152   unsigned_type Generate(URBG& g,  // NOLINT(runtime/references)
0153                          unsigned_type R);
0154   param_type param_;
0155 };
0156 
0157 // -----------------------------------------------------------------------------
0158 // Implementation details follow
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,  // NOLINT(runtime/references)
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     // If the interval's length is a power of two range, just take the low bits.
0204     return bits & R;
0205   }
0206 
0207   // Generates a uniform variate on [0, Lim) using fixed-point multiplication.
0208   // The above fast-path guarantees that Lim is representable in unsigned_type.
0209   //
0210   // Algorithm adapted from
0211   // http://lemire.me/blog/2016/06/30/fast-random-shuffling/, with added
0212   // explanation.
0213   //
0214   // The algorithm creates a uniform variate `bits` in the interval [0, 2^N),
0215   // and treats it as the fractional part of a fixed-point real value in [0, 1),
0216   // multiplied by 2^N.  For example, 0.25 would be represented as 2^(N - 2),
0217   // because 2^N * 0.25 == 2^(N - 2).
0218   //
0219   // Next, `bits` and `Lim` are multiplied with a wide-multiply to bring the
0220   // value into the range [0, Lim).  The integral part (the high word of the
0221   // multiplication result) is then very nearly the desired result.  However,
0222   // this is not quite accurate; viewing the multiplication result as one
0223   // double-width integer, the resulting values for the sample are mapped as
0224   // follows:
0225   //
0226   // If the result lies in this interval:       Return this value:
0227   //        [0, 2^N)                                    0
0228   //        [2^N, 2 * 2^N)                              1
0229   //        ...                                         ...
0230   //        [K * 2^N, (K + 1) * 2^N)                    K
0231   //        ...                                         ...
0232   //        [(Lim - 1) * 2^N, Lim * 2^N)                Lim - 1
0233   //
0234   // While all of these intervals have the same size, the result of `bits * Lim`
0235   // must be a multiple of `Lim`, and not all of these intervals contain the
0236   // same number of multiples of `Lim`.  In particular, some contain
0237   // `F = floor(2^N / Lim)` and some contain `F + 1 = ceil(2^N / Lim)`.  This
0238   // difference produces a small nonuniformity, which is corrected by applying
0239   // rejection sampling to one of the values in the "larger intervals" (i.e.,
0240   // the intervals containing `F + 1` multiples of `Lim`.
0241   //
0242   // An interval contains `F + 1` multiples of `Lim` if and only if its smallest
0243   // value modulo 2^N is less than `2^N % Lim`.  The unique value satisfying
0244   // this property is used as the one for rejection.  That is, a value of
0245   // `bits * Lim` is rejected if `(bit * Lim) % 2^N < (2^N % Lim)`.
0246 
0247   using helper = random_internal::wide_multiply<unsigned_type>;
0248   auto product = helper::multiply(bits, Lim);
0249 
0250   // Two optimizations here:
0251   // * Rejection occurs with some probability less than 1/2, and for reasonable
0252   //   ranges considerably less (in particular, less than 1/(F+1)), so
0253   //   ABSL_PREDICT_FALSE is apt.
0254   // * `Lim` is an overestimate of `threshold`, and doesn't require a divide.
0255   if (ABSL_PREDICT_FALSE(helper::lo(product) < Lim)) {
0256     // This quantity is exactly equal to `2^N % Lim`, but does not require high
0257     // precision calculations: `2^N % Lim` is congruent to `(2^N - Lim) % Lim`.
0258     // Ideally this could be expressed simply as `-X` rather than `2^N - X`, but
0259     // for types smaller than int, this calculation is incorrect due to integer
0260     // promotion rules.
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 }  // namespace absl
0274 
0275 #endif  // ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_