-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_table.h
332 lines (288 loc) · 9.95 KB
/
hash_table.h
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
// Copyright (c) 2020, redotter
#pragma once
#include <algorithm>
#include <list>
#include <stdexcept>
#include <utility>
#include <vector>
template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
class HashMap {
public:
using KeyValuePair = std::pair<const KeyType, ValueType>;
using iterator = typename std::list<KeyValuePair>::iterator;
using const_iterator = typename std::list<KeyValuePair>::const_iterator;
HashMap(Hash hasher = Hash());
template<class ConstructIterator>
explicit HashMap(
ConstructIterator begin_iter,
ConstructIterator end_iter,
Hash hasher = Hash());
explicit HashMap(
std::initializer_list<KeyValuePair> init,
Hash hasher = Hash());
HashMap<KeyType, ValueType, Hash>& operator= (
const HashMap<KeyType, ValueType, Hash>& other);
size_t size() const;
bool empty() const;
Hash hash_function() const;
void insert(const KeyValuePair& item);
void erase(const KeyType& key);
void clear();
ValueType& operator[] (const KeyType& key);
const ValueType& at(const KeyType& key) const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
iterator find(const KeyType& key);
const_iterator find(const KeyType& key) const;
private:
class TableElement {
public:
bool is_used = false;
bool is_erased = false;
iterator iter;
size_t hash = 0;
TableElement() {}
explicit TableElement(iterator iter)
: is_used(true), iter(iter) {}
explicit TableElement(iterator iter, size_t hash)
: is_used(true), iter(iter), hash(hash) {}
static TableElement ErasedElement() {
TableElement erased_element;
erased_element.is_erased = true;
return erased_element;
}
bool UsedAndEqualsOrErased(const KeyType& key) const {
return (is_used && !(iter->first == key)) || is_erased;
}
};
class HashAndPosition {
public:
size_t hash;
size_t position;
HashAndPosition(size_t hash, size_t position)
: hash(hash), position(position) {}
};
const double maxLoadFactor_ = 0.75;
const size_t initialSize_ = 3;
size_t GetHash(const KeyType& key) const;
HashAndPosition GetHashAndPosition(const KeyType& key) const;
void RebuildTableIfNeeded();
size_t IncrementTableIndex(size_t table_index) const;
std::vector<TableElement> table_;
std::list<KeyValuePair> key_value_pairs_;
std::vector<size_t> elements_indices_;
Hash hasher_;
size_t table_size_ = 0;
size_t key_value_pairs_size_ = 0;
};
template<class KeyType, class ValueType, class Hash>
size_t HashMap<KeyType, ValueType, Hash>::GetHash(const KeyType& key) const {
return hasher_(key) % table_size_;
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::HashAndPosition
HashMap<KeyType, ValueType, Hash>::GetHashAndPosition(
const KeyType& key) const {
const size_t hash = GetHash(key);
size_t table_index = hash;
while (table_[table_index].UsedAndEqualsOrErased(key)) {
table_index = IncrementTableIndex(table_index);
}
return HashAndPosition(hash, table_index);
}
template<class KeyType, class ValueType, class Hash>
void HashMap<KeyType, ValueType, Hash>::RebuildTableIfNeeded() {
size_t actual_size = elements_indices_.size();
double load_factor = (actual_size + 1) * 1.0 / table_size_;
if (load_factor < maxLoadFactor_) {
return;
}
for (size_t table_index : elements_indices_) {
table_[table_index] = TableElement();
}
table_size_ = actual_size * 2;
table_.resize(table_size_);
elements_indices_.clear();
for (auto iter = key_value_pairs_.begin();
iter != key_value_pairs_.end(); iter++) {
HashAndPosition key_hash_pos = GetHashAndPosition(iter->first);
size_t table_index = key_hash_pos.position;
size_t hash = key_hash_pos.hash;
table_[table_index] = TableElement(iter, hash);
elements_indices_.push_back(table_index);
}
}
template<class KeyType, class ValueType, class Hash>
size_t HashMap<KeyType, ValueType, Hash>::IncrementTableIndex(
size_t table_index) const {
++table_index;
if (table_index >= table_size_) {
table_index = 0;
}
return table_index;
}
template<class KeyType, class ValueType, class Hash>
HashMap<KeyType, ValueType, Hash>::HashMap(Hash hasher)
: hasher_(hasher) {
clear();
}
template<class KeyType, class ValueType, class Hash>
template<class ConstructIterator>
inline HashMap<KeyType, ValueType, Hash>::HashMap(
ConstructIterator begin_iter,
ConstructIterator end_iter,
Hash hasher)
: hasher_(hasher) {
clear();
for (ConstructIterator iter = begin_iter; iter != end_iter; iter++) {
insert(*iter);
}
}
template<class KeyType, class ValueType, class Hash>
HashMap<KeyType, ValueType, Hash>::HashMap(
std::initializer_list<KeyValuePair> init,
Hash hasher)
: hasher_(hasher) {
clear();
for (const KeyValuePair& item : init) {
insert(item);
}
}
template<class KeyType, class ValueType, class Hash>
HashMap<KeyType, ValueType, Hash>&
HashMap<KeyType, ValueType, Hash>::operator=(
const HashMap<KeyType, ValueType, Hash>& other) {
const HashMap<KeyType, ValueType, Hash> copy = other;
clear();
hasher_ = copy.hasher_;
for (auto iter = copy.begin(); iter != copy.end(); iter++) {
insert(*iter);
}
return *this;
}
template<class KeyType, class ValueType, class Hash>
size_t HashMap<KeyType, ValueType, Hash>::size() const {
return key_value_pairs_size_;
}
template<class KeyType, class ValueType, class Hash>
bool HashMap<KeyType, ValueType, Hash>::empty() const {
return key_value_pairs_.empty();
}
template<class KeyType, class ValueType, class Hash>
Hash HashMap<KeyType, ValueType, Hash>::hash_function() const {
return hasher_;
}
template<class KeyType, class ValueType, class Hash>
void HashMap<KeyType, ValueType, Hash>::insert(const KeyValuePair& item) {
RebuildTableIfNeeded();
HashAndPosition key_hash_pos = GetHashAndPosition(item.first);
size_t table_index = key_hash_pos.position;
size_t hash = key_hash_pos.hash;
if (table_[table_index].is_used) {
return;
}
key_value_pairs_.push_back(item);
++key_value_pairs_size_;
const auto iter = std::prev(key_value_pairs_.end());
table_[table_index] = TableElement(iter, hash);
elements_indices_.push_back(table_index);
}
template<class KeyType, class ValueType, class Hash>
void HashMap<KeyType, ValueType, Hash>::erase(const KeyType& key) {
HashAndPosition key_hash_pos = GetHashAndPosition(key);
size_t table_index = key_hash_pos.position;
size_t hash = key_hash_pos.hash;
if (!table_[table_index].is_used) {
return;
}
auto iter = table_[table_index].iter;
key_value_pairs_.erase(iter);
--key_value_pairs_size_;
table_[table_index] = TableElement::ErasedElement();
size_t erased_table_index_to_fill = table_index;
table_index = IncrementTableIndex(table_index);
while (table_[table_index].is_used && table_index != hash) {
const size_t other_hash = table_[table_index].hash;
if (other_hash == hash) {
table_[erased_table_index_to_fill] = table_[table_index];
table_[table_index] = TableElement::ErasedElement();
erased_table_index_to_fill = table_index;
}
table_index = IncrementTableIndex(table_index);
}
}
template<class KeyType, class ValueType, class Hash>
ValueType& HashMap<KeyType, ValueType, Hash>::operator[](const KeyType& key) {
HashAndPosition key_hash_pos = GetHashAndPosition(key);
size_t table_index = key_hash_pos.position;
if (!table_[table_index].is_used) {
insert(std::make_pair(key, ValueType()));
}
key_hash_pos = GetHashAndPosition(key);
table_index = key_hash_pos.position;
return table_[table_index].iter->second;
}
template<class KeyType, class ValueType, class Hash>
const ValueType&
HashMap<KeyType, ValueType, Hash>::at(const KeyType& key) const {
HashAndPosition key_hash_pos = GetHashAndPosition(key);
size_t table_index = key_hash_pos.position;
if (!table_[table_index].is_used) {
throw std::out_of_range("");
}
return table_[table_index].iter->second;
}
template<class KeyType, class ValueType, class Hash>
void HashMap<KeyType, ValueType, Hash>::clear() {
for (size_t table_index : elements_indices_) {
table_[table_index] = TableElement();
}
table_size_ = std::max(table_size_, initialSize_);
table_.resize(table_size_);
elements_indices_.clear();
key_value_pairs_.clear();
key_value_pairs_size_ = 0;
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::iterator
HashMap<KeyType, ValueType, Hash>::begin() {
return iterator(key_value_pairs_.begin());
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::iterator
HashMap<KeyType, ValueType, Hash>::end() {
return iterator(key_value_pairs_.end());
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::const_iterator
HashMap<KeyType, ValueType, Hash>::begin() const {
return const_iterator(key_value_pairs_.begin());
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::const_iterator
HashMap<KeyType, ValueType, Hash>::end() const {
return const_iterator(key_value_pairs_.end());
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::iterator
HashMap<KeyType, ValueType, Hash>::find(const KeyType& key) {
HashAndPosition key_hash_pos = GetHashAndPosition(key);
size_t table_index = key_hash_pos.position;
if (!table_[table_index].is_used) {
return iterator(key_value_pairs_.end());
}
return iterator(table_[table_index].iter);
}
template<class KeyType, class ValueType, class Hash>
typename HashMap<KeyType, ValueType, Hash>::const_iterator
HashMap<KeyType, ValueType, Hash>::find(
const KeyType& key) const {
HashAndPosition key_hash_pos = GetHashAndPosition(key);
size_t table_index = key_hash_pos.position;
if (!table_[table_index].is_used) {
return const_iterator(key_value_pairs_.end());
}
return const_iterator(table_[table_index].iter);
}