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
|
#ifndef MERCURY_KERNEL_UTILITY_HPP
#define MERCURY_KERNEL_UTILITY_HPP
#include <optional>
#include <cstdint>
namespace mercury::kernel::utility {
template <class t>
static inline t min(t a, t b) {
return a < b ? a : b;
}
//includes start_i, does not include end_i
void mark_bitmap_region_zero(uint64_t *bitmap, uint64_t start_i, uint64_t end_i);
//includes start_i, does not include end_i
void mark_bitmap_region_one(uint64_t *bitmap, uint64_t start_i, uint64_t end_i);
struct uuid {
uint8_t bytes[16];
};
//if c appears in str, this returns the index of the first time it appears.
//otherwise, this returns len.
static inline unsigned find(const char *str, unsigned len, char c) {
for (unsigned i = 0; i < len; ++i)
if (str[i] == c)
return i;
return len;
}
//if c appears in str, this returns the index of the last time it appears.
//otherwise, this returns len.
static inline unsigned find_last(const char *str, unsigned len, char c) {
for (unsigned i = len; i > 0; --i)
if (str[i - 1] == c)
return i - 1;
return len;
}
//str1 starts with str2
static inline bool starts_with(
const char *str1, unsigned str1_len, const char *str2, unsigned str2_len
) {
if (str1_len < str2_len)
return false;
for (unsigned i = 0; i < str2_len; ++i)
if (str1[i] != str2[i])
return false;
return true;
}
template <class value_t>
struct list {
struct node {
value_t value;
node *next;
node *prev;
};
node *first;
node *last;
list() : first(0), last(0) {}
~list() {
if (first) {
for (node *n = first->next; n; n = n->next)
delete n->prev;
delete last;
}
}
void insert_end(const value_t &value) {
node *n = new node {};
n->value = value;
n->next = 0;
n->prev = last;
last = n;
}
void insert_end(value_t &&value) {
node *n = new node {};
n->value = value;
n->next = 0;
n->prev = last;
last = n;
}
void clear() {
for (node *n = first; n; n = n->next)
delete n;
first = 0;
last = 0;
}
//assumes there is a last.
void remove_last() {
node *new_last = last->prev;
if (new_last)
new_last->next = 0;
else
first = 0;
delete last;
last = new_last;
}
};
template <class value_t>
struct vector {
value_t *buffer;
unsigned buffer_len;
unsigned count;
vector(unsigned buffer_len = 16)
: buffer(new value_t[buffer_len]), buffer_len(buffer_len), count(0) {}
vector(const value_t *copy_from, unsigned len)
: buffer(new value_t[len]), buffer_len(len), count(len) {
for (unsigned i = 0; i < len; ++i)
buffer[i] = copy_from[i];
}
~vector() {
if (buffer)
delete[] buffer;
}
vector<value_t> &operator =(const vector<value_t> &other) {
if (buffer)
delete[] buffer;
buffer = new value_t[other.buffer_len];
buffer_len = other.buffer_len;
for (unsigned i = 0; i < other.count; ++i)
buffer[i] = other.buffer[i];
return *this;
}
vector<value_t> &operator =(vector<value_t> &&other) {
if (buffer)
delete[] buffer;
buffer = other.buffer;
buffer_len = other.buffer_len;
count = other.count;
other.buffer = 0;
return *this;
}
bool operator ==(const vector<value_t> &other) {
if (other.count != count)
return false;
for (unsigned i = 0; i < count; ++i)
if (other.buffer[i] != buffer[i])
return false;
return true;
}
void verify_buffer_len(unsigned target_len) {
if (buffer_len >= target_len)
return;
unsigned new_buffer_len = buffer_len;
do
new_buffer_len *= 2;
while (new_buffer_len < target_len);
value_t *new_buffer = new value_t[new_buffer_len];
for (unsigned i = 0; i < count; ++i)
new_buffer[i] = std::move(buffer[i]);
delete[] buffer;
buffer = new_buffer;
buffer_len = new_buffer_len;
}
void add_end(value_t &&v) {
verify_buffer_len(count + 1);
buffer[count] = std::move(v);
++count;
}
void add_end(const value_t &v) {
verify_buffer_len(count + 1);
buffer[count] = v;
++count;
}
};
typedef vector<char> string;
template <class key_component_t, class value_t>
struct trie {
typedef vector<key_component_t> key_t;
struct child {
key_component_t component;
trie tr;
};
//really this should be a hashmap or something
vector<child> children;
trie *try_get_child(const key_component_t &component) const {
for (unsigned i = 0; i < children.count; ++i)
if (children.buffer[i].component == component)
return &children.buffer[i].tr;
return 0;
}
std::optional<value_t> value_here;
trie() : children(0) {}
//prefix length is in key components, with the root node having a prefix
//length of 0. if no prefix of key has a value, then this just returns the
//root node and sets prefix length to 0.
const trie &longest_prefix_with_value(
const key_t &key, unsigned &prefix_length_out
) const {
const trie *on = this;
unsigned with_i = 0;
const trie *longest_found = this;
prefix_length_out = 0;
while (true) {
if (with_i == key.count)
return *longest_found;
on = on->try_get_child(key.buffer[with_i]);
if (!on)
return *longest_found;
++with_i;
if (on->value_here) {
longest_found = on;
prefix_length_out = with_i;
}
}
}
bool has_key(const key_t &key) {
const trie *on = this;
for (unsigned i = 0; i < key.count; ++i) {
on = on->try_get_child(key.buffer[i]);
if (!on)
return false;
}
return on->value_here.has_value();
}
//returns false if this key is already in use.
bool try_insert(const key_t &key, value_t value) {
trie *on = this;
for (unsigned i = 0; i < key.count; ++i) {
on = on->try_get_child(key.buffer[i]);
if (!on) {
child ch;
ch.component = key.buffer[i];
on->children.add_end(std::move(ch));
on = &on->children.buffer[on->children.count - 1].tr;
}
}
if (on->value_here)
return false;
*on->value_here = value;
return true;
}
};
}
#endif
|