-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobin_hood_with_deletion.h
152 lines (136 loc) · 4.53 KB
/
robin_hood_with_deletion.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
#pragma once
#include "base.h"
template<uint64_t LF_>
struct Robin_Hood_With_Deletion {
static constexpr uint64_t EMPTY = UINT64_MAX;
static constexpr double LF = static_cast<double>(LF_) / 100.0;
Robin_Hood_With_Deletion() {
size_ = 0;
capacity = 8;
data = reinterpret_cast<Slot*>(__aligned_alloc(CACHE_LINE, sizeof(Slot) * capacity));
std::memset(data, 0xff, sizeof(Slot) * capacity);
}
~Robin_Hood_With_Deletion() { __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);
uint64_t dist = 0;
size_++;
for(;;) {
if(data[index].key == EMPTY) {
data[index].key = key;
data[index].value = value;
return;
}
uint64_t desired = index_for(data[index].key);
uint64_t cur_dist = (index + capacity - desired) & (capacity - 1);
if(cur_dist < dist) {
std::swap(key, data[index].key);
std::swap(value, data[index].value);
dist = cur_dist;
}
dist++;
index = (index + 1) & (capacity - 1);
}
}
uint64_t find(uint64_t key, uint64_t* steps) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
for(;;) {
if(data[index].key == key) return data[index].value;
(*steps)++;
index = (index + 1) & (capacity - 1);
}
}
bool contains(uint64_t key, uint64_t* steps) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
uint64_t dist = 0;
for(;;) {
if(data[index].key == EMPTY) return false;
if(data[index].key == key) return true;
uint64_t desired = index_for(data[index].key);
uint64_t cur_dist = (index + capacity - desired) & (capacity - 1);
if(cur_dist < dist) return false;
(*steps)++;
dist++;
index = (index + 1) & (capacity - 1);
}
}
void erase(uint64_t key) {
uint64_t hash = squirrel3(key);
uint64_t index = hash & (capacity - 1);
for(;;) {
if(data[index].key == key) {
size_--;
remove(index);
return;
}
index = (index + 1) & (capacity - 1);
}
}
void remove(uint64_t index) {
for(;;) {
data[index].key = EMPTY;
uint64_t next = (index + 1) & (capacity - 1);
if(data[next].key == EMPTY) return;
uint64_t desired = index_for(data[next].key);
if(next == desired) return;
data[index].key = data[next].key;
data[index].value = data[next].value;
index = next;
}
}
void grow() {
uint64_t old_capacity = capacity;
Slot* old_data = data;
size_ = 0;
capacity *= 2;
data = reinterpret_cast<Slot*>(__aligned_alloc(CACHE_LINE, sizeof(Slot) * capacity));
std::memset(data, 0xff, sizeof(Slot) * capacity);
for(uint64_t i = 0; i < old_capacity; i++) {
if(old_data[i].key < EMPTY) insert(old_data[i].key, old_data[i].value);
}
__aligned_free(old_data);
}
void clear() {
size_ = 0;
max_probe = 0;
std::memset(data, 0xff, sizeof(Slot) * capacity);
}
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) {
for(;;) {
if(data[index].key == key) return data[index].value;
(*steps)++;
index = (index + 1) & (capacity - 1);
}
}
uint64_t size() { return size_; }
uint64_t memory_usage() { return sizeof(Slot) * capacity + sizeof(Robin_Hood_With_Deletion); }
uint64_t sum_all_values() {
uint64_t sum = 0;
for(uint64_t i = 0; i < capacity; i++) {
if(data[i].key < EMPTY) sum += data[i].value;
}
return sum;
}
struct Slot {
uint64_t key, value;
};
Slot* data;
uint64_t capacity;
uint64_t size_;
uint64_t max_probe;
};