-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvec.c
More file actions
40 lines (36 loc) · 926 Bytes
/
vec.c
File metadata and controls
40 lines (36 loc) · 926 Bytes
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
#include "vec.h"
#include <stdlib.h>
void vec_clean_impl(struct Vec *vec) {
for (unsigned int i = 0; i < vec->count; i++) {
free(vec->values[i]);
}
free(vec->values);
free(vec);
}
void vec_push_impl(struct Vec *vec, void *v) {
if (vec->count < vec->capacity) {
vec->values[vec->count] = v;
vec->count += 1;
return;
}
vec->capacity *= 2;
vec->values = realloc(vec->values, vec->capacity * sizeof(void *));
vec->values[vec->count] = v;
vec->count += 1;
}
void *vec_get_impl(struct Vec *vec, unsigned int index) {
if (index >= vec->count) {
return NULL;
}
return vec->values[index];
}
struct Vec *new_vec() {
struct Vec *vec = (struct Vec *)malloc(sizeof(struct Vec));
vec->capacity = 10;
vec->count = 0;
vec->clean = vec_clean_impl;
vec->push = vec_push_impl;
vec->get = vec_get_impl;
vec->values = (void **)malloc(sizeof(void *) * vec->capacity);
return vec;
}