| 1 |
#ifndef MINIMAL_PERFECT_HASH_HH |
| 2 |
#define MINIMAL_PERFECT_HASH_HH |
| 3 |
|
| 4 |
#include "cstd.hh" |
| 5 |
#include "static_vector.hh" |
| 6 |
#include "stl.hh" |
| 7 |
#include "xrange.hh" |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <array> |
| 11 |
#include <bit> |
| 12 |
#include <cassert> |
| 13 |
#include <cstdint> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
namespace PerfectMinimalHash { |
| 58 |
|
| 59 |
template<size_t M, typename Hash> |
| 60 |
struct Result |
| 61 |
{ |
| 62 |
static_assert(std::has_single_bit(M)); |
| 63 |
static constexpr auto M2 = M; |
| 64 |
|
| 65 |
std::array<uint8_t, M > tab1; |
| 66 |
std::array<uint8_t, M2> tab2; |
| 67 |
[[no_unique_address]] Hash hash; |
| 68 |
|
| 69 |
[[nodiscard]] constexpr uint8_t lookupIndex(const auto& key) const { |
| 70 |
const uint32_t h = hash(key); |
| 71 |
const uint8_t d = tab1[h % M]; |
| 72 |
if ((d & 0x80) == 0) { |
| 73 |
return d; |
| 74 |
} else { |
| 75 |
return tab2[(h >> (d & 31)) % M2]; |
| 76 |
} |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
template<size_t N, typename Hash, typename GetKey> |
| 81 |
[[nodiscard]] constexpr auto create(const Hash& hash, const GetKey& getKey) |
| 82 |
{ |
| 83 |
static_assert(N < 128); |
| 84 |
constexpr size_t M = std::bit_ceil(N); |
| 85 |
constexpr auto bucket_max = size_t(2 * cstd::sqrt(M)); |
| 86 |
|
| 87 |
Result<M, Hash> r{}; |
| 88 |
r.hash = hash; |
| 89 |
|
| 90 |
|
| 91 |
std::array<static_vector<uint8_t, bucket_max>, r.tab1.size()> buckets; |
| 92 |
for (auto i : xrange(N)) { |
| 93 |
buckets[hash(getKey(i)) % r.tab1.size()].push_back(uint8_t(i)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
std::ranges::sort(buckets, [](const auto& x, const auto& y) { |
| 98 |
|
| 99 |
if (x.size() != y.size()) { |
| 100 |
return x.size() > y.size(); |
| 101 |
} |
| 102 |
|
| 103 |
return std::ranges::lexicographical_compare(x, y); |
| 104 |
}); |
| 105 |
|
| 106 |
|
| 107 |
constexpr auto UNUSED = uint8_t(-1); |
| 108 |
std::ranges::fill(r.tab2, UNUSED); |
| 109 |
for (const auto& bucket : buckets) { |
| 110 |
auto const bSize = bucket.size(); |
| 111 |
if (bSize == 0) break; |
| 112 |
auto hash1 = hash(getKey(bucket[0])) % r.tab1.size(); |
| 113 |
if (bSize == 1) { |
| 114 |
|
| 115 |
r.tab1[hash1] = bucket[0]; |
| 116 |
} else { |
| 117 |
|
| 118 |
|
| 119 |
uint8_t shift = 1; |
| 120 |
static_vector<uint8_t, bucket_max> bucket_slots; |
| 121 |
|
| 122 |
while (bucket_slots.size() < bSize) { |
| 123 |
auto hash2 = hash(getKey(bucket[bucket_slots.size()])); |
| 124 |
assert((hash2 % r.tab1.size()) == hash1); |
| 125 |
auto slot = (hash2 >> shift) % r.tab2.size(); |
| 126 |
if (r.tab2[slot] != UNUSED || contains(bucket_slots, slot)) { |
| 127 |
++shift; |
| 128 |
assert(shift < 32); |
| 129 |
bucket_slots.clear(); |
| 130 |
continue; |
| 131 |
} |
| 132 |
bucket_slots.push_back(uint8_t(slot)); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
r.tab1[hash1] = shift | 0x80; |
| 137 |
for (auto i : xrange(bSize)) { |
| 138 |
r.tab2[bucket_slots[i]] = bucket[i]; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
std::ranges::replace(r.tab2, UNUSED, uint8_t(0)); |
| 145 |
return r; |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
#endif |