-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHashMap.hpp
286 lines (243 loc) · 6.68 KB
/
HashMap.hpp
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
#pragma once
#include <nstd/Memory.hpp>
template<typename T, typename V> class HashMap
{
private:
struct Item;
public:
class Iterator
{
public:
Iterator() : item(0) {}
const T& key() const {return item->key;}
const V& operator*() const {return item->value;}
V& operator*() {return item->value;}
const V* operator->() const {return &item->value;}
V* operator->() {return &item->value;}
const Iterator& operator++() {item = item->next; return *this;}
const Iterator& operator--() {item = item->prev; return *this;}
Iterator operator++() const {return item->next;}
Iterator operator--() const {return item->prev;}
bool operator==(const Iterator& other) const {return item == other.item;}
bool operator!=(const Iterator& other) const {return item != other.item;}
private:
Item* item;
Iterator(Item* item) : item(item) {}
friend class HashMap;
};
HashMap() : _end(&endItem), _begin(&endItem), _size(0), capacity(500), data(0), freeItem(0), blocks(0)
{
endItem.prev = 0;
endItem.next = 0;
}
HashMap(const HashMap& other) : _end(&endItem), _begin(&endItem), _size(0), capacity(500), data(0), freeItem(0), blocks(0)
{
endItem.prev = 0;
endItem.next = 0;
for(const Item* i = other._begin.item, * end = &other.endItem; i != end; i = i->next)
append(i->key, i->value);
}
explicit HashMap(usize capacity) : _end(&endItem), _begin(&endItem), _size(0), capacity(capacity), data(0), freeItem(0), blocks(0)
{
endItem.prev = 0;
endItem.next = 0;
this->capacity |= (usize)!capacity;
}
~HashMap()
{
delete[] (char*)data;
for(Item* i = _begin.item, * end = &endItem; i != end; i = i->next)
i->~Item();
for(ItemBlock* i = blocks, * next; i; i = next)
{
next = i->next;
delete[] (char*)i;
}
}
HashMap& operator=(const HashMap& other)
{
clear();
for(const Item* i = other._begin.item, * end = &other.endItem; i != end; i = i->next)
append(i->key, i->value);
return *this;
}
const Iterator& begin() const {return _begin;}
const Iterator& end() const {return _end;}
const T& front() const { return _begin.item->value; }
const T& back() const { return _end.item->prev->value; }
V& front() { return _begin.item->value; }
V& back() { return _end.item->prev->value; }
Iterator removeFront() {return remove(_begin);}
Iterator removeBack() {return remove(_end.item->prev);}
usize size() const {return _size;}
bool isEmpty() const {return endItem.prev == 0;}
V& prepend(const T& key, const V& value) {return insert(_begin, key, value).item->value;}
V& append(const T& key, const V& value) {return insert(_end, key, value).item->value;}
void clear()
{
for(Item* i = _begin.item, * end = &endItem; i != end; i = i->next)
{
i->~Item();
*i->cell = 0;
i->prev = freeItem;
freeItem = i;
}
_begin.item = &endItem;
endItem.prev = 0;
_size = 0;
}
void swap(HashMap& other)
{
Item* tmpFirst = _begin.item;
Item* tmpLast = endItem.prev;
usize tmpSize = _size;
usize tmpCapacity = capacity;
Item** tmpData = data;
Item* tmpFreeItem = freeItem;
ItemBlock* tmpBlocks = blocks;
if((endItem.prev = other.endItem.prev))
{
endItem.prev->next = &endItem;
_begin.item = other._begin.item;
}
else
_begin.item = &endItem;
_size = other._size;
capacity = other.capacity;
data = other.data;
freeItem = other.freeItem;
blocks = other.blocks;
if((other.endItem.prev = tmpLast))
{
tmpLast->next = &other.endItem;
other._begin.item = tmpFirst;
}
else
other._begin.item = &other.endItem;
other._size = tmpSize;
other.capacity = tmpCapacity;
other.data = tmpData;
other.freeItem = tmpFreeItem;
other.blocks = tmpBlocks;
}
Iterator find(const T& key) const
{
if(!data) return _end;
usize hashCode = hash(key);
Item* item = data[hashCode % capacity];
while(item)
{
if(item->key == key) return item;
item = item->nextCell;
}
return _end;
}
bool contains(const T& key) const {return find(key) != _end;}
Iterator insert(const Iterator& position, const T& key, const V& value)
{
Iterator it = find(key);
if(it != _end)
{
*it = value;
return it;
}
if(!data)
{
data = (Item**)new char[sizeof(Item*) * capacity];
Memory::zero(data, sizeof(Item*) * capacity);
}
Item* item;
if(freeItem)
{
item = freeItem;
freeItem = freeItem->prev;
}
else
{
ItemBlock* itemBlock = (ItemBlock*)new char[sizeof(ItemBlock) + sizeof(Item) * 4];
itemBlock->next = blocks;
blocks = itemBlock;
item = (Item*)((char*)itemBlock + sizeof(ItemBlock));
for(Item* i = item + 1, * end = item + 4; i < end; ++i)
{
i->prev = freeItem;
freeItem = i;
}
}
usize hashCode = hash(key);
#ifdef VERIFY
VERIFY(new(item) Item(key, value) == item);
#else
new(item) Item(key, value);
#endif
Item** cell;
item->cell = (cell = &data[hashCode % capacity]);
if((item->nextCell = *cell))
item->nextCell->cell = &item->nextCell;
*cell = item;
Item* insertPos = position.item;
if((item->prev = insertPos->prev))
insertPos->prev->next = item;
else
_begin.item = item;
item->next = insertPos;
insertPos->prev = item;
++_size;
return item;
}
Iterator remove(const Iterator& it)
{
Item* item = it.item;
if((*item->cell = item->nextCell))
item->nextCell->cell = item->cell;
if(!item->prev)
(_begin.item = item->next)->prev = 0;
else
(item->prev->next = item->next)->prev = item->prev;
--_size;
item->~Item();
item->prev = freeItem;
freeItem = item;
return item->next;
}
void remove(const T& key)
{
Iterator it = find(key);
if(it != _end)
remove(it);
}
bool operator==(const HashMap& other) const
{
if(_size != other._size)
return false;
for(const Item* a = _begin.item, * b = other._begin.item; a != &endItem; a = a->next, b = b->next)
if(a->key != b->key || a->value != b->value)
return false;
return true;
}
bool operator!=(const HashMap& other) const {return !(*this == other);}
private:
struct Item
{
const T key;
V value;
Item** cell;
Item* nextCell;
Item* prev;
Item* next;
Item() : key(), value() {}
Item(const T& key, const V& value) : key(key), value(value) {}
};
struct ItemBlock
{
ItemBlock* next;
};
Iterator _end;
Iterator _begin;
usize _size;
usize capacity;
Item** data;
Item endItem;
Item* freeItem;
ItemBlock* blocks;
};