-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_vector.h
More file actions
86 lines (73 loc) · 4.39 KB
/
a_vector.h
File metadata and controls
86 lines (73 loc) · 4.39 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* a_string/a_vector: a scuffed dynamic vector/string implementation.
*
* Copyright (c) Eason Qin, 2025.
*
* This source code form is licensed under the MIT/Expat license.
* Visit the OSI website for a digital version.
*/
#ifndef _A_VECTOR_H
#define _A_VECTOR_H
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#define AV_INITIAL_SIZE 5
#define AV_GROWTH_FACTOR 2
#define AV_DECL(T, name) \
typedef struct { \
T* data; \
u32 len; \
u32 cap; \
} name;
#define av_append(v, item) \
do { \
if ((v)->len + 1 > (v)->cap) { \
if ((v)->cap == 0) \
(v)->cap = AV_INITIAL_SIZE; \
else \
(v)->cap *= AV_GROWTH_FACTOR; \
(v)->data = realloc((v)->data, sizeof(*(v)->data) * (v)->cap); \
check_alloc((v)->data); \
} \
(v)->data[(v)->len++] = (item); \
} while (0)
#define av_clear(v) \
do { \
memset((v)->data, 0, sizeof(*(v)->data) * (v)->cap); \
(v)->len = 0; \
} while (0)
#define av_free(v) \
do { \
if ((v)->data) { \
free((v)->data); \
(v)->data = NULL; \
} \
} while (0)
#define av_reserve(v, amt) \
do { \
if (amt >= (v)->len) { \
(v)->data = realloc((v)->data, sizeof(*(v)->data) * amt); \
check_alloc((v)->data); \
(v)->cap = amt; \
} \
} while (0)
#define av_append_many(v, itms, itms_len) \
do { \
if ((v)->len + itms_len > (v)->cap) { \
(v)->cap += itms_len; \
(v)->data = realloc((v)->data, sizeof(*(v)->data) * (v)->cap); \
check_alloc((v)->data); \
} \
memcpy(&(v)->data[(v)->len], itms, sizeof(*(v)->data) * itms_len); \
(v)->len += itms_len; \
} while (0)
#define av_last(v) ((v)->data[(assert((v)->len > 0), (v)->len - 1)])
#define av_at(v, pos) ((v)->data[(assert(0 <= pos && pos < (v)->len), pos)])
#define av_pop(v, pos) ((v)->data[(assert((v)->len > 0), --(v)->len)])
#define av_pop_many(v, count) \
do { \
assert(count < (v)->len); \
(v)->len -= count; \
} while (0)
#endif // _A_VECTOR_H