Abseil Common Libraries (C++) (grcp 依赖) https://abseil.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
10 KiB

Export of internal Abseil changes -- f012012ef78234a6a4585321b67d7b7c92ebc266 by Laramie Leavitt <lar@google.com>: Slight restructuring of absl/random/internal randen implementation. Convert round-keys.inc into randen_round_keys.cc file. Consistently use a 128-bit pointer type for internal method parameters. This allows simpler pointer arithmetic in C++ & permits removal of some constants and casts. Remove some redundancy in comments & constexpr variables. Specifically, all references to Randen algorithm parameters use RandenTraits; duplication in RandenSlow removed. PiperOrigin-RevId: 312190313 -- dc8b42e054046741e9ed65335bfdface997c6063 by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 312167304 -- f13d248fafaf206492c1362c3574031aea3abaf7 by Matthew Brown <matthewbr@google.com>: Cleanup StrFormat extensions a little. PiperOrigin-RevId: 312166336 -- 9d9117589667afe2332bb7ad42bc967ca7c54502 by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 312105213 -- 9a12b9b3aa0e59b8ee6cf9408ed0029045543a9b by Abseil Team <absl-team@google.com>: Complete IGNORE_TYPE macro renaming. PiperOrigin-RevId: 311999699 -- 64756f20d61021d999bd0d4c15e9ad3857382f57 by Gennadiy Rozental <rogeeff@google.com>: Switch to fixed bytes specific default value. This fixes the Abseil Flags for big endian platforms. PiperOrigin-RevId: 311844448 -- bdbe6b5b29791dbc3816ada1828458b3010ff1e9 by Laramie Leavitt <lar@google.com>: Change many distribution tests to use pcg_engine as a deterministic source of entropy. It's reasonable to test that the BitGen itself has good entropy, however when testing the cross product of all random distributions x all the architecture variations x all submitted changes results in a large number of tests. In order to account for these failures while still using good entropy requires that our allowed sigma need to account for all of these independent tests. Our current sigma values are too restrictive, and we see a lot of failures, so we have to either relax the sigma values or convert some of the statistical tests to use deterministic values. This changelist does the latter. PiperOrigin-RevId: 311840096 GitOrigin-RevId: f012012ef78234a6a4585321b67d7b7c92ebc266 Change-Id: Ic84886f38ff30d7d72c126e9b63c9a61eb729a1a
5 years ago
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: uniform_int_distribution.h
// -----------------------------------------------------------------------------
//
// This header defines a class for representing a uniform integer distribution
// over the closed (inclusive) interval [a,b]. You use this distribution in
// combination with an Abseil random bit generator to produce random values
// according to the rules of the distribution.
//
// `absl::uniform_int_distribution` is a drop-in replacement for the C++11
// `std::uniform_int_distribution` [rand.dist.uni.int] but is considerably
// faster than the libstdc++ implementation.
#ifndef ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_
#define ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_
#include <cassert>
#include <istream>
#include <limits>
#include <type_traits>
#include "absl/base/optimization.h"
#include "absl/random/internal/fast_uniform_bits.h"
#include "absl/random/internal/iostream_state_saver.h"
#include "absl/random/internal/traits.h"
#include "absl/random/internal/wide_multiply.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
// absl::uniform_int_distribution<T>
//
// This distribution produces random integer values uniformly distributed in the
// closed (inclusive) interval [a, b].
//
// Example:
//
// absl::BitGen gen;
//
// // Use the distribution to produce a value between 1 and 6, inclusive.
// int die_roll = absl::uniform_int_distribution<int>(1, 6)(gen);
//
template <typename IntType = int>
class uniform_int_distribution {
private:
using unsigned_type =
typename random_internal::make_unsigned_bits<IntType>::type;
public:
using result_type = IntType;
class param_type {
public:
using distribution_type = uniform_int_distribution;
explicit param_type(
result_type lo = 0,
result_type hi = (std::numeric_limits<result_type>::max)())
: lo_(lo),
range_(static_cast<unsigned_type>(hi) -
static_cast<unsigned_type>(lo)) {
// [rand.dist.uni.int] precondition 2
assert(lo <= hi);
}
result_type a() const { return lo_; }
result_type b() const {
return static_cast<result_type>(static_cast<unsigned_type>(lo_) + range_);
}
friend bool operator==(const param_type& a, const param_type& b) {
return a.lo_ == b.lo_ && a.range_ == b.range_;
}
friend bool operator!=(const param_type& a, const param_type& b) {
return !(a == b);
}
private:
friend class uniform_int_distribution;
unsigned_type range() const { return range_; }
result_type lo_;
unsigned_type range_;
static_assert(std::is_integral<result_type>::value,
"Class-template absl::uniform_int_distribution<> must be "
"parameterized using an integral type.");
}; // param_type
uniform_int_distribution() : uniform_int_distribution(0) {}
explicit uniform_int_distribution(
result_type lo,
result_type hi = (std::numeric_limits<result_type>::max)())
: param_(lo, hi) {}
explicit uniform_int_distribution(const param_type& param) : param_(param) {}
// uniform_int_distribution<T>::reset()
//
// Resets the uniform int distribution. Note that this function has no effect
// because the distribution already produces independent values.
void reset() {}
template <typename URBG>
result_type operator()(URBG& gen) { // NOLINT(runtime/references)
return (*this)(gen, param());
}
template <typename URBG>
result_type operator()(
URBG& gen, const param_type& param) { // NOLINT(runtime/references)
return param.a() + Generate(gen, param.range());
}
result_type a() const { return param_.a(); }
result_type b() const { return param_.b(); }
param_type param() const { return param_; }
void param(const param_type& params) { param_ = params; }
result_type(min)() const { return a(); }
result_type(max)() const { return b(); }
friend bool operator==(const uniform_int_distribution& a,
const uniform_int_distribution& b) {
return a.param_ == b.param_;
}
friend bool operator!=(const uniform_int_distribution& a,
const uniform_int_distribution& b) {
return !(a == b);
}
private:
// Generates a value in the *closed* interval [0, R]
template <typename URBG>
unsigned_type Generate(URBG& g, // NOLINT(runtime/references)
unsigned_type R);
param_type param_;
};
// -----------------------------------------------------------------------------
// Implementation details follow
// -----------------------------------------------------------------------------
template <typename CharT, typename Traits, typename IntType>
std::basic_ostream<CharT, Traits>& operator<<(
std::basic_ostream<CharT, Traits>& os,
const uniform_int_distribution<IntType>& x) {
using stream_type =
typename random_internal::stream_format_type<IntType>::type;
auto saver = random_internal::make_ostream_state_saver(os);
os << static_cast<stream_type>(x.a()) << os.fill()
<< static_cast<stream_type>(x.b());
return os;
}
template <typename CharT, typename Traits, typename IntType>
std::basic_istream<CharT, Traits>& operator>>(
std::basic_istream<CharT, Traits>& is,
uniform_int_distribution<IntType>& x) {
using param_type = typename uniform_int_distribution<IntType>::param_type;
using result_type = typename uniform_int_distribution<IntType>::result_type;
using stream_type =
typename random_internal::stream_format_type<IntType>::type;
stream_type a;
stream_type b;
auto saver = random_internal::make_istream_state_saver(is);
is >> a >> b;
if (!is.fail()) {
x.param(
param_type(static_cast<result_type>(a), static_cast<result_type>(b)));
}
return is;
}
template <typename IntType>
template <typename URBG>
typename random_internal::make_unsigned_bits<IntType>::type
uniform_int_distribution<IntType>::Generate(
URBG& g, // NOLINT(runtime/references)
typename random_internal::make_unsigned_bits<IntType>::type R) {
Export of internal Abseil changes -- 92811d3307196b2810bdc3c7e50ef9544db3f23b by CJ Johnson <johnsoncj@google.com>: Refactor InlinedVector's OverheadTest.Storage test to be easier to understand and modify in the future PiperOrigin-RevId: 342718098 -- cf3f2af201775f9c4e68dd2f9806126aecbd0748 by Abseil Team <absl-team@google.com>: Implement `reserve` more explicit to avoid calling `rehash`. `reserve` is much more widely used method and doesn't need extra logic present in `rehash`. E. g., accidental `t.reserve(0)` on non empty table shouldn't cause rehashing, which was a case before this change. It also remove some unnecessary computations from `reserve`. Was: ``` GrowthToLowerboundCapacity 2x NormalizeCapacity 1x bitwise | 1x n == 0 && capacity_ == 0 1x n == 0 && size_ == 0 1x n == 0 1x || 1x m > capacity_ 1x overall branches 6x (GrowthToLowerboundCapacity 2x, NormalizeCapacity 1x, rehash 3x) ``` Now: ``` GrowthToLowerboundCapacity 1x NormalizeCapacity 1x bitwise | 0x n == 0 && capacity_ == 0 0x n == 0 && size_ == 0 0x n == 0 0x || 0x m > capacity_ 1x overall branches 3x (GrowthToLowerboundCapacity 1x, NormalizeCapacity 1x, reserve 1x) ``` PiperOrigin-RevId: 342714022 -- c2ab8c1e4091ff685110c81bae12e3567e0cded3 by Abseil Team <absl-team@google.com>: Remove `reset_growth_left` call, which already happen in `initialize_slots`. PiperOrigin-RevId: 342701073 -- 3f41ccb70afabec8bc0dcfcca3e3ac918726bb92 by Derek Mauro <dmauro@google.com>: Use memmove instead of memcpy in situations where the source and destination may point to the same buffer Note that the OSS Abseil code never calls CUnescapeInternal with leave_nulls_scaped=true, so there is no bug in the OSS code. Fixes #844 PiperOrigin-RevId: 342633781 -- 57afb2c307b008b9f9daaa736b49c066e0075e39 by Abseil Team <absl-team@google.com>: Add absl::Round() for absl::Duration as a complementary to Floor, Ceil and Trunc. Rounding halfway cases away from zero as std::round() does. PiperOrigin-RevId: 342610871 -- c49754ecddb9339eff60b826dc17b3b459333bc0 by Abseil Team <absl-team@google.com>: Add absl::Round() for absl::Duration as a complementary to Floor, Ceil and Trunc. Rounding halfway cases away from zero as std::round() does. PiperOrigin-RevId: 342594847 -- b51bd29233aaee6ef241de984635356d26c93e4d by Abseil Team <absl-team@google.com>: Move `ConvertDeletedToEmptyAndFullToDeleted` to cc file. This function is cold and only used when table become polluted with deleted slots. So this shouldn't negatively affect performance and considered safe. This change is reducing linkage and binary size. PiperOrigin-RevId: 342319685 -- acb83c004d14e563a3b47dcfcb6c5508bee6408f by Abseil Team <absl-team@google.com>: Fix indentation in uniform_int_distribution.h. PiperOrigin-RevId: 342297575 GitOrigin-RevId: 92811d3307196b2810bdc3c7e50ef9544db3f23b Change-Id: I4fbaf4aab122d5c939ae9a3ef46ee8cca3df75e6
4 years ago
random_internal::FastUniformBits<unsigned_type> fast_bits;
Export of internal Abseil changes -- f012012ef78234a6a4585321b67d7b7c92ebc266 by Laramie Leavitt <lar@google.com>: Slight restructuring of absl/random/internal randen implementation. Convert round-keys.inc into randen_round_keys.cc file. Consistently use a 128-bit pointer type for internal method parameters. This allows simpler pointer arithmetic in C++ & permits removal of some constants and casts. Remove some redundancy in comments & constexpr variables. Specifically, all references to Randen algorithm parameters use RandenTraits; duplication in RandenSlow removed. PiperOrigin-RevId: 312190313 -- dc8b42e054046741e9ed65335bfdface997c6063 by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 312167304 -- f13d248fafaf206492c1362c3574031aea3abaf7 by Matthew Brown <matthewbr@google.com>: Cleanup StrFormat extensions a little. PiperOrigin-RevId: 312166336 -- 9d9117589667afe2332bb7ad42bc967ca7c54502 by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 312105213 -- 9a12b9b3aa0e59b8ee6cf9408ed0029045543a9b by Abseil Team <absl-team@google.com>: Complete IGNORE_TYPE macro renaming. PiperOrigin-RevId: 311999699 -- 64756f20d61021d999bd0d4c15e9ad3857382f57 by Gennadiy Rozental <rogeeff@google.com>: Switch to fixed bytes specific default value. This fixes the Abseil Flags for big endian platforms. PiperOrigin-RevId: 311844448 -- bdbe6b5b29791dbc3816ada1828458b3010ff1e9 by Laramie Leavitt <lar@google.com>: Change many distribution tests to use pcg_engine as a deterministic source of entropy. It's reasonable to test that the BitGen itself has good entropy, however when testing the cross product of all random distributions x all the architecture variations x all submitted changes results in a large number of tests. In order to account for these failures while still using good entropy requires that our allowed sigma need to account for all of these independent tests. Our current sigma values are too restrictive, and we see a lot of failures, so we have to either relax the sigma values or convert some of the statistical tests to use deterministic values. This changelist does the latter. PiperOrigin-RevId: 311840096 GitOrigin-RevId: f012012ef78234a6a4585321b67d7b7c92ebc266 Change-Id: Ic84886f38ff30d7d72c126e9b63c9a61eb729a1a
5 years ago
unsigned_type bits = fast_bits(g);
const unsigned_type Lim = R + 1;
if ((R & Lim) == 0) {
// If the interval's length is a power of two range, just take the low bits.
return bits & R;
}
// Generates a uniform variate on [0, Lim) using fixed-point multiplication.
// The above fast-path guarantees that Lim is representable in unsigned_type.
//
// Algorithm adapted from
// http://lemire.me/blog/2016/06/30/fast-random-shuffling/, with added
// explanation.
//
// The algorithm creates a uniform variate `bits` in the interval [0, 2^N),
// and treats it as the fractional part of a fixed-point real value in [0, 1),
// multiplied by 2^N. For example, 0.25 would be represented as 2^(N - 2),
// because 2^N * 0.25 == 2^(N - 2).
//
// Next, `bits` and `Lim` are multiplied with a wide-multiply to bring the
// value into the range [0, Lim). The integral part (the high word of the
// multiplication result) is then very nearly the desired result. However,
// this is not quite accurate; viewing the multiplication result as one
// double-width integer, the resulting values for the sample are mapped as
// follows:
//
// If the result lies in this interval: Return this value:
// [0, 2^N) 0
// [2^N, 2 * 2^N) 1
// ... ...
// [K * 2^N, (K + 1) * 2^N) K
// ... ...
// [(Lim - 1) * 2^N, Lim * 2^N) Lim - 1
//
// While all of these intervals have the same size, the result of `bits * Lim`
// must be a multiple of `Lim`, and not all of these intervals contain the
// same number of multiples of `Lim`. In particular, some contain
// `F = floor(2^N / Lim)` and some contain `F + 1 = ceil(2^N / Lim)`. This
// difference produces a small nonuniformity, which is corrected by applying
// rejection sampling to one of the values in the "larger intervals" (i.e.,
// the intervals containing `F + 1` multiples of `Lim`.
//
// An interval contains `F + 1` multiples of `Lim` if and only if its smallest
// value modulo 2^N is less than `2^N % Lim`. The unique value satisfying
// this property is used as the one for rejection. That is, a value of
// `bits * Lim` is rejected if `(bit * Lim) % 2^N < (2^N % Lim)`.
using helper = random_internal::wide_multiply<unsigned_type>;
auto product = helper::multiply(bits, Lim);
// Two optimizations here:
// * Rejection occurs with some probability less than 1/2, and for reasonable
// ranges considerably less (in particular, less than 1/(F+1)), so
// ABSL_PREDICT_FALSE is apt.
// * `Lim` is an overestimate of `threshold`, and doesn't require a divide.
if (ABSL_PREDICT_FALSE(helper::lo(product) < Lim)) {
// This quantity is exactly equal to `2^N % Lim`, but does not require high
// precision calculations: `2^N % Lim` is congruent to `(2^N - Lim) % Lim`.
// Ideally this could be expressed simply as `-X` rather than `2^N - X`, but
// for types smaller than int, this calculation is incorrect due to integer
// promotion rules.
const unsigned_type threshold =
((std::numeric_limits<unsigned_type>::max)() - Lim + 1) % Lim;
while (helper::lo(product) < threshold) {
bits = fast_bits(g);
product = helper::multiply(bits, Lim);
}
}
return helper::hi(product);
}
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_