-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.c
63 lines (53 loc) · 1.36 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "vector.h"
#include "std.h"
#include "std_io.h"
struct Vector init_vector(void) { return *init_vector_(); }
struct Vector *init_vector_(void)
{
struct Vector *ptr_res = calloc(1, sizeof(struct Vector));
ptr_res->length = 0;
ptr_res->capacity = 256;
ptr_res->vector = calloc(ptr_res->capacity, sizeof(void *));
return ptr_res;
}
void extend_vector(struct Vector *ptr)
{
if (ptr->capacity < ptr->length + 1) {
ptr->vector =
realloc(ptr->vector, ptr->capacity * 2 * sizeof(void *));
ptr->capacity *= 2;
}
}
void push_vector(struct Vector *ptr, const void *tok)
{
extend_vector(ptr);
ptr->vector[ptr->length] = tok;
++(ptr->length);
}
const void *pop_vector(struct Vector *ptr)
{
if (ptr->length == 0) {
assert("tried to pop an empty vector" && 0);
}
--(ptr->length);
return ptr->vector[ptr->length]; /* safe, since it is not yet released or
anything */
}
void concat_vector(struct Vector *ptr_ans, const struct Vector *ptr_vec)
{
if (!ptr_ans) {
fprintf(stderr, "NULL POINTER IN THE FIRST ARGUMENT\n");
exit(1);
}
if (!ptr_vec) {
fprintf(stderr, "NULL POINTER IN THE SECOND ARGUMENT\n");
exit(1);
}
if (!ptr_vec->vector) {
fprintf(stderr, "OH MY GOD: INVALID VECTOR IN THE SECOND ARGUMENT\n");
exit(1);
}
for (int i = 0; i < ptr_vec->length; i++) {
push_vector(ptr_ans, ptr_vec->vector[i]);
}
}