-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhash_table.h
More file actions
290 lines (202 loc) · 6.58 KB
/
hash_table.h
File metadata and controls
290 lines (202 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Header guard
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
// Header files
using namespace std;
// Classes
// Hash table class
template<typename ValueType, const uint32_t size> class HashTable final {
// Public
public:
// Constructor
inline explicit HashTable() noexcept;
// Bool operator
inline explicit operator bool() const noexcept;
// Set unique
inline void setUnique(const uint32_t key, const ValueType *__restrict__ value) noexcept;
// Set unique and get index
inline uint32_t setUniqueAndGetIndex(const uint32_t key, const ValueType *__restrict__ value) noexcept;
// Replace
inline const ValueType *replace(const uint32_t key, const ValueType *__restrict__ value) noexcept;
// Remove most recent set unique
inline void removeMostRecentSetUique(const uint32_t index) noexcept;
// Clear
inline void clear() noexcept;
// Contains
inline bool contains(const uint32_t key) const noexcept;
// Get
inline const ValueType *get(const uint32_t key) const noexcept;
// Get values
inline void getValues(ValueType *__restrict__ values) const noexcept;
// Private
private:
// Hash table entry structure
struct HashTableEntry {
// Key
uint32_t key;
// Value
const ValueType *value;
};
// Entries
unique_ptr<HashTableEntry[]> entries;
};
// Supporting function implementation
// Constructor
template<typename ValueType, const uint32_t size> HashTable<ValueType, size>::HashTable() noexcept :
// Create entries
entries(new(nothrow) HashTableEntry[bitCeilingConstantExpression(size + 1)])
{
// Throw error if size is invalid
static_assert(size <= bitCeilingConstantExpression(UINT32_MAX >> 1) - 1, "Hash table's size is invalid");
// Check if creating entries was successful
if(entries) {
// Clear entries
memset(entries.get(), 0, sizeof(HashTableEntry) * bitCeilingConstantExpression(size + 1));
}
}
// Bool operator
template<typename ValueType, const uint32_t size> HashTable<ValueType, size>::operator bool() const noexcept {
// Return if creating entries was successful
return entries.get();
}
// Set unique
template<typename ValueType, const uint32_t size> void HashTable<ValueType, size>::setUnique(const uint32_t key, const ValueType *__restrict__ value) noexcept {
// Get key's index
uint32_t index = key % bitCeilingConstantExpression(size + 1);
// Loop while entry at index exists
while(entries[index].value) {
// Check if index isn't for the last entry
if(index != bitCeilingConstantExpression(size + 1) - 1) {
// Increment index
++index;
}
// Otherwise
else {
// Set index to the first entry
index = 0;
}
}
// Set entry at index to the value
entries[index] = {key, value};
}
// Set unique and get index
template<typename ValueType, const uint32_t size> uint32_t HashTable<ValueType, size>::setUniqueAndGetIndex(const uint32_t key, const ValueType *__restrict__ value) noexcept {
// Get key's index
uint32_t index = key % bitCeilingConstantExpression(size + 1);
// Loop while entry at index exists
while(entries[index].value) {
// Check if index isn't for the last entry
if(index != bitCeilingConstantExpression(size + 1) - 1) {
// Increment index
++index;
}
// Otherwise
else {
// Set index to the first entry
index = 0;
}
}
// Set entry at index to the value
entries[index] = {key, value};
// Return index
return index;
}
// Replace
template<typename ValueType, const uint32_t size> const ValueType *HashTable<ValueType, size>::replace(const uint32_t key, const ValueType *__restrict__ value) noexcept {
// Get key's index
uint32_t index = key % bitCeilingConstantExpression(size + 1);
// Loop while entry at index exists
while(entries[index].value) {
// Check if entry has the same key
if(entries[index].key == key) {
// Get current value
const ValueType *currentValue = entries[index].value;
// Set entry at index to the value
entries[index].value = value;
// Return current value
return currentValue;
}
// Check if index isn't for the last entry
if(index != bitCeilingConstantExpression(size + 1) - 1) {
// Increment index
++index;
}
// Otherwise
else {
// Set index to the first entry
index = 0;
}
}
// Set entry at index to the value
entries[index] = {key, value};
// Return nothing
return nullptr;
}
// Remove most recent set unique
template<typename ValueType, const uint32_t size> void HashTable<ValueType, size>::removeMostRecentSetUique(const uint32_t index) noexcept {
// Clear entry at index
entries[index].value = nullptr;
}
// Clear
template<typename ValueType, const uint32_t size> void HashTable<ValueType, size>::clear() noexcept {
// Clear entries
memset(entries.get(), 0, sizeof(HashTableEntry) * bitCeilingConstantExpression(size + 1));
}
// Contains
template<typename ValueType, const uint32_t size> bool HashTable<ValueType, size>::contains(const uint32_t key) const noexcept {
// Go through all existing entries starting at the key's index
for(uint32_t index = key % bitCeilingConstantExpression(size + 1); entries[index].value;) {
// Check if entry has the same key
if(entries[index].key == key) {
// Return true
return true;
}
// Check if index isn't for the last entry
if(index != bitCeilingConstantExpression(size + 1) - 1) {
// Increment index
++index;
}
// Otherwise
else {
// Set index to the first entry
index = 0;
}
}
// Return false
return false;
}
// Get
template<typename ValueType, const uint32_t size> const ValueType *HashTable<ValueType, size>::get(const uint32_t key) const noexcept {
// Go through all existing entries starting at the key's index
for(uint32_t index = key % bitCeilingConstantExpression(size + 1); entries[index].value;) {
// Check if entry has the same key
if(entries[index].key == key) {
// Return entry's value
return entries[index].value;
}
// Check if index isn't for the last entry
if(index != bitCeilingConstantExpression(size + 1) - 1) {
// Increment index
++index;
}
// Otherwise
else {
// Set index to the first entry
index = 0;
}
}
// Return nothing
return nullptr;
}
// Get values
template<typename ValueType, const uint32_t size> void HashTable<ValueType, size>::getValues(ValueType *__restrict__ values) const noexcept {
// Go through all entries
for(uint32_t index = 0, valuesIndex = 0; index < bitCeilingConstantExpression(size + 1); ++index) {
// Check if entry exists
if(entries[index].value) {
// Set value in values
values[valuesIndex++] = *entries[index].value;
}
}
}
#endif