forked from rgriege/Violet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
283 lines (247 loc) · 10.4 KB
/
array.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
#ifndef VIOLET_ARRAY_H
#define VIOLET_ARRAY_H
#define array_size_t u32
#ifdef ARRAY_STATIC
#define ARRDEF static
#else
#define ARRDEF
#endif
#if defined ARRAY_STATIC && !defined ARRAY_IMPLEMENTATION
#define ARRAY_IMPLEMENTATION
#endif
typedef struct array__head
{
array_size_t sz, cap;
allocator_t *allocator;
} array__head;
#define array(type) type*
#define ENFORCE_ARRAY_OF_ARRAY(a) ((void)(**(a)), (array(array(void)))(a))
#define array__get_head(a) (((array__head*)(a)) - 1)
#define array__allocator(a) (array__get_head(a)->allocator)
#define array_create() array_create_ex(g_allocator)
#define array_create_ex(alc) array__create(0, 0, alc MEMCALL_LOCATION)
#define array_init(a, cap) array_init_ex(a, cap, g_allocator)
#define array_init_ex(a, cap, alc) (a)=array__create(cap, sizeof(*a), alc \
MEMCALL_LOCATION)
#define array_destroy(a) afree(array__get_head(a), array__allocator(a))
#define array__esz(a) (sizeof(*(a)))
#define array_sz(a) (array__get_head(a)->sz)
#define array_cap(a) (array__get_head(a)->cap)
#define array_empty(a) (array_sz(a) == 0)
#define array_copy(dst, src) ((dst)=array__copy(dst, src, array__esz(src) \
MEMCALL_LOCATION))
#define array_memcpy(dst, src) memcpy((dst), (src), array_sz(src) * array__esz(src))
#define array_from_end(a, i) ((a)[array_sz(a)-i])
#define array_first(a) ((a)[0])
#define array_last(a) array_from_end(a, 1)
#define array_end(a) ((a)+array_sz(a))
#define array_foreach(a, type, it) for (type *it = (a); it != array_end(a); ++it)
#define array_iterate(a, it, n) for (array_size_t it = 0, n = array_sz(a); \
it < n; ++it)
#define array_reserve(a, n) ((a)=array__reserve(a, n, array__esz(a) \
MEMCALL_LOCATION))
#define array_reserve_addl(a, n) array_reserve(a, array_sz(a) + n)
#define array_set_sz(a, n) (array_reserve(a, n), array_sz(a) = n)
#define array_extend_sz(a, n) (array_reserve_addl(a, n), array_sz(a) += n)
#define array_grow(a, n) ((a)=array__grow(a, n, array__esz(a) \
MEMCALL_LOCATION))
#define array_append_null(a) ((a)=array__append_null(a, array__esz(a) \
MEMCALL_LOCATION), \
(a)+array_sz(a) - 1)
#define array_append(a, e) (*array_append_null(a) = e)
#define array_appendn(a, p, n) (assert((a) != (p)), \
(a)=array__appendn(a, p, n, array__esz(a) \
MEMCALL_LOCATION))
#define array_insert_null(a, i) ((a)=array__insert_null(a, i, array__esz(a) \
MEMCALL_LOCATION), \
(a)+(i))
#define array_insert(a, i, e) (*array_insert_null(a, i) = e)
#define array_insert_fast(a, i, e) ((a)=array__append_null(a, array__esz(a) \
MEMCALL_LOCATION), \
array_last(a) = (a)[(i)], (a)[(i)] = (e))
#define array_remove(a, i) array_remove_n(a, i, 1)
#define array_remove_n(a, i, n) array__remove(a, i, n, array__esz(a))
#define array_remove_fast(a, i) array__remove_fast(a, i, array__esz(a))
#define array_pop(a) (--array_sz(a))
#define array_clear(a) (array_sz(a) = 0)
#define array_reverse(a) reverse(a, array__esz(a), array_sz(a))
#define array_qsort(a, cmp) qsort(a, array_sz(a), array__esz(a), cmp)
#define array_ssort(a, cmp) ssort(a, array_sz(a), array__esz(a), cmp)
#define array_bsearch(a, e, cmp) bsearch(&(e), a, array_sz(a), \
array__esz(a), cmp)
#define array_find(a, p, cmp) array__find(a, p, array__esz(a), cmp)
#define array_index(a, p, cmp) array__index(a, p, array__esz(a), cmp)
#define array_upper(a, e, cmp) array__upper(a, &(e), array__esz(a), cmp)
#define array_array_destroy(a) array__array_destroy(ENFORCE_ARRAY_OF_ARRAY(a))
#define array_array_clear(a) array__array_clear(ENFORCE_ARRAY_OF_ARRAY(a))
#define array_array_copy(dst, src) ((dst)=array__array_copy(ENFORCE_ARRAY_OF_ARRAY(dst), \
ENFORCE_ARRAY_OF_ARRAY(src), \
array__esz(dst[0]) \
MEMCALL_LOCATION))
/* Array To Pointer + Number */
#define A2PN(a) (a), array_sz(a)
ARRDEF void *array__create(array_size_t cap, size_t sz, allocator_t *a
MEMCALL_ARGS);
ARRDEF void *array__reserve(void *a, array_size_t nmemb, size_t sz
MEMCALL_ARGS);
ARRDEF void *array__copy(void *dst, const void *src, size_t sz MEMCALL_ARGS);
ARRDEF void *array__grow(void *a, array_size_t nmemb, size_t sz MEMCALL_ARGS);
ARRDEF void *array__append_null(void *a, size_t sz MEMCALL_ARGS);
ARRDEF void *array__appendn(void *a, const void *p, array_size_t n, size_t sz
MEMCALL_ARGS);
ARRDEF void *array__insert_null(void *a, array_size_t idx, size_t sz
MEMCALL_ARGS);
ARRDEF void array__remove(void *a, array_size_t idx, array_size_t n, size_t sz);
ARRDEF void array__remove_fast(void *a, array_size_t idx, size_t sz);
ARRDEF void *array__find(void *a, const void *userp, size_t sz,
int(*cmp)(const void *, const void *));
ARRDEF array_size_t array__index(void *a, const void *userp, size_t sz,
int(*cmp)(const void *, const void *));
ARRDEF void *array__upper(void *a, const void *elem, size_t sz,
int(*cmp)(const void *, const void*));
ARRDEF void array__array_destroy(array(array(void)) a);
ARRDEF void array__array_clear(array(array(void)) a);
ARRDEF void *array__array_copy(array(array(void)) dst, array(array(void)) src,
size_t sz MEMCALL_ARGS);
#endif
#ifdef ARRAY_IMPLEMENTATION
typedef char *arr_bytep;
ARRDEF void *array__create(array_size_t cap, size_t sz, allocator_t *a
MEMCALL_ARGS)
{
array__head *head = a->malloc_(sizeof(array__head) + cap * sz, a MEMCALL_VARS);
if (!head) { fatal("array__create: oom"); }
head->sz = 0;
head->cap = cap;
head->allocator = a;
return head + 1;
}
ARRDEF void *array__reserve(void *array, array_size_t nmemb, size_t sz
MEMCALL_ARGS)
{
array__head *head = array__get_head(array);
if (nmemb > head->cap) {
head = head->allocator->realloc_(head, sizeof(array__head) + nmemb * sz,
head->allocator MEMCALL_VARS);
if (!head) { fatal("array__reserve: oom"); }
head->cap = nmemb;
return head + 1;
}
return array;
}
ARRDEF void *array__copy(void *dst, const void *src, size_t sz MEMCALL_ARGS)
{
dst = array__reserve(dst, array_sz(src), sz MEMCALL_VARS);
array_sz(dst) = array_sz(src);
memcpy(dst, src, array_sz(src) * sz);
return dst;
}
ARRDEF void *array__grow(void *a, array_size_t nmemb, size_t sz MEMCALL_ARGS)
{
const array_size_t req = array_sz(a) + nmemb;
return req > array_cap(a)
? array__reserve(a, max(req, array_cap(a)*3/2), sz MEMCALL_VARS)
: a;
}
ARRDEF void *array__append_null(void *a, size_t sz MEMCALL_ARGS)
{
a = array__grow(a, 1, sz MEMCALL_VARS);
++array_sz(a);
return a;
}
ARRDEF void *array__appendn(void *a, const void *p, array_size_t n, size_t sz
MEMCALL_ARGS)
{
a = array__grow(a, n, sz MEMCALL_VARS);
memcpy(((char*)a)+array_sz(a)*sz, p, n*sz);
array_sz(a) += n;
return a;
}
ARRDEF void *array__insert_null(void *a, array_size_t idx, size_t sz
MEMCALL_ARGS)
{
assert(idx <= array_sz(a));
a = array__append_null(a, sz MEMCALL_VARS);
for (arr_bytep p = (arr_bytep)a+(array_sz(a)-1)*sz,
end = (arr_bytep)a+idx*sz;
p != end; p -= sz)
memcpy(p, p - sz, sz);
return a;
}
ARRDEF void array__remove(void *a, array_size_t idx, array_size_t n, size_t sz)
{
assert(n > 0);
assert(idx + n - 1 < array_sz(a));
for (arr_bytep p = (arr_bytep)a + (idx + n) * sz,
end = (arr_bytep)a + array_sz(a) * sz;
p != end; p += sz)
memcpy(p - n * sz, p, sz);
array_sz(a) -= n;
}
ARRDEF void array__remove_fast(void *a, array_size_t idx, size_t sz)
{
assert(array_sz(a) > 0);
if (idx != array_sz(a) - 1)
memcpy((arr_bytep)a + idx * sz, (arr_bytep)a + (array_sz(a) - 1) * sz, sz);
--array_sz(a);
}
ARRDEF void *array__find(void *a, const void *userp, size_t sz,
int(*cmp)(const void *, const void *))
{
for (arr_bytep p=a, end=(arr_bytep)a+array_sz(a)*sz; p!=end; p+=sz)
if (cmp(p, userp) == 0)
return p;
return NULL;
}
ARRDEF array_size_t array__index(void *a, const void *userp, size_t sz,
int(*cmp)(const void *, const void *))
{
for (arr_bytep p=a, end=(arr_bytep)a+array_sz(a)*sz; p!=end; p+=sz)
if (cmp(p, userp) == 0)
return (array_size_t)((p-(arr_bytep)a)/sz);
return ~0;
}
ARRDEF void *array__upper(void *a, const void *elem, size_t sz,
int(*cmp)(const void *, const void*))
{
size_t left = 0, right = array_sz(a), mid = right/2;
void *res = NULL;
while (left != right)
{
void *p = (arr_bytep)a+mid*sz;
if (cmp(elem, p) < 0)
{
res = p;
right = mid;
}
else
left = mid+1;
mid = (right+left)/2;
}
return res;
}
ARRDEF void array__array_destroy(array(array(void)) a)
{
array_foreach(a, array(void), b)
array_destroy(*b);
array_destroy(a);
}
ARRDEF void array__array_clear(array(array(void)) a)
{
array_foreach(a, array(void), b)
array_destroy(*b);
array_clear(a);
}
ARRDEF void *array__array_copy(array(array(void)) dst, array(array(void)) src,
size_t sz MEMCALL_ARGS)
{
assert(array_empty(dst));
array_set_sz(dst, array_sz(src));
array_iterate(src, i, n)
dst[i] = array__copy(array__create(array_sz(src[i]), sz,
array__allocator(dst) MEMCALL_VARS),
src[i], sz MEMCALL_VARS);
return dst;
}
#undef ARRAY_IMPLEMENTATION
#endif // ARRAY_IMPLEMENTATION