-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.h
More file actions
30 lines (21 loc) · 890 Bytes
/
Copy patharray.h
File metadata and controls
30 lines (21 loc) · 890 Bytes
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
#ifndef Array_H
#define Array_H
#include <stdlib.h>
#include <string.h>
#define typeof __typeof__
struct ArrayHeader { unsigned long length, capacity;};
#define Array(T) T*
#define array_length(a) (((struct ArrayHeader*)(a)-1)->length)
#define array_capacity(a) (((struct ArrayHeader*)(a)-1)->capacity)
#define array_new() \
(void*)((struct ArrayHeader*)malloc(sizeof(struct ArrayHeader)) + 1)
#define array_free(a) free((struct ArrayHeader*)(a) - 1)
#define array_ensure(a, c) \
*(a) = (void*)((struct ArrayHeader*) realloc( (struct ArrayHeader*)(*(a)) - 1, \
sizeof(struct ArrayHeader) + (array_capacity(*(a)) = (c))*sizeof(**(a)) ) + 1)
#define array_append(a, v) \
(array_length(*(a)) >= array_capacity(*(a)) ? \
(typeof(**(a))*)(array_ensure((a), array_capacity(*(a)) * 2)) \
: *(a))[array_length(*(a))++] = (v)
#define array_pop(a) --array_length(a)
#endif