-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec.c
56 lines (41 loc) · 965 Bytes
/
vec.c
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
#include "vec.h"
#include "std.h"
Vec *vec_new(void) {
Vec *v;
v = malloc(sizeof(*v));
v->capacity = 8;
v->size = 0;
v->data = malloc(sizeof(void *) * v->capacity);
return v;
}
void vec_reserve(Vec *v, int capacity) {
assert(v != NULL);
assert(capacity >= 0);
if (capacity > v->capacity) {
v->capacity = capacity;
v->data = realloc(v->data, sizeof(void *) * v->capacity);
}
}
void vec_resize(Vec *v, int size) {
assert(v != NULL);
assert(size >= 0);
vec_reserve(v, size);
v->size = size;
}
void *vec_back(Vec *v) {
assert(v != NULL);
assert(v->size > 0);
return v->data[v->size - 1];
}
void vec_push(Vec *v, void *value) {
assert(v != NULL);
if (v->capacity == v->size) {
vec_reserve(v, v->capacity * 2);
}
v->data[v->size++] = value;
}
void *vec_pop(Vec *v) {
assert(v != NULL);
assert(v->size > 0);
return v->data[--v->size];
}