-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaining.h
More file actions
165 lines (150 loc) · 4.2 KB
/
Copy pathchaining.h
File metadata and controls
165 lines (150 loc) · 4.2 KB
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
#pragma once
#include "base.h"
template<uint64_t LF_>
struct Chaining {
static constexpr double LF = static_cast<double>(LF_) / 100.0;
Chaining() {
size_ = 0;
capacity = 8;
data = reinterpret_cast<Slot**>(__aligned_alloc(CACHE_LINE, sizeof(Slot*) * capacity));
std::memset(data, 0, sizeof(Slot*) * capacity);
}
~Chaining() {
for(uint64_t i = 0; i < capacity; i++) {
Slot* s = data[i];
while(s) {
Slot* next = s->next;
delete s;
s = next;
}
}
__aligned_free(data);
}
// assumes key is not in the map
void insert(uint64_t key, uint64_t value) {
if(size_ >= capacity * LF) grow();
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
Slot* s = new Slot;
s->key = key;
s->value = value;
s->next = data[index];
data[index] = s;
size_++;
}
uint64_t find(uint64_t key, uint64_t* steps) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
Slot* s = data[index];
while(s) {
(*steps)++;
if(s->key == key) return s->value;
s = s->next;
}
assert(false);
return 0;
}
bool contains(uint64_t key, uint64_t* steps) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
Slot* s = data[index];
while(s) {
(*steps)++;
if(s->key == key) return true;
s = s->next;
}
return false;
}
void erase(uint64_t key) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
Slot* s = data[index];
Slot* prev = nullptr;
while(s) {
if(s->key == key) {
if(prev)
prev->next = s->next;
else
data[index] = s->next;
delete s;
size_--;
return;
}
prev = s;
s = s->next;
}
}
void grow() {
uint64_t old_capacity = capacity;
Slot** old_data = data;
capacity *= 2;
data = reinterpret_cast<Slot**>(__aligned_alloc(CACHE_LINE, sizeof(Slot*) * capacity));
std::memset(data, 0, sizeof(Slot*) * capacity);
for(uint64_t i = 0; i < old_capacity; i++) {
Slot* s = old_data[i];
while(s) {
Slot* next = s->next;
uint64_t hash = squirrel3(s->key);
uint64_t index = hash & (capacity - 1);
s->next = data[index];
data[index] = s;
s = next;
}
}
__aligned_free(old_data);
}
void clear() {
size_ = 0;
for(uint64_t i = 0; i < capacity; i++) {
Slot* s = data[i];
while(s) {
Slot* next = s->next;
delete s;
s = next;
}
data[i] = nullptr;
}
}
uint64_t index_for(uint64_t key) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
return index;
}
uint64_t prefetch(uint64_t key) {
uint64_t index = index_for(key);
::prefetch(data[index]);
return index;
}
uint64_t find_indexed(uint64_t key, uint64_t index, uint64_t* steps) {
Slot* s = data[index];
while(s) {
(*steps)++;
if(s->key == key) return s->value;
s = s->next;
}
assert(false);
return 0;
}
uint64_t size() { return size_; }
uint64_t memory_usage() {
return sizeof(Slot*) * capacity + sizeof(Slot) * size_ + sizeof(Chaining);
}
uint64_t sum_all_values() {
uint64_t sum = 0;
for(uint64_t i = 0; i < capacity; i++) {
Slot* s = data[i];
while(s) {
sum += s->value;
s = s->next;
}
}
return sum;
}
struct Slot {
uint64_t key, value;
Slot* next;
};
Slot** data;
uint64_t capacity;
uint64_t size_;
};