-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_arr.h
60 lines (49 loc) · 1.43 KB
/
dyn_arr.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
#include <stddef.h>
#include <assert.h>
#include <string.h>
#pragma once
#define DecDynArr(T) \
typedef struct{ \
T *ptr; \
size_t len; \
size_t cap; \
}ArrOf ## T;
#define apush(a, v) \
(dyn_arr_make_space( \
((void**)(&(a)->ptr)), \
&(a)->cap, \
&(a)->len, \
sizeof((a)->ptr[0]) \
), (a)->ptr[(a)->len - 1] = (v), &(a)->ptr[(a)->len -1])
#define apush_ptr(a, v) \
(dyn_arr_make_space( \
((void**)(&(a)->ptr)), \
&(a)->cap, \
&(a)->len, \
sizeof(*(v)) \
), ((a)->ptr)[(a)->len - 1] = *(v), &(a)->ptr[(a)->len -1])
#define aget(a, i) \
(assert((i) < (a)->len && "out of bounds"), ((a)->ptr)[i])
#define aget_ptr(a, i) \
(assert((i) < (a)->len && "out of bounds"), &((a)->ptr)[i])
#define alast(a) \
(assert((a)->len && "out of bounds"), ((a)->ptr)[(a)->len - 1])
#define alast_ptr(a) \
(assert((a)->len && "out of bounds"), &((a)->ptr)[(a)->len - 1])
#define arev(a) \
dyn_arr_rev((a)->ptr, (a)->len, sizeof((a)->ptr[0]))
#define aremove(a, i) \
(memmove( \
&(a)->ptr[i], \
&(a)->ptr[(i)+1], \
sizeof((a)->ptr[0])*((a)->len-1-(i)) \
), ((a)->len)--)
#define dynarr_free(a) (free((a)->ptr), memset(a, 0, sizeof(*(a))))
void dyn_arr_make_space(
void **arr_ptr, size_t *cap, size_t *len,
size_t data_size
);
void dyn_arr_rev(
void *arr_ptr, size_t len,
size_t data_size
);