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.

248 lines
7.8 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.
#ifndef ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_
#define ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_
#include <cassert>
#include <cmath>
#include <istream>
#include <limits>
#include <numeric>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/random/bernoulli_distribution.h"
#include "absl/random/internal/iostream_state_saver.h"
#include "absl/random/uniform_int_distribution.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
// absl::discrete_distribution
//
// A discrete distribution produces random integers i, where 0 <= i < n
// distributed according to the discrete probability function:
//
// P(i|p0,...,pn−1)=pi
//
// This class is an implementation of discrete_distribution (see
// [rand.dist.samp.discrete]).
//
// The algorithm used is Walker's Aliasing algorithm, described in Knuth, Vol 2.
// absl::discrete_distribution takes O(N) time to precompute the probabilities
// (where N is the number of possible outcomes in the distribution) at
// construction, and then takes O(1) time for each variate generation. Many
// other implementations also take O(N) time to construct an ordered sequence of
// partial sums, plus O(log N) time per variate to binary search.
//
template <typename IntType = int>
class discrete_distribution {
public:
using result_type = IntType;
class param_type {
public:
using distribution_type = discrete_distribution;
param_type() { init(); }
template <typename InputIterator>
explicit param_type(InputIterator begin, InputIterator end)
: p_(begin, end) {
init();
}
explicit param_type(std::initializer_list<double> weights) : p_(weights) {
init();
}
template <class UnaryOperation>
explicit param_type(size_t nw, double xmin, double xmax,
UnaryOperation fw) {
if (nw > 0) {
p_.reserve(nw);
double delta = (xmax - xmin) / static_cast<double>(nw);
assert(delta > 0);
double t = delta * 0.5;
for (size_t i = 0; i < nw; ++i) {
p_.push_back(fw(xmin + i * delta + t));
}
}
init();
}
const std::vector<double>& probabilities() const { return p_; }
size_t n() const { return p_.size() - 1; }
friend bool operator==(const param_type& a, const param_type& b) {
return a.probabilities() == b.probabilities();
}
friend bool operator!=(const param_type& a, const param_type& b) {
return !(a == b);
}
private:
friend class discrete_distribution;
void init();
std::vector<double> p_; // normalized probabilities
std::vector<std::pair<double, size_t>> q_; // (acceptance, alternate) pairs
static_assert(std::is_integral<result_type>::value,
"Class-template absl::discrete_distribution<> must be "
"parameterized using an integral type.");
};
discrete_distribution() : param_() {}
explicit discrete_distribution(const param_type& p) : param_(p) {}
template <typename InputIterator>
explicit discrete_distribution(InputIterator begin, InputIterator end)
: param_(begin, end) {}
explicit discrete_distribution(std::initializer_list<double> weights)
: param_(weights) {}
template <class UnaryOperation>
explicit discrete_distribution(size_t nw, double xmin, double xmax,
UnaryOperation fw)
: param_(nw, xmin, xmax, std::move(fw)) {}
void reset() {}
// generating functions
template <typename URBG>
result_type operator()(URBG& g) { // NOLINT(runtime/references)
return (*this)(g, param_);
}
template <typename URBG>
result_type operator()(URBG& g, // NOLINT(runtime/references)
const param_type& p);
const param_type& param() const { return param_; }
void param(const param_type& p) { param_ = p; }
result_type(min)() const { return 0; }
result_type(max)() const {
return static_cast<result_type>(param_.n());
} // inclusive
// NOTE [rand.dist.sample.discrete] returns a std::vector<double> not a
// const std::vector<double>&.
const std::vector<double>& probabilities() const {
return param_.probabilities();
}
friend bool operator==(const discrete_distribution& a,
const discrete_distribution& b) {
return a.param_ == b.param_;
}
friend bool operator!=(const discrete_distribution& a,
const discrete_distribution& b) {
return a.param_ != b.param_;
}
private:
param_type param_;
};
// --------------------------------------------------------------------------
// Implementation details only below
// --------------------------------------------------------------------------
namespace random_internal {
// Using the vector `*probabilities`, whose values are the weights or
// probabilities of an element being selected, constructs the proportional
// probabilities used by the discrete distribution. `*probabilities` will be
// scaled, if necessary, so that its entries sum to a value sufficiently close
// to 1.0.
std::vector<std::pair<double, size_t>> InitDiscreteDistribution(
std::vector<double>* probabilities);
} // namespace random_internal
template <typename IntType>
void discrete_distribution<IntType>::param_type::init() {
if (p_.empty()) {
p_.push_back(1.0);
q_.emplace_back(1.0, 0);
} else {
assert(n() <= (std::numeric_limits<IntType>::max)());
q_ = random_internal::InitDiscreteDistribution(&p_);
}
}
template <typename IntType>
template <typename URBG>
typename discrete_distribution<IntType>::result_type
discrete_distribution<IntType>::operator()(
URBG& g, // NOLINT(runtime/references)
const param_type& p) {
const auto idx = absl::uniform_int_distribution<result_type>(0, p.n())(g);
const auto& q = p.q_[idx];
const bool selected = absl::bernoulli_distribution(q.first)(g);
return selected ? idx : static_cast<result_type>(q.second);
}
template <typename CharT, typename Traits, typename IntType>
std::basic_ostream<CharT, Traits>& operator<<(
std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
const discrete_distribution<IntType>& x) {
auto saver = random_internal::make_ostream_state_saver(os);
const auto& probabilities = x.param().probabilities();
os << probabilities.size();
os.precision(random_internal::stream_precision_helper<double>::kPrecision);
for (const auto& p : probabilities) {
os << os.fill() << p;
}
return os;
}
template <typename CharT, typename Traits, typename IntType>
std::basic_istream<CharT, Traits>& operator>>(
std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
discrete_distribution<IntType>& x) { // NOLINT(runtime/references)
using param_type = typename discrete_distribution<IntType>::param_type;
auto saver = random_internal::make_istream_state_saver(is);
size_t n;
std::vector<double> p;
is >> n;
if (is.fail()) return is;
if (n > 0) {
p.reserve(n);
for (IntType i = 0; i < n && !is.fail(); ++i) {
auto tmp = random_internal::read_floating_point<double>(is);
if (is.fail()) return is;
p.push_back(tmp);
}
}
x.param(param_type(p.begin(), p.end()));
return is;
}
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_