-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.h
327 lines (265 loc) · 5.18 KB
/
Map.h
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
//
// Created by pedro on 29/04/18.
//
#pragma once
#include "Vector.h"
template <class Key, class Value>
class Map
{
private:
//Linked list structure to handle collisions
struct MapNode
{
Key key;
Value value;
MapNode* next;
MapNode(const Key& key, const Value& value, MapNode* next)
: key(key), value(value), next(next)
{
}
};
//Capacity of slots in the
size_t slotsNum;
//Current size of the map
size_t size;
//Raw data representing the map
Vector<MapNode*> data;
public:
Map()
: slotsNum(16), data(Vector<MapNode*>(slotsNum)), size(0)
{
for(size_t i = 0; i < slotsNum; i++)
{
data.push(NULL);
}
}
Map(size_t slotsNum)
: slotsNum(slotsNum), data(Vector<MapNode*>(slotsNum)), size(0)
{
for(size_t i = 0; i < slotsNum; i++)
{
data.push(NULL);
}
}
size_t getSize() const
{
return size;
}
bool isEmpty() const
{
return size == 0;
}
/**
* Gets Value from a given Key. Is key does not exist, NULL is returned
* @param key Key. Must be hashable
* @return Value pointer or NULL
*/
Value* get(const Key& key)
{
size_t hash = Algorithm::hash<Key>()(key) % slotsNum;
MapNode* node = data[hash];
//Search for Value in list
while(node != NULL)
{
if(node->key == key)
{
return &node->value;
}
node = node->next;
}
return NULL;
}
/**
* Gets Value from a given Key. Is key does not exist, NULL is returned
* @param key Key. Must be hashable
* @return Value pointer or NULL
*/
const Value* get(const Key& key) const
{
size_t hash = Algorithm::hash<Key>()(key) % slotsNum;
MapNode* node = data[hash];
//Search for Value in list
while(node != NULL)
{
if(node->key == key)
{
return &node->value;
}
node = node->next;
}
return NULL;
}
/**
* Adds key pair value to the map. If key is already present, the value is updated.
* @param key Key. Must be hashable
* @param value Value. If key is already present, this becomes the new value.
*/
Value& add(const Key& key, const Value& value)
{
size_t hash = Algorithm::hash<Key>()(key) % slotsNum;
MapNode* head = data[hash];
//Check if key is already present
while (head != NULL)
{
if(head->key == key)
{
head->value = value;
return head->value;
}
head = head->next;
}
//Insert key in chain
size++;
head = data[hash];
MapNode* newNode = new MapNode(key, value, head);
data[hash] = newNode;
//If load factor is beyond threshold, double hash table size and rehash
if(float(size) / slotsNum >= 0.7)
{
Vector<MapNode*> tmp = data;
slotsNum *= 2;
data = Vector<MapNode*>(slotsNum);
size = 0;
for(size_t i = 0; i < slotsNum; i++)
{
data.push(NULL);
}
for(size_t i = 0; i < tmp.getSize(); i++)
{
MapNode* node = tmp[i];
while(node != NULL)
{
add(node->key, node->value);
MapNode* tmpNode = node->next;
delete node;
node = tmpNode;
}
}
//Search for new node
size_t newHash = Algorithm::hash<Key>()(key) % slotsNum;
MapNode* returnNode = data[newHash];
while(returnNode != NULL)
{
if(returnNode->key == key)
{
return returnNode->value;
}
returnNode = returnNode->next;
}
}
return newNode->value;
}
/**
* Deletes object from map. Does nothing if key is not found
* @param key Key. Must be hashable
*/
void remove(const Key& key)
{
size_t hash = Algorithm::hash<Key>()(key) % slotsNum;
MapNode* head = data[hash];
MapNode* prev = NULL;
while(head != NULL)
{
if(head->key == key)
{
break;
}
prev = head;
head = head->next;
}
//Key not found
if(head == NULL)
{
return;
}
size--;
if(prev == NULL)
{
//First in linked list
data[hash] = head->next;
}
else
{
prev->next = head->next;
}
delete head;
}
~Map()
{
//Destruct all created MapNodes
for(size_t i = 0; i < slotsNum; i++)
{
MapNode* head = data[i];
while(head != NULL)
{
MapNode* newHead = head->next;
delete head;
head = newHead;
}
}
}
Map(const Map<Key, Value>& other)
{
slotsNum = other.slotsNum;
size = 0;
data = Vector<MapNode*>(slotsNum);
for(size_t i = 0; i < slotsNum; i++)
{
data.push(NULL);
}
for(size_t i = 0; i < other.data.getSize(); i++)
{
MapNode* node = other.data[i];
while(node != NULL)
{
add(node->key, node->value);
node = node->next;
}
}
}
Map<Key, Value>& operator=(const Map<Key, Value>& other)
{
//Delete previous nodes
for(size_t i = 0; i < slotsNum; i++)
{
MapNode* head = data[i];
while(head != NULL)
{
MapNode* newHead = head->next;
delete head;
head = newHead;
}
}
//Create copy
slotsNum = other.slotsNum;
size = 0;
data = Vector<MapNode*>(slotsNum);
for(size_t i = 0; i < slotsNum; i++)
{
data.push(NULL);
}
for(size_t i = 0; i < other.data.getSize(); i++)
{
MapNode* node = other.data[i];
while(node != NULL)
{
add(node->key, node->value);
node = node->next;
}
}
return *this;
}
Value& operator[](const Key& key)
{
Value* value = get(key);
if(value == NULL)
{
return add(key, Value());
}
return *value;
}
bool exists(const Key& key) const
{
return get(key) != NULL;
}
};