-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmstructs.h
More file actions
267 lines (217 loc) · 11.4 KB
/
mstructs.h
File metadata and controls
267 lines (217 loc) · 11.4 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
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
#ifndef MSTRUCTS_H
#define MSTRUCTS_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* Hash table */
#ifndef mhash_malloc
#define mhash_malloc(size) malloc(size)
#endif
#ifndef mhash_free
#define mhash_free(ptr) free(ptr)
#endif
#define MHASH_MIN_CAPACITY 2
#define MHASH_MIN_LOAD_FACTOR 0.2
#define MHASH_MAX_LOAD_FACTOR 0.75
#define MHASH_ENTRY_FIELDS(ktype) ktype hkey
#define MHASH_FIELDS(type, ktype) \
type *hentries; \
int hcapacity; \
int hcount; \
ktype hempty; \
ktype hdeleted
#define MHASH_FUNC(type, ktype) int mhash_func_name_(type)(const ktype key)
#define MHASH_EQUALS_FUNC(type, ktype) \
int mhash_equals_func_name_(type)(const ktype key1, ktype key2)
static inline unsigned mhash_fnv1a(const void *data, size_t length) {
unsigned h = 0x811c9dc5;
unsigned char *p = (unsigned char *)data;
unsigned char *e = (unsigned char *)data + length;
while (p < e) {
h ^= *p++;
h *= 0x01000193;
}
return h;
}
#define mhash_is_empty(h, type, entry) \
mhash_equals_func_name_(type)((entry)->hkey, (h)->hempty)
#define mhash_is_deleted(h, type, entry) \
mhash_equals_func_name_(type)((entry)->hkey, (h)->hdeleted)
#define mhash_is_vacant_(h, type, entry, allow_deleted) \
(mhash_is_empty(h, type, entry) || \
(allow_deleted && mhash_is_deleted(h, type, entry)))
#define mhash_func_name_(type) mhash_##type##_func_
#define mhash_equals_func_name_(type) mhash_##type##_equals_func_
#define mhash_lookup_(h, type, entries, capacity, key, entry, action, \
allow_deleted) \
do { \
int j; \
int hv = mhash_func_name_(type)(key); \
int mask = capacity - 1; \
for (j = 0; j < capacity; ++j) { \
type *e = &(entries)[(hv + ((j + j * j) >> 1)) & mask]; \
if (mhash_is_vacant_(h, type, e, allow_deleted) || \
mhash_equals_func_name_(type)(key, e->hkey)) { \
action(e, entry); \
break; \
} \
} \
} while (0)
#define mhash_find_action_(found, entry) entry = found
#define mhash_resize_action_(found, entry) *found = entry
#define mhash_change_capacity_(h, type, capacity) \
do { \
int new_capacity = MHASH_MIN_CAPACITY; \
int next_capacity = capacity; \
type *new_entries; \
while (new_capacity < next_capacity) \
new_capacity <<= 1; \
new_entries = mhash_malloc(sizeof(type) * new_capacity); \
for (i = 0; i < new_capacity; ++i) { \
new_entries[i].hkey = (h)->hempty; \
} \
for (i = 0; i < (h)->hcapacity; ++i) { \
if (!mhash_is_vacant_(h, type, &(h)->hentries[i], 1)) { \
mhash_lookup_(h, type, new_entries, new_capacity, \
(h)->hentries[i].hkey, (h)->hentries[i], \
mhash_resize_action_, 1); \
} \
} \
mhash_free((h)->hentries); \
(h)->hentries = new_entries; \
(h)->hcapacity = new_capacity; \
} while (0)
#define mhash_init(h, type, empty, deleted, capacity) \
do { \
int i; \
(h)->hcapacity = MHASH_MIN_CAPACITY; \
while ((h)->hcapacity < capacity) \
(h)->hcapacity <<= 1; \
(h)->hcount = 0; \
(h)->hentries = mhash_malloc(sizeof(type) * (h)->hcapacity); \
(h)->hempty = empty; \
(h)->hdeleted = deleted; \
for (i = 0; i < (h)->hcapacity; ++i) { \
(h)->hentries[i].hkey = empty; \
} \
} while (0)
#define mhash_destroy(h) mhash_free((h)->hentries);
#define mhash_find(h, type, key, entry, allow_deleted) \
do { \
entry = NULL; \
mhash_lookup_(h, type, (h)->hentries, (h)->hcapacity, key, entry, \
mhash_find_action_, allow_deleted); \
} while (0)
#define mhash_resize(h, type, capacity) \
do { \
int i; \
if (capacity < (h)->hcapacity && \
(double)(h)->hcount / (h)->hcapacity < MHASH_MIN_LOAD_FACTOR) { \
mhash_change_capacity_(h, type, (h)->hcount / MHASH_MAX_LOAD_FACTOR); \
} else if (capacity >= (h)->hcapacity && \
(double)(h)->hcount / (h)->hcapacity > MHASH_MAX_LOAD_FACTOR) { \
mhash_change_capacity_(h, type, (h)->hcount / MHASH_MIN_LOAD_FACTOR); \
} \
} while (0)
#define mhash_add(h, type) \
do { \
(h)->hcount++; \
mhash_resize(h, type, (h)->hcapacity); \
} while (0)
#define mhash_delete(h, type, entry) \
do { \
(h)->hcount--; \
entry->hkey = (h)->hdeleted; \
} while (0)
#define mhash_foreach(h, entry) \
for (entry = (h)->hentries; entry < (h)->hentries + (h)->hcapacity; ++entry)
/* Linked list */
typedef struct MList_ {
struct MList_ *prev;
struct MList_ *next;
} MList;
#define mlist_entry(type, ptr, member) \
((type *)(((char *)ptr) - (uintptr_t) & ((type *)0)->member))
#define mlist_foreach(list) \
for (pos = (list)->next; pos != (list); pos = pos->next)
#define mlist_is_empty(list) (list)->next == (list);
static inline void mlist_init(MList *list) { list->next = list->prev = list; }
static inline void mlist_copy(MList *src, MList *dst) {
src->next->prev = dst;
src->prev->next = dst;
dst->next = src->next;
dst->prev = src->prev;
}
static inline void mlist_add(MList *prev, MList *next, MList *entry) {
prev->next = entry;
entry->prev = prev;
entry->next = next;
next->prev = entry;
}
static inline void mlist_remove(MList *node) {
node->prev->next = node->next;
node->next->prev = node->prev;
node->next = NULL;
node->prev = NULL;
}
static inline void mlist_append(MList *list, MList *node) {
mlist_add(list->prev, list, node);
}
static inline void mlist_prepend(MList *list, MList *node) {
mlist_add(list, list->next, node);
}
static inline int mlist_forward_count(MList *first, MList *last) {
int count = 0;
MList *curr = first;
while (curr != last) {
count++;
curr = curr->next;
}
return count++;
}
static inline int mlist_backward_count(MList *first, MList *last) {
int count = 0;
MList *curr = first;
while (curr != last) {
count++;
curr = curr->prev;
}
return count++;
}
/* Vector */
#ifndef mvec_realloc
#define mvec_realloc(ptr, size) realloc(ptr, size)
#endif
#ifndef mvec_free
#define mvec_free(ptr) free(ptr)
#endif
#define MVEC_FIELDS(type) \
type *vitems; \
int vcapacity; \
int vsize
static inline void mvec_maybe_resize_(int size, int *capacity, size_t num_bytes,
void **items) {
if (size >= *capacity) {
*capacity *= ((*capacity < 4096) ? 4 : 2);
*items = mvec_realloc(*items, num_bytes * *capacity);
}
}
#define mvec_init(v, type, capacity) \
do { \
if (capacity > 0) { \
(v)->vitems = (type *)mvec_realloc(NULL, capacity * sizeof(type)); \
} \
(v)->vcapacity = capacity; \
(v)->vsize = 0; \
} while (0)
#define mvec_destroy(v) mvec_free((v)->vitems)
#define mvec_check_size(v, type) \
mvec_maybe_resize_((v)->vsize, &(v)->vcapacity, sizeof(type), \
(void **)&(v)->vitems)
#define mvec_push(v, type) \
(mvec_check_size(v, type), &(v)->vitems[(v)->vsize++])
#define mvec_back(v) (v)->vitems[(v)->vsize - 1]
#define mvec_pop(v) (((v)->vsize > 0) ? --(v)->vsize : (void)0)
#define mvec_foreach(v, item) \
for (item = (v)->vitems; item < (v)->vitems + (v)->vsize; ++item)
#endif /* MSTRUCTS_H */