-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.h
More file actions
265 lines (220 loc) · 7.25 KB
/
hashmap.h
File metadata and controls
265 lines (220 loc) · 7.25 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
#include <memory>
#include <algorithm>
#include <vector>
template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
class HashMap {
using TableType = std::vector<std::unique_ptr<std::pair<const KeyType, ValueType>>>;
using TableIterator = typename TableType::iterator;
using ConstTableIterator = typename TableType::const_iterator;
public:
using SizeType = size_t;
using PositionType = size_t;
template<typename TDataType, typename TIterator>
class HashMapIterator {
public:
using value_type = TDataType; // NOLINT
explicit HashMapIterator(TIterator it, TIterator end) : iterator_(it), end_(end) {
}
HashMapIterator() : iterator_(), end_() {
}
HashMapIterator &operator=(HashMapIterator other) {
iterator_ = other.iterator_;
end_ = other.end_;
return *this;
}
HashMapIterator &operator++() {
++iterator_;
while (iterator_ != end_ && !(*iterator_)) ++iterator_;
return *this;
}
HashMapIterator operator++(int) {
auto tmp = *this;
++(*this);
return tmp;
}
HashMapIterator &operator--() {
--iterator_;
while (iterator_ != end_ && !(*iterator_)) --iterator_;
return *this;
}
HashMapIterator operator--(int) {
auto tmp = *this;
--(*this);
return tmp;
}
TDataType &operator*() const {
return *(*iterator_);
}
TDataType *operator->() const {
return (*iterator_).get();
}
bool operator==(const HashMapIterator &other) const {
return iterator_ == other.iterator_ && end_ == other.end_;
}
bool operator!=(const HashMapIterator &other) const {
return iterator_ != other.iterator_ || end_ != other.end_;
}
protected:
TIterator iterator_;
TIterator end_;
};
using iterator = HashMapIterator<std::pair<const KeyType, ValueType>, TableIterator>;
using const_iterator = HashMapIterator<const std::pair<const KeyType, ValueType>, ConstTableIterator>;
HashMap(Hash hasher = Hash()) : hasher_(hasher), size_(0) {
capacity_ = 1;
table_.resize(1);
from_.resize(1);
};
template<typename Iterator>
HashMap(Iterator begin, Iterator end, Hash hasher = Hash()) : HashMap(hasher) {
for (auto it = begin; it != end; ++it) {
insert(*it);
}
}
HashMap(std::initializer_list<std::pair<KeyType, ValueType>> list, Hash hasher = Hash()) : HashMap(list.begin(),
list.end(),
hasher) {
}
HashMap &operator=(const HashMap &other) {
hasher_ = other.hash_function();
std::vector<std::pair<KeyType, ValueType>> values;
for (auto &p: other) values.push_back(p);
clear();
for (auto &p: values) insert(p);
return *this;
}
HashMap(const HashMap &other) : HashMap(other.begin(), other.end(), other.hash_function()) {
}
SizeType size() const {
return size_;
}
bool empty() const {
return size() == 0;
}
Hash hash_function() const {
return hasher_;
}
const_iterator begin() const {
auto it = const_iterator(table_.begin(), table_.end());
if (!table_[0]) it++;
return it;
}
const_iterator end() const {
return const_iterator(table_.end(), table_.end());
}
iterator begin() {
auto it = iterator(table_.begin(), table_.end());
if (!table_[0]) it++;
return it;
}
iterator end() {
return iterator(table_.end(), table_.end());
}
iterator find(KeyType key) {
PositionType h = FindPosition(key);
return iterator(h != capacity_ ? (table_.begin() + h) : table_.end(), table_.end());
}
const_iterator find(KeyType key) const {
PositionType h = FindPosition(key);
return const_iterator(h != capacity_ ? (table_.begin() + h) : table_.end(), table_.end());
}
ValueType &operator[](KeyType key) {
auto it = find(key);
if (it == end()) {
std::pair<KeyType, ValueType> elem;
elem.first = key;
insert(elem);
}
return find(key)->second;
}
const ValueType &at(KeyType key) const {
auto it = find(key);
if (it == end()) {
throw std::out_of_range("Key not found");
}
return it->second;
}
void clear() {
size_ = 0;
capacity_ = 1;
table_.resize(capacity_);
table_[0] = nullptr;
from_.resize(capacity_);
BLOCK_SIZE = DEFAULT_BLOCK_SIZE;
}
void insert(std::pair<KeyType, ValueType> elem) {
if (find(elem.first) != end()) return;
auto ptr = std::make_unique<std::pair<const KeyType, ValueType>>(elem);
while (true) {
PositionType h = GetBucket(ptr->first);
PositionType h2 = h;
PositionType diff = 0;
while (diff < BLOCK_SIZE && ptr) {
if (!table_[h2] || from_[h2] < diff) {
std::swap(table_[h2], ptr);
std::swap(from_[h2], diff);
}
diff++;
h2 = GetNext(h2);
}
if (!ptr) {
size_++;
return;
}
Rebuild();
}
}
void erase(KeyType key) {
PositionType h = FindPosition(key);
if (h == capacity_) {
return; // Key is not exist
}
size_--;
table_[h] = nullptr;
}
private:
static const size_t STANDARD_DIFF = 5;
const size_t DEFAULT_BLOCK_SIZE = 64;
size_t BLOCK_SIZE = DEFAULT_BLOCK_SIZE;
Hash hasher_;
SizeType size_;
SizeType capacity_;
TableType table_;
std::vector<PositionType> from_;
PositionType GetBucket(const KeyType &key) const {
return hasher_(key) & (capacity_ - 1);
}
inline PositionType GetNext(PositionType h) const {
return (h + 1) & (capacity_ - 1);
}
PositionType FindPosition(const KeyType &key) const {
PositionType h = GetBucket(key);
for (size_t it = 0; it < BLOCK_SIZE; ++it, h = GetNext(h)) {
if (table_[h] && table_[h]->first == key) {
return h;
}
}
return capacity_;
}
void Rebuild() {
TableType values;
for (auto &it_elem: table_) {
if (it_elem) {
values.push_back(std::move(it_elem));
}
}
capacity_ = 1;
while (size_ * STANDARD_DIFF > capacity_) capacity_ <<= 1;
table_.resize(capacity_);
from_.resize(capacity_);
std::vector<size_t> cnt(capacity_);
for (auto &elem: values) {
++cnt[GetBucket(elem->first)];
}
BLOCK_SIZE = std::max(DEFAULT_BLOCK_SIZE, 2 * (*std::max_element(cnt.begin(), cnt.end())));
size_ = 0;
for (auto &p: values) {
insert(*p);
}
}
};