-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
65 lines (62 loc) · 2.78 KB
/
vector.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
61
62
63
64
65
#ifndef VECTOR_H
#define VECTOR_H
#include <inttypes.h>
#include <stdlib.h>
#define INIT_CAPACITY 8
#define CAPACITY_MUL 2
#define DECLARE_VECTOR(T) \
typedef struct Vec_##T Vec_##T; \
struct Vec_##T { \
T* data; \
uint32_t size; \
uint32_t capacity; \
T (*Get)(Vec_##T*, uint32_t); \
void (*Set)(Vec_##T*, uint32_t, T); \
void (*Push)(Vec_##T*, T); \
}; \
\
void VecFree_##T(Vec_##T* v) { \
free(v->data); \
} \
\
T VecGet_##T(Vec_##T* v, uint32_t idx) { \
return v->data[idx]; \
} \
\
void VecSet_##T(Vec_##T* v, uint32_t idx, T val) { \
v->data[idx] = val; \
} \
\
void VecExpand_##T(Vec_##T* v) { \
v->capacity *= CAPACITY_MUL; \
T* tmp = v->data; \
v->data = malloc(sizeof(T) * v->capacity); \
for (uint32_t _idx = 0; _idx < v->size; _idx++) VecSet_##T(v, _idx, tmp[_idx]); \
free(tmp); \
} \
\
void VecResize_##T(Vec_##T* v, uint32_t sz) { \
if (sz > v->capacity) { \
v->capacity = sz; \
T* tmp = v->data; \
v->data = malloc(sizeof(T) * v->capacity); \
for (uint32_t _idx = 0; _idx < v->size; _idx++) VecSet_##T(v, _idx, tmp[_idx]); \
free(tmp); \
} \
v->size = sz; \
} \
\
void VecPush_##T(Vec_##T* v, T val) { \
if (v->size == v->capacity - 1) VecExpand_##T(v); \
VecSet_##T(v, v->size++, val); \
} \
\
void VecInit_##T(Vec_##T* v) { \
v->data = malloc(sizeof(T) * INIT_CAPACITY); \
v->size = 0; \
v->capacity = INIT_CAPACITY; \
v->Get = VecGet_##T; \
v->Set = VecSet_##T; \
v->Push = VecPush_##T; \
}
#endif