Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
FrozenKeyIdxBiMap.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <cstring>
9#include <limits>
10#include <memory>
11#include <optional>
12#include <string>
13#include <string_view>
14#include <vector>
15
16#include "error_handling.h"
17
18namespace utils
19{
20
21namespace bimap_detail
22{
23
26 std::uint16_t keylen;
27 std::uint32_t hash;
28};
29
31inline bool operator==(const HashRsltPack& a, const HashRsltPack& b)
32{
33 return a.keylen == b.keylen && a.hash == b.hash;
34}
35
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");
49
50 inline static constexpr uint32_t FNV1A_PRIME = 16777619;
51 inline static constexpr uint32_t FNV1A_OFFSET = 2166136261;
52
56 static std::optional<HashRsltPack> calc(const char* key)
57 {
58 std::uint32_t hash = FNV1A_OFFSET;
59 for (int i = 0; i <= MaxKeyLen; i++) { // the `<=` is intentional
60 if (key[i] == '\0') {
61 return {HashRsltPack{static_cast<uint16_t>(i), hash}};
62 }
63 hash = (hash ^ key[i]) * FNV1A_PRIME;
64 }
65 return std::nullopt;
66 }
67
68 // this mostly exists as a convenience
69 static std::optional<HashRsltPack> calc(std::string_view key)
70 {
71 int len = key.size();
72 if (len > MaxKeyLen) {
73 return std::nullopt;
74 }
75 std::uint32_t hash = FNV1A_OFFSET;
76 for (int i = 0; i < len; i++) {
77 hash = (hash ^ key[i]) * FNV1A_PRIME;
78 }
79 return {HashRsltPack{static_cast<uint16_t>(len), hash}};
80 }
81};
82
85struct Row {
86 // smallest structs members are listed first to minimize struct size
87 uint16_t value = 0;
88 uint16_t keylen = 0;
89 const char* key = nullptr;
90};
91
92} // namespace bimap_detail
93
111{
112 // define attributes:
113
115 std::shared_ptr<bimap_detail::Row[]> table_rows_;
117 std::shared_ptr<uint16_t[]> ordered_row_indices_;
119 uint16_t capacity_;
121 uint16_t length_;
123 uint16_t max_probe_;
124
125 // define a few constants
126
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);
131
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;
134
148 inline static constexpr uint16_t MAX_KEY_LEN = 21;
149
150 public: // Interface:
152 FrozenKeyIdxBiMap() : table_rows_(nullptr), ordered_row_indices_(nullptr), capacity_(0), length_(0), max_probe_(0) {}
153
158 explicit FrozenKeyIdxBiMap(const std::vector<std::string>& keys) noexcept;
159
164 std::optional<int> find(const char* key) const noexcept;
165 std::optional<int> find(std::string_view key) const noexcept;
166 /*
167 std::optional<int> find(const std::string& key) const noexcept {
168 return this->find(std::string_view(key));
169 }*/
170
179 std::string inverse_find(int index) const
180 {
181 uint16_t row_index = ordered_row_indices_.get()[index];
182 return {table_rows_.get()[row_index].key};
183 }
184
186 std::size_t size() const noexcept { return length_; }
187};
188
189} // namespace utils
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