create
This commit is contained in:
424
Pods/abseil/absl/hash/hash.h
generated
Normal file
424
Pods/abseil/absl/hash/hash.h
generated
Normal file
@@ -0,0 +1,424 @@
|
||||
// Copyright 2018 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: hash.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file defines the Abseil `hash` library and the Abseil hashing
|
||||
// framework. This framework consists of the following:
|
||||
//
|
||||
// * The `absl::Hash` functor, which is used to invoke the hasher within the
|
||||
// Abseil hashing framework. `absl::Hash<T>` supports most basic types and
|
||||
// a number of Abseil types out of the box.
|
||||
// * `AbslHashValue`, an extension point that allows you to extend types to
|
||||
// support Abseil hashing without requiring you to define a hashing
|
||||
// algorithm.
|
||||
// * `HashState`, a type-erased class which implements the manipulation of the
|
||||
// hash state (H) itself; contains member functions `combine()`,
|
||||
// `combine_contiguous()`, and `combine_unordered()`; and which you can use
|
||||
// to contribute to an existing hash state when hashing your types.
|
||||
//
|
||||
// Unlike `std::hash` or other hashing frameworks, the Abseil hashing framework
|
||||
// provides most of its utility by abstracting away the hash algorithm (and its
|
||||
// implementation) entirely. Instead, a type invokes the Abseil hashing
|
||||
// framework by simply combining its state with the state of known, hashable
|
||||
// types. Hashing of that combined state is separately done by `absl::Hash`.
|
||||
//
|
||||
// One should assume that a hash algorithm is chosen randomly at the start of
|
||||
// each process. E.g., `absl::Hash<int>{}(9)` in one process and
|
||||
// `absl::Hash<int>{}(9)` in another process are likely to differ.
|
||||
//
|
||||
// `absl::Hash` may also produce different values from different dynamically
|
||||
// loaded libraries. For this reason, `absl::Hash` values must never cross
|
||||
// boundaries in dynamically loaded libraries (including when used in types like
|
||||
// hash containers.)
|
||||
//
|
||||
// `absl::Hash` is intended to strongly mix input bits with a target of passing
|
||||
// an [Avalanche Test](https://en.wikipedia.org/wiki/Avalanche_effect).
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Suppose we have a class `Circle` for which we want to add hashing:
|
||||
// class Circle {
|
||||
// public:
|
||||
// ...
|
||||
// private:
|
||||
// std::pair<int, int> center_;
|
||||
// int radius_;
|
||||
// };
|
||||
//
|
||||
// // To add hashing support to `Circle`, we simply need to add a free
|
||||
// // (non-member) function `AbslHashValue()`, and return the combined hash
|
||||
// // state of the existing hash state and the class state. You can add such a
|
||||
// // free function using a friend declaration within the body of the class:
|
||||
// class Circle {
|
||||
// public:
|
||||
// ...
|
||||
// template <typename H>
|
||||
// friend H AbslHashValue(H h, const Circle& c) {
|
||||
// return H::combine(std::move(h), c.center_, c.radius_);
|
||||
// }
|
||||
// ...
|
||||
// };
|
||||
//
|
||||
// For more information, see Adding Type Support to `absl::Hash` below.
|
||||
//
|
||||
#ifndef ABSL_HASH_HASH_H_
|
||||
#define ABSL_HASH_HASH_H_
|
||||
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/function_ref.h"
|
||||
#include "absl/hash/internal/hash.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// `absl::Hash`
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// `absl::Hash<T>` is a convenient general-purpose hash functor for any type `T`
|
||||
// satisfying any of the following conditions (in order):
|
||||
//
|
||||
// * T is an arithmetic or pointer type
|
||||
// * T defines an overload for `AbslHashValue(H, const T&)` for an arbitrary
|
||||
// hash state `H`.
|
||||
// - T defines a specialization of `std::hash<T>`
|
||||
//
|
||||
// `absl::Hash` intrinsically supports the following types:
|
||||
//
|
||||
// * All integral types (including bool)
|
||||
// * All enum types
|
||||
// * All floating-point types (although hashing them is discouraged)
|
||||
// * All pointer types, including nullptr_t
|
||||
// * std::pair<T1, T2>, if T1 and T2 are hashable
|
||||
// * std::tuple<Ts...>, if all the Ts... are hashable
|
||||
// * std::unique_ptr and std::shared_ptr
|
||||
// * All string-like types including:
|
||||
// * absl::Cord
|
||||
// * std::string (as well as any instance of std::basic_string that
|
||||
// uses one of {char, wchar_t, char16_t, char32_t} and its associated
|
||||
// std::char_traits)
|
||||
// * std::string_view (as well as any instance of std::basic_string_view
|
||||
// that uses one of {char, wchar_t, char16_t, char32_t} and its associated
|
||||
// std::char_traits)
|
||||
// * All the standard sequence containers (provided the elements are hashable)
|
||||
// * All the standard associative containers (provided the elements are
|
||||
// hashable)
|
||||
// * absl types such as the following:
|
||||
// * absl::string_view
|
||||
// * absl::uint128
|
||||
// * absl::Time, absl::Duration, and absl::TimeZone
|
||||
// * absl containers (provided the elements are hashable) such as the
|
||||
// following:
|
||||
// * absl::flat_hash_set, absl::node_hash_set, absl::btree_set
|
||||
// * absl::flat_hash_map, absl::node_hash_map, absl::btree_map
|
||||
// * absl::btree_multiset, absl::btree_multimap
|
||||
// * absl::InlinedVector
|
||||
// * absl::FixedArray
|
||||
//
|
||||
// When absl::Hash is used to hash an unordered container with a custom hash
|
||||
// functor, the elements are hashed using default absl::Hash semantics, not
|
||||
// the custom hash functor. This is consistent with the behavior of
|
||||
// operator==() on unordered containers, which compares elements pairwise with
|
||||
// operator==() rather than the custom equality functor. It is usually a
|
||||
// mistake to use either operator==() or absl::Hash on unordered collections
|
||||
// that use functors incompatible with operator==() equality.
|
||||
//
|
||||
// Note: the list above is not meant to be exhaustive. Additional type support
|
||||
// may be added, in which case the above list will be updated.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// absl::Hash Invocation Evaluation
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// When invoked, `absl::Hash<T>` searches for supplied hash functions in the
|
||||
// following order:
|
||||
//
|
||||
// * Natively supported types out of the box (see above)
|
||||
// * Types for which an `AbslHashValue()` overload is provided (such as
|
||||
// user-defined types). See "Adding Type Support to `absl::Hash`" below.
|
||||
// * Types which define a `std::hash<T>` specialization
|
||||
//
|
||||
// The fallback to legacy hash functions exists mainly for backwards
|
||||
// compatibility. If you have a choice, prefer defining an `AbslHashValue`
|
||||
// overload instead of specializing any legacy hash functors.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// The Hash State Concept, and using `HashState` for Type Erasure
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// The `absl::Hash` framework relies on the Concept of a "hash state." Such a
|
||||
// hash state is used in several places:
|
||||
//
|
||||
// * Within existing implementations of `absl::Hash<T>` to store the hashed
|
||||
// state of an object. Note that it is up to the implementation how it stores
|
||||
// such state. A hash table, for example, may mix the state to produce an
|
||||
// integer value; a testing framework may simply hold a vector of that state.
|
||||
// * Within implementations of `AbslHashValue()` used to extend user-defined
|
||||
// types. (See "Adding Type Support to absl::Hash" below.)
|
||||
// * Inside a `HashState`, providing type erasure for the concept of a hash
|
||||
// state, which you can use to extend the `absl::Hash` framework for types
|
||||
// that are otherwise difficult to extend using `AbslHashValue()`. (See the
|
||||
// `HashState` class below.)
|
||||
//
|
||||
// The "hash state" concept contains three member functions for mixing hash
|
||||
// state:
|
||||
//
|
||||
// * `H::combine(state, values...)`
|
||||
//
|
||||
// Combines an arbitrary number of values into a hash state, returning the
|
||||
// updated state. Note that the existing hash state is move-only and must be
|
||||
// passed by value.
|
||||
//
|
||||
// Each of the value types T must be hashable by H.
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// state = H::combine(std::move(state), value1, value2, value3);
|
||||
//
|
||||
// must be guaranteed to produce the same hash expansion as
|
||||
//
|
||||
// state = H::combine(std::move(state), value1);
|
||||
// state = H::combine(std::move(state), value2);
|
||||
// state = H::combine(std::move(state), value3);
|
||||
//
|
||||
// * `H::combine_contiguous(state, data, size)`
|
||||
//
|
||||
// Combines a contiguous array of `size` elements into a hash state,
|
||||
// returning the updated state. Note that the existing hash state is
|
||||
// move-only and must be passed by value.
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// state = H::combine_contiguous(std::move(state), data, size);
|
||||
//
|
||||
// need NOT be guaranteed to produce the same hash expansion as a loop
|
||||
// (it may perform internal optimizations). If you need this guarantee, use a
|
||||
// loop instead.
|
||||
//
|
||||
// * `H::combine_unordered(state, begin, end)`
|
||||
//
|
||||
// Combines a set of elements denoted by an iterator pair into a hash
|
||||
// state, returning the updated state. Note that the existing hash
|
||||
// state is move-only and must be passed by value.
|
||||
//
|
||||
// Unlike the other two methods, the hashing is order-independent.
|
||||
// This can be used to hash unordered collections.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// Adding Type Support to `absl::Hash`
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// To add support for your user-defined type, add a proper `AbslHashValue()`
|
||||
// overload as a free (non-member) function. The overload will take an
|
||||
// existing hash state and should combine that state with state from the type.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// template <typename H>
|
||||
// H AbslHashValue(H state, const MyType& v) {
|
||||
// return H::combine(std::move(state), v.field1, ..., v.fieldN);
|
||||
// }
|
||||
//
|
||||
// where `(field1, ..., fieldN)` are the members you would use on your
|
||||
// `operator==` to define equality.
|
||||
//
|
||||
// Notice that `AbslHashValue` is not a class member, but an ordinary function.
|
||||
// An `AbslHashValue` overload for a type should only be declared in the same
|
||||
// file and namespace as said type. The proper `AbslHashValue` implementation
|
||||
// for a given type will be discovered via ADL.
|
||||
//
|
||||
// Note: unlike `std::hash', `absl::Hash` should never be specialized. It must
|
||||
// only be extended by adding `AbslHashValue()` overloads.
|
||||
//
|
||||
template <typename T>
|
||||
using Hash = absl::hash_internal::Hash<T>;
|
||||
|
||||
// HashOf
|
||||
//
|
||||
// absl::HashOf() is a helper that generates a hash from the values of its
|
||||
// arguments. It dispatches to absl::Hash directly, as follows:
|
||||
// * HashOf(t) == absl::Hash<T>{}(t)
|
||||
// * HashOf(a, b, c) == HashOf(std::make_tuple(a, b, c))
|
||||
//
|
||||
// HashOf(a1, a2, ...) == HashOf(b1, b2, ...) is guaranteed when
|
||||
// * The argument lists have pairwise identical C++ types
|
||||
// * a1 == b1 && a2 == b2 && ...
|
||||
//
|
||||
// The requirement that the arguments match in both type and value is critical.
|
||||
// It means that `a == b` does not necessarily imply `HashOf(a) == HashOf(b)` if
|
||||
// `a` and `b` have different types. For example, `HashOf(2) != HashOf(2.0)`.
|
||||
template <int&... ExplicitArgumentBarrier, typename... Types>
|
||||
size_t HashOf(const Types&... values) {
|
||||
auto tuple = std::tie(values...);
|
||||
return absl::Hash<decltype(tuple)>{}(tuple);
|
||||
}
|
||||
|
||||
// HashState
|
||||
//
|
||||
// A type erased version of the hash state concept, for use in user-defined
|
||||
// `AbslHashValue` implementations that can't use templates (such as PImpl
|
||||
// classes, virtual functions, etc.). The type erasure adds overhead so it
|
||||
// should be avoided unless necessary.
|
||||
//
|
||||
// Note: This wrapper will only erase calls to
|
||||
// combine_contiguous(H, const unsigned char*, size_t)
|
||||
// RunCombineUnordered(H, CombinerF)
|
||||
//
|
||||
// All other calls will be handled internally and will not invoke overloads
|
||||
// provided by the wrapped class.
|
||||
//
|
||||
// Users of this class should still define a template `AbslHashValue` function,
|
||||
// but can use `absl::HashState::Create(&state)` to erase the type of the hash
|
||||
// state and dispatch to their private hashing logic.
|
||||
//
|
||||
// This state can be used like any other hash state. In particular, you can call
|
||||
// `HashState::combine()` and `HashState::combine_contiguous()` on it.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// class Interface {
|
||||
// public:
|
||||
// template <typename H>
|
||||
// friend H AbslHashValue(H state, const Interface& value) {
|
||||
// state = H::combine(std::move(state), std::type_index(typeid(*this)));
|
||||
// value.HashValue(absl::HashState::Create(&state));
|
||||
// return state;
|
||||
// }
|
||||
// private:
|
||||
// virtual void HashValue(absl::HashState state) const = 0;
|
||||
// };
|
||||
//
|
||||
// class Impl : Interface {
|
||||
// private:
|
||||
// void HashValue(absl::HashState state) const override {
|
||||
// absl::HashState::combine(std::move(state), v1_, v2_);
|
||||
// }
|
||||
// int v1_;
|
||||
// std::string v2_;
|
||||
// };
|
||||
class HashState : public hash_internal::HashStateBase<HashState> {
|
||||
public:
|
||||
// HashState::Create()
|
||||
//
|
||||
// Create a new `HashState` instance that wraps `state`. All calls to
|
||||
// `combine()` and `combine_contiguous()` on the new instance will be
|
||||
// redirected to the original `state` object. The `state` object must outlive
|
||||
// the `HashState` instance.
|
||||
template <typename T>
|
||||
static HashState Create(T* state) {
|
||||
HashState s;
|
||||
s.Init(state);
|
||||
return s;
|
||||
}
|
||||
|
||||
HashState(const HashState&) = delete;
|
||||
HashState& operator=(const HashState&) = delete;
|
||||
HashState(HashState&&) = default;
|
||||
HashState& operator=(HashState&&) = default;
|
||||
|
||||
// HashState::combine()
|
||||
//
|
||||
// Combines an arbitrary number of values into a hash state, returning the
|
||||
// updated state.
|
||||
using HashState::HashStateBase::combine;
|
||||
|
||||
// HashState::combine_contiguous()
|
||||
//
|
||||
// Combines a contiguous array of `size` elements into a hash state, returning
|
||||
// the updated state.
|
||||
static HashState combine_contiguous(HashState hash_state,
|
||||
const unsigned char* first, size_t size) {
|
||||
hash_state.combine_contiguous_(hash_state.state_, first, size);
|
||||
return hash_state;
|
||||
}
|
||||
using HashState::HashStateBase::combine_contiguous;
|
||||
|
||||
private:
|
||||
HashState() = default;
|
||||
|
||||
friend class HashState::HashStateBase;
|
||||
|
||||
template <typename T>
|
||||
static void CombineContiguousImpl(void* p, const unsigned char* first,
|
||||
size_t size) {
|
||||
T& state = *static_cast<T*>(p);
|
||||
state = T::combine_contiguous(std::move(state), first, size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Init(T* state) {
|
||||
state_ = state;
|
||||
combine_contiguous_ = &CombineContiguousImpl<T>;
|
||||
run_combine_unordered_ = &RunCombineUnorderedImpl<T>;
|
||||
}
|
||||
|
||||
template <typename HS>
|
||||
struct CombineUnorderedInvoker {
|
||||
template <typename T, typename ConsumerT>
|
||||
void operator()(T inner_state, ConsumerT inner_cb) {
|
||||
f(HashState::Create(&inner_state),
|
||||
[&](HashState& inner_erased) { inner_cb(inner_erased.Real<T>()); });
|
||||
}
|
||||
|
||||
absl::FunctionRef<void(HS, absl::FunctionRef<void(HS&)>)> f;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
static HashState RunCombineUnorderedImpl(
|
||||
HashState state,
|
||||
absl::FunctionRef<void(HashState, absl::FunctionRef<void(HashState&)>)>
|
||||
f) {
|
||||
// Note that this implementation assumes that inner_state and outer_state
|
||||
// are the same type. This isn't true in the SpyHash case, but SpyHash
|
||||
// types are move-convertible to each other, so this still works.
|
||||
T& real_state = state.Real<T>();
|
||||
real_state = T::RunCombineUnordered(
|
||||
std::move(real_state), CombineUnorderedInvoker<HashState>{f});
|
||||
return state;
|
||||
}
|
||||
|
||||
template <typename CombinerT>
|
||||
static HashState RunCombineUnordered(HashState state, CombinerT combiner) {
|
||||
auto* run = state.run_combine_unordered_;
|
||||
return run(std::move(state), std::ref(combiner));
|
||||
}
|
||||
|
||||
// Do not erase an already erased state.
|
||||
void Init(HashState* state) {
|
||||
state_ = state->state_;
|
||||
combine_contiguous_ = state->combine_contiguous_;
|
||||
run_combine_unordered_ = state->run_combine_unordered_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T& Real() {
|
||||
return *static_cast<T*>(state_);
|
||||
}
|
||||
|
||||
void* state_;
|
||||
void (*combine_contiguous_)(void*, const unsigned char*, size_t);
|
||||
HashState (*run_combine_unordered_)(
|
||||
HashState state,
|
||||
absl::FunctionRef<void(HashState, absl::FunctionRef<void(HashState&)>)>);
|
||||
};
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_HASH_HASH_H_
|
||||
349
Pods/abseil/absl/hash/internal/city.cc
generated
Normal file
349
Pods/abseil/absl/hash/internal/city.cc
generated
Normal file
@@ -0,0 +1,349 @@
|
||||
// Copyright 2018 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.
|
||||
//
|
||||
// This file provides CityHash64() and related functions.
|
||||
//
|
||||
// It's probably possible to create even faster hash functions by
|
||||
// writing a program that systematically explores some of the space of
|
||||
// possible hash functions, by using SIMD instructions, or by
|
||||
// compromising on hash quality.
|
||||
|
||||
#include "absl/hash/internal/city.h"
|
||||
|
||||
#include <string.h> // for memcpy and memset
|
||||
#include <algorithm>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/endian.h"
|
||||
#include "absl/base/internal/unaligned_access.h"
|
||||
#include "absl/base/optimization.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace hash_internal {
|
||||
|
||||
#ifdef ABSL_IS_BIG_ENDIAN
|
||||
#define uint32_in_expected_order(x) (absl::gbswap_32(x))
|
||||
#define uint64_in_expected_order(x) (absl::gbswap_64(x))
|
||||
#else
|
||||
#define uint32_in_expected_order(x) (x)
|
||||
#define uint64_in_expected_order(x) (x)
|
||||
#endif
|
||||
|
||||
static uint64_t Fetch64(const char *p) {
|
||||
return uint64_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
|
||||
}
|
||||
|
||||
static uint32_t Fetch32(const char *p) {
|
||||
return uint32_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
|
||||
}
|
||||
|
||||
// Some primes between 2^63 and 2^64 for various uses.
|
||||
static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
|
||||
static const uint64_t k1 = 0xb492b66fbe98f273ULL;
|
||||
static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
|
||||
|
||||
// Magic numbers for 32-bit hashing. Copied from Murmur3.
|
||||
static const uint32_t c1 = 0xcc9e2d51;
|
||||
static const uint32_t c2 = 0x1b873593;
|
||||
|
||||
// A 32-bit to 32-bit integer hash copied from Murmur3.
|
||||
static uint32_t fmix(uint32_t h) {
|
||||
h ^= h >> 16;
|
||||
h *= 0x85ebca6b;
|
||||
h ^= h >> 13;
|
||||
h *= 0xc2b2ae35;
|
||||
h ^= h >> 16;
|
||||
return h;
|
||||
}
|
||||
|
||||
static uint32_t Rotate32(uint32_t val, int shift) {
|
||||
// Avoid shifting by 32: doing so yields an undefined result.
|
||||
return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
|
||||
}
|
||||
|
||||
#undef PERMUTE3
|
||||
#define PERMUTE3(a, b, c) \
|
||||
do { \
|
||||
std::swap(a, b); \
|
||||
std::swap(a, c); \
|
||||
} while (0)
|
||||
|
||||
static uint32_t Mur(uint32_t a, uint32_t h) {
|
||||
// Helper from Murmur3 for combining two 32-bit values.
|
||||
a *= c1;
|
||||
a = Rotate32(a, 17);
|
||||
a *= c2;
|
||||
h ^= a;
|
||||
h = Rotate32(h, 19);
|
||||
return h * 5 + 0xe6546b64;
|
||||
}
|
||||
|
||||
static uint32_t Hash32Len13to24(const char *s, size_t len) {
|
||||
uint32_t a = Fetch32(s - 4 + (len >> 1));
|
||||
uint32_t b = Fetch32(s + 4);
|
||||
uint32_t c = Fetch32(s + len - 8);
|
||||
uint32_t d = Fetch32(s + (len >> 1));
|
||||
uint32_t e = Fetch32(s);
|
||||
uint32_t f = Fetch32(s + len - 4);
|
||||
uint32_t h = static_cast<uint32_t>(len);
|
||||
|
||||
return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
|
||||
}
|
||||
|
||||
static uint32_t Hash32Len0to4(const char *s, size_t len) {
|
||||
uint32_t b = 0;
|
||||
uint32_t c = 9;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
signed char v = static_cast<signed char>(s[i]);
|
||||
b = b * c1 + static_cast<uint32_t>(v);
|
||||
c ^= b;
|
||||
}
|
||||
return fmix(Mur(b, Mur(static_cast<uint32_t>(len), c)));
|
||||
}
|
||||
|
||||
static uint32_t Hash32Len5to12(const char *s, size_t len) {
|
||||
uint32_t a = static_cast<uint32_t>(len), b = a * 5, c = 9, d = b;
|
||||
a += Fetch32(s);
|
||||
b += Fetch32(s + len - 4);
|
||||
c += Fetch32(s + ((len >> 1) & 4));
|
||||
return fmix(Mur(c, Mur(b, Mur(a, d))));
|
||||
}
|
||||
|
||||
uint32_t CityHash32(const char *s, size_t len) {
|
||||
if (len <= 24) {
|
||||
return len <= 12
|
||||
? (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len))
|
||||
: Hash32Len13to24(s, len);
|
||||
}
|
||||
|
||||
// len > 24
|
||||
uint32_t h = static_cast<uint32_t>(len), g = c1 * h, f = g;
|
||||
|
||||
uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
|
||||
uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
|
||||
uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
|
||||
uint32_t a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
|
||||
uint32_t a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
|
||||
h ^= a0;
|
||||
h = Rotate32(h, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
h ^= a2;
|
||||
h = Rotate32(h, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
g ^= a1;
|
||||
g = Rotate32(g, 19);
|
||||
g = g * 5 + 0xe6546b64;
|
||||
g ^= a3;
|
||||
g = Rotate32(g, 19);
|
||||
g = g * 5 + 0xe6546b64;
|
||||
f += a4;
|
||||
f = Rotate32(f, 19);
|
||||
f = f * 5 + 0xe6546b64;
|
||||
size_t iters = (len - 1) / 20;
|
||||
do {
|
||||
uint32_t b0 = Rotate32(Fetch32(s) * c1, 17) * c2;
|
||||
uint32_t b1 = Fetch32(s + 4);
|
||||
uint32_t b2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
|
||||
uint32_t b3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
|
||||
uint32_t b4 = Fetch32(s + 16);
|
||||
h ^= b0;
|
||||
h = Rotate32(h, 18);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
f += b1;
|
||||
f = Rotate32(f, 19);
|
||||
f = f * c1;
|
||||
g += b2;
|
||||
g = Rotate32(g, 18);
|
||||
g = g * 5 + 0xe6546b64;
|
||||
h ^= b3 + b1;
|
||||
h = Rotate32(h, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
g ^= b4;
|
||||
g = absl::gbswap_32(g) * 5;
|
||||
h += b4 * 5;
|
||||
h = absl::gbswap_32(h);
|
||||
f += b0;
|
||||
PERMUTE3(f, h, g);
|
||||
s += 20;
|
||||
} while (--iters != 0);
|
||||
g = Rotate32(g, 11) * c1;
|
||||
g = Rotate32(g, 17) * c1;
|
||||
f = Rotate32(f, 11) * c1;
|
||||
f = Rotate32(f, 17) * c1;
|
||||
h = Rotate32(h + g, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
h = Rotate32(h, 17) * c1;
|
||||
h = Rotate32(h + f, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
h = Rotate32(h, 17) * c1;
|
||||
return h;
|
||||
}
|
||||
|
||||
// Bitwise right rotate. Normally this will compile to a single
|
||||
// instruction, especially if the shift is a manifest constant.
|
||||
static uint64_t Rotate(uint64_t val, int shift) {
|
||||
// Avoid shifting by 64: doing so yields an undefined result.
|
||||
return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
|
||||
}
|
||||
|
||||
static uint64_t ShiftMix(uint64_t val) { return val ^ (val >> 47); }
|
||||
|
||||
static uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
|
||||
// Murmur-inspired hashing.
|
||||
uint64_t a = (u ^ v) * mul;
|
||||
a ^= (a >> 47);
|
||||
uint64_t b = (v ^ a) * mul;
|
||||
b ^= (b >> 47);
|
||||
b *= mul;
|
||||
return b;
|
||||
}
|
||||
|
||||
static uint64_t HashLen16(uint64_t u, uint64_t v) {
|
||||
const uint64_t kMul = 0x9ddfea08eb382d69ULL;
|
||||
return HashLen16(u, v, kMul);
|
||||
}
|
||||
|
||||
static uint64_t HashLen0to16(const char *s, size_t len) {
|
||||
if (len >= 8) {
|
||||
uint64_t mul = k2 + len * 2;
|
||||
uint64_t a = Fetch64(s) + k2;
|
||||
uint64_t b = Fetch64(s + len - 8);
|
||||
uint64_t c = Rotate(b, 37) * mul + a;
|
||||
uint64_t d = (Rotate(a, 25) + b) * mul;
|
||||
return HashLen16(c, d, mul);
|
||||
}
|
||||
if (len >= 4) {
|
||||
uint64_t mul = k2 + len * 2;
|
||||
uint64_t a = Fetch32(s);
|
||||
return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
|
||||
}
|
||||
if (len > 0) {
|
||||
uint8_t a = static_cast<uint8_t>(s[0]);
|
||||
uint8_t b = static_cast<uint8_t>(s[len >> 1]);
|
||||
uint8_t c = static_cast<uint8_t>(s[len - 1]);
|
||||
uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
|
||||
uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
|
||||
return ShiftMix(y * k2 ^ z * k0) * k2;
|
||||
}
|
||||
return k2;
|
||||
}
|
||||
|
||||
// This probably works well for 16-byte strings as well, but it may be overkill
|
||||
// in that case.
|
||||
static uint64_t HashLen17to32(const char *s, size_t len) {
|
||||
uint64_t mul = k2 + len * 2;
|
||||
uint64_t a = Fetch64(s) * k1;
|
||||
uint64_t b = Fetch64(s + 8);
|
||||
uint64_t c = Fetch64(s + len - 8) * mul;
|
||||
uint64_t d = Fetch64(s + len - 16) * k2;
|
||||
return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
|
||||
a + Rotate(b + k2, 18) + c, mul);
|
||||
}
|
||||
|
||||
// Return a 16-byte hash for 48 bytes. Quick and dirty.
|
||||
// Callers do best to use "random-looking" values for a and b.
|
||||
static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
|
||||
uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
|
||||
a += w;
|
||||
b = Rotate(b + a + z, 21);
|
||||
uint64_t c = a;
|
||||
a += x;
|
||||
a += y;
|
||||
b += Rotate(a, 44);
|
||||
return std::make_pair(a + z, b + c);
|
||||
}
|
||||
|
||||
// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
|
||||
static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char *s,
|
||||
uint64_t a,
|
||||
uint64_t b) {
|
||||
return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16),
|
||||
Fetch64(s + 24), a, b);
|
||||
}
|
||||
|
||||
// Return an 8-byte hash for 33 to 64 bytes.
|
||||
static uint64_t HashLen33to64(const char *s, size_t len) {
|
||||
uint64_t mul = k2 + len * 2;
|
||||
uint64_t a = Fetch64(s) * k2;
|
||||
uint64_t b = Fetch64(s + 8);
|
||||
uint64_t c = Fetch64(s + len - 24);
|
||||
uint64_t d = Fetch64(s + len - 32);
|
||||
uint64_t e = Fetch64(s + 16) * k2;
|
||||
uint64_t f = Fetch64(s + 24) * 9;
|
||||
uint64_t g = Fetch64(s + len - 8);
|
||||
uint64_t h = Fetch64(s + len - 16) * mul;
|
||||
uint64_t u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
|
||||
uint64_t v = ((a + g) ^ d) + f + 1;
|
||||
uint64_t w = absl::gbswap_64((u + v) * mul) + h;
|
||||
uint64_t x = Rotate(e + f, 42) + c;
|
||||
uint64_t y = (absl::gbswap_64((v + w) * mul) + g) * mul;
|
||||
uint64_t z = e + f + c;
|
||||
a = absl::gbswap_64((x + z) * mul + y) + b;
|
||||
b = ShiftMix((z + a) * mul + d + h) * mul;
|
||||
return b + x;
|
||||
}
|
||||
|
||||
uint64_t CityHash64(const char *s, size_t len) {
|
||||
if (len <= 32) {
|
||||
if (len <= 16) {
|
||||
return HashLen0to16(s, len);
|
||||
} else {
|
||||
return HashLen17to32(s, len);
|
||||
}
|
||||
} else if (len <= 64) {
|
||||
return HashLen33to64(s, len);
|
||||
}
|
||||
|
||||
// For strings over 64 bytes we hash the end first, and then as we
|
||||
// loop we keep 56 bytes of state: v, w, x, y, and z.
|
||||
uint64_t x = Fetch64(s + len - 40);
|
||||
uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
|
||||
uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
|
||||
std::pair<uint64_t, uint64_t> v =
|
||||
WeakHashLen32WithSeeds(s + len - 64, len, z);
|
||||
std::pair<uint64_t, uint64_t> w =
|
||||
WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
|
||||
x = x * k1 + Fetch64(s);
|
||||
|
||||
// Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
|
||||
len = (len - 1) & ~static_cast<size_t>(63);
|
||||
do {
|
||||
x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
|
||||
y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
|
||||
x ^= w.second;
|
||||
y += v.first + Fetch64(s + 40);
|
||||
z = Rotate(z + w.first, 33) * k1;
|
||||
v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
|
||||
w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
|
||||
std::swap(z, x);
|
||||
s += 64;
|
||||
len -= 64;
|
||||
} while (len != 0);
|
||||
return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
|
||||
HashLen16(v.second, w.second) + x);
|
||||
}
|
||||
|
||||
uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) {
|
||||
return CityHash64WithSeeds(s, len, k2, seed);
|
||||
}
|
||||
|
||||
uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
|
||||
uint64_t seed1) {
|
||||
return HashLen16(CityHash64(s, len) - seed0, seed1);
|
||||
}
|
||||
|
||||
} // namespace hash_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
78
Pods/abseil/absl/hash/internal/city.h
generated
Normal file
78
Pods/abseil/absl/hash/internal/city.h
generated
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright 2018 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.
|
||||
//
|
||||
// https://code.google.com/p/cityhash/
|
||||
//
|
||||
// This file provides a few functions for hashing strings. All of them are
|
||||
// high-quality functions in the sense that they pass standard tests such
|
||||
// as Austin Appleby's SMHasher. They are also fast.
|
||||
//
|
||||
// For 64-bit x86 code, on short strings, we don't know of anything faster than
|
||||
// CityHash64 that is of comparable quality. We believe our nearest competitor
|
||||
// is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash
|
||||
// tables and most other hashing (excluding cryptography).
|
||||
//
|
||||
// For 32-bit x86 code, we don't know of anything faster than CityHash32 that
|
||||
// is of comparable quality. We believe our nearest competitor is Murmur3A.
|
||||
// (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)
|
||||
//
|
||||
// Functions in the CityHash family are not suitable for cryptography.
|
||||
//
|
||||
// Please see CityHash's README file for more details on our performance
|
||||
// measurements and so on.
|
||||
//
|
||||
// WARNING: This code has been only lightly tested on big-endian platforms!
|
||||
// It is known to work well on little-endian platforms that have a small penalty
|
||||
// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
|
||||
// It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
|
||||
// bug reports are welcome.
|
||||
//
|
||||
// By the way, for some hash functions, given strings a and b, the hash
|
||||
// of a+b is easily derived from the hashes of a and b. This property
|
||||
// doesn't hold for any hash functions in this file.
|
||||
|
||||
#ifndef ABSL_HASH_INTERNAL_CITY_H_
|
||||
#define ABSL_HASH_INTERNAL_CITY_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h> // for size_t.
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace hash_internal {
|
||||
|
||||
// Hash function for a byte array.
|
||||
uint64_t CityHash64(const char *s, size_t len);
|
||||
|
||||
// Hash function for a byte array. For convenience, a 64-bit seed is also
|
||||
// hashed into the result.
|
||||
uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed);
|
||||
|
||||
// Hash function for a byte array. For convenience, two seeds are also
|
||||
// hashed into the result.
|
||||
uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
|
||||
uint64_t seed1);
|
||||
|
||||
// Hash function for a byte array. Most useful in 32-bit binaries.
|
||||
uint32_t CityHash32(const char *s, size_t len);
|
||||
|
||||
} // namespace hash_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_HASH_INTERNAL_CITY_H_
|
||||
69
Pods/abseil/absl/hash/internal/hash.cc
generated
Normal file
69
Pods/abseil/absl/hash/internal/hash.cc
generated
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2018 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.
|
||||
|
||||
#include "absl/hash/internal/hash.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace hash_internal {
|
||||
|
||||
uint64_t MixingHashState::CombineLargeContiguousImpl32(
|
||||
uint64_t state, const unsigned char* first, size_t len) {
|
||||
while (len >= PiecewiseChunkSize()) {
|
||||
state = Mix(state,
|
||||
hash_internal::CityHash32(reinterpret_cast<const char*>(first),
|
||||
PiecewiseChunkSize()));
|
||||
len -= PiecewiseChunkSize();
|
||||
first += PiecewiseChunkSize();
|
||||
}
|
||||
// Handle the remainder.
|
||||
return CombineContiguousImpl(state, first, len,
|
||||
std::integral_constant<int, 4>{});
|
||||
}
|
||||
|
||||
uint64_t MixingHashState::CombineLargeContiguousImpl64(
|
||||
uint64_t state, const unsigned char* first, size_t len) {
|
||||
while (len >= PiecewiseChunkSize()) {
|
||||
state = Mix(state, Hash64(first, PiecewiseChunkSize()));
|
||||
len -= PiecewiseChunkSize();
|
||||
first += PiecewiseChunkSize();
|
||||
}
|
||||
// Handle the remainder.
|
||||
return CombineContiguousImpl(state, first, len,
|
||||
std::integral_constant<int, 8>{});
|
||||
}
|
||||
|
||||
ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
|
||||
|
||||
// The salt array used by LowLevelHash. This array is NOT the mechanism used to
|
||||
// make absl::Hash non-deterministic between program invocations. See `Seed()`
|
||||
// for that mechanism.
|
||||
//
|
||||
// Any random values are fine. These values are just digits from the decimal
|
||||
// part of pi.
|
||||
// https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number
|
||||
constexpr uint64_t kHashSalt[5] = {
|
||||
uint64_t{0x243F6A8885A308D3}, uint64_t{0x13198A2E03707344},
|
||||
uint64_t{0xA4093822299F31D0}, uint64_t{0x082EFA98EC4E6C89},
|
||||
uint64_t{0x452821E638D01377},
|
||||
};
|
||||
|
||||
uint64_t MixingHashState::LowLevelHashImpl(const unsigned char* data,
|
||||
size_t len) {
|
||||
return LowLevelHash(data, len, Seed(), kHashSalt);
|
||||
}
|
||||
|
||||
} // namespace hash_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
1375
Pods/abseil/absl/hash/internal/hash.h
generated
Normal file
1375
Pods/abseil/absl/hash/internal/hash.h
generated
Normal file
@@ -0,0 +1,1375 @@
|
||||
// Copyright 2018 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: hash.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
#ifndef ABSL_HASH_INTERNAL_HASH_H_
|
||||
#define ABSL_HASH_INTERNAL_HASH_H_
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <Availability.h>
|
||||
#include <TargetConditionals.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bitset>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <forward_list>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/unaligned_access.h"
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/container/fixed_array.h"
|
||||
#include "absl/hash/internal/city.h"
|
||||
#include "absl/hash/internal/low_level_hash.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/numeric/bits.h"
|
||||
#include "absl/numeric/int128.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "absl/utility/utility.h"
|
||||
|
||||
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
|
||||
!defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
|
||||
#include <filesystem> // NOLINT
|
||||
#endif
|
||||
|
||||
#ifdef ABSL_HAVE_STD_STRING_VIEW
|
||||
#include <string_view>
|
||||
#endif
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
class HashState;
|
||||
|
||||
namespace hash_internal {
|
||||
|
||||
// Internal detail: Large buffers are hashed in smaller chunks. This function
|
||||
// returns the size of these chunks.
|
||||
constexpr size_t PiecewiseChunkSize() { return 1024; }
|
||||
|
||||
// PiecewiseCombiner
|
||||
//
|
||||
// PiecewiseCombiner is an internal-only helper class for hashing a piecewise
|
||||
// buffer of `char` or `unsigned char` as though it were contiguous. This class
|
||||
// provides two methods:
|
||||
//
|
||||
// H add_buffer(state, data, size)
|
||||
// H finalize(state)
|
||||
//
|
||||
// `add_buffer` can be called zero or more times, followed by a single call to
|
||||
// `finalize`. This will produce the same hash expansion as concatenating each
|
||||
// buffer piece into a single contiguous buffer, and passing this to
|
||||
// `H::combine_contiguous`.
|
||||
//
|
||||
// Example usage:
|
||||
// PiecewiseCombiner combiner;
|
||||
// for (const auto& piece : pieces) {
|
||||
// state = combiner.add_buffer(std::move(state), piece.data, piece.size);
|
||||
// }
|
||||
// return combiner.finalize(std::move(state));
|
||||
class PiecewiseCombiner {
|
||||
public:
|
||||
PiecewiseCombiner() : position_(0) {}
|
||||
PiecewiseCombiner(const PiecewiseCombiner&) = delete;
|
||||
PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
|
||||
|
||||
// PiecewiseCombiner::add_buffer()
|
||||
//
|
||||
// Appends the given range of bytes to the sequence to be hashed, which may
|
||||
// modify the provided hash state.
|
||||
template <typename H>
|
||||
H add_buffer(H state, const unsigned char* data, size_t size);
|
||||
template <typename H>
|
||||
H add_buffer(H state, const char* data, size_t size) {
|
||||
return add_buffer(std::move(state),
|
||||
reinterpret_cast<const unsigned char*>(data), size);
|
||||
}
|
||||
|
||||
// PiecewiseCombiner::finalize()
|
||||
//
|
||||
// Finishes combining the hash sequence, which may may modify the provided
|
||||
// hash state.
|
||||
//
|
||||
// Once finalize() is called, add_buffer() may no longer be called. The
|
||||
// resulting hash state will be the same as if the pieces passed to
|
||||
// add_buffer() were concatenated into a single flat buffer, and then provided
|
||||
// to H::combine_contiguous().
|
||||
template <typename H>
|
||||
H finalize(H state);
|
||||
|
||||
private:
|
||||
unsigned char buf_[PiecewiseChunkSize()];
|
||||
size_t position_;
|
||||
};
|
||||
|
||||
// is_hashable()
|
||||
//
|
||||
// Trait class which returns true if T is hashable by the absl::Hash framework.
|
||||
// Used for the AbslHashValue implementations for composite types below.
|
||||
template <typename T>
|
||||
struct is_hashable;
|
||||
|
||||
// HashStateBase
|
||||
//
|
||||
// An internal implementation detail that contains common implementation details
|
||||
// for all of the "hash state objects" objects generated by Abseil. This is not
|
||||
// a public API; users should not create classes that inherit from this.
|
||||
//
|
||||
// A hash state object is the template argument `H` passed to `AbslHashValue`.
|
||||
// It represents an intermediate state in the computation of an unspecified hash
|
||||
// algorithm. `HashStateBase` provides a CRTP style base class for hash state
|
||||
// implementations. Developers adding type support for `absl::Hash` should not
|
||||
// rely on any parts of the state object other than the following member
|
||||
// functions:
|
||||
//
|
||||
// * HashStateBase::combine()
|
||||
// * HashStateBase::combine_contiguous()
|
||||
// * HashStateBase::combine_unordered()
|
||||
//
|
||||
// A derived hash state class of type `H` must provide a public member function
|
||||
// with a signature similar to the following:
|
||||
//
|
||||
// `static H combine_contiguous(H state, const unsigned char*, size_t)`.
|
||||
//
|
||||
// It must also provide a private template method named RunCombineUnordered.
|
||||
//
|
||||
// A "consumer" is a 1-arg functor returning void. Its argument is a reference
|
||||
// to an inner hash state object, and it may be called multiple times. When
|
||||
// called, the functor consumes the entropy from the provided state object,
|
||||
// and resets that object to its empty state.
|
||||
//
|
||||
// A "combiner" is a stateless 2-arg functor returning void. Its arguments are
|
||||
// an inner hash state object and an ElementStateConsumer functor. A combiner
|
||||
// uses the provided inner hash state object to hash each element of the
|
||||
// container, passing the inner hash state object to the consumer after hashing
|
||||
// each element.
|
||||
//
|
||||
// Given these definitions, a derived hash state class of type H
|
||||
// must provide a private template method with a signature similar to the
|
||||
// following:
|
||||
//
|
||||
// `template <typename CombinerT>`
|
||||
// `static H RunCombineUnordered(H outer_state, CombinerT combiner)`
|
||||
//
|
||||
// This function is responsible for constructing the inner state object and
|
||||
// providing a consumer to the combiner. It uses side effects of the consumer
|
||||
// and combiner to mix the state of each element in an order-independent manner,
|
||||
// and uses this to return an updated value of `outer_state`.
|
||||
//
|
||||
// This inside-out approach generates efficient object code in the normal case,
|
||||
// but allows us to use stack storage to implement the absl::HashState type
|
||||
// erasure mechanism (avoiding heap allocations while hashing).
|
||||
//
|
||||
// `HashStateBase` will provide a complete implementation for a hash state
|
||||
// object in terms of these two methods.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Use CRTP to define your derived class.
|
||||
// struct MyHashState : HashStateBase<MyHashState> {
|
||||
// static H combine_contiguous(H state, const unsigned char*, size_t);
|
||||
// using MyHashState::HashStateBase::combine;
|
||||
// using MyHashState::HashStateBase::combine_contiguous;
|
||||
// using MyHashState::HashStateBase::combine_unordered;
|
||||
// private:
|
||||
// template <typename CombinerT>
|
||||
// static H RunCombineUnordered(H state, CombinerT combiner);
|
||||
// };
|
||||
template <typename H>
|
||||
class HashStateBase {
|
||||
public:
|
||||
// HashStateBase::combine()
|
||||
//
|
||||
// Combines an arbitrary number of values into a hash state, returning the
|
||||
// updated state.
|
||||
//
|
||||
// Each of the value types `T` must be separately hashable by the Abseil
|
||||
// hashing framework.
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// state = H::combine(std::move(state), value1, value2, value3);
|
||||
//
|
||||
// is guaranteed to produce the same hash expansion as:
|
||||
//
|
||||
// state = H::combine(std::move(state), value1);
|
||||
// state = H::combine(std::move(state), value2);
|
||||
// state = H::combine(std::move(state), value3);
|
||||
template <typename T, typename... Ts>
|
||||
static H combine(H state, const T& value, const Ts&... values);
|
||||
static H combine(H state) { return state; }
|
||||
|
||||
// HashStateBase::combine_contiguous()
|
||||
//
|
||||
// Combines a contiguous array of `size` elements into a hash state, returning
|
||||
// the updated state.
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// state = H::combine_contiguous(std::move(state), data, size);
|
||||
//
|
||||
// is NOT guaranteed to produce the same hash expansion as a for-loop (it may
|
||||
// perform internal optimizations). If you need this guarantee, use the
|
||||
// for-loop instead.
|
||||
template <typename T>
|
||||
static H combine_contiguous(H state, const T* data, size_t size);
|
||||
|
||||
template <typename I>
|
||||
static H combine_unordered(H state, I begin, I end);
|
||||
|
||||
using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
|
||||
|
||||
template <typename T>
|
||||
using is_hashable = absl::hash_internal::is_hashable<T>;
|
||||
|
||||
private:
|
||||
// Common implementation of the iteration step of a "combiner", as described
|
||||
// above.
|
||||
template <typename I>
|
||||
struct CombineUnorderedCallback {
|
||||
I begin;
|
||||
I end;
|
||||
|
||||
template <typename InnerH, typename ElementStateConsumer>
|
||||
void operator()(InnerH inner_state, ElementStateConsumer cb) {
|
||||
for (; begin != end; ++begin) {
|
||||
inner_state = H::combine(std::move(inner_state), *begin);
|
||||
cb(inner_state);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// is_uniquely_represented
|
||||
//
|
||||
// `is_uniquely_represented<T>` is a trait class that indicates whether `T`
|
||||
// is uniquely represented.
|
||||
//
|
||||
// A type is "uniquely represented" if two equal values of that type are
|
||||
// guaranteed to have the same bytes in their underlying storage. In other
|
||||
// words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
|
||||
// zero. This property cannot be detected automatically, so this trait is false
|
||||
// by default, but can be specialized by types that wish to assert that they are
|
||||
// uniquely represented. This makes them eligible for certain optimizations.
|
||||
//
|
||||
// If you have any doubt whatsoever, do not specialize this template.
|
||||
// The default is completely safe, and merely disables some optimizations
|
||||
// that will not matter for most types. Specializing this template,
|
||||
// on the other hand, can be very hazardous.
|
||||
//
|
||||
// To be uniquely represented, a type must not have multiple ways of
|
||||
// representing the same value; for example, float and double are not
|
||||
// uniquely represented, because they have distinct representations for
|
||||
// +0 and -0. Furthermore, the type's byte representation must consist
|
||||
// solely of user-controlled data, with no padding bits and no compiler-
|
||||
// controlled data such as vptrs or sanitizer metadata. This is usually
|
||||
// very difficult to guarantee, because in most cases the compiler can
|
||||
// insert data and padding bits at its own discretion.
|
||||
//
|
||||
// If you specialize this template for a type `T`, you must do so in the file
|
||||
// that defines that type (or in this file). If you define that specialization
|
||||
// anywhere else, `is_uniquely_represented<T>` could have different meanings
|
||||
// in different places.
|
||||
//
|
||||
// The Enable parameter is meaningless; it is provided as a convenience,
|
||||
// to support certain SFINAE techniques when defining specializations.
|
||||
template <typename T, typename Enable = void>
|
||||
struct is_uniquely_represented : std::false_type {};
|
||||
|
||||
// is_uniquely_represented<unsigned char>
|
||||
//
|
||||
// unsigned char is a synonym for "byte", so it is guaranteed to be
|
||||
// uniquely represented.
|
||||
template <>
|
||||
struct is_uniquely_represented<unsigned char> : std::true_type {};
|
||||
|
||||
// is_uniquely_represented for non-standard integral types
|
||||
//
|
||||
// Integral types other than bool should be uniquely represented on any
|
||||
// platform that this will plausibly be ported to.
|
||||
template <typename Integral>
|
||||
struct is_uniquely_represented<
|
||||
Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
|
||||
: std::true_type {};
|
||||
|
||||
// is_uniquely_represented<bool>
|
||||
//
|
||||
//
|
||||
template <>
|
||||
struct is_uniquely_represented<bool> : std::false_type {};
|
||||
|
||||
// hash_bytes()
|
||||
//
|
||||
// Convenience function that combines `hash_state` with the byte representation
|
||||
// of `value`.
|
||||
template <typename H, typename T>
|
||||
H hash_bytes(H hash_state, const T& value) {
|
||||
const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
|
||||
return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Basic Types
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Note: Default `AbslHashValue` implementations live in `hash_internal`. This
|
||||
// allows us to block lexical scope lookup when doing an unqualified call to
|
||||
// `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
|
||||
// only be found via ADL.
|
||||
|
||||
// AbslHashValue() for hashing bool values
|
||||
//
|
||||
// We use SFINAE to ensure that this overload only accepts bool, not types that
|
||||
// are convertible to bool.
|
||||
template <typename H, typename B>
|
||||
typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
|
||||
H hash_state, B value) {
|
||||
return H::combine(std::move(hash_state),
|
||||
static_cast<unsigned char>(value ? 1 : 0));
|
||||
}
|
||||
|
||||
// AbslHashValue() for hashing enum values
|
||||
template <typename H, typename Enum>
|
||||
typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
|
||||
H hash_state, Enum e) {
|
||||
// In practice, we could almost certainly just invoke hash_bytes directly,
|
||||
// but it's possible that a sanitizer might one day want to
|
||||
// store data in the unused bits of an enum. To avoid that risk, we
|
||||
// convert to the underlying type before hashing. Hopefully this will get
|
||||
// optimized away; if not, we can reopen discussion with c-toolchain-team.
|
||||
return H::combine(std::move(hash_state),
|
||||
static_cast<typename std::underlying_type<Enum>::type>(e));
|
||||
}
|
||||
// AbslHashValue() for hashing floating-point values
|
||||
template <typename H, typename Float>
|
||||
typename std::enable_if<std::is_same<Float, float>::value ||
|
||||
std::is_same<Float, double>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state, Float value) {
|
||||
return hash_internal::hash_bytes(std::move(hash_state),
|
||||
value == 0 ? 0 : value);
|
||||
}
|
||||
|
||||
// Long double has the property that it might have extra unused bytes in it.
|
||||
// For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
|
||||
// of it. This means we can't use hash_bytes on a long double and have to
|
||||
// convert it to something else first.
|
||||
template <typename H, typename LongDouble>
|
||||
typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
|
||||
AbslHashValue(H hash_state, LongDouble value) {
|
||||
const int category = std::fpclassify(value);
|
||||
switch (category) {
|
||||
case FP_INFINITE:
|
||||
// Add the sign bit to differentiate between +Inf and -Inf
|
||||
hash_state = H::combine(std::move(hash_state), std::signbit(value));
|
||||
break;
|
||||
|
||||
case FP_NAN:
|
||||
case FP_ZERO:
|
||||
default:
|
||||
// Category is enough for these.
|
||||
break;
|
||||
|
||||
case FP_NORMAL:
|
||||
case FP_SUBNORMAL:
|
||||
// We can't convert `value` directly to double because this would have
|
||||
// undefined behavior if the value is out of range.
|
||||
// std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
|
||||
// guaranteed to be in range for `double`. The truncation is
|
||||
// implementation defined, but that works as long as it is deterministic.
|
||||
int exp;
|
||||
auto mantissa = static_cast<double>(std::frexp(value, &exp));
|
||||
hash_state = H::combine(std::move(hash_state), mantissa, exp);
|
||||
}
|
||||
|
||||
return H::combine(std::move(hash_state), category);
|
||||
}
|
||||
|
||||
// Without this overload, an array decays to a pointer and we hash that, which
|
||||
// is not likely to be what the caller intended.
|
||||
template <typename H, typename T, size_t N>
|
||||
H AbslHashValue(H hash_state, T (&)[N]) {
|
||||
static_assert(
|
||||
sizeof(T) == -1,
|
||||
"Hashing C arrays is not allowed. For string literals, wrap the literal "
|
||||
"in absl::string_view(). To hash the array contents, use "
|
||||
"absl::MakeSpan() or make the array an std::array. To hash the array "
|
||||
"address, use &array[0].");
|
||||
return hash_state;
|
||||
}
|
||||
|
||||
// AbslHashValue() for hashing pointers
|
||||
template <typename H, typename T>
|
||||
std::enable_if_t<std::is_pointer<T>::value, H> AbslHashValue(H hash_state,
|
||||
T ptr) {
|
||||
auto v = reinterpret_cast<uintptr_t>(ptr);
|
||||
// Due to alignment, pointers tend to have low bits as zero, and the next few
|
||||
// bits follow a pattern since they are also multiples of some base value.
|
||||
// Mixing the pointer twice helps prevent stuck low bits for certain alignment
|
||||
// values.
|
||||
return H::combine(std::move(hash_state), v, v);
|
||||
}
|
||||
|
||||
// AbslHashValue() for hashing nullptr_t
|
||||
template <typename H>
|
||||
H AbslHashValue(H hash_state, std::nullptr_t) {
|
||||
return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
|
||||
}
|
||||
|
||||
// AbslHashValue() for hashing pointers-to-member
|
||||
template <typename H, typename T, typename C>
|
||||
H AbslHashValue(H hash_state, T C::*ptr) {
|
||||
auto salient_ptm_size = [](std::size_t n) -> std::size_t {
|
||||
#if defined(_MSC_VER)
|
||||
// Pointers-to-member-function on MSVC consist of one pointer plus 0, 1, 2,
|
||||
// or 3 ints. In 64-bit mode, they are 8-byte aligned and thus can contain
|
||||
// padding (namely when they have 1 or 3 ints). The value below is a lower
|
||||
// bound on the number of salient, non-padding bytes that we use for
|
||||
// hashing.
|
||||
if (alignof(T C::*) == alignof(int)) {
|
||||
// No padding when all subobjects have the same size as the total
|
||||
// alignment. This happens in 32-bit mode.
|
||||
return n;
|
||||
} else {
|
||||
// Padding for 1 int (size 16) or 3 ints (size 24).
|
||||
// With 2 ints, the size is 16 with no padding, which we pessimize.
|
||||
return n == 24 ? 20 : n == 16 ? 12 : n;
|
||||
}
|
||||
#else
|
||||
// On other platforms, we assume that pointers-to-members do not have
|
||||
// padding.
|
||||
#ifdef __cpp_lib_has_unique_object_representations
|
||||
static_assert(std::has_unique_object_representations<T C::*>::value);
|
||||
#endif // __cpp_lib_has_unique_object_representations
|
||||
return n;
|
||||
#endif
|
||||
};
|
||||
return H::combine_contiguous(std::move(hash_state),
|
||||
reinterpret_cast<unsigned char*>(&ptr),
|
||||
salient_ptm_size(sizeof ptr));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Composite Types
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue() for hashing pairs
|
||||
template <typename H, typename T1, typename T2>
|
||||
typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
|
||||
return H::combine(std::move(hash_state), p.first, p.second);
|
||||
}
|
||||
|
||||
// hash_tuple()
|
||||
//
|
||||
// Helper function for hashing a tuple. The third argument should
|
||||
// be an index_sequence running from 0 to tuple_size<Tuple> - 1.
|
||||
template <typename H, typename Tuple, size_t... Is>
|
||||
H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
|
||||
return H::combine(std::move(hash_state), std::get<Is>(t)...);
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing tuples
|
||||
template <typename H, typename... Ts>
|
||||
#if defined(_MSC_VER)
|
||||
// This SFINAE gets MSVC confused under some conditions. Let's just disable it
|
||||
// for now.
|
||||
H
|
||||
#else // _MSC_VER
|
||||
typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
|
||||
#endif // _MSC_VER
|
||||
AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
|
||||
return hash_internal::hash_tuple(std::move(hash_state), t,
|
||||
absl::make_index_sequence<sizeof...(Ts)>());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Pointers
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing unique_ptr
|
||||
template <typename H, typename T, typename D>
|
||||
H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
|
||||
return H::combine(std::move(hash_state), ptr.get());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing shared_ptr
|
||||
template <typename H, typename T>
|
||||
H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
|
||||
return H::combine(std::move(hash_state), ptr.get());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for String-Like Types
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing strings
|
||||
//
|
||||
// All the string-like types supported here provide the same hash expansion for
|
||||
// the same character sequence. These types are:
|
||||
//
|
||||
// - `absl::Cord`
|
||||
// - `std::string` (and std::basic_string<T, std::char_traits<T>, A> for
|
||||
// any allocator A and any T in {char, wchar_t, char16_t, char32_t})
|
||||
// - `absl::string_view`, `std::string_view`, `std::wstring_view`,
|
||||
// `std::u16string_view`, and `std::u32_string_view`.
|
||||
//
|
||||
// For simplicity, we currently support only strings built on `char`, `wchar_t`,
|
||||
// `char16_t`, or `char32_t`. This support may be broadened, if necessary, but
|
||||
// with some caution - this overload would misbehave in cases where the traits'
|
||||
// `eq()` member isn't equivalent to `==` on the underlying character type.
|
||||
template <typename H>
|
||||
H AbslHashValue(H hash_state, absl::string_view str) {
|
||||
return H::combine(
|
||||
H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
|
||||
str.size());
|
||||
}
|
||||
|
||||
// Support std::wstring, std::u16string and std::u32string.
|
||||
template <typename Char, typename Alloc, typename H,
|
||||
typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
|
||||
std::is_same<Char, char16_t>::value ||
|
||||
std::is_same<Char, char32_t>::value>>
|
||||
H AbslHashValue(
|
||||
H hash_state,
|
||||
const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
|
||||
return H::combine(
|
||||
H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
|
||||
str.size());
|
||||
}
|
||||
|
||||
#ifdef ABSL_HAVE_STD_STRING_VIEW
|
||||
|
||||
// Support std::wstring_view, std::u16string_view and std::u32string_view.
|
||||
template <typename Char, typename H,
|
||||
typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
|
||||
std::is_same<Char, char16_t>::value ||
|
||||
std::is_same<Char, char32_t>::value>>
|
||||
H AbslHashValue(H hash_state, std::basic_string_view<Char> str) {
|
||||
return H::combine(
|
||||
H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
|
||||
str.size());
|
||||
}
|
||||
|
||||
#endif // ABSL_HAVE_STD_STRING_VIEW
|
||||
|
||||
#if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
|
||||
!defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) && \
|
||||
(!defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) || \
|
||||
__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 130000)
|
||||
|
||||
#define ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE 1
|
||||
|
||||
// Support std::filesystem::path. The SFINAE is required because some string
|
||||
// types are implicitly convertible to std::filesystem::path.
|
||||
template <typename Path, typename H,
|
||||
typename = absl::enable_if_t<
|
||||
std::is_same_v<Path, std::filesystem::path>>>
|
||||
H AbslHashValue(H hash_state, const Path& path) {
|
||||
// This is implemented by deferring to the standard library to compute the
|
||||
// hash. The standard library requires that for two paths, `p1 == p2`, then
|
||||
// `hash_value(p1) == hash_value(p2)`. `AbslHashValue` has the same
|
||||
// requirement. Since `operator==` does platform specific matching, deferring
|
||||
// to the standard library is the simplest approach.
|
||||
return H::combine(std::move(hash_state), std::filesystem::hash_value(path));
|
||||
}
|
||||
|
||||
#endif // ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Sequence Containers
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing std::array
|
||||
template <typename H, typename T, size_t N>
|
||||
typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::array<T, N>& array) {
|
||||
return H::combine_contiguous(std::move(hash_state), array.data(),
|
||||
array.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::deque
|
||||
template <typename H, typename T, typename Allocator>
|
||||
typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::deque<T, Allocator>& deque) {
|
||||
// TODO(gromer): investigate a more efficient implementation taking
|
||||
// advantage of the chunk structure.
|
||||
for (const auto& t : deque) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
}
|
||||
return H::combine(std::move(hash_state), deque.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::forward_list
|
||||
template <typename H, typename T, typename Allocator>
|
||||
typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::forward_list<T, Allocator>& list) {
|
||||
size_t size = 0;
|
||||
for (const T& t : list) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
++size;
|
||||
}
|
||||
return H::combine(std::move(hash_state), size);
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::list
|
||||
template <typename H, typename T, typename Allocator>
|
||||
typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::list<T, Allocator>& list) {
|
||||
for (const auto& t : list) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
}
|
||||
return H::combine(std::move(hash_state), list.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::vector
|
||||
//
|
||||
// Do not use this for vector<bool> on platforms that have a working
|
||||
// implementation of std::hash. It does not have a .data(), and a fallback for
|
||||
// std::hash<> is most likely faster.
|
||||
template <typename H, typename T, typename Allocator>
|
||||
typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
|
||||
return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
|
||||
vector.size()),
|
||||
vector.size());
|
||||
}
|
||||
|
||||
// AbslHashValue special cases for hashing std::vector<bool>
|
||||
|
||||
#if defined(ABSL_IS_BIG_ENDIAN) && \
|
||||
(defined(__GLIBCXX__) || defined(__GLIBCPP__))
|
||||
|
||||
// std::hash in libstdc++ does not work correctly with vector<bool> on Big
|
||||
// Endian platforms therefore we need to implement a custom AbslHashValue for
|
||||
// it. More details on the bug:
|
||||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
|
||||
template <typename H, typename T, typename Allocator>
|
||||
typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
|
||||
typename H::AbslInternalPiecewiseCombiner combiner;
|
||||
for (const auto& i : vector) {
|
||||
unsigned char c = static_cast<unsigned char>(i);
|
||||
hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
|
||||
}
|
||||
return H::combine(combiner.finalize(std::move(hash_state)), vector.size());
|
||||
}
|
||||
#else
|
||||
// When not working around the libstdc++ bug above, we still have to contend
|
||||
// with the fact that std::hash<vector<bool>> is often poor quality, hashing
|
||||
// directly on the internal words and on no other state. On these platforms,
|
||||
// vector<bool>{1, 1} and vector<bool>{1, 1, 0} hash to the same value.
|
||||
//
|
||||
// Mixing in the size (as we do in our other vector<> implementations) on top
|
||||
// of the library-provided hash implementation avoids this QOI issue.
|
||||
template <typename H, typename T, typename Allocator>
|
||||
typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
|
||||
return H::combine(std::move(hash_state),
|
||||
std::hash<std::vector<T, Allocator>>{}(vector),
|
||||
vector.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Ordered Associative Containers
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing std::map
|
||||
template <typename H, typename Key, typename T, typename Compare,
|
||||
typename Allocator>
|
||||
typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
|
||||
for (const auto& t : map) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
}
|
||||
return H::combine(std::move(hash_state), map.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::multimap
|
||||
template <typename H, typename Key, typename T, typename Compare,
|
||||
typename Allocator>
|
||||
typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state,
|
||||
const std::multimap<Key, T, Compare, Allocator>& map) {
|
||||
for (const auto& t : map) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
}
|
||||
return H::combine(std::move(hash_state), map.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::set
|
||||
template <typename H, typename Key, typename Compare, typename Allocator>
|
||||
typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::set<Key, Compare, Allocator>& set) {
|
||||
for (const auto& t : set) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
}
|
||||
return H::combine(std::move(hash_state), set.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::multiset
|
||||
template <typename H, typename Key, typename Compare, typename Allocator>
|
||||
typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
|
||||
for (const auto& t : set) {
|
||||
hash_state = H::combine(std::move(hash_state), t);
|
||||
}
|
||||
return H::combine(std::move(hash_state), set.size());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Unordered Associative Containers
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing std::unordered_set
|
||||
template <typename H, typename Key, typename Hash, typename KeyEqual,
|
||||
typename Alloc>
|
||||
typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
|
||||
H hash_state, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s) {
|
||||
return H::combine(
|
||||
H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
|
||||
s.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::unordered_multiset
|
||||
template <typename H, typename Key, typename Hash, typename KeyEqual,
|
||||
typename Alloc>
|
||||
typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
|
||||
H hash_state,
|
||||
const std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& s) {
|
||||
return H::combine(
|
||||
H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
|
||||
s.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::unordered_set
|
||||
template <typename H, typename Key, typename T, typename Hash,
|
||||
typename KeyEqual, typename Alloc>
|
||||
typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state,
|
||||
const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& s) {
|
||||
return H::combine(
|
||||
H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
|
||||
s.size());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing std::unordered_multiset
|
||||
template <typename H, typename Key, typename T, typename Hash,
|
||||
typename KeyEqual, typename Alloc>
|
||||
typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
|
||||
H>::type
|
||||
AbslHashValue(H hash_state,
|
||||
const std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& s) {
|
||||
return H::combine(
|
||||
H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
|
||||
s.size());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Wrapper Types
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing std::reference_wrapper
|
||||
template <typename H, typename T>
|
||||
typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
|
||||
H hash_state, std::reference_wrapper<T> opt) {
|
||||
return H::combine(std::move(hash_state), opt.get());
|
||||
}
|
||||
|
||||
// AbslHashValue for hashing absl::optional
|
||||
template <typename H, typename T>
|
||||
typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
|
||||
H hash_state, const absl::optional<T>& opt) {
|
||||
if (opt) hash_state = H::combine(std::move(hash_state), *opt);
|
||||
return H::combine(std::move(hash_state), opt.has_value());
|
||||
}
|
||||
|
||||
// VariantVisitor
|
||||
template <typename H>
|
||||
struct VariantVisitor {
|
||||
H&& hash_state;
|
||||
template <typename T>
|
||||
H operator()(const T& t) const {
|
||||
return H::combine(std::move(hash_state), t);
|
||||
}
|
||||
};
|
||||
|
||||
// AbslHashValue for hashing absl::variant
|
||||
template <typename H, typename... T>
|
||||
typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
|
||||
AbslHashValue(H hash_state, const absl::variant<T...>& v) {
|
||||
if (!v.valueless_by_exception()) {
|
||||
hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
|
||||
}
|
||||
return H::combine(std::move(hash_state), v.index());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// AbslHashValue for Other Types
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// AbslHashValue for hashing std::bitset is not defined on Little Endian
|
||||
// platforms, for the same reason as for vector<bool> (see std::vector above):
|
||||
// It does not expose the raw bytes, and a fallback to std::hash<> is most
|
||||
// likely faster.
|
||||
|
||||
#if defined(ABSL_IS_BIG_ENDIAN) && \
|
||||
(defined(__GLIBCXX__) || defined(__GLIBCPP__))
|
||||
// AbslHashValue for hashing std::bitset
|
||||
//
|
||||
// std::hash in libstdc++ does not work correctly with std::bitset on Big Endian
|
||||
// platforms therefore we need to implement a custom AbslHashValue for it. More
|
||||
// details on the bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
|
||||
template <typename H, size_t N>
|
||||
H AbslHashValue(H hash_state, const std::bitset<N>& set) {
|
||||
typename H::AbslInternalPiecewiseCombiner combiner;
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
unsigned char c = static_cast<unsigned char>(set[i]);
|
||||
hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
|
||||
}
|
||||
return H::combine(combiner.finalize(std::move(hash_state)), N);
|
||||
}
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// hash_range_or_bytes()
|
||||
//
|
||||
// Mixes all values in the range [data, data+size) into the hash state.
|
||||
// This overload accepts only uniquely-represented types, and hashes them by
|
||||
// hashing the entire range of bytes.
|
||||
template <typename H, typename T>
|
||||
typename std::enable_if<is_uniquely_represented<T>::value, H>::type
|
||||
hash_range_or_bytes(H hash_state, const T* data, size_t size) {
|
||||
const auto* bytes = reinterpret_cast<const unsigned char*>(data);
|
||||
return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
|
||||
}
|
||||
|
||||
// hash_range_or_bytes()
|
||||
template <typename H, typename T>
|
||||
typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
|
||||
hash_range_or_bytes(H hash_state, const T* data, size_t size) {
|
||||
for (const auto end = data + size; data < end; ++data) {
|
||||
hash_state = H::combine(std::move(hash_state), *data);
|
||||
}
|
||||
return hash_state;
|
||||
}
|
||||
|
||||
#if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
|
||||
ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
|
||||
#define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
|
||||
#else
|
||||
#define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
|
||||
#endif
|
||||
|
||||
// HashSelect
|
||||
//
|
||||
// Type trait to select the appropriate hash implementation to use.
|
||||
// HashSelect::type<T> will give the proper hash implementation, to be invoked
|
||||
// as:
|
||||
// HashSelect::type<T>::Invoke(state, value)
|
||||
// Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
|
||||
// valid `Invoke` function. Types that are not hashable will have a ::value of
|
||||
// `false`.
|
||||
struct HashSelect {
|
||||
private:
|
||||
struct State : HashStateBase<State> {
|
||||
static State combine_contiguous(State hash_state, const unsigned char*,
|
||||
size_t);
|
||||
using State::HashStateBase::combine_contiguous;
|
||||
};
|
||||
|
||||
struct UniquelyRepresentedProbe {
|
||||
template <typename H, typename T>
|
||||
static auto Invoke(H state, const T& value)
|
||||
-> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
|
||||
return hash_internal::hash_bytes(std::move(state), value);
|
||||
}
|
||||
};
|
||||
|
||||
struct HashValueProbe {
|
||||
template <typename H, typename T>
|
||||
static auto Invoke(H state, const T& value) -> absl::enable_if_t<
|
||||
std::is_same<H,
|
||||
decltype(AbslHashValue(std::move(state), value))>::value,
|
||||
H> {
|
||||
return AbslHashValue(std::move(state), value);
|
||||
}
|
||||
};
|
||||
|
||||
struct LegacyHashProbe {
|
||||
#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
|
||||
template <typename H, typename T>
|
||||
static auto Invoke(H state, const T& value) -> absl::enable_if_t<
|
||||
std::is_convertible<
|
||||
decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
|
||||
size_t>::value,
|
||||
H> {
|
||||
return hash_internal::hash_bytes(
|
||||
std::move(state),
|
||||
ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
|
||||
}
|
||||
#endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
|
||||
};
|
||||
|
||||
struct StdHashProbe {
|
||||
template <typename H, typename T>
|
||||
static auto Invoke(H state, const T& value)
|
||||
-> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
|
||||
return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Hash, typename T>
|
||||
struct Probe : Hash {
|
||||
private:
|
||||
template <typename H, typename = decltype(H::Invoke(
|
||||
std::declval<State>(), std::declval<const T&>()))>
|
||||
static std::true_type Test(int);
|
||||
template <typename U>
|
||||
static std::false_type Test(char);
|
||||
|
||||
public:
|
||||
static constexpr bool value = decltype(Test<Hash>(0))::value;
|
||||
};
|
||||
|
||||
public:
|
||||
// Probe each implementation in order.
|
||||
// disjunction provides short circuiting wrt instantiation.
|
||||
template <typename T>
|
||||
using Apply = absl::disjunction< //
|
||||
Probe<UniquelyRepresentedProbe, T>, //
|
||||
Probe<HashValueProbe, T>, //
|
||||
Probe<LegacyHashProbe, T>, //
|
||||
Probe<StdHashProbe, T>, //
|
||||
std::false_type>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_hashable
|
||||
: std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
|
||||
|
||||
// MixingHashState
|
||||
class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
|
||||
// absl::uint128 is not an alias or a thin wrapper around the intrinsic.
|
||||
// We use the intrinsic when available to improve performance.
|
||||
#ifdef ABSL_HAVE_INTRINSIC_INT128
|
||||
using uint128 = __uint128_t;
|
||||
#else // ABSL_HAVE_INTRINSIC_INT128
|
||||
using uint128 = absl::uint128;
|
||||
#endif // ABSL_HAVE_INTRINSIC_INT128
|
||||
|
||||
static constexpr uint64_t kMul =
|
||||
sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51}
|
||||
: uint64_t{0x9ddfea08eb382d69};
|
||||
|
||||
template <typename T>
|
||||
using IntegralFastPath =
|
||||
conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
|
||||
|
||||
public:
|
||||
// Move only
|
||||
MixingHashState(MixingHashState&&) = default;
|
||||
MixingHashState& operator=(MixingHashState&&) = default;
|
||||
|
||||
// MixingHashState::combine_contiguous()
|
||||
//
|
||||
// Fundamental base case for hash recursion: mixes the given range of bytes
|
||||
// into the hash state.
|
||||
static MixingHashState combine_contiguous(MixingHashState hash_state,
|
||||
const unsigned char* first,
|
||||
size_t size) {
|
||||
return MixingHashState(
|
||||
CombineContiguousImpl(hash_state.state_, first, size,
|
||||
std::integral_constant<int, sizeof(size_t)>{}));
|
||||
}
|
||||
using MixingHashState::HashStateBase::combine_contiguous;
|
||||
|
||||
// MixingHashState::hash()
|
||||
//
|
||||
// For performance reasons in non-opt mode, we specialize this for
|
||||
// integral types.
|
||||
// Otherwise we would be instantiating and calling dozens of functions for
|
||||
// something that is just one multiplication and a couple xor's.
|
||||
// The result should be the same as running the whole algorithm, but faster.
|
||||
template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
|
||||
static size_t hash(T value) {
|
||||
return static_cast<size_t>(
|
||||
Mix(Seed(), static_cast<std::make_unsigned_t<T>>(value)));
|
||||
}
|
||||
|
||||
// Overload of MixingHashState::hash()
|
||||
template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
|
||||
static size_t hash(const T& value) {
|
||||
return static_cast<size_t>(combine(MixingHashState{}, value).state_);
|
||||
}
|
||||
|
||||
private:
|
||||
// Invoked only once for a given argument; that plus the fact that this is
|
||||
// move-only ensures that there is only one non-moved-from object.
|
||||
MixingHashState() : state_(Seed()) {}
|
||||
|
||||
friend class MixingHashState::HashStateBase;
|
||||
|
||||
template <typename CombinerT>
|
||||
static MixingHashState RunCombineUnordered(MixingHashState state,
|
||||
CombinerT combiner) {
|
||||
uint64_t unordered_state = 0;
|
||||
combiner(MixingHashState{}, [&](MixingHashState& inner_state) {
|
||||
// Add the hash state of the element to the running total, but mix the
|
||||
// carry bit back into the low bit. This in intended to avoid losing
|
||||
// entropy to overflow, especially when unordered_multisets contain
|
||||
// multiple copies of the same value.
|
||||
auto element_state = inner_state.state_;
|
||||
unordered_state += element_state;
|
||||
if (unordered_state < element_state) {
|
||||
++unordered_state;
|
||||
}
|
||||
inner_state = MixingHashState{};
|
||||
});
|
||||
return MixingHashState::combine(std::move(state), unordered_state);
|
||||
}
|
||||
|
||||
// Allow the HashState type-erasure implementation to invoke
|
||||
// RunCombinedUnordered() directly.
|
||||
friend class absl::HashState;
|
||||
|
||||
// Workaround for MSVC bug.
|
||||
// We make the type copyable to fix the calling convention, even though we
|
||||
// never actually copy it. Keep it private to not affect the public API of the
|
||||
// type.
|
||||
MixingHashState(const MixingHashState&) = default;
|
||||
|
||||
explicit MixingHashState(uint64_t state) : state_(state) {}
|
||||
|
||||
// Implementation of the base case for combine_contiguous where we actually
|
||||
// mix the bytes into the state.
|
||||
// Dispatch to different implementations of the combine_contiguous depending
|
||||
// on the value of `sizeof(size_t)`.
|
||||
static uint64_t CombineContiguousImpl(uint64_t state,
|
||||
const unsigned char* first, size_t len,
|
||||
std::integral_constant<int, 4>
|
||||
/* sizeof_size_t */);
|
||||
static uint64_t CombineContiguousImpl(uint64_t state,
|
||||
const unsigned char* first, size_t len,
|
||||
std::integral_constant<int, 8>
|
||||
/* sizeof_size_t */);
|
||||
|
||||
// Slow dispatch path for calls to CombineContiguousImpl with a size argument
|
||||
// larger than PiecewiseChunkSize(). Has the same effect as calling
|
||||
// CombineContiguousImpl() repeatedly with the chunk stride size.
|
||||
static uint64_t CombineLargeContiguousImpl32(uint64_t state,
|
||||
const unsigned char* first,
|
||||
size_t len);
|
||||
static uint64_t CombineLargeContiguousImpl64(uint64_t state,
|
||||
const unsigned char* first,
|
||||
size_t len);
|
||||
|
||||
// Reads 9 to 16 bytes from p.
|
||||
// The least significant 8 bytes are in .first, the rest (zero padded) bytes
|
||||
// are in .second.
|
||||
static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
|
||||
size_t len) {
|
||||
uint64_t low_mem = absl::base_internal::UnalignedLoad64(p);
|
||||
uint64_t high_mem = absl::base_internal::UnalignedLoad64(p + len - 8);
|
||||
#ifdef ABSL_IS_LITTLE_ENDIAN
|
||||
uint64_t most_significant = high_mem;
|
||||
uint64_t least_significant = low_mem;
|
||||
#else
|
||||
uint64_t most_significant = low_mem;
|
||||
uint64_t least_significant = high_mem;
|
||||
#endif
|
||||
return {least_significant, most_significant};
|
||||
}
|
||||
|
||||
// Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
|
||||
static uint64_t Read4To8(const unsigned char* p, size_t len) {
|
||||
uint32_t low_mem = absl::base_internal::UnalignedLoad32(p);
|
||||
uint32_t high_mem = absl::base_internal::UnalignedLoad32(p + len - 4);
|
||||
#ifdef ABSL_IS_LITTLE_ENDIAN
|
||||
uint32_t most_significant = high_mem;
|
||||
uint32_t least_significant = low_mem;
|
||||
#else
|
||||
uint32_t most_significant = low_mem;
|
||||
uint32_t least_significant = high_mem;
|
||||
#endif
|
||||
return (static_cast<uint64_t>(most_significant) << (len - 4) * 8) |
|
||||
least_significant;
|
||||
}
|
||||
|
||||
// Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
|
||||
static uint32_t Read1To3(const unsigned char* p, size_t len) {
|
||||
// The trick used by this implementation is to avoid branches if possible.
|
||||
unsigned char mem0 = p[0];
|
||||
unsigned char mem1 = p[len / 2];
|
||||
unsigned char mem2 = p[len - 1];
|
||||
#ifdef ABSL_IS_LITTLE_ENDIAN
|
||||
unsigned char significant2 = mem2;
|
||||
unsigned char significant1 = mem1;
|
||||
unsigned char significant0 = mem0;
|
||||
#else
|
||||
unsigned char significant2 = mem0;
|
||||
unsigned char significant1 = len == 2 ? mem0 : mem1;
|
||||
unsigned char significant0 = mem2;
|
||||
#endif
|
||||
return static_cast<uint32_t>(significant0 | //
|
||||
(significant1 << (len / 2 * 8)) | //
|
||||
(significant2 << ((len - 1) * 8)));
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
|
||||
// Though the 128-bit product on AArch64 needs two instructions, it is
|
||||
// still a good balance between speed and hash quality.
|
||||
using MultType =
|
||||
absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
|
||||
// We do the addition in 64-bit space to make sure the 128-bit
|
||||
// multiplication is fast. If we were to do it as MultType the compiler has
|
||||
// to assume that the high word is non-zero and needs to perform 2
|
||||
// multiplications instead of one.
|
||||
MultType m = state + v;
|
||||
m *= kMul;
|
||||
return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
|
||||
}
|
||||
|
||||
// An extern to avoid bloat on a direct call to LowLevelHash() with fixed
|
||||
// values for both the seed and salt parameters.
|
||||
static uint64_t LowLevelHashImpl(const unsigned char* data, size_t len);
|
||||
|
||||
ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Hash64(const unsigned char* data,
|
||||
size_t len) {
|
||||
#ifdef ABSL_HAVE_INTRINSIC_INT128
|
||||
return LowLevelHashImpl(data, len);
|
||||
#else
|
||||
return hash_internal::CityHash64(reinterpret_cast<const char*>(data), len);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Seed()
|
||||
//
|
||||
// A non-deterministic seed.
|
||||
//
|
||||
// The current purpose of this seed is to generate non-deterministic results
|
||||
// and prevent having users depend on the particular hash values.
|
||||
// It is not meant as a security feature right now, but it leaves the door
|
||||
// open to upgrade it to a true per-process random seed. A true random seed
|
||||
// costs more and we don't need to pay for that right now.
|
||||
//
|
||||
// On platforms with ASLR, we take advantage of it to make a per-process
|
||||
// random value.
|
||||
// See https://en.wikipedia.org/wiki/Address_space_layout_randomization
|
||||
//
|
||||
// On other platforms this is still going to be non-deterministic but most
|
||||
// probably per-build and not per-process.
|
||||
ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
|
||||
#if (!defined(__clang__) || __clang_major__ > 11) && \
|
||||
(!defined(__apple_build_version__) || \
|
||||
__apple_build_version__ >= 19558921) // Xcode 12
|
||||
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&kSeed));
|
||||
#else
|
||||
// Workaround the absence of
|
||||
// https://github.com/llvm/llvm-project/commit/bc15bf66dcca76cc06fe71fca35b74dc4d521021.
|
||||
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
|
||||
#endif
|
||||
}
|
||||
static const void* const kSeed;
|
||||
|
||||
uint64_t state_;
|
||||
};
|
||||
|
||||
// MixingHashState::CombineContiguousImpl()
|
||||
inline uint64_t MixingHashState::CombineContiguousImpl(
|
||||
uint64_t state, const unsigned char* first, size_t len,
|
||||
std::integral_constant<int, 4> /* sizeof_size_t */) {
|
||||
// For large values we use CityHash, for small ones we just use a
|
||||
// multiplicative hash.
|
||||
uint64_t v;
|
||||
if (len > 8) {
|
||||
if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
|
||||
return CombineLargeContiguousImpl32(state, first, len);
|
||||
}
|
||||
v = hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
|
||||
} else if (len >= 4) {
|
||||
v = Read4To8(first, len);
|
||||
} else if (len > 0) {
|
||||
v = Read1To3(first, len);
|
||||
} else {
|
||||
// Empty ranges have no effect.
|
||||
return state;
|
||||
}
|
||||
return Mix(state, v);
|
||||
}
|
||||
|
||||
// Overload of MixingHashState::CombineContiguousImpl()
|
||||
inline uint64_t MixingHashState::CombineContiguousImpl(
|
||||
uint64_t state, const unsigned char* first, size_t len,
|
||||
std::integral_constant<int, 8> /* sizeof_size_t */) {
|
||||
// For large values we use LowLevelHash or CityHash depending on the platform,
|
||||
// for small ones we just use a multiplicative hash.
|
||||
uint64_t v;
|
||||
if (len > 16) {
|
||||
if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
|
||||
return CombineLargeContiguousImpl64(state, first, len);
|
||||
}
|
||||
v = Hash64(first, len);
|
||||
} else if (len > 8) {
|
||||
// This hash function was constructed by the ML-driven algorithm discovery
|
||||
// using reinforcement learning. We fed the agent lots of inputs from
|
||||
// microbenchmarks, SMHasher, low hamming distance from generated inputs and
|
||||
// picked up the one that was good on micro and macrobenchmarks.
|
||||
auto p = Read9To16(first, len);
|
||||
uint64_t lo = p.first;
|
||||
uint64_t hi = p.second;
|
||||
// Rotation by 53 was found to be most often useful when discovering these
|
||||
// hashing algorithms with ML techniques.
|
||||
lo = absl::rotr(lo, 53);
|
||||
state += kMul;
|
||||
lo += state;
|
||||
state ^= hi;
|
||||
uint128 m = state;
|
||||
m *= lo;
|
||||
return static_cast<uint64_t>(m ^ (m >> 64));
|
||||
} else if (len >= 4) {
|
||||
v = Read4To8(first, len);
|
||||
} else if (len > 0) {
|
||||
v = Read1To3(first, len);
|
||||
} else {
|
||||
// Empty ranges have no effect.
|
||||
return state;
|
||||
}
|
||||
return Mix(state, v);
|
||||
}
|
||||
|
||||
struct AggregateBarrier {};
|
||||
|
||||
// HashImpl
|
||||
|
||||
// Add a private base class to make sure this type is not an aggregate.
|
||||
// Aggregates can be aggregate initialized even if the default constructor is
|
||||
// deleted.
|
||||
struct PoisonedHash : private AggregateBarrier {
|
||||
PoisonedHash() = delete;
|
||||
PoisonedHash(const PoisonedHash&) = delete;
|
||||
PoisonedHash& operator=(const PoisonedHash&) = delete;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct HashImpl {
|
||||
size_t operator()(const T& value) const {
|
||||
return MixingHashState::hash(value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Hash
|
||||
: absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
|
||||
|
||||
template <typename H>
|
||||
template <typename T, typename... Ts>
|
||||
H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
|
||||
return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
|
||||
std::move(state), value),
|
||||
values...);
|
||||
}
|
||||
|
||||
// HashStateBase::combine_contiguous()
|
||||
template <typename H>
|
||||
template <typename T>
|
||||
H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
|
||||
return hash_internal::hash_range_or_bytes(std::move(state), data, size);
|
||||
}
|
||||
|
||||
// HashStateBase::combine_unordered()
|
||||
template <typename H>
|
||||
template <typename I>
|
||||
H HashStateBase<H>::combine_unordered(H state, I begin, I end) {
|
||||
return H::RunCombineUnordered(std::move(state),
|
||||
CombineUnorderedCallback<I>{begin, end});
|
||||
}
|
||||
|
||||
// HashStateBase::PiecewiseCombiner::add_buffer()
|
||||
template <typename H>
|
||||
H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
|
||||
size_t size) {
|
||||
if (position_ + size < PiecewiseChunkSize()) {
|
||||
// This partial chunk does not fill our existing buffer
|
||||
memcpy(buf_ + position_, data, size);
|
||||
position_ += size;
|
||||
return state;
|
||||
}
|
||||
|
||||
// If the buffer is partially filled we need to complete the buffer
|
||||
// and hash it.
|
||||
if (position_ != 0) {
|
||||
const size_t bytes_needed = PiecewiseChunkSize() - position_;
|
||||
memcpy(buf_ + position_, data, bytes_needed);
|
||||
state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
|
||||
data += bytes_needed;
|
||||
size -= bytes_needed;
|
||||
}
|
||||
|
||||
// Hash whatever chunks we can without copying
|
||||
while (size >= PiecewiseChunkSize()) {
|
||||
state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
|
||||
data += PiecewiseChunkSize();
|
||||
size -= PiecewiseChunkSize();
|
||||
}
|
||||
// Fill the buffer with the remainder
|
||||
memcpy(buf_, data, size);
|
||||
position_ = size;
|
||||
return state;
|
||||
}
|
||||
|
||||
// HashStateBase::PiecewiseCombiner::finalize()
|
||||
template <typename H>
|
||||
H PiecewiseCombiner::finalize(H state) {
|
||||
// Hash the remainder left in the buffer, which may be empty
|
||||
return H::combine_contiguous(std::move(state), buf_, position_);
|
||||
}
|
||||
|
||||
} // namespace hash_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_HASH_INTERNAL_HASH_H_
|
||||
118
Pods/abseil/absl/hash/internal/low_level_hash.cc
generated
Normal file
118
Pods/abseil/absl/hash/internal/low_level_hash.cc
generated
Normal file
@@ -0,0 +1,118 @@
|
||||
// Copyright 2020 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.
|
||||
|
||||
#include "absl/hash/internal/low_level_hash.h"
|
||||
|
||||
#include "absl/base/internal/unaligned_access.h"
|
||||
#include "absl/base/prefetch.h"
|
||||
#include "absl/numeric/int128.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace hash_internal {
|
||||
|
||||
static uint64_t Mix(uint64_t v0, uint64_t v1) {
|
||||
absl::uint128 p = v0;
|
||||
p *= v1;
|
||||
return absl::Uint128Low64(p) ^ absl::Uint128High64(p);
|
||||
}
|
||||
|
||||
uint64_t LowLevelHash(const void* data, size_t len, uint64_t seed,
|
||||
const uint64_t salt[5]) {
|
||||
// Prefetch the cacheline that data resides in.
|
||||
PrefetchToLocalCache(data);
|
||||
const uint8_t* ptr = static_cast<const uint8_t*>(data);
|
||||
uint64_t starting_length = static_cast<uint64_t>(len);
|
||||
uint64_t current_state = seed ^ salt[0];
|
||||
|
||||
if (len > 64) {
|
||||
// If we have more than 64 bytes, we're going to handle chunks of 64
|
||||
// bytes at a time. We're going to build up two separate hash states
|
||||
// which we will then hash together.
|
||||
uint64_t duplicated_state = current_state;
|
||||
|
||||
do {
|
||||
// Always prefetch the next cacheline.
|
||||
PrefetchToLocalCache(ptr + ABSL_CACHELINE_SIZE);
|
||||
|
||||
uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
|
||||
uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
|
||||
uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
|
||||
uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
|
||||
uint64_t e = absl::base_internal::UnalignedLoad64(ptr + 32);
|
||||
uint64_t f = absl::base_internal::UnalignedLoad64(ptr + 40);
|
||||
uint64_t g = absl::base_internal::UnalignedLoad64(ptr + 48);
|
||||
uint64_t h = absl::base_internal::UnalignedLoad64(ptr + 56);
|
||||
|
||||
uint64_t cs0 = Mix(a ^ salt[1], b ^ current_state);
|
||||
uint64_t cs1 = Mix(c ^ salt[2], d ^ current_state);
|
||||
current_state = (cs0 ^ cs1);
|
||||
|
||||
uint64_t ds0 = Mix(e ^ salt[3], f ^ duplicated_state);
|
||||
uint64_t ds1 = Mix(g ^ salt[4], h ^ duplicated_state);
|
||||
duplicated_state = (ds0 ^ ds1);
|
||||
|
||||
ptr += 64;
|
||||
len -= 64;
|
||||
} while (len > 64);
|
||||
|
||||
current_state = current_state ^ duplicated_state;
|
||||
}
|
||||
|
||||
// We now have a data `ptr` with at most 64 bytes and the current state
|
||||
// of the hashing state machine stored in current_state.
|
||||
while (len > 16) {
|
||||
uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
|
||||
uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
|
||||
|
||||
current_state = Mix(a ^ salt[1], b ^ current_state);
|
||||
|
||||
ptr += 16;
|
||||
len -= 16;
|
||||
}
|
||||
|
||||
// We now have a data `ptr` with at most 16 bytes.
|
||||
uint64_t a = 0;
|
||||
uint64_t b = 0;
|
||||
if (len > 8) {
|
||||
// When we have at least 9 and at most 16 bytes, set A to the first 64
|
||||
// bits of the input and B to the last 64 bits of the input. Yes, they will
|
||||
// overlap in the middle if we are working with less than the full 16
|
||||
// bytes.
|
||||
a = absl::base_internal::UnalignedLoad64(ptr);
|
||||
b = absl::base_internal::UnalignedLoad64(ptr + len - 8);
|
||||
} else if (len > 3) {
|
||||
// If we have at least 4 and at most 8 bytes, set A to the first 32
|
||||
// bits and B to the last 32 bits.
|
||||
a = absl::base_internal::UnalignedLoad32(ptr);
|
||||
b = absl::base_internal::UnalignedLoad32(ptr + len - 4);
|
||||
} else if (len > 0) {
|
||||
// If we have at least 1 and at most 3 bytes, read all of the provided
|
||||
// bits into A, with some adjustments.
|
||||
a = static_cast<uint64_t>((ptr[0] << 16) | (ptr[len >> 1] << 8) |
|
||||
ptr[len - 1]);
|
||||
b = 0;
|
||||
} else {
|
||||
a = 0;
|
||||
b = 0;
|
||||
}
|
||||
|
||||
uint64_t w = Mix(a ^ salt[1], b ^ current_state);
|
||||
uint64_t z = salt[1] ^ starting_length;
|
||||
return Mix(w, z);
|
||||
}
|
||||
|
||||
} // namespace hash_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
50
Pods/abseil/absl/hash/internal/low_level_hash.h
generated
Normal file
50
Pods/abseil/absl/hash/internal/low_level_hash.h
generated
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2020 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.
|
||||
//
|
||||
// This file provides the Google-internal implementation of LowLevelHash.
|
||||
//
|
||||
// LowLevelHash is a fast hash function for hash tables, the fastest we've
|
||||
// currently (late 2020) found that passes the SMHasher tests. The algorithm
|
||||
// relies on intrinsic 128-bit multiplication for speed. This is not meant to be
|
||||
// secure - just fast.
|
||||
//
|
||||
// It is closely based on a version of wyhash, but does not maintain or
|
||||
// guarantee future compatibility with it.
|
||||
|
||||
#ifndef ABSL_HASH_INTERNAL_LOW_LEVEL_HASH_H_
|
||||
#define ABSL_HASH_INTERNAL_LOW_LEVEL_HASH_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace hash_internal {
|
||||
|
||||
// Hash function for a byte array. A 64-bit seed and a set of five 64-bit
|
||||
// integers are hashed into the result.
|
||||
//
|
||||
// To allow all hashable types (including string_view and Span) to depend on
|
||||
// this algorithm, we keep the API low-level, with as few dependencies as
|
||||
// possible.
|
||||
uint64_t LowLevelHash(const void* data, size_t len, uint64_t seed,
|
||||
const uint64_t salt[5]);
|
||||
|
||||
} // namespace hash_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_HASH_INTERNAL_LOW_LEVEL_HASH_H_
|
||||
Reference in New Issue
Block a user