-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.h
286 lines (264 loc) · 8.81 KB
/
set.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
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
/// @file
#pragma once
#include <stddef.h>
#include "arena.h"
typedef struct Set Set;
typedef struct SetItem SetItem;
static constexpr long set_hash_shift = 2; ///< Number of hash bits to shift out
static constexpr long set_branch_select = 64 - set_hash_shift; ///< Bit mask to select branch
/**
* @brief Represents a set of unique keys
*/
struct Set {
Arena *arena; ///< Pointer to an arena allocator
long length; ///< Number of items in the set
SetItem *begin; ///< Pointer to the root item in the set
SetItem *end; ///< Pointer to the last item in the set
};
/**
* @brief Represents a single item in a set
*/
struct SetItem {
struct {
void *data; ///< Pointer to the key data
long size; ///< Size of the key data in bytes
} key; ///< Key properties
SetItem *child[1 << set_hash_shift]; ///< Array of child items for hash traversal
SetItem *next; ///< Pointer to the next item
};
/**
* @brief Iterate over all items in a set
* @param item Current set item
* @param self Pointer to a set
*/
#define set_for_each(item, self) \
for (auto(item) = (self)->begin; item; (item) = (item)->next) \
if ((item)->key.size)
/**
* @brief Create a new set
* @param arena Pointer to an arena allocator
* @return New set instance
*/
static Set set_create(Arena *arena) {
Set set = {};
set.arena = arena;
return set;
}
/// @private
static uint64_t x__set_hash_fnv1a(const char *key, long size) {
constexpr uint64_t basis = 0xcbf29ce484222325;
constexpr uint64_t prime = 0x00000100000001b3;
uint64_t hash = basis;
for (auto byte = key; byte < key + size; byte++) {
hash ^= *byte;
hash *= prime;
}
return hash;
}
/// @private
static bool x__set_key_equals(const SetItem *item, const void *key, long size) {
return item->key.size == size && !memcmp(item->key.data, key, size);
}
/// @private
static void x__set_item_init(const Set *self, SetItem *item, const void *key, long size) {
item->key.data = arena_memdup(self->arena, key, 1, size, alignof(max_align_t));
item->key.size = size;
}
/**
* @brief Insert a new item into a set
* @param self Pointer to a set
* @param key Pointer to the key data
* @param size Size of the key data in bytes (optional)
* @return `true` if the key is newly added, or `false` if it already exists
* @note If `size == 0`, `key` is assumed to be a null-terminated string
*/
static bool set_insert(Set *self, const void *key, long size) {
if (!size) {
size = strlen(key) + 1;
}
auto item = &self->begin;
for (auto hash = x__set_hash_fnv1a(key, size); *item; hash <<= set_hash_shift) {
if (!(*item)->key.size) {
break;
}
if (x__set_key_equals(*item, key, size)) {
return false;
}
item = &(*item)->child[hash >> set_branch_select];
}
if (!*item) {
*item = arena_calloc(self->arena, 1, sizeof(SetItem), alignof(SetItem));
if (self->end) {
self->end->next = *item;
}
self->end = *item;
}
x__set_item_init(self, *item, key, size);
self->length += 1;
return true;
}
/**
* @brief Remove an item from a set
* @param self Pointer to a set
* @param key Pointer to the key data
* @param size Size of the key data in bytes (optional)
* @return `true` if the key is successfully removed, or `false` if it is not found
* @note If `size == 0`, `key` is assumed to be a null-terminated string
*/
static bool set_remove(Set *self, const void *key, long size) {
if (!size) {
size = strlen(key) + 1;
}
auto item = self->begin;
for (auto hash = x__set_hash_fnv1a(key, size); item; hash <<= set_hash_shift) {
if (x__set_key_equals(item, key, size)) {
item->key.size = 0;
self->length -= 1;
return true;
}
item = item->child[hash >> set_branch_select];
}
return false;
}
/**
* @brief Find an item of a set
* @param self Pointer to a set
* @param key Pointer to the key data
* @param size Size of the key data in bytes (optional)
* @return `true` if the key exists, or `false` if it is not found
* @note If `size == 0`, `key` is assumed to be a null-terminated string
*/
static bool set_find(const Set *self, const void *key, long size) {
if (!size) {
size = strlen(key) + 1;
}
auto item = self->begin;
for (auto hash = x__set_hash_fnv1a(key, size); item; hash <<= set_hash_shift) {
if (x__set_key_equals(item, key, size)) {
return true;
}
item = item->child[hash >> set_branch_select];
}
return false;
}
/**
* @brief Create a clone of a set
* @param self Pointer to a set
* @param arena Pointer to an arena allocator (optional)
* @return Cloned set instance
* @note If no arena allocator is passed, the arena allocator of the set is used
*/
static Set set_clone(const Set *self, Arena *arena) {
Set dict = {};
dict.arena = arena ? arena : self->arena;
set_for_each(item, self) {
set_insert(&dict, item->key.data, item->key.size);
}
return dict;
}
/**
* @brief Retrieve an array of set items
* @param self Pointer to a set
* @param arena Pointer to an arena allocator (optional)
* @return Pointer to an array of items
* @note If no arena allocator is passed, the arena allocator of the set is used
*/
static SetItem *set_items(const Set *self, Arena *arena) {
arena = arena ? arena : self->arena;
SetItem *items = arena_malloc(arena, self->length, sizeof(SetItem), alignof(SetItem));
long index = 0;
set_for_each(item, self) {
items[index++] = *item;
}
return items;
}
/**
* @brief Compute the union of two sets
* @param self Pointer to a set
* @param other Pointer to another set
* @param arena Pointer to an arena allocator (optional)
* @return New set containing all unique items from both sets
*/
static Set set_union(const Set *self, const Set *other, Arena *arena) {
Set set = set_clone(self, arena);
set_for_each(item, other) {
set_insert(&set, item->key.data, item->key.size);
}
return set;
}
/**
* @brief Compute the intersection of two sets
* @param self Pointer to a set
* @param other Pointer to another set
* @param arena Pointer to an arena allocator (optional)
* @return New set containing items common to both sets
*/
static Set set_intersection(const Set *self, const Set *other, Arena *arena) {
Set set = set_create(arena ? arena : self->arena);
set_for_each(item, self) {
if (set_find(other, item->key.data, item->key.size)) {
set_insert(&set, item->key.data, item->key.size);
}
}
return set;
}
/**
* @brief Compute the difference of two sets
* @param self Pointer to a set
* @param other Pointer to another set
* @param arena Pointer to an arena allocator (optional)
* @return New set containing items in the first set but not in the second
*/
static Set set_difference(const Set *self, const Set *other, Arena *arena) {
Set set = set_create(arena ? arena : self->arena);
set_for_each(item, self) {
if (!set_find(other, item->key.data, item->key.size)) {
set_insert(&set, item->key.data, item->key.size);
}
}
return set;
}
/**
* @brief Compute the symmetric difference of two sets
* @param self Pointer to a set
* @param other Pointer to another set
* @param arena Pointer to an arena allocator (optional)
* @return New set containing items that are in either of the sets but not in both
*/
static Set set_symmetric_difference(const Set *self, const Set *other, Arena *arena) {
Set set = set_create(arena ? arena : self->arena);
set_for_each(item, self) {
if (!set_find(other, item->key.data, item->key.size)) {
set_insert(&set, item->key.data, item->key.size);
}
}
set_for_each(item, other) {
if (!set_find(self, item->key.data, item->key.size)) {
set_insert(&set, item->key.data, item->key.size);
}
}
return set;
}
/**
* @brief Check if one set is a subset of another
* @param self Pointer to a set
* @param other Pointer to another set
* @return `true` if all items in the first set are also in the second, or `false` otherwise
*/
static bool set_is_subset(const Set *self, const Set *other) {
set_for_each(item, self) {
if (!set_find(other, item->key.data, item->key.size)) {
return false;
}
}
return true;
}
/**
* @brief Check if one set is a superset of another
* @param self Pointer to a set
* @param other Pointer to another set
* @return `true` if all items in the second set are also in the first, or `false` otherwise
*/
static bool set_is_superset(const Set *self, const Set *other) {
return set_is_subset(other, self);
}