-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.hpp
341 lines (269 loc) · 7.51 KB
/
map.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#pragma once
#include <cstddef>
#include <functional>
#include <vector>
#include <list>
#include <optional>
#include <initializer_list>
#include <iostream>
namespace dtf
{
template<typename K, typename V>
struct Record
{
K key;
V value;
Record(K &&key, V &&value) :
key(std::move(key)),
value(std::move(value))
{}
Record(const K &key, const V &value) :
key(key),
value(value)
{}
Record(Record &&item) noexcept :
key(std::move(item.key)),
value(std::move(item.value))
{}
Record(const Record &item) noexcept :
key(item.key),
value(item.value)
{}
Record& operator=(const Record &item)
{
key = item.key;
value = item.value;
return *this;
}
Record& operator=(Record &&item) noexcept
{
key = std::move(item.key);
value = std::move(item.value);
return *this;
}
};
// a hash table implementation that maintains insertion order using std::list
template<class K, class V>
class Map
{
public:
using IterType = typename std::list<Record<K, V>>::iterator;
using Chain = std::list<IterType>;
Map(std::initializer_list<Record<K&&, V&&>> list)
{
m_size = list.size();
construct(m_size * 2);
for (auto &[key, value] : list)
set(std::forward<K>(key), std::forward<V>(value));
}
Map()
{
construct(10);
}
Map(Map<K, V> &&map) noexcept
{
move(std::forward<Map<K, V>>(map));
}
Map(const Map<K, V> &map)
{
copy(map);
}
~Map()
{
delete[] m_bucket;
}
Map<K, V>& operator=(Map<K, V> &&map) noexcept
{
move(std::forward<Map<K, V>>(map));
return *this;
}
Map<K, V>& operator=(const Map<K, V> &map)
{
copy(map);
return *this;
}
[[nodiscard]]
constexpr inline
size_t size() const
{
return m_size;
}
[[nodiscard]]
constexpr inline
size_t capacity() const
{
return m_capacity;
}
[[nodiscard]]
constexpr inline
bool empty() const
{
return !m_size;
}
Record<K, V>& set(K &&key, V &&value)
{
Record<K, V> item(std::forward<K>(key), std::forward<V>(value));
return set_item(item);
}
Record<K, V>& set(const K &key, V &&value)
{
Record<K, V> item(key, value);
return set_item(item);
}
template<class ...A>
Record<K, V>& emplace(A &&...a)
{
Record<K, V> item(a...);
return set_item(item);
}
// returns a pointer instead of an optional because realistically it would end in the same operation ie checking if its valid and using it
// this just results in less verbose code
V* get(const K &key) const
{
return search(key);
}
V& operator[](const K &key)
{
V *value = search(key);
if (!value)
return set(key, V()).value;
return *value;
}
V& get(const K &key, const V &def_value) const
{
V *value = search(key);
return value ? *value : const_cast<V&>(def_value);
}
// gets all values of duplicate keys
std::vector<V*> get_all(const K &key) const
{
size_t h = hash(key);
std::vector<V*> output;
std::cout << m_bucket[h].size() << '\n';
for (IterType item : m_bucket[h])
{
if (item->key == key)
output.push_back(&item->value);
}
return output;
}
bool contains(const K &key) const
{
return search(key);
}
// returns true if the entry was erased
bool erase(const K &key)
{
size_t h = hash(key);
Chain &chain = m_bucket[h];
for (auto it = chain.begin(); it != chain.end(); it++)
{
if ((*it)->key == key)
{
m_items.erase(*it);
chain.erase(it);
m_size--;
return true;
}
}
return false;
}
// removes all entries in map
void clear()
{
m_items.clear();
delete[] m_bucket;
m_size = 0;
construct(10);
}
auto begin() const
{
return m_items.begin();
}
auto end() const
{
return m_items.end();
}
private:
size_t m_size{};
size_t m_capacity{};
Chain *m_bucket{};
std::list<Record<K, V>> m_items;
std::hash<K> m_hash;
Record<K, V>& set_item(Record<K, V> &item)
{
if (++m_size >= m_capacity)
rehash();
size_t h = hash(item.key);
Chain &chain = m_bucket[h];
IterType iter = m_items.emplace(m_items.end(), std::move(item));
chain.emplace_back(iter);
return *iter;
}
constexpr inline
size_t hash(const K &k) const
{
return m_hash(k) % m_capacity;
}
V* search(const K &key) const
{
size_t h = hash(key);
for (IterType item : m_bucket[h])
{
if (item->key == key)
return &item->value;
}
return nullptr;
}
inline void construct(int n)
{
m_capacity = n;
m_bucket = new Chain[m_capacity];
for (int i = 0; i < m_capacity; i++)
m_bucket[i] = Chain();
}
void rehash()
{
const size_t n = m_capacity;
m_capacity *= 2;
auto temp = new Chain[m_capacity];
size_t i;
for (i = 0; i < n; i++)
{
temp[i] = Chain();
Chain &old = m_bucket[i];
if (old.empty())
continue;
for (IterType item : old)
{
size_t h = hash(item->key);
Chain *chain = &temp[h];
if (!chain)
temp[h] = Chain();
chain->push_back(item);
}
}
for(; i < m_capacity; i++)
temp[i] = Chain();
delete[] m_bucket;
m_bucket = temp;
}
void move(Map<K, V> &&map) noexcept
{
m_bucket = map.m_bucket;
map.m_bucket = nullptr;
m_size = map.m_size;
m_capacity = map.m_capacity;
m_items = std::move(map.m_items);
}
void copy(const Map<K, V> &map)
{
m_items = map.m_items;
m_capacity = map.m_capacity;
m_size = map.m_size;
m_bucket = new Chain[m_capacity];
for (size_t i = 0; i < m_capacity; i++)
m_bucket[i] = map.m_bucket[i];
}
};
}