44#ifndef WPIUTIL_WPI_HASHING_H
45#define WPIUTIL_WPI_HASHING_H
61#pragma warning(disable : 26495)
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);
142 memcpy(&result, p,
sizeof(result));
150 memcpy(&result, p,
sizeof(result));
157static constexpr uint64_t
k0 = 0xc3a5c85c97cb3127ULL;
158static constexpr uint64_t
k1 = 0xb492b66fbe98f273ULL;
159static constexpr uint64_t
k2 = 0x9ae16a3b2f90404fULL;
160static constexpr uint64_t
k3 = 0xc949d7c7509e6557ULL;
165inline uint64_t
rotate(uint64_t val,
size_t shift) {
167 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
171 return val ^ (val >> 47);
176 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
177 uint64_t a = (low ^ high) * kMul;
179 uint64_t b = (high ^ a) * kMul;
187 uint8_t b = s[len >> 1];
188 uint8_t c = s[len - 1];
189 uint32_t y =
static_cast<uint32_t
>(a) + (
static_cast<uint32_t
>(b) << 8);
190 uint32_t z =
static_cast<uint32_t
>(len) + (
static_cast<uint32_t
>(c) << 2);
201 uint64_t b =
fetch64(s + len - 8);
211 std::rotr<uint64_t>(c ^ seed, 30) + d,
212 a + std::rotr<uint64_t>(b ^
k3, 20) - c + len + seed);
218 uint64_t b = std::rotr<uint64_t>(a + z, 52);
219 uint64_t c = std::rotr<uint64_t>(a, 37);
221 c += std::rotr<uint64_t>(a, 7);
224 uint64_t vs = b + std::rotr<uint64_t>(a, 31) + c;
227 b = std::rotr<uint64_t>(a + z, 52);
228 c = std::rotr<uint64_t>(a, 37);
230 c += std::rotr<uint64_t>(a, 7);
233 uint64_t ws = b + std::rotr<uint64_t>(a, 31) + c;
238inline uint64_t
hash_short(
const char *s,
size_t length, uint64_t seed) {
239 if (length >= 4 && length <= 8)
241 if (length > 8 && length <= 16)
243 if (length > 16 && length <= 32)
266 std::rotr<uint64_t>(seed ^
k1, 49),
280 b = std::rotr<uint64_t>(b + a + c, 21);
283 b += std::rotr<uint64_t>(a, 44) + d;
295 h2 = std::rotr<uint64_t>(
h2 +
h5, 33) *
k1;
320#if LLVM_ENABLE_ABI_BREAKING_CHECKS && \
321 (!defined(__clang__) || __clang_major__ > 11)
322 return static_cast<uint64_t
>(
325 return 0xff51afd7ed558ccdULL;
343 : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
344 std::is_pointer<T>::value) &&
345 64 % sizeof(T) == 0)> {};
352 : std::integral_constant<bool, (is_hashable_data<T>::value &&
353 is_hashable_data<U>::value &&
354 (sizeof(T) + sizeof(U)) ==
355 sizeof(std::pair<T, U>))> {};
360std::enable_if_t<is_hashable_data<T>::value, T>
368std::enable_if_t<!is_hashable_data<T>::value,
size_t>
370 using ::wpi::hash_value;
384 size_t store_size =
sizeof(value) - offset;
385 if (buffer_ptr + store_size > buffer_end)
387 const char *value_data =
reinterpret_cast<const char *
>(&value);
388 memcpy(buffer_ptr, value_data + offset, store_size);
389 buffer_ptr += store_size;
398template <
typename InputIteratorT>
401 char buffer[64], *buffer_ptr = buffer;
402 char *
const buffer_end = std::end(buffer);
407 return hash_short(buffer, buffer_ptr - buffer, seed);
408 assert(buffer_ptr == buffer_end);
410 hash_state state = state.create(buffer, seed);
412 while (first != last) {
423 std::rotate(buffer, buffer_ptr, buffer_end);
427 length += buffer_ptr - buffer;
430 return state.finalize(length);
441template <
typename ValueT>
442std::enable_if_t<is_hashable_data<ValueT>::value,
hash_code>
445 const char *s_begin =
reinterpret_cast<const char *
>(first);
446 const char *s_end =
reinterpret_cast<const char *
>(last);
447 const size_t length = std::distance(s_begin, s_end);
451 const char *s_aligned_end = s_begin + (length & ~63);
452 hash_state state = state.create(s_begin, seed);
454 while (s_begin != s_aligned_end) {
459 state.mix(s_end - 64);
461 return state.finalize(length);
474template <
typename InputIteratorT>
476 return ::wpi::hashing::detail::hash_combine_range_impl(first, last);
510 template <
typename T>
511 char *
combine_data(
size_t &length,
char *buffer_ptr,
char *buffer_end, T data) {
517 size_t partial_store_size = buffer_end - buffer_ptr;
518 memcpy(buffer_ptr, &data, partial_store_size);
549 template <
typename T,
typename ...Ts>
551 const T &
arg,
const Ts &...args) {
555 return combine(length, buffer_ptr, buffer_end, args...);
573 std::rotate(
buffer, buffer_ptr, buffer_end);
577 length += buffer_ptr -
buffer;
616 const char *s =
reinterpret_cast<const char *
>(&value);
628 return ::wpi::hashing::detail::hash_integer_value(
629 static_cast<uint64_t
>(value));
635 return ::wpi::hashing::detail::hash_integer_value(
636 reinterpret_cast<uintptr_t
>(
ptr));
641template <
typename T,
typename U>
647 return std::apply([](
const auto &...xs) {
return hash_combine(xs...); },
arg);
665 return static_cast<unsigned>(size_t(val));
676struct hash<
wpi::hash_code> {
#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 and nanopb were all modified for use in 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:123
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
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
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, cert-dcl58-cpp) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.h:5258
uint32_t fetch32(const char *p)
Definition Hashing.h:148
static constexpr uint64_t k1
Definition Hashing.h:158
uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed)
Definition Hashing.h:194
hash_code hash_integer_value(uint64_t value)
Helper to hash the value of a single integer.
Definition Hashing.h:613
uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed)
Definition Hashing.h:185
uint64_t rotate(uint64_t val, size_t shift)
Bitwise right rotate.
Definition Hashing.h:165
hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last)
Implement the combining of integral values into a hash_code.
Definition Hashing.h:399
static constexpr uint64_t k3
Definition Hashing.h:160
static constexpr uint64_t k2
Definition Hashing.h:159
static constexpr uint64_t k0
Some primes between 2^63 and 2^64 for various uses.
Definition Hashing.h:157
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:361
uint64_t hash_16_bytes(uint64_t low, uint64_t high)
Definition Hashing.h:174
uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed)
Definition Hashing.h:199
uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed)
Definition Hashing.h:215
uint64_t fetch64(const char *p)
Definition Hashing.h:140
uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed)
Definition Hashing.h:205
uint64_t get_execution_seed()
In LLVM_ENABLE_ABI_BREAKING_CHECKS builds, the seed is non-deterministic per process (address of a fu...
Definition Hashing.h:317
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:382
uint64_t shift_mix(uint64_t val)
Definition Hashing.h:170
uint64_t hash_short(const char *s, size_t length, uint64_t seed)
Definition Hashing.h:238
void swapByteOrder(T &Value)
Definition SwapByteOrder.h:61
constexpr bool IsBigEndianHost
Definition SwapByteOrder.h:26
Foonathan namespace.
Definition ntcore_cpp.h:26
void install_fatal_error_handler(fatal_error_handler_t handler, void *user_data=nullptr)
install_fatal_error_handler - Installs a new error handler to be used whenever a serious (non-recover...
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:475
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:597
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:627
size_t operator()(wpi::hash_code const &Val) const
Definition Hashing.h:677
static unsigned getHashValue(hash_code val)
Definition Hashing.h:664
static hash_code getTombstoneKey()
Definition Hashing.h:663
static bool isEqual(hash_code LHS, hash_code RHS)
Definition Hashing.h:667
static hash_code getEmptyKey()
Definition Hashing.h:662
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition Hashing.h:65
Helper class to manage the recursive combining of hash_combine arguments.
Definition Hashing.h:491
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:511
const uint64_t seed
Definition Hashing.h:494
hash_state state
Definition Hashing.h:493
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:550
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end)
Base case for recursive, variadic combining.
Definition Hashing.h:563
char buffer[64]
Definition Hashing.h:492
hash_combine_recursive_helper()
Construct a recursive hash combining helper.
Definition Hashing.h:501
The intermediate state used during hashing.
Definition Hashing.h:256
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:262
uint64_t h2
Definition Hashing.h:257
uint64_t h3
Definition Hashing.h:257
uint64_t h6
Definition Hashing.h:257
uint64_t h5
Definition Hashing.h:257
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:307
uint64_t h4
Definition Hashing.h:257
uint64_t h1
Definition Hashing.h:257
void mix(const char *s)
Mix in a 64-byte buffer of data.
Definition Hashing.h:290
uint64_t h0
Definition Hashing.h:257
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:277
Trait to indicate whether a type's bits can be hashed directly.
Definition Hashing.h:345
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Returns a named argument to be used in a formatting function.
Definition base.h:2775