-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharray.h
40 lines (30 loc) · 931 Bytes
/
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
#ifndef ARRAY_H
#define ARRAY_H
#include "types.h"
typedef struct Array Array;
struct Array {
Size size;
Size capacity;
};
#define ARRAY(T) T*
#define array_meta(array) \
((Array*)(((Uint8*)(array)) - sizeof(Array)))
// grow [array] by [expand] elements.
#define array_try_grow(array, expand) \
(((!(array) || array_meta(array)->size + (expand) >= array_meta(array)->capacity)) \
? array_grow(((void **)&(array)), (expand), sizeof(*(array))) \
: true)
// push [value] into [array]
#define array_push(array, value) \
(array_try_grow((array), 1) \
? ((array)[array_meta(array)->size++] = (value), true) \
: false)
// free [array]
#define array_free(array) \
((void)((array) ? (array_delete((void*)(array)), (array) = 0) : 0))
// size of [array]
#define array_size(array) \
((array) ? array_meta(array)->size : 0)
Bool array_grow(void**, Size, Size);
void array_delete(void*);
#endif // ARRAY_H