1#ifndef SOURCEMETA_CORE_JSON_HASH_H_
2#define SOURCEMETA_CORE_JSON_HASH_H_
4#include <sourcemeta/core/numeric.h>
13namespace sourcemeta::core {
18 using hash_type = std::uint64_t;
20 auto operator()(
const T &value)
const noexcept -> hash_type {
21 if constexpr (
requires { value.get().fast_hash(); }) {
22 return value.get().fast_hash();
24 return value.fast_hash();
30 auto is_perfect([[maybe_unused]]
const hash_type hash)
const noexcept
48 auto operator==(
const HashType &)
const noexcept ->
bool =
default;
53 constexpr auto packed(
const char *data,
const std::size_t size,
54 const std::uint8_t leading)
const noexcept ->
HashType {
63 static_assert(std::endian::native == std::endian::little);
65 for (std::size_t index = 0; index < size; index += 1) {
67 static_cast<unsigned char>(data[index]))};
68 const auto position{index + 1};
70 result.
a |= byte << static_cast<int>(8 * position);
72 result.
b |= byte << static_cast<int>(8 * (position - 16));
79 auto *
const bytes{
reinterpret_cast<unsigned char *
>(&result)};
81 std::memcpy(bytes + 1, data, size);
90 const std::size_t size)
const noexcept ->
HashType {
95 return this->
packed(data, size,
static_cast<std::uint8_t
>(size));
102 constexpr auto operator()(
const T &value)
const noexcept -> HashType {
103 const auto size{value.size()};
108 return this->
perfect(value.data(), 1);
110 return this->
perfect(value.data(), 2);
112 return this->
perfect(value.data(), 3);
114 return this->
perfect(value.data(), 4);
116 return this->
perfect(value.data(), 5);
118 return this->
perfect(value.data(), 6);
120 return this->
perfect(value.data(), 7);
122 return this->
perfect(value.data(), 8);
124 return this->
perfect(value.data(), 9);
126 return this->
perfect(value.data(), 10);
128 return this->
perfect(value.data(), 11);
130 return this->
perfect(value.data(), 12);
132 return this->
perfect(value.data(), 13);
134 return this->
perfect(value.data(), 14);
136 return this->
perfect(value.data(), 15);
138 return this->
perfect(value.data(), 16);
140 return this->
perfect(value.data(), 17);
142 return this->
perfect(value.data(), 18);
144 return this->
perfect(value.data(), 19);
146 return this->
perfect(value.data(), 20);
148 return this->
perfect(value.data(), 21);
150 return this->
perfect(value.data(), 22);
152 return this->
perfect(value.data(), 23);
154 return this->
perfect(value.data(), 24);
156 return this->
perfect(value.data(), 25);
158 return this->
perfect(value.data(), 26);
160 return this->
perfect(value.data(), 27);
162 return this->
perfect(value.data(), 28);
164 return this->
perfect(value.data(), 29);
166 return this->
perfect(value.data(), 30);
168 return this->
perfect(value.data(), 31);
176 const auto leading{32 +
177 ((size +
static_cast<unsigned char>(value.front()) +
178 static_cast<unsigned char>(value.back())) %
180 return this->
packed(value.data(), 31,
181 static_cast<std::uint8_t
>(leading));
185 constexpr auto operator()(
const char *data,
186 const std::size_t size)
const noexcept ->
HashType {
209 return this->
perfect(data, 10);
211 return this->
perfect(data, 11);
213 return this->
perfect(data, 12);
215 return this->
perfect(data, 13);
217 return this->
perfect(data, 14);
219 return this->
perfect(data, 15);
221 return this->
perfect(data, 16);
223 return this->
perfect(data, 17);
225 return this->
perfect(data, 18);
227 return this->
perfect(data, 19);
229 return this->
perfect(data, 20);
231 return this->
perfect(data, 21);
233 return this->
perfect(data, 22);
235 return this->
perfect(data, 23);
237 return this->
perfect(data, 24);
239 return this->
perfect(data, 25);
241 return this->
perfect(data, 26);
243 return this->
perfect(data, 27);
245 return this->
perfect(data, 28);
247 return this->
perfect(data, 29);
249 return this->
perfect(data, 30);
251 return this->
perfect(data, 31);
259 const auto leading{32 + ((size +
static_cast<unsigned char>(data[0]) +
260 static_cast<unsigned char>(data[size - 1])) %
262 return this->
packed(data, 31,
static_cast<std::uint8_t
>(leading));
274 return (hash.a & 224) == 0;
284 auto operator()(
const T &left,
const T &right)
const ->
bool {
285 if constexpr (
requires { left.get() == right.get(); }) {
286 return left.get() == right.get();
288 return left == right;
constexpr auto perfect(const char *data, const std::size_t size) const noexcept -> HashType
Compute a perfect hash from raw data.
Definition json_hash.h:89
constexpr auto packed(const char *data, const std::size_t size, const std::uint8_t leading) const noexcept -> HashType
Compute a hash from up to 31 bytes of raw data, behind a leading byte.
Definition json_hash.h:53
constexpr auto is_perfect(const HashType &hash) const noexcept -> bool
Check whether the given hash is a perfect hash.
Definition json_hash.h:268
auto is_perfect(const hash_type hash) const noexcept -> bool
Check whether the given hash is a perfect hash.
Definition json_hash.h:30