-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.cpp
259 lines (208 loc) · 6.59 KB
/
HashMap.cpp
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
#include <initializer_list>
#include <list>
#include <stdexcept>
#include <utility>
#include <vector>
template<class KeyType, class ValueType, class Hash = std::hash<KeyType>>
class HashMap {
public:
HashMap(const Hash& hasher = Hash())
: hasher(hasher) {
clear();
}
template<typename Iter>
HashMap(Iter begin, Iter end, const Hash& hasher = Hash())
: hasher(hasher) {
clear();
while (begin != end) {
insert(*begin);
++begin;
}
}
HashMap(const std::initializer_list<std::pair<const KeyType, ValueType>>& items,
const Hash& hasher = Hash()) {
HashMap(items.begin(), items.end(), hasher);
}
void insert(const std::pair<const KeyType, ValueType>& item) {
if (static_cast<double>(sz) / data.size() >= LOAD_FACTOR_THRESHOLD) {
rebuild();
}
size_t ind = hasher(item.first) % data.size();
for (auto it : data[ind]) {
if (it.first == item.first) return;
}
++sz;
data[ind].push_back(item);
}
void erase(const KeyType& key) {
size_t ind = hasher(key) % data.size();
for (auto it = data[ind].begin(); it != data[ind].end(); ++it) {
if (it->first == key) {
data[ind].erase(it);
--sz;
return;
}
}
}
void rebuild() {
auto dataCopy = data;
size_t prevSize = data.size();
data.clear();
data.resize(prevSize * 2);
sz = 0;
for (auto lst : dataCopy) {
for (auto it : lst) {
insert(it);
}
}
}
size_t size() const {
return sz;
}
bool empty() const {
return size() == 0;
}
Hash hash_function() const {
return hasher;
}
template<bool isConst>
class AnyIterator {
public:
using iterator_category = std::forward_iterator_tag;
using reference = typename std::conditional<isConst, const std::pair<const KeyType,
ValueType>&, std::pair<const KeyType, ValueType>&>::type;
using pointer = typename std::conditional<isConst, const std::pair<const KeyType,
ValueType>*, std::pair<const KeyType, ValueType>*>::type;
using in_type = typename std::conditional<isConst, const HashMap<KeyType,
ValueType, Hash>*, HashMap<KeyType, ValueType, Hash>*>::type;
using list_iterator = typename std::conditional<isConst, typename std::list<std::pair<
const KeyType, ValueType>>::const_iterator, typename std::list<std::pair<
const KeyType, ValueType>>::iterator>::type;
AnyIterator() {}
AnyIterator(in_type hm, size_t ind, list_iterator now)
: hashMap(hm)
, ind(ind)
, now(now) {}
AnyIterator(in_type hm, size_t ind)
: hashMap(hm)
, ind(ind) {
if (ind >= hashMap->data.size()) {
this->ind = hashMap->data.size();
} else {
now = hashMap->data[ind].begin();
if (hashMap->data[ind].empty()) {
++(*this);
}
}
}
AnyIterator(in_type hm)
: hashMap(hm) {
ind = 0;
now = hashMap->data[ind].begin();
if (hashMap->data[ind].empty()) {
++(*this);
}
}
bool operator == (const AnyIterator& other) const {
if (hashMap != other.hashMap) return false; // Exception?
if (ind >= hashMap->data.size() && other.ind >= other.hashMap->data.size()) return true;
return ind == other.ind && now == other.now;
}
bool operator != (const AnyIterator& other) const {
return !(*this == other);
}
reference operator * () {
return *now;
}
pointer operator -> () {
return &(operator*());
}
AnyIterator& operator++ () {
if (now != hashMap->data[ind].end()) ++now;
if (now == hashMap->data[ind].end()) {
++ind;
while (ind < hashMap->data.size() && hashMap->data[ind].empty()) ++ind;
if (ind >= hashMap->data.size()) return *this;
now = hashMap->data[ind].begin();
}
return *this;
}
AnyIterator operator++ (int) {
auto copy = *this;
++(*this);
return copy;
}
size_t getInd() const {
return ind;
}
private:
in_type hashMap;
size_t ind;
list_iterator now;
};
using iterator = AnyIterator<false>;
using const_iterator = AnyIterator<true>;
iterator begin() {
return iterator(this);
}
iterator end() {
return iterator(this, data.size());
}
const_iterator begin() const {
return const_iterator(this);
}
const_iterator end() const {
return const_iterator(this, data.size());
}
iterator find(const KeyType& key) {
size_t ind = hasher(key) % data.size();
for (auto it = data[ind].begin(); it != data[ind].end(); ++it) {
if (key == it->first) {
return iterator(this, ind, it);
}
}
return end();
}
const_iterator find(const KeyType& key) const {
size_t ind = hasher(key) % data.size();
for (auto it = data[ind].begin(); it != data[ind].end(); ++it) {
if (key == it->first) {
return const_iterator(this, ind, it);
}
}
return end();
}
ValueType& operator[] (const KeyType& key) {
iterator it = find(key);
if (it == end()) {
insert({key, ValueType()});
it = find(key);
}
return it->second;
}
const ValueType& at(const KeyType& key) const {
const_iterator it = find(key);
if (it == end()) {
throw std::out_of_range("Such key doesn't exist.");
}
return it->second;
}
void clear() {
sz = 0;
data.clear();
data.resize(START_SIZE);
}
HashMap& operator= (HashMap other) {
clear();
for (auto &item : other) {
insert(item);
}
return *this;
}
private:
size_t sz;
std::vector<std::list<std::pair<const KeyType, ValueType>>> data;
Hash hasher;
static constexpr double LOAD_FACTOR_THRESHOLD = 0.75;
static constexpr int START_SIZE = 1;
};