-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
294 lines (249 loc) · 8.72 KB
/
main.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
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
#include <iostream>
#include <istream>
#include <sstream>
#include <assert.h>
#include <string>
#include <vector>
#define PRIME_NUM1 13
#define PRIME_NUM2 17
// MARK:- HashFunc
template<class T> struct HashFunc;
template<> struct HashFunc<int> {
size_t operator() (const int& key, const int& simpleNum) {
return key;
}
};
template<> struct HashFunc<std::string> {
size_t operator() (const std::string& key, const int& simpleNum) {
size_t hash = 0;
int a = simpleNum;
for(int i = 0; i < key.size(); i++) {
hash = (hash * a + key[i]);
}
return hash;
}
};
// MARK:- Node
template <class Key, class Value>
struct Node {
Key key;
Value value;
// -1 - deleted
// 0 - free
// 1 - not free
int status;
void set(const Key& key, const Value& value, int status) {
this->key = key;
this->value = value;
this->status = status;
}
Node() {
key = "";
value = "";
status = 0;
}
};
// MARK:- HashTable
template <class Key, class Value, class Hash = HashFunc<Key>>
class HashTable {
public:
// MARK: Public Methods
explicit HashTable(Hash hash = Hash()): buckets(0), buckets_count(0), items_count(0), hash(hash) {
}
~HashTable() {
buckets.clear();
}
bool insert(const Key& key, const Value& value) {
if (!buckets.size() || (3 * buckets_count) < (4 * items_count)) {
grow();
}
size_t first_deleted = -1;
for (int i = 0; i < buckets.size(); i++) {
size_t index = hash_double(key, i);
if (buckets[index].status == 1 && buckets[index].key == key) {
return false;
} else if (buckets[index].status == 0 && first_deleted == -1) {
buckets[index].set(key, value, 1);
items_count++;
return true;
} else if (buckets[index].status == -1 && first_deleted == -1) {
first_deleted = index;
}
}
if (first_deleted != -1) {
buckets[first_deleted].set(key, value, 1);
items_count++;
return true;
} else {
return false;
}
}
bool find(const Key& key) {
for (int i = 0; i < buckets.size(); i++) {
size_t index = hash_double(key, i);
if (buckets[index].status == 1 && buckets[index].key == key) {
return true;
} else if (buckets[index].status == 0) {
return false;
}
}
return false;
}
bool remove(const Key& key) {
for (int i = 0; i < buckets.size(); i++) {
size_t index = hash_double(key, i);
if (buckets[index].status == 1 && buckets[index].key == key) {
buckets[index].status = -1;
items_count--;
return true;
} else if (buckets[index].status == 0) {
return false;
}
}
return false;
}
private:
// MARK: Properties
std::vector<Node<Key, Value>> buckets;
size_t buckets_count;
size_t items_count;
Hash hash;
// MARK: Private Methods
void grow() {
buckets_count = std::max(size_t(1), buckets_count * 2);
std::vector<Node<Key, Value>> old_buckets = buckets;
buckets = std::vector<Node<Key, Value>>(0);
items_count = 0;
for (int i = 0; i < buckets_count; i++) {
buckets.push_back(Node<Key, Value>());
}
for (int i = 0; i < old_buckets.size(); i++) {
if (old_buckets[i].status == 1) {
insert(old_buckets[i].key, old_buckets[i].value);
}
}
}
size_t hash_double(const Key& key, int iteration) {
size_t size = buckets_count;
size_t hash_value1 = hash(key, PRIME_NUM1);
size_t hash_value2 = 2 * hash(key, PRIME_NUM2) + 1;
return (hash_value1 + hash_value2 * iteration) % size;
}
};
int run(std::istream& input, std::ostream& output) {
HashTable<std::string, std::string> hash;
char operation;
std::string key;
while (input >> operation >> key) {
bool res = false;
switch (operation) {
case '+':
res = hash.insert(key, key);
break;
case '-':
res = hash.remove(key);
break;
case '?':
res = hash.find(key);
break;
default:
return 1;
}
if (res) {
output << "OK" << std::endl;
} else {
output << "FAIL" << std::endl;
}
}
return 0;
}
void testLogic() {
{
std::stringstream sstr_input;
std::string operation = "+";
for (int i = 0; i < 10000; i++) {
sstr_input << operation << " "<< std::to_string(i) << std::endl;
}
operation = "-";
for (int i = 0; i < 10000; i++) {
sstr_input << "?" << " " << std::to_string(i) << std::endl;
sstr_input << operation << " "<< std::to_string(i) << std::endl;
sstr_input << "?" << " " << std::to_string(i) << std::endl;
}
std::stringstream sstr_output;
run(sstr_input, sstr_output);
}
{
std::stringstream sstr_input;
for (int i = 0; i < 2; i++) {
sstr_input << "?" << " "<< std::to_string(i) << std::endl;
sstr_input << "+" << " "<< std::to_string(i) << std::endl;
sstr_input << "?" << " "<< std::to_string(i) << std::endl;
}
for (int i = 0; i < 2; i++) {
sstr_input << "-" << " "<< std::to_string(i) << std::endl;
sstr_input << "?" << " "<< std::to_string(i) << std::endl;
}
std::stringstream sstr_output;
run(sstr_input, sstr_output);
assert(sstr_output.str() == "FAIL\nOK\nOK\nFAIL\nOK\nOK\nOK\nFAIL\nOK\nFAIL\n");
std::cout << sstr_output.str() << std::endl;
}
{
std::stringstream sstr_input;
sstr_input << "?" << " " << "123" << std::endl;
sstr_input << "+" << " " << "123" << std::endl;
sstr_input << "+" << " " << "123" << std::endl;
sstr_input << "?" << " " << "123" << std::endl;
sstr_input << "-" << " " << "123" << std::endl;
sstr_input << "?" << " " << "123" << std::endl;
std::stringstream sstr_output;
run(sstr_input, sstr_output);
assert(sstr_output.str() == "FAIL\nOK\nFAIL\nOK\nOK\nFAIL\n");
std::cout << sstr_output.str() << std::endl;
}
{
std::stringstream sstr_input;
sstr_input << "+" << " " << "a" << std::endl;
sstr_input << "+" << " " << "ab" << std::endl;
sstr_input << "+" << " " << "abc" << std::endl;
sstr_input << "+" << " " << "abc1" << std::endl;
sstr_input << "+" << " " << "abc2" << std::endl;
sstr_input << "-" << " " << "a" << std::endl;
sstr_input << "+" << " " << "abcd" << std::endl;
sstr_input << "-" << " " << "ab" << std::endl;
sstr_input << "+" << " " << "abcde" << std::endl;
sstr_input << "+" << " " << "abcdef" << std::endl;
sstr_input << "?" << " " << "a" << std::endl;
sstr_input << "+" << " " << "a" << std::endl;
sstr_input << "?" << " " << "a" << std::endl;
sstr_input << "+" << " " << "a" << std::endl;
std::stringstream sstr_output;
run(sstr_input, sstr_output);
std::cout << sstr_output.str() << std::endl;
assert(sstr_output.str() == "OK\nOK\nOK\nOK\nOK\nOK\nOK\nOK\nOK\nOK\nFAIL\nOK\nOK\nFAIL\n");
std::cout << sstr_output.str() << std::endl;
}
{
std::stringstream sstr_input;
sstr_input << "+" << " " << "%%%%%%" << std::endl;
sstr_input << "+" << " " << "JJJ" << std::endl;
sstr_input << "+" << " " << "%%JJ" << std::endl;
sstr_input << "+" << " " << "J%%J" << std::endl;
sstr_input << "?" << " " << "J%%J" << std::endl;
sstr_input << "+" << " " << "JJ%%" << std::endl;
sstr_input << "+" << " " << "J%%%%" << std::endl;
sstr_input << "+" << " " << "%%%%J" << std::endl;
std::stringstream sstr_output;
run(sstr_input, sstr_output);
std::cout << sstr_output.str() << std::endl;
assert(sstr_output.str() == "OK\nOK\nOK\nOK\nOK\nOK\nOK\nOK\n");
std::cout << sstr_output.str() << std::endl;
}
}
int main(int argc, const char * argv[]) {
// testLogic();
// std::cout << "TEST PASSED" << std::endl;
// return 0;
return run(std::cin, std::cout);
}