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
|
// src/da.h --- Dynamic Array
#ifndef INCLUDED_DA_H
#define INCLUDED_DA_H
#ifndef DA_REALLOC
void *realloc (void *_ptr, size_t _size);
#define DA_REALLOC realloc
#endif
#ifndef DA_ASSERT
#include <assert.h>
#define DA_ASSERT assert
#endif
#ifndef DA_INIT_CAP
#define DA_INIT_CAP 256
#endif
#define DA_ENSURE_CAPACITY(DA, COUNT) \
({ \
if ((DA).count + (COUNT) > (DA).capacity) { \
if ((DA).capacity == 0) \
(DA).capacity = DA_INIT_CAP; \
while ((DA).count + (COUNT) > (DA).capacity) \
(DA).capacity *= 2; \
(DA).items = DA_REALLOC ((DA).items, \
(DA).capacity*sizeof (*(DA).items)); \
DA_ASSERT((DA).items); \
} \
})
#define DA_SHRINK(DA) \
({ \
(DA).capacity = (DA).count; \
(DA).items = REALLOC((DA).items, \
(DA).capacity * \
sizeof (*(DA).items)); \
DA_ASSERT((DA).items); \
})
#define DA_APPEND(DA, ITEM) \
({ \
DA_ENSURE_CAPACITY((DA), 1); \
(DA).items[(DA).count++] = (ITEM); \
})
#define DA_APPEND_MANY(DA, NEW_ITEMS, NEW_ITEMS_COUNT) \
({ \
DA_ENSURE_CAPACITY ((DA), (NEW_ITEMS_COUNT)); \
memcpy ((DA).items + (DA).count, (NEW_ITEMS), \
(NEW_ITEMS_COUNT)*sizeof (*(DA).items)); \
(DA).count += (NEW_ITEMS_COUNT); \
})
#define DA_FIRST(DA) (DA).items[0]
#define DA_LAST(DA) (DA).items[(DA).count - 1]
#define DA_RESET(DA) ({ (DA).count = 0; })
#define DA_FREE(DA) (void) DA_REALLOC ((DA).items, 0)
#define DA_FOR_EACH(ITEM, DA) for ( \
typeof ((DA).items) ITEM = (DA).items; \
ITEM < (DA).items + (DA).count; ++ITEM)
#endif // INCLUDED_DA_H
// src/da.h ends here
|