-
Notifications
You must be signed in to change notification settings - Fork 0
/
AhMap.hpp
351 lines (262 loc) · 8.27 KB
/
AhMap.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
342
343
344
345
346
347
348
349
350
351
#ifndef AH_UNORDERED_MAP_HPP
#define AH_UNORDERED_MAP_HPP
#include <stdexcept>
#include <utility>
#include <cstddef>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <tuple>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include "Tracer.hpp"
template<typename V, bool cache_hash>
struct ah_entry;
template<typename K, typename V,
bool cache_hash = false,
bool trace_accesses = false,
typename Hash = std::hash<K>,
typename Pred = std::equal_to<K>
>
class AhMap {
using ah_entry = ah_entry<std::pair<K, V>, cache_hash>;
using ah_map = AhMap<K, V, cache_hash, trace_accesses, Hash, Pred>;
static constexpr unsigned AH_EXPAND_FACTOR = 11;
static constexpr unsigned AH_REORDER_CHECK = 10000000;
static constexpr unsigned AH_INITIAL_SIZE = 3;
static constexpr float AH_LOAD_FACTOR = 0.15;
public:
AhMap() : epoc(0), num_bins(AH_INITIAL_SIZE), num_entries(0),
total_capacity((size_t)ceil((1.0 + AH_LOAD_FACTOR) * AH_INITIAL_SIZE)),
free_list(nullptr), last_entry(0), trace(this) {
entries = new ah_entry[total_capacity]();
bins = new ah_entry *[AH_INITIAL_SIZE]();
}
~AhMap() {
delete[] entries;
delete[] bins;
}
V& insert(const K& key, V value) {
check_reorder();
size_t hash = hash_fn(key);
// Resize the table if needed.
if (free_list == nullptr && last_entry == total_capacity)
ah_expand_table();
// Find a entry to store in.
ah_entry* entry;
if (free_list != nullptr) {
entry = free_list;
free_list = free_list->next;
} else entry = &entries[last_entry++];
// Write new entry and store insert to a bin.
entry->value = std::make_pair(key, value);
if (cache_hash) entry->write_hash(hash);
ah_bin_insert(entry, hash);
num_entries++;
return std::get<1>(entry->value);
}
V& find(const K& key) {
check_reorder();
size_t hash = hash_fn(key);
return ah_bin_locate(key, hash);
}
bool remove(K key) {
return true;
}
V& operator[](const K& key) {
return find(key);
}
ah_entry* entries;
private:
friend class Tracer<ah_map, ah_entry>;
size_t epoc;
size_t num_bins;
size_t num_entries;
size_t total_capacity;
ah_entry** bins;
ah_entry* free_list;
size_t last_entry;
Hash hash_fn;
Pred equa_fn;
Tracer<ah_map, ah_entry> trace;
inline void check_reorder() {
if (trace_accesses && epoc++ % AH_REORDER_CHECK == 0) {
reorder();
}
}
inline ah_entry** bin_from_hash(size_t hash) {
return &bins[hash % num_bins];
}
inline void ah_bin_insert(ah_entry* entry, size_t hash) {
ah_entry** bin = bin_from_hash(hash);
entry->next = *bin;
*bin = entry;
if (trace_accesses) trace.record(entry);
}
inline V& ah_bin_locate(const K& key, size_t hash) {
ah_entry* entry = *bin_from_hash(hash);
while (entry != nullptr && !equa_fn(std::get<0>(entry->value), key)) {
if (trace_accesses) trace.record(entry);
entry = entry->next;
}
if (entry != nullptr) {
return std::get<1>(entry->value);
} else {
return insert(key, V());
}
}
inline void ah_bin_remove(ah_entry* entry) { /* ... */ }
// Second should never be invalid.
// If first is invalid then it's on the free list.
inline void ah_bin_swap(ah_entry* first, ah_entry* second) {
if (first == second) return;
ah_entry **first_ls_start, **second_ls_start;
if (!cache_hash) {
first_ls_start = bin_from_hash(hash_fn(std::get<0>(first->value)));
second_ls_start = bin_from_hash(hash_fn(std::get<0>(second->value)));
} else {
first_ls_start = bin_from_hash(first->read_hash());
second_ls_start = bin_from_hash(second->read_hash());
}
ah_entry* first_ls = *first_ls_start;
ah_entry* second_ls = *second_ls_start;
bool first_head = false, second_head = false;
// Search through both bins and find the previous entry.
if (first_ls != first && first_ls != nullptr) {
while (first_ls->next != first && first_ls->next != nullptr)
first_ls = first_ls->next;
} else {
first_head = true;
}
if (second_ls != second && second_ls != nullptr) {
while (second_ls->next != second && second_ls->next != nullptr)
second_ls = second_ls->next;
} else {
second_head = true;
}
if (first_head) {
*first_ls_start = second;
} else {
first_ls->next = second;
}
if (second_head) {
*second_ls_start = first;
} else {
second_ls->next = first;
}
std::swap(*first, *second);
}
void reorder() {
auto new_ordering = trace.results();
// Check for duplicates.
std::unordered_set<entry_t> dups;
for (const auto e : new_ordering) {
if (dups.find(e) != dups.end()) {
std::cout << "Error! Duplicate!\n";
}
dups.insert(e);
}
// Check that all entries in new ordering are valid.
for (const auto e : new_ordering) {
auto bin = *bin_from_hash(hash_fn(std::get<0>(entries[e].value)));
while (bin != nullptr && bin != &entries[e])
bin = bin->next;
if (bin != &entries[e]) {
std::cout << "Error! Invalid entry!\n";
exit(0);
}
}
// r: current_position -> original_position
// r_i: original_position -> current_position
std::unordered_map<entry_t, entry_t> r, r_i;
for (auto i = 0; i < new_ordering.size(); ++i) {
// If this position has been changed.
if (r.find(new_ordering[i]) != r.end()) {
if (r[new_ordering[i]] < i) {
std::cout << "INVARIANT!\n";
exit(0);
}
ah_bin_swap(&entries[i], &entries[r[new_ordering[i]]]);
// If i isn't the original value of the position
if (r_i.find(i) != r_i.end()) {
r[r_i[i]] = r[new_ordering[i]];
r_i[r[new_ordering[i]]] = r_i[i];
} else {
r[i] = r[new_ordering[i]];
r_i[r[new_ordering[i]]] = i;
}
// Otherwise this position contains the original.
} else {
if (new_ordering[i] < i) {
std::cout << "INVARIANT!\n";
exit(0);
}
ah_bin_swap(&entries[i], &entries[new_ordering[i]]);
if (r_i.find(i) != r_i.end()) {
r[r_i[i]] = new_ordering[i];
r_i[new_ordering[i]] = r_i[i];
} else {
r[i] = new_ordering[i];
r_i[new_ordering[i]] = i;
}
}
}
}
void ah_expand_table() {
// Assuming num_entries >= num_bins.
size_t new_size = AH_EXPAND_FACTOR * num_bins;
// Prepare to iterate all old bins.
size_t old_bin_num = num_bins, old_free_list;
size_t old_total_capacity = total_capacity;
ah_entry* old_entries = entries;
ah_entry** old_bins = bins;
if (free_list != nullptr) old_free_list = free_list - old_entries;
bins = new ah_entry *[new_size]();
num_bins = new_size;
total_capacity = (size_t)ceil((1.0 + AH_LOAD_FACTOR) * new_size);
entries = new ah_entry[total_capacity]();
if (free_list != nullptr) free_list = &entries[old_free_list];
ah_entry* current_entry, *temp_entry;
std::copy(old_entries, old_entries + old_total_capacity, entries);
delete[] old_entries;
// Reinsert all entries into new bin list.
for (size_t i = 0; i < old_bin_num; i++) {
if (old_bins[i] == nullptr) continue;
current_entry = &entries[old_bins[i] - old_entries];
// Iterate through each bin and move entries to new array.
while (current_entry != nullptr) {
temp_entry = (current_entry->next != nullptr) ?
&entries[current_entry->next - old_entries] : nullptr;
if (cache_hash) ah_bin_insert(current_entry, current_entry->read_hash());
else ah_bin_insert(current_entry, hash_fn(std::get<0>(current_entry->value)));
current_entry = temp_entry;
}
}
delete[] old_bins;
}
};
template<typename V>
struct ah_entry<V, true> {
V value;
ah_entry* next;
size_t hash;
void write_hash(size_t hash) {
this->hash = hash;
}
size_t read_hash() {
return hash;
}
};
template<typename V>
struct ah_entry<V, false> {
V value;
ah_entry* next;
void write_hash(size_t hash) {
throw std::logic_error("");
}
size_t read_hash() {
throw std::logic_error("");
}
};
#endif