-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvec.c
122 lines (107 loc) · 2.65 KB
/
svec.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Svec data structure src
// Thomas Kaunzinger
// Imports
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "svec.h"
// Makes an empty svec
svec* make_svec() {
svec* sv = malloc(sizeof(svec));
sv->data = malloc(2 * sizeof(char*));
sv->size = 0;
sv->capacity = 2;
return sv;
}
// Frees the memory of the svec
void free_svec(svec* sv) {
if (sv->data) {
for (int ii = 0; ii < sv->size; ii++) {
if (sv->data[ii]) {
free(sv->data[ii]);
sv->data[ii] = NULL;
}
}
free(sv->data);
sv->data = NULL;
}
if (sv) {
free(sv);
sv = NULL;
}
}
// Returns the value at the desired svec index
char* svec_get(svec* sv, int ii) {
assert(ii >= 0 && ii < sv->size);
return sv->data[ii];
}
// Inserts a duplicated char* into the svec
void svec_put(svec* sv, int ii, char* item) {
assert(ii >= 0 && ii < sv->size);
sv->data[ii] = strdup(item);
}
// Returns the index of the item if it exists and -1 otherwise
int svec_find(svec* sv, char* item) {
for (int ii = 0; ii < sv->size; ii++) {
if (!strcmp(item, sv->data[ii])) {
return ii;
}
}
return -1;
}
// Creates a sliced svec along the desired indicies
svec* svec_slice(svec* sv, int startIndex, int endIndex) {
svec* subSvec = make_svec();
for (int ii = startIndex; ii < endIndex; ii++) {
svec_push_back(subSvec, svec_get(sv, ii));
}
return subSvec;
}
// Pushes an element to the end of the svec
void svec_push_back(svec* sv, char* item) {
int ii = sv->size;
// Grows the vector if needed
if (sv->capacity <= ii) {
svec_grow(sv);
}
sv->size = ii + 1;
svec_put(sv, ii, item);
}
// Doubles the vector capacity
void svec_grow(svec* sv) {
int newCap = sv->capacity * 2;
char** newData = realloc(sv->data, sizeof(char*) * newCap);
sv->data = newData;
sv->capacity = newCap;
}
// Swaps two indicies in a svec
void svec_swap(svec* sv, int ii, int jj) {
// Ensures that the data isn't OOB
assert(ii >= 0 && ii < sv->size && jj >= 0 && jj < sv->size);
char* temp = sv->data[jj];
sv->data[jj] = sv->data[ii];
sv->data[ii] = temp;
}
// Prints the contents of the svec
void svec_print(svec* sv) {
for (int ii = 0; ii < sv->size; ii++) {
puts(sv->data[ii]);
}
}
// Reverses the svec
void svec_reverse(svec* sv) {
// Swaps everything until the middle is reached
for (int ii = 0; ii < sv->size / 2; ii++) {
svec_swap(sv, ii, sv->size - ii - 1);
}
}
// I don't think this is needed anymore
// Returns the svec as just an array... well actually a char**
// char** svec_as_array(svec* sv) {
// char* running[sv->size];
// for (int ii = 0; ii < sv->size; ii++) {
// running[ii] = svec_get(sv, ii);
// }
// return running;
// }