16#include "error_handling.h"
33 return a.keylen == b.keylen && a.hash == b.hash;
45template <int MaxKeyLen = std::numeric_limits<std::uint16_t>::max()>
47 static_assert(0 <= MaxKeyLen && MaxKeyLen <= std::numeric_limits<std::uint16_t>::max(),
48 "MaxKeyLen can't be encoded by HashRsltPack");
50 inline static constexpr uint32_t FNV1A_PRIME = 16777619;
51 inline static constexpr uint32_t FNV1A_OFFSET = 2166136261;
56 static std::optional<HashRsltPack>
calc(
const char* key)
58 std::uint32_t hash = FNV1A_OFFSET;
59 for (
int i = 0; i <= MaxKeyLen; i++) {
63 hash = (hash ^ key[i]) * FNV1A_PRIME;
69 static std::optional<HashRsltPack> calc(std::string_view key)
72 if (len > MaxKeyLen) {
75 std::uint32_t hash = FNV1A_OFFSET;
76 for (
int i = 0; i < len; i++) {
77 hash = (hash ^ key[i]) * FNV1A_PRIME;
79 return {HashRsltPack{
static_cast<uint16_t
>(len), hash}};
89 const char*
key =
nullptr;
115 std::shared_ptr<bimap_detail::Row[]> table_rows_;
117 std::shared_ptr<uint16_t[]> ordered_row_indices_;
128 inline static constexpr int LOAD_FACTOR_NUMERATOR = 2;
129 inline static constexpr int LOAD_FACTOR_DENOMINATOR = 3;
130 static_assert(LOAD_FACTOR_NUMERATOR <= LOAD_FACTOR_DENOMINATOR);
132 inline static constexpr int64_t MAX_CAPACITY =
static_cast<int64_t
>(std::numeric_limits<uint16_t>::max());
133 inline static constexpr int64_t MAX_LEN = MAX_CAPACITY * LOAD_FACTOR_NUMERATOR / LOAD_FACTOR_DENOMINATOR;
148 inline static constexpr uint16_t MAX_KEY_LEN = 21;
152 FrozenKeyIdxBiMap() : table_rows_(nullptr), ordered_row_indices_(nullptr), capacity_(0), length_(0), max_probe_(0) {}
164 std::optional<int>
find(
const char* key)
const noexcept;
165 std::optional<int>
find(std::string_view key)
const noexcept;
181 uint16_t row_index = ordered_row_indices_.get()[index];
182 return {table_rows_.get()[row_index].key};
186 std::size_t
size() const noexcept {
return length_; }
bool operator==(const HashRsltPack &a, const HashRsltPack &b)
implement equality operation (primarily for unit-testing)
Definition FrozenKeyIdxBiMap.h:31
A bidirectional map (bimap), specialized to map n unique string keys to unique indexes with values of...
Definition FrozenKeyIdxBiMap.h:111
std::size_t size() const noexcept
Definition FrozenKeyIdxBiMap.h:186
std::optional< int > find(const char *key) const noexcept
Definition FrozenKeyIdxBiMap.cpp:144
FrozenKeyIdxBiMap()
Definition FrozenKeyIdxBiMap.h:152
std::string inverse_find(int index) const
Definition FrozenKeyIdxBiMap.h:179
Definition FrozenKeyIdxBiMap.cpp:11
Definition FrozenKeyIdxBiMap.h:46
static std::optional< HashRsltPack > calc(const char *key)
Definition FrozenKeyIdxBiMap.h:56
Definition FrozenKeyIdxBiMap.h:25
Definition FrozenKeyIdxBiMap.h:85
uint16_t keylen
length of the key (not including the '\0')
Definition FrozenKeyIdxBiMap.h:88
const char * key
identifies the address of this entry's key
Definition FrozenKeyIdxBiMap.h:89
uint16_t value
value associated with the current key
Definition FrozenKeyIdxBiMap.h:87