-
Notifications
You must be signed in to change notification settings - Fork 0
/
unordered_map.cpp
207 lines (183 loc) · 6.5 KB
/
unordered_map.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <list>
template <class KeyType, class ValueType, class Hash = std::hash<KeyType>>
class HashMap {
private:
using PairValue = std::pair<const KeyType, ValueType>;
std::vector<std::list<typename std::list<PairValue>::iterator>> MainTable;
std::list<PairValue> TableOfValuesAndKeys;
Hash hasher;
size_t length = 0;
int RelocationConstant = 2;
void reallocation(std::vector<std::list<typename std::list<PairValue>::iterator>>& A, std::list<PairValue> &T2, size_t new_len) {
std::vector<std::list<iterator>> NewVersion(new_len);
std::list<PairValue> NewTableOfValuesAndKeys;
for (auto bucket : A) {
for (auto to = bucket.begin(); to != bucket.end(); ++to) {
size_t pos = hasher((*to)->first) % new_len;
if (!NewVersion[pos].empty()) {
auto next = NewVersion[pos].back();
auto cur = nex++;
NewTableOfValuesAndKeys.insert(next, *(*to));
NewVersion[pos].push_back(cur);
} else {
auto cur = NewTableOfValuesAndKeys.end();
NewTableOfValuesAndKeys.insert(cur, *(*to));
NewVersion[pos].push_back(--cur);
}
}
}
this->MainTable = std::move(NewVersion);
this-> TableOfValuesAndKeys = std::move(NewTableOfValuesAndKeys);
}
public:
using iterator = typename std::list<PairValue>::iterator;
using const_iterator = typename std::list<PairValue>::const_iterator;
void insert(const std::pair<KeyType,ValueType> &A) {
size_t pos = hasher(A.first) % MainTable.size();
bool ok = false;
for (auto to = MainTable[pos].begin(); to != MainTable[pos].end(); ++to) {
if ((*to)->first == A.first) {
ok = true;
break;
}
}
if (!ok) {
if (MainTable[pos].size()) {
auto next = MainTable[pos].back();
auto cur = next++;
TableOfValuesAndKeys.insert(next, A);
MainTable[pos].push_back(cur);
} else {
auto cur = TableOfValuesAndKeys.end();
TableOfValuesAndKeys.insert(cur, A);
--cur;
MainTable[pos].push_back(cur);
}
++length;
}
if (length == MainTable.size()) {
reallocation(MainTable, TableOfValuesAndKeys, MainTable.size() * RelocationConstant);
}
}
template<class it>
HashMap(it begin, it end, Hash new_hasher = Hash()) : hasher(new_hasher) {
MainTable.resize(RelocationConstant);
while (begin != end) {
auto new_element = *begin;
insert(new_element);
begin++;
}
}
HashMap(const std::initializer_list<std::pair<const KeyType, ValueType>> &A, Hash new_hasher = Hash()) : HashMap(A.begin(), A.end(), new_hasher) {
}
HashMap() : HashMap({}, std::hash<KeyType>()) {
}
size_t size() const {
return length;
}
bool empty() const {
return (length == 0);
}
Hash hash_function() const {
return hasher;
}
void erase(const KeyType &key) {
size_t pos = hasher(key) % MainTable.size();
for (auto iter = MainTable[pos].begin(); iter != MainTable[pos].end(); ++iter) {
if ((*iter)->first == key) {
TableOfValuesAndKeys.erase(*iter);
MainTable[pos].erase(iter);
--length;
break;
}
}
if (length <= MainTable.size() / (RelocationConstant * RelocationConstant)) {
size_t new_size = length * RelocationConstant;
if (new_size == 0) {
new_size = RelocationConstant;
}
reallocation(MainTable, TableOfValuesAndKeys, new_size);
}
}
iterator begin() {
return TableOfValuesAndKeys.begin();
}
iterator end() {
return TableOfValuesAndKeys.end();
}
const_iterator begin() const {
return TableOfValuesAndKeys.cbegin();
}
const_iterator end() const {
return TableOfValuesAndKeys.end();
}
iterator find(const KeyType &key) {
size_t pos = hasher(key) % MainTable.size();
for (auto to = MainTable[pos].begin(); to != MainTable[pos].end(); ++to) {
if ((*to)->first == key) {
return *to;
}
}
return end();
}
const_iterator find(const KeyType &key) const {
size_t pos = hasher(key) % MainTable.size();
for (auto to = MainTable[pos].begin(); to != MainTable[pos].end(); ++to) {
if ((*to)->first == key) {
return *to;
}
}
return end();
}
ValueType &operator[](const KeyType &K) {
auto to = find(K);
if (to == end()) {
insert({K, ValueType()});
}
to = find(K);
return to->second;
}
const ValueType &at(const KeyType &K) const {
auto to = find(K);
if (to == end()) {
throw std::out_of_range("Shuba_Buba");
}
return (*to).second;
}
HashMap(const HashMap &other) {
clear();
hasher = other.hasher;
for (const auto &to : other.TableOfValuesAndKeys) {
insert(to);
}
}
HashMap &operator=(const HashMap &other) {
if (this == &other) {
return *this;
}
this->clear();
this->hasher = other.hasher;
for (const auto &it : other.TableOfValuesAndKeys) {
this->insert(it);
}
return *this;
}
HashMap(Hash _) : hasher(_) {
clear();
}
void clear() {
for (size_t t = 0; t < MainTable.size(); ++t) {
for (auto &to : MainTable[t]) {
to = TableOfValuesAndKeys.end();
}
}
MainTable.clear();
TableOfValuesAndKeys.clear();
MainTable.resize(RelocationConstant);
length = 0;
}
};