-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.h
More file actions
368 lines (321 loc) · 10.9 KB
/
HashTable.h
File metadata and controls
368 lines (321 loc) · 10.9 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef HASH_TABLE_IMPLEMENTATION
#define HASH_TABLE_IMPLEMENTATION
#include <algorithm>
#include <cstddef>
#include <list>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <stdexcept>
#include <utility>
#include <vector>
namespace container {
template<typename Key, typename Type, typename Hash = std::hash<Key>>
class HashTable {
private:
using hash_table = std::vector<std::list<std::pair<const Key, Type>>>;
std::size_t m_size{}; // Total elements inserted - not size of the vector.
Hash m_hash; // Keep track of the constructed hash through e.g the constructors, so we can use it for the hashing functions in the STL
// Grow factor must be defined before m_bucket_count, otherwise we can't initialize the latter properly (initialization happens from top-bottom)
inline static const double grow_factor = 2.0; // The size of the table has to be a bit bigger than the total elements, to avoid too many collisions
inline static const double m_max_load_factor = 1.0; // Whenever the load factor is > than 0.75 we'll need to rehash
std::size_t m_bucket_count{}; // size of vector (= total buckets)
std::vector<std::list<std::pair<const Key, Type>>> m_table; // Actual hash table - each vector's element is composed off by a list - each list contains Key-Value pairs
// static used since all classes will share the same value (which is const), but also to make sure we can use the implicitly-declared move constructor
public:
using key_type = Key;
using mapped_type = Type;
using value_type = std::pair<const Key, mapped_type>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using hasher = Hash;
using reference = value_type&;
using const_reference = const reference;
using pointer = value_type*;
using const_pointer = const pointer;
// Default constructor
constexpr HashTable(const hasher& hash = Hash())
: m_size{ 0 }
, m_hash{ hash }
, m_bucket_count{ 0 }
{}
// Init-list constructor.
// Each item in the list is a key-value pair: thus we can directly use the hash functions through the Key, and use said key with the hash-function, which returns a value of type std::size_t. This is used
// for fast-indexing on our vector (which itself contains a hash table)
constexpr explicit HashTable(std::initializer_list<value_type> list, const hasher& hash = Hash())
: m_size{ list.size() }
, m_hash{ hash }
, m_bucket_count{ static_cast<size_type>(list.size() * grow_factor) }
, m_table(static_cast<size_type>(list.size()* grow_factor)) // Note: () needed here to disambiguate ({} would call vector's initializer_list constructor)
{
for (auto current : list) {
insert(current);
}
}
// By principle we don't need any copy/move constructors/assignment operators, since we own no resources.
// But since the implicit move constructor/operator= does not reset our m_size variable, we'll define them nonetheless.
// We could just use the implicit ones, too.
constexpr HashTable(const HashTable& other)
: m_bucket_count{ other.m_bucket_count }
, m_hash{ other.m_hash }
, m_size{ other.m_size }
, m_table{ other.m_table }
{}
constexpr HashTable(HashTable&& other) noexcept
: HashTable() {
other.swap(*this);
}
constexpr HashTable& operator=(HashTable&& other) noexcept {
HashTable temp;
other.swap(*this);
temp.swap(other);
return *this;
}
constexpr HashTable& operator=(const HashTable& other) {
HashTable temp(other);
temp.swap(*this);
return *this;
}
constexpr HashTable& operator=(std::initializer_list<value_type> list) {
HashTable temp{ list };
temp.swap(*this);
return *this;
}
// Capacity related
constexpr bool empty() const noexcept {
return m_size == 0;
}
constexpr size_type size() const noexcept {
return m_size;
}
// Modifiers
constexpr void clear() noexcept {
m_table.clear();
m_size = 0;
m_bucket_count = 0;
}
private:
constexpr bool key_found(const std::list<std::pair<const Key, mapped_type>>& bucket, const value_type& value) {
for (const auto& element : bucket) {
if (element.first == value.first) {
return true;
}
}
return false;
}
public:
// Should be O(1) average, O(n) if rehashing is done (on average)
constexpr bool insert(const value_type& value) {
size_type index{ m_hash(value.first) % m_bucket_count };
auto& bucket{ m_table.at(index) };
if (key_found(bucket, value)) return false;
bucket.push_front(value);
++m_size;
if (calculate_load_factor() > m_max_load_factor) {
rehash();
}
return true;
}
constexpr bool insert(value_type&& value) {
size_type index{ m_hash(value.first) % m_bucket_count };
auto& bucket{ m_table.at(index) };
if (key_found(bucket, value)) return false;
bucket.push_front(std::move(value));
++m_size;
if (calculate_load_factor() > m_max_load_factor) {
rehash();
}
return true;
}
constexpr void insert(std::initializer_list<value_type> list) {
for (auto elem : list) {
insert(elem);
}
}
template<typename Val>
// Amortized O(1); worst case O(n)
constexpr bool insert_or_assign(const Key& key, Val&& value) {
size_type index{ m_hash(key) % m_bucket_count };
auto& bucket{ m_table.at(index) };
// Search for the key. If it exists, assign value to that key. Otherwise, insert the new value (through std::pair<key, std::forward<Val>(value))
for (auto& element : bucket) {
if (element.first == key) {
element.second = value;
return false;
}
}
return insert(std::pair<Key, mapped_type>(key, std::forward<Val>(value)));
}
template<typename...Args>
constexpr bool emplace(Args&&...args) {
size_type index{ m_hash(std::pair<Key,Type>(std::forward<Args>(args)...).first) % m_bucket_count };
auto& bucket{ m_table.at(index) };
if (key_found(bucket, std::pair<Key, Type>(std::forward<Args>(args)...))) return false;
bucket.emplace_front(std::forward<Args>(args)...);
++m_size;
if (calculate_load_factor() > m_max_load_factor) {
rehash();
}
return true;
}
// Average O(1) [e.g list doesn't contain lots of elements], forst O(n)
constexpr bool remove_by_key(const Key& key) {
size_type index{ m_hash(key) % m_bucket_count };
auto& bucket{ m_table.at(index) };
for (const auto& elem : bucket) {
if (elem.first == key) {
bucket.remove(elem);
--m_size;
return true;
}
}
return false;
}
// Average O(n) [eg. list doesn't contain lots of elements], worst O(n^2)
constexpr bool remove_by_value(const Type& value) {
for (size_type index{ 0 }; auto & current_list : m_table) {
for (auto& current_pair : current_list) {
if (current_pair.second == value) {
m_table.at(index).remove(current_pair);
--m_size;
return true;
}
}
++index;
}
return false;
}
// Lookup functions
constexpr Type& at(const Key& key) {
size_type index{ m_hash(key) % m_bucket_count };
auto& current_list{ m_table.at(index) };
for (auto& current : current_list) {
if (current.first == key)
return current.second;
}
throw std::out_of_range("Specified key is not associated with any element");
}
constexpr const Type& at(const Key& key) const {
size_type index{ m_hash(key) % m_size };
auto& current_list{ m_table.at(index) };
for (const auto& current : current_list) {
if (current.first == key)
return current.second;
}
throw std::out_of_range("Specified key is not associated with any element");
}
constexpr Type& operator[](const Key& key) {
size_type index{ m_hash(key) % m_bucket_count };
auto& current_list{ m_table.at(index) };
for (auto& current : current_list) {
if (current.first == key)
return current.second;
}
return current_list.front().second;
}
constexpr const Type& operator[](const Key& key) const {
size_type index{ m_hash(key) % m_bucket_count };
auto& current_list{ m_table.at(index) };
for (const auto& current : current_list) {
if (current.first == key)
return current.second;
}
return current_list.front().second;
}
// Average O(1), worst case O(n)
constexpr size_type count(const Key& key) const {
size_type index{ m_hash(key) % m_bucket_count };
auto& bucket{ m_table.at(index) };
for (const auto& elem : bucket) {
if (elem.first == key) {
return 1;
}
}
return 0;
}
// Average O(1), worst O(n)
constexpr bool contains_key(const Key& key) const {
return count(key) == 1 ? true : false;
}
// Average O(n), worst O(n^2)
constexpr bool contains_value(const Type& value) const {
for (size_type index{ 0 }; auto & current_list : m_table) {
for (auto& current_pair : current_list) {
if (current_pair.second == value) {
return true;
}
}
}
return false;
}
// Bucket interface
constexpr size_type bucket_count() const noexcept {
return m_bucket_count;
}
constexpr size_type max_bucket_count() const noexcept {
return m_table.max_size();
}
constexpr size_type bucket_size(size_type index) const noexcept {
return m_table.at(index).size();
}
constexpr size_type bucket(const Key& key) const {
const size_type index{ m_hash(key) % m_bucket_count };
return index;
}
// Hash related
constexpr double load_factor() const noexcept {
return calculate_load_factor();
}
constexpr double max_load_factor() const noexcept {
return m_max_load_factor;
}
constexpr void max_load_factor(double new_factor) {
m_max_load_factor = new_factor;
}
constexpr void reserve(size_type count) {
m_bucket_count = count;
rehash(count);
}
private:
constexpr double calculate_load_factor() const noexcept {
return static_cast<double>(m_size) / m_bucket_count;
}
// Average case O(n), worst O(n^2)
constexpr void rehash(size_type n) {
hash_table temp{ m_table }; // Copy the contents of the current hash table
m_table.clear(); // Remove all elements from our table
m_size = 0; // Reset the size (total elements in the table). The insert function will increase it on each insertion.
m_bucket_count = n; // Double the total number of buckets
m_table.resize(m_bucket_count);
for (const auto& current_bucket : temp) {
for (const auto& current_pair : current_bucket) {
insert(current_pair);
}
}
}
public:
constexpr void rehash() {
hash_table temp{ m_table };
m_table.clear();
m_size = 0;
m_bucket_count = static_cast<size_type>(m_bucket_count * grow_factor);
m_table.resize(m_bucket_count);
for (const auto& current_bucket : temp) {
for (const auto& current_pair : current_bucket) {
insert(current_pair);
}
}
}
public:
constexpr const hash_table get_table() const noexcept {
return m_table;
}
constexpr void swap(HashTable& other) noexcept {
std::swap(m_size, other.m_size);
std::swap(m_bucket_count, other.m_bucket_count);
std::swap(m_hash, other.m_hash);
std::swap(m_table, other.m_table);
}
};
}
#endif