-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvec.c
More file actions
51 lines (42 loc) · 1.07 KB
/
svec.c
File metadata and controls
51 lines (42 loc) · 1.07 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
#include "svec.h"
#include <stdlib.h>
#include <string.h>
svec*
make_svec() {
svec* xs = malloc(sizeof(svec));
xs->size = 0;
xs->capacity = 2;
xs->data = malloc((xs->capacity + 1) * sizeof(char*));
return xs;
}
void
free_svec(svec* xs) {
int ii;
for(ii = 0; ii < xs->size; ++ii) {
if(xs->data[ii] != 0) {
free(xs->data[ii]);
}
}
free(xs->data);
free(xs);
}
void
svec_push(svec* xs, char* x) {
if(xs->size >= xs->capacity) {
xs->capacity *= 2;
xs->data = (char**) realloc(xs->data, (xs->capacity + 1) * sizeof(char*));
}
// note: when I need to put into index i, I will need to free(xs->data[ii]) before i insert (or mem leak)
// this is currently fine because we are only pushing to the end of the vector here
xs->data[xs->size++] = strdup(x);
}
svec*
svec_subvec(svec* xs, int start, int end) {
svec* result = make_svec();
int ii;
for(ii = start; ii < end; ++ii) {
svec_push(result, xs->data[ii]);
}
result->data[result->size] = 0;
return result;
}