-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynarray.h
46 lines (40 loc) · 1.88 KB
/
dynarray.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
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#define DynArray(type) \
struct { \
type *arr; \
uint64_t size; \
uint64_t capacity; \
Arena *alloc; \
}
#define elem_size(dyn) sizeof(*((dyn)->arr))
#define dyn_init(allo, dyn, start_capacity) \
do { \
(dyn)->capacity = (start_capacity); \
(dyn)->size = 0; \
(dyn)->alloc = (allo); \
(dyn)->arr = arena_alloc((dyn)->alloc, elem_size(dyn) * (start_capacity)); \
} while (0)
#define dyn_resize(dyn, new_size) \
do { \
(dyn)->arr = arena_realloc((dyn)->alloc, (dyn)->arr, elem_size(dyn) * (dyn)->capacity, elem_size(dyn) * (new_size)); \
(dyn)->capacity = (new_size); \
} while (0)
#define dyn_append(dyn, elem) \
do { \
if ((dyn)->size + 1 > (dyn)->capacity) { \
dyn_resize((dyn), (dyn)->capacity * 2); \
} \
(dyn)->arr[(dyn)->size] = elem; \
(dyn)->size += 1; \
} while (0)
#define dyn_get(dyn, idx) (dyn)->arr[(idx)]
#define dyn_assign(dyn1, dyn2) \
do { \
(dyn1)->arr = (dyn2)->arr; \
(dyn1)->size = (dyn2)->size; \
(dyn1)->capacity = (dyn2)->capacity; \
} while (0)