-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_map.h
399 lines (348 loc) · 12.8 KB
/
hash_map.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <algorithm>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <vector>
using std::vector;
using std::pair;
//Iterator class, to iterate through HashMap, made with builtin iterators to container which is used in HashMap realisation.
//Hashmap contains vector of vectors(chains), therefore iterator consists of two iterators - one (id) point at the specific chain,
//another(pos) points at the position in the chain. Also iterator last is stored, it points at the last chain, it is
//needed for realization needs. While iterator is constructed, id is moved to the first non-empty chain from current, pos is moved to its
//first element, the similar is done in increment operator(after we go to next element, if we were in the last element of a chain
//we go to the next chain, and search for the first non-empty one (if it wasn't last element of chain we go to next in this chain)),
//that is why iterator is always either on valid position or end(). That also means, that as soon as hashtable contains O(n)
//empty chains where n is number of elements, iteration through hashtable takes n + O(n) = O(n) operations.
//Iterators become invalid if table is rebuilt.
//Sadly I can't find my algorithm in internet, soon of all it is not used in real life.
template<class KeyType, class ValueType>
class Iterator : public std::iterator<std::forward_iterator_tag, pair<const KeyType, ValueType> > {
public:
typedef pair<const KeyType, ValueType> value_type;
typedef typename vector<vector<value_type>>::iterator chains_it_type;
typedef typename vector<value_type>::iterator in_chain_it_type;
chains_it_type id, last;
in_chain_it_type pos;
//default constructor
Iterator() {}
//consturcts iterator from given iterators to builtin container, contained in HashMap.
Iterator(chains_it_type _id, chains_it_type _last, in_chain_it_type _pos) {
id = _id;
last = _last;
pos = _pos;
while(pos == id->end() && id != last) {
++id;
pos = id->begin();
}
}
//Copy constructor
Iterator(const Iterator& it) : id(it.id), last(it.last), pos(it.pos) {}
//Increment, makes iterator point to the next element and return its new version. O(1) amortized.
Iterator& operator++() {
if (id == last && pos == id->end()) {
return *this;
}
++pos;
while(pos == id->end() && id != last) {
++id;
pos = id->begin();
}
return *this;
}
//Increment, but returns the old version of iterator. O(1) amortized.
Iterator operator++(int) {
if (id == last && pos == id->end()) {
return *this;
}
Iterator tmp = *this;
++pos;
while(pos == id->end() && id != last) {
++id;
pos = id->begin();
}
return tmp;
}
//dereference operator, returns element, at which iterator points
value_type& operator*() {
return *pos;
}
//convinient dereference, giving direct acces to any field of object obtained by iterator.
value_type* operator->() {
return &(*pos);
}
//comparsion operator, returns true if iterators are pointing at the same element
bool operator ==(const Iterator& a) const {
return (id == a.id && pos == a.pos);
}
//anticomparsion
bool operator !=(const Iterator& a) const {
return !(id == a.id && pos == a.pos);
}
};
//Constant iterator similar to usual iterator, but doesn't allow to change object obtained by iterator.
template<class KeyType, class ValueType>
class Const_iterator : public std::iterator<std::forward_iterator_tag, const pair<const KeyType, ValueType> > {
public:
typedef const pair<const KeyType, ValueType> cvalue_type;
typedef pair<const KeyType, ValueType> value_type;
typedef typename vector<vector<value_type>>::const_iterator chains_it_type;
typedef typename vector<value_type>::const_iterator in_chain_it_type;
chains_it_type id, last;
in_chain_it_type pos;
Const_iterator() {}
Const_iterator(chains_it_type _id, chains_it_type _last, in_chain_it_type _pos) {
id = _id;
last = _last;
pos = _pos;
while(pos == id->end() && id != last) {
++id;
pos = id->begin();
}
}
Const_iterator(const Const_iterator& it) : id(it.id), last(it.last), pos(it.pos) {}
Const_iterator& operator++() {
if (id == last && pos == id->end()) {
return *this;
}
++pos;
while(pos == id->end() && id != last) {
++id;
pos = id->begin();
}
return *this;
}
Const_iterator operator++(int) {
if (id == last && pos == id->end()) {
return *this;
}
Const_iterator tmp = *this;
++pos;
while(pos == id->end() && id != last) {
++id;
pos = id->begin();
}
return tmp;
}
cvalue_type& operator*() const {
return *pos;
}
cvalue_type* operator->() const {
return &(*pos);
}
bool operator ==(const Const_iterator& a) const {
return (id == a.id && pos == a.pos);
}
bool operator !=(const Const_iterator& a) const {
return !(id == a.id && pos == a.pos);
}
};
//HashTable with chain method, elements are stored in std::vector.
//https://en.wikipedia.org/wiki/Hash_table
//Makes table _multiplicator times bigger/smaller if load factor is too big/small.
template<class KeyType, class ValueType, class Hash = std::hash<KeyType> >
class HashMap {
public:
typedef pair<const KeyType, ValueType> value_type;
using iterator = Iterator<KeyType, ValueType>;
using const_iterator = Const_iterator<KeyType, ValueType>;
size_t _maxload = 1;
size_t _minload = 8;
//how many times table size is decreased or increased while being rebuilt.
constexpr static size_t _multiplicator = 2;
//constructs empty Hash table with given hash function.
HashMap(const Hash& _hasher = Hash()) : hasher(_hasher) {
table.resize(1);
}
//copy constructor.
HashMap(const HashMap& other) {
hasher = other.hasher;
number = other.number;
table.resize(other.table.size());
for(size_t id = 0; id < other.table.size(); ++id) {
table[id].clear();
for(const value_type& x : other.table[id]) {
table[id].push_back(x);
}
}
}
//operator=, returns reference to new version.
HashMap& operator =(const HashMap& tother) {
HashMap other(tother);
hasher = other.hasher;
number = other.number;
table.resize(other.table.size());
for(size_t id = 0; id < other.table.size(); ++id) {
table[id].clear();
for(value_type x : other.table[id]) {
table[id].push_back(x);
}
}
return *this;
}
//constucts hash table from iterable container.
template<typename Iter>
HashMap(Iter first_element, Iter last_element, const Hash& _hasher = Hash()) : hasher(_hasher) {
Iter tf = first_element;
while (tf != last_element) {
++number;
++tf;
}
table.resize(_maxload * number);
while (first_element != last_element) {
size_t id = hasher(first_element->first) % table.size();
table[id].push_back(*first_element);
++first_element;
}
}
//constructs Hash table from std::initializer_list.
HashMap(std::initializer_list<value_type> l, const Hash& _hasher = Hash()) : hasher(_hasher) {
table.resize(_maxload * l.size());
for (value_type p : l) {
size_t id = hasher(p.first) % table.size();
table[id].push_back(p);
++number;
}
}
void set_maxload(size_t load) {
_maxload = load;
}
void set_minload(size_t load) {
_minload = load;
}
size_t get_maxload() {
return _maxload;
}
size_t get_minload() {
return _minload;
}
size_t size() const {
return number;
}
bool empty() const {
return number == 0;
}
Hash hash_function() const {
return hasher;
}
//insert given element in container, O(1) expected.
void insert(const value_type& p) {
size_t id = hasher(p.first) % table.size();
if (find_pos(p.first) == table[id].size()) {
table[id].push_back(p);
++number;
fit_load();
}
}
//erase element with given key if such exists. O(1) expected.
void erase(const KeyType &key) {
size_t id = hasher(key) % table.size(), pos = find_pos(key);
if (pos != table[id].size()) {
vector<value_type> s;
for(size_t i = table[id].size() - (size_t)1; i > pos; --i) {
s.push_back(table[id][i]);
table[id].pop_back();
}
table[id].pop_back();
for(value_type x : s) table[id].push_back(x);
s.clear();
--number;
fit_load();
}
}
iterator begin() {
return iterator(table.begin(), table.end() - 1, table[0].begin());
}
const_iterator begin() const {
return const_iterator(table.begin(), table.end() - 1, table[0].begin());
}
iterator end() {
return iterator(table.end() - 1, table.end() - 1, (table.end() - 1)->end());
}
const_iterator end() const {
return const_iterator(table.end() - 1, table.end() - 1, (table.end() - 1)->end());
}
//returns iterator to element with given key, returns end() otherwise. O(1) expected.
iterator find(const KeyType& key) {
size_t id = hasher(key) % table.size(), pos = find_pos(key);
if (pos != table[id].size()) {
return iterator(table.begin() + id, table.end() - 1, table[id].begin() + pos);
}
return end();
}
//returns constanst iterator to element with given key, through which elemnt can't be changed, returns end() otherwise. O(1) expected.
const_iterator find(const KeyType& key) const {
size_t id = hasher(key) % table.size(), pos = find_pos(key);
if (pos != table[id].size()) {
return const_iterator(table.begin() + id, table.end() - 1, table[id].begin() + pos);
}
return end();
}
//this operator takes key, and returns reference to object with such key, located in container. If table doesnt't contain such key, element with such key and default value is added. O(1) expected.
ValueType& operator[](const KeyType& key) {
size_t id = hasher(key) % table.size(), pos = find_pos(key);
if (pos == table[id].size()) {
table[id].push_back({key, ValueType()});
++number;
if (fit_load()) {
return find(key)->second;
} else {
return table[id].back().second;
}
}
return table[id][pos].second;
}
//return constant reference to the element with given key, which doesn't allow to change element. If no such key contained in table, throws exception std::out_of_range. O(1) expected.
const ValueType& at(const KeyType& key) const {
const_iterator it = find(key);
if(it == end()) {
throw std::out_of_range("");
}
return it->second;
}
//make this table empty.
void clear() {
table.clear();
table.resize(1);
number = 0;
}
private:
vector<vector<value_type>> table;
Hash hasher;
size_t number = 0;
//method used to find specific key in the chain. Returns position if found, chain size otherwise. O(chain_length).
size_t find_pos(const KeyType &key) const {
size_t id = hasher(key) % table.size(), pos = 0;
for(value_type p : table[id]) {
if(p.first == key) {
break;
}
++pos;
}
return pos;
}
//Rebuilds table by specified rules if load factor is not between 1/_minload and 1/_maxload. O(table size). returns true if table is rebuilt, false otherwise.
bool fit_load() {
if(number * _maxload <= table.size() && number * _minload >= table.size()) {
return false;
}
vector<value_type> values;
for(size_t id = 0; id < table.size(); ++id) {
for(const value_type& x : table[id]) {
values.push_back(x);
}
}
size_t nsz;
if (number * _maxload > table.size()) {
nsz = _multiplicator * table.size();
} else {
nsz = (table.size() + _multiplicator - 1) / _multiplicator;
}
table.clear();
table.resize(nsz);
for (const value_type& p : values) {
size_t id = hasher(p.first) % table.size();
table[id].push_back(p);
}
return true;
}
};