44#ifndef WPIUTIL_WPI_HASHING_H
45#define WPIUTIL_WPI_HASHING_H
61#pragma warning(disable : 26495)
65template <
typename T,
typename Enable>
struct DenseMapInfo;
91 operator size_t()
const {
return value; }
94 return lhs.value == rhs.value;
97 return lhs.value != rhs.value;
112std::enable_if_t<is_integral_or_enum<T>::value, hash_code>
hash_value(T value);
120template <
typename T,
typename U>
124template <
typename... Ts>
132template <
typename T> hash_code
hash_value(
const std::optional<T> &
arg);
159 memcpy(&result, p,
sizeof(result));
167 memcpy(&result, p,
sizeof(result));
174static constexpr uint64_t
k0 = 0xc3a5c85c97cb3127ULL;
175static constexpr uint64_t
k1 = 0xb492b66fbe98f273ULL;
176static constexpr uint64_t
k2 = 0x9ae16a3b2f90404fULL;
177static constexpr uint64_t
k3 = 0xc949d7c7509e6557ULL;
182inline uint64_t
rotate(uint64_t val,
size_t shift) {
184 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
188 return val ^ (val >> 47);
193 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
194 uint64_t a = (low ^ high) * kMul;
196 uint64_t
b = (high ^ a) * kMul;
204 uint8_t
b = s[len >> 1];
205 uint8_t
c = s[len - 1];
206 uint32_t y =
static_cast<uint32_t
>(a) + (
static_cast<uint32_t
>(
b) << 8);
207 uint32_t z =
static_cast<uint32_t
>(len) + (
static_cast<uint32_t
>(
c) << 2);
228 std::rotr<uint64_t>(
c ^ seed, 30) + d,
229 a + std::rotr<uint64_t>(
b ^
k3, 20) -
c + len + seed);
235 uint64_t
b = std::rotr<uint64_t>(a + z, 52);
236 uint64_t
c = std::rotr<uint64_t>(a, 37);
238 c += std::rotr<uint64_t>(a, 7);
241 uint64_t vs =
b + std::rotr<uint64_t>(a, 31) +
c;
244 b = std::rotr<uint64_t>(a + z, 52);
245 c = std::rotr<uint64_t>(a, 37);
247 c += std::rotr<uint64_t>(a, 7);
250 uint64_t ws =
b + std::rotr<uint64_t>(a, 31) +
c;
255inline uint64_t
hash_short(
const char *s,
size_t length, uint64_t seed) {
256 if (length >= 4 && length <= 8)
258 if (length > 8 && length <= 16)
260 if (length > 16 && length <= 32)
283 std::rotr<uint64_t>(seed ^
k1, 49),
297 b = std::rotr<uint64_t>(
b + a +
c, 21);
300 b += std::rotr<uint64_t>(a, 44) + d;
312 h2 = std::rotr<uint64_t>(
h2 +
h5, 33) *
k1;
345 const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
364 : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
365 std::is_pointer<T>::value) &&
366 64 % sizeof(T) == 0)> {};
373 : std::integral_constant<bool, (is_hashable_data<T>::value &&
374 is_hashable_data<U>::value &&
375 (sizeof(T) + sizeof(U)) ==
376 sizeof(std::pair<T, U>))> {};
381std::enable_if_t<is_hashable_data<T>::value, T>
389std::enable_if_t<!is_hashable_data<T>::value,
size_t>
405 size_t store_size =
sizeof(value) - offset;
406 if (buffer_ptr + store_size > buffer_end)
408 const char *value_data =
reinterpret_cast<const char *
>(&value);
409 memcpy(buffer_ptr, value_data + offset, store_size);
410 buffer_ptr += store_size;
419template <
typename InputIteratorT>
422 char buffer[64], *buffer_ptr = buffer;
423 char *
const buffer_end = std::end(buffer);
428 return hash_short(buffer, buffer_ptr - buffer, seed);
429 assert(buffer_ptr == buffer_end);
433 while (
first != last) {
448 length += buffer_ptr - buffer;
451 return state.finalize(length);
462template <
typename ValueT>
463std::enable_if_t<is_hashable_data<ValueT>::value,
hash_code>
466 const char *s_begin =
reinterpret_cast<const char *
>(
first);
467 const char *s_end =
reinterpret_cast<const char *
>(last);
468 const size_t length = std::distance(s_begin, s_end);
472 const char *s_aligned_end = s_begin + (length & ~63);
475 while (s_begin != s_aligned_end) {
480 state.mix(s_end - 64);
482 return state.finalize(length);
495template <
typename InputIteratorT>
531 template <
typename T>
532 char *
combine_data(
size_t &length,
char *buffer_ptr,
char *buffer_end, T data) {
538 size_t partial_store_size = buffer_end - buffer_ptr;
539 memcpy(buffer_ptr, &data, partial_store_size);
570 template <
typename T,
typename ...Ts>
572 const T &
arg,
const Ts &...args) {
576 return combine(length, buffer_ptr, buffer_end, args...);
598 length += buffer_ptr -
buffer;
637 const char *s =
reinterpret_cast<const char *
>(&value);
650 static_cast<uint64_t
>(value));
662template <
typename T,
typename U>
668 return std::apply([](
const auto &...xs) {
return hash_combine(xs...); },
arg);
#define wpi_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:142
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source code
Definition: ThirdPartyNotices.txt:110
An opaque object representing a hash code.
Definition: Hashing.h:79
hash_code(size_t value)
Form a hash code directly from a numerical value.
Definition: Hashing.h:88
hash_code()=default
Default construct a hash_code.
friend size_t hash_value(const hash_code &code)
Allow a hash_code to be directly run through hash_value.
Definition: Hashing.h:101
friend bool operator==(const hash_code &lhs, const hash_code &rhs)
Definition: Hashing.h:93
friend bool operator!=(const hash_code &lhs, const hash_code &rhs)
Definition: Hashing.h:96
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
\rst Returns a named argument to be used in a formatting function.
Definition: core.h:1841
detail namespace with internal helper functions
Definition: xchar.h:20
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
state
Definition: core.h:2271
uint128_t uintptr_t
Definition: format.h:484
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
static constexpr const velocity::meters_per_second_t c(299792458.0)
Speed of light in vacuum.
uint32_t fetch32(const char *p)
Definition: Hashing.h:165
static constexpr uint64_t k1
Definition: Hashing.h:175
uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:211
hash_code hash_integer_value(uint64_t value)
Helper to hash the value of a single integer.
Definition: Hashing.h:634
std::enable_if_t< is_hashable_data< ValueT >::value, hash_code > hash_combine_range_impl(ValueT *first, ValueT *last)
Implement the combining of integral values into a hash_code.
Definition: Hashing.h:464
uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:202
uint64_t rotate(uint64_t val, size_t shift)
Bitwise right rotate.
Definition: Hashing.h:182
hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last)
Implement the combining of integral values into a hash_code.
Definition: Hashing.h:420
static constexpr uint64_t k3
Definition: Hashing.h:177
static constexpr uint64_t k2
Definition: Hashing.h:176
uint64_t fixed_seed_override
A global, fixed seed-override variable.
static constexpr uint64_t k0
Some primes between 2^63 and 2^64 for various uses.
Definition: Hashing.h:174
std::enable_if_t< is_hashable_data< T >::value, T > get_hashable_data(const T &value)
Helper to get the hashable data representation for a type.
Definition: Hashing.h:382
uint64_t hash_16_bytes(uint64_t low, uint64_t high)
Definition: Hashing.h:191
uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:216
uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:232
uint64_t fetch64(const char *p)
Definition: Hashing.h:157
uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:222
uint64_t get_execution_seed()
Definition: Hashing.h:338
bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T &value, size_t offset=0)
Helper to store data from a value into a buffer and advance the pointer into that buffer.
Definition: Hashing.h:403
uint64_t shift_mix(uint64_t val)
Definition: Hashing.h:187
uint64_t hash_short(const char *s, size_t length, uint64_t seed)
Definition: Hashing.h:255
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:102
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:54
Definition: ntcore_cpp.h:26
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:496
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:618
hash_code hash_value(const std::optional< T > &arg)
Compute a hash_code for a standard string.
Definition: Hashing.h:678
std::enable_if_t< is_integral_or_enum< T >::value, hash_code > hash_value(T value)
Compute a hash_code for any integer value.
Definition: Hashing.h:648
void set_fixed_execution_hash_seed(uint64_t fixed_value)
Override the execution seed with a fixed value.
static unsigned getHashValue(hash_code val)
Definition: Hashing.h:685
static hash_code getTombstoneKey()
Definition: Hashing.h:684
static bool isEqual(hash_code LHS, hash_code RHS)
Definition: Hashing.h:686
static hash_code getEmptyKey()
Definition: Hashing.h:683
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:50
Helper class to manage the recursive combining of hash_combine arguments.
Definition: Hashing.h:512
char * combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data)
Combine one chunk of data into the current in-flight hash.
Definition: Hashing.h:532
const uint64_t seed
Definition: Hashing.h:515
hash_state state
Definition: Hashing.h:514
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, const T &arg, const Ts &...args)
Recursive, variadic combining method.
Definition: Hashing.h:571
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end)
Base case for recursive, variadic combining.
Definition: Hashing.h:584
char buffer[64]
Definition: Hashing.h:513
hash_combine_recursive_helper()
Construct a recursive hash combining helper.
Definition: Hashing.h:522
The intermediate state used during hashing.
Definition: Hashing.h:273
static hash_state create(const char *s, uint64_t seed)
Create a new hash_state structure and initialize it based on the seed and the first 64-byte chunk.
Definition: Hashing.h:279
uint64_t h2
Definition: Hashing.h:274
uint64_t h3
Definition: Hashing.h:274
uint64_t h6
Definition: Hashing.h:274
uint64_t h5
Definition: Hashing.h:274
uint64_t finalize(size_t length)
Compute the final 64-bit hash code value based on the current state and the length of bytes hashed.
Definition: Hashing.h:324
uint64_t h4
Definition: Hashing.h:274
uint64_t h1
Definition: Hashing.h:274
void mix(const char *s)
Mix in a 64-byte buffer of data.
Definition: Hashing.h:307
uint64_t h0
Definition: Hashing.h:274
static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b)
Mix 32-bytes from the input sequence into the 16-bytes of 'a' and 'b', including whatever is already ...
Definition: Hashing.h:294
Trait to indicate whether a type's bits can be hashed directly.
Definition: Hashing.h:366