-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
50 lines (41 loc) · 948 Bytes
/
vector.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
#include "common.h"
#include "vector.h"
#define INITIAL_BUF_LEN 16
void vector_init(Vector *vector)
{
vector->buf = emalloc(sizeof(*vector->buf)*INITIAL_BUF_LEN, "vector init");
vector->buf_len = INITIAL_BUF_LEN;
vector->len = 0;
}
void vector_free(Vector *vector)
{
free(vector->buf);
}
size_t vector_len(Vector *vector)
{
return vector->len;
}
static void grow(Vector *vector)
{
vector->buf_len *= 2;
vector->buf = erealloc(vector->buf, sizeof(*vector->buf)*vector->buf_len,
"vector grow");
}
void vector_append(Vector *vector, void *val)
{
if (vector->len == vector->buf_len)
grow(vector);
vector->buf[vector->len++] = val;
}
void vector_set(Vector *vector, size_t index, void *val)
{
vector->buf[index] = val;
}
void *vector_get(Vector *vector, size_t index)
{
return vector->buf[index];
}
void **vector_storage(Vector *vector)
{
return vector->buf;
}