-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.h
More file actions
357 lines (295 loc) · 9.07 KB
/
hash_map.h
File metadata and controls
357 lines (295 loc) · 9.07 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
#pragma once
#include <iostream>
#include <vector>
#include <initializer_list>
template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
class HashMap {
public:
using mypair = std::pair<KeyType, ValueType>;
using const_mypair = std::pair<const KeyType, ValueType>;
class Node {
public:
Node() = default;
explicit Node(const mypair& pair) : pair_(pair), exist_(true) {}
void delValue() {
exist_ = false;
dist_ = 0;
}
mypair& getValue(){
return pair_;
}
const mypair& getValue() const{
return pair_;
}
size_t &getDist() {
return dist_;
}
bool isExist() const {
return exist_;
}
protected:
mypair pair_;
bool exist_ = false;
size_t dist_ = 0;
};
using iter = typename std::vector<Node>::iterator;
using const_iter = typename std::vector<Node>::const_iterator;
class iterator {
public:
iterator() = default;
iterator(iter it, iter begin, iter end) {
iterator_ = it;
begin_ = begin;
end_ = end;
}
const_mypair& operator*() const {
return *(operator->());
}
const_mypair* operator->() const {
return reinterpret_cast<std::pair<const KeyType, ValueType>*>(&iterator_->getValue());
}
iterator &operator=(const iterator& other) {
iterator_ = other.iterator_;
begin_ = other.begin_;
end_ = other.end_;
return *this;
}
iterator &operator++() {
do {
++iterator_;
} while (iterator_ != end_ && !iterator_->isExist());
return *this;
}
iterator operator++(int) {
iterator new_iterator = *this;
do {
++iterator_;
} while (iterator_ != end_ && !iterator_->isExist());
return new_iterator;
}
iterator &operator--() {
do {
--iterator_;
} while (iterator_ != begin_ && !iterator_->isExist());
return *this;
}
iterator operator--(int) {
iterator new_iterator = *this;
do {
--iterator_;
} while (iterator_ != begin_ && !iterator_->isExist());
return new_iterator;
}
bool operator==(const iterator& other) const {
return other.iterator_ == iterator_;
}
bool operator!=(const iterator& other) const {
return other.iterator_ != iterator_;
}
iter& getIter(){
return iterator_;
}
size_t getInd(){
return iterator_ - begin_;
}
protected:
iter iterator_, begin_, end_;
};
class const_iterator {
public:
const_iterator() = default;
const_iterator(const_iter it, const_iter begin, const_iter end) {
iterator_ = it;
begin_ = begin;
end_ = end;
}
const const_mypair& operator*() const {
return *(operator->());
}
const const_mypair* operator->() const {
return reinterpret_cast<const std::pair<const KeyType, ValueType>*>(&iterator_->getValue());
}
const_iterator &operator=(const const_iterator& other) {
iterator_ = other.iterator_;
begin_ = other.begin_;
end_ = other.end_;
return *this;
}
const_iterator &operator++() {
do {
++iterator_;
} while (iterator_ != end_ && !iterator_->isExist());
return *this;
}
const const_iterator operator++(int) {
const_iterator new_iterator = *this;
do {
++iterator_;
} while (iterator_ != end_ && !iterator_->isExist());
return new_iterator;
}
const_iterator &operator--() {
do {
--iterator_;
} while (iterator_ != begin_ && !iterator_->isExist());
return *this;
}
const_iterator operator--(int) {
const_iterator new_iterator = *this;
do {
--iterator_;
} while (iterator_ != begin_ && !iterator_->isExist());
return new_iterator;
}
bool operator==(const const_iterator& other) const {
return other.iterator_ == iterator_;
}
bool operator!=(const const_iterator& other) const {
return other.iterator_ != iterator_;
}
protected:
const_iter iterator_, begin_, end_;
};
explicit HashMap(Hash hasher = Hash()) : hasher_(hasher) {}
HashMap(const std::initializer_list<mypair> list, Hash hasher = Hash()) : hasher_(hasher) {
resize(list.size() * 10);
for (auto &pair: list) insert(pair);
}
HashMap(const iterator begin, const iterator end,
Hash hasher = Hash()) : hasher_(hasher) {
for (auto it = begin; it != end; ++it) {
insert(*it);
}
}
size_t mod(size_t x) const{
if (x >= capacity_) return x - capacity_;
return x;
}
size_t size() const {
return n_;
}
bool empty() const {
return !n_;
}
Hash hash_function() const {
return hasher_;
}
bool needRebuild() {
return n_ * 10 > capacity_;
}
void insert(const mypair pair) {
if (find(pair.first) != end()) {
return;
}
size_t ind = hasher_(pair.first) % capacity_;
Node new_node = Node(pair);
while (new_node.isExist()) {
if (new_node.getDist() >= table_[ind].getDist()) {
std::swap(new_node, table_[ind]);
}
++new_node.getDist();
ind = mod(ind + 1);
}
++n_;
if (needRebuild()) {
rebuild();
}
}
void erase(const KeyType key) {
iterator it = find(key);
if (it == end()){
return;
}
size_t ind = it.getInd();
table_[ind].delValue();
while (table_[mod(ind + 1)].isExist() and table_[mod(ind + 1)].getDist()){
std::swap(table_[ind], table_[mod(ind + 1)]);
--table_[ind].getDist();
ind = mod(ind + 1);
}
--n_;
}
void rebuild() {
capacity_ *= 10;
std::vector<Node> new_table = std::vector<Node>(capacity_);
new_table.swap(table_);
n_ = 0;
for (auto &node: new_table) {
if (node.isExist()) {
insert(node.getValue());
}
}
}
iterator find(const KeyType key){
size_t ind = hasher_(key) % capacity_;
while (table_[ind].isExist()) {
if (table_[ind].getValue().first == key) {
return iterator(table_.begin() + ind, table_.begin(), table_.end());
}
ind = mod(ind + 1);
}
return end();
}
const_iterator find(const KeyType key) const {
size_t ind = hasher_(key) % capacity_;
while (table_[ind].isExist()) {
if (table_[ind].getValue().first == key) {
return const_iterator(table_.begin() + ind, table_.begin(), table_.end());
}
ind = mod(ind + 1);
}
return end();
}
ValueType &operator[](const KeyType key) {
insert({key, ValueType()});
return find(key)->second;
}
const ValueType& at(const KeyType key) const{
const_iterator it = find(key);
if (it == end()) {
throw std::out_of_range("out_of_range");
}
return it->second;
}
void clear() {
table_ = std::vector<Node>(capacity_);
n_ = 0;
}
void resize(size_t size){
table_ = std::vector<Node>(size);
capacity_ = size;
}
iterator begin() {
if (!n_) {
return iterator(table_.end(), table_.begin(), table_.end());
}
for (size_t i = 0; i < capacity_; ++i) {
if (table_[i].isExist()) {
return iterator(table_.begin() + i, table_.begin(), table_.end());
}
}
return iterator(table_.end(), table_.begin(), table_.end());
}
iterator end() {
return iterator(table_.end(), table_.begin(), table_.end());
}
const_iterator begin() const{
if (!n_) {
return const_iterator(table_.end(), table_.begin(), table_.end());
}
for (size_t i = 0; i < capacity_; ++i) {
if (table_[i].isExist()) {
return const_iterator(table_.begin() + i, table_.begin(), table_.end());
}
}
return const_iterator(table_.end(), table_.begin(), table_.end());
}
const_iterator end() const{
return const_iterator(table_.end(), table_.begin(), table_.end());
}
protected:
size_t capacity_ = 10;
size_t n_ = 0;
Hash hasher_;
std::vector<Node> table_ = std::vector<Node>(capacity_);
};