-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_list.c
84 lines (69 loc) · 1.81 KB
/
generic_list.c
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
#include <stdlib.h> // realloc, free
#define list(T) T*
#define reserve(plist, num_items)\
private__reserve((plist), (num_items), sizeof *(*plist))
#define add(plist, item) do{\
int private__index = count(*(plist));\
reserve((plist), private__index + 1);\
(*plist)[private__index] = (item);\
(plist)[++((int*)(*(plist)))[-1]];\
}while(0)
#define pop(plist)\
(((int*)(*(plist)))[-1]--, (*(plist))[((int*)(*(plist)))[-1]])
#define swap_delete(list, index)do{\
if(index < count(list))\
(list)[index] = (list)[--((int*)(list))[-1]];\
}while(0)
int count(const list(void) list) {
return list ? ((int *)list)[-1] : 0;
}
int capacity(const list(void) list) {
return list ? ((int *)list)[-2] : 0;
}
void destroy(list(void) *plist) {
free((int *)(*plist) - 4);
*plist = NULL;
}
static void private__reserve(list(void) *plist, int min_capacity, int item_size) {
int cap = capacity(*plist);
if (cap < min_capacity) {
cap *= 2;
if (cap < 64)
cap = 64;
while (cap < min_capacity)
cap *= 2;
// Overallocate by 4 ints to keep overall alignment to 16 bytes.
int cnt = count(*plist);
int *newlist = (int *)realloc(*plist ? (int *)(*plist) - 4 : NULL, cap * item_size + 4 * sizeof(int)) + 4;
newlist[-2] = cap;
newlist[-1] = cnt;
*plist = newlist;
}
}
#include <assert.h>
int main(void) {
{
list(int) *ints = NULL;
assert(count(ints) == 0);
assert(capacity(ints) == 0);
for (int i = 0; i < 1024; ++i)
add(&ints, i);
assert(count(ints) == 1024);
for (int i = 0; i < 1024; ++i)
assert(ints[i] == i);
for (int i = 1023; i >= 0; --i)
assert(pop(&ints) == i);
assert(count(ints) == 0);
destroy(&ints);
assert(!ints);
}
{
// This shouldn't leak.
for (int i = 0; i < 100000; ++i) {
list(int) *ints = NULL;
for (int j = 0; j < 10000; ++j)
add(&ints, j);
destroy(&ints);
}
}
}