-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
167 lines (136 loc) · 3.67 KB
/
Copy pathvector.c
File metadata and controls
167 lines (136 loc) · 3.67 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <wchar.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "vector.h"
/*
struct strVec{
size_t capacity;
size_t size;
char** arrP;
};
*/
//Returns a strvec, or NULL if allocation failed.
strVec* strVec_make(void){
strVec* vec = malloc(sizeof(strVec));
if(!vec){ return vec; }
vec->capacity = 1;
vec->size = 0;
vec->arrP = malloc(sizeof(char*));
if(!vec->arrP){
free(vec);
return NULL;
}
return vec;
}
//Adds a copy of str to rear of vec. Returns 0 if success, 1 otherwise;
int strVec_append(strVec* vec, const char* str){
//assert(vec);
return strVec_insert(vec, str, vec->size);
}
//Adds a copy of str into vec at position pos. Returns 0 if success, 1 otherwise
int strVec_insert(strVec* vec, const char* str, size_t pos){
//assert(vec);
//assert(vec->arrP);
//assert(vec->capacity >= vec->size);
//assert(pos <= vec->size);
//Grow
if(vec->capacity == vec->size){
char** expand = realloc(vec->arrP, sizeof(char*)*(vec->capacity*2));
if(!expand) return 1;
vec->arrP = expand;
vec->capacity *=2;
}
//Shift
if(vec->size != 0 && pos < vec->size){
memmove(&(vec->arrP[pos+1]), &(vec->arrP[pos]), sizeof(char*) * (vec->size-pos));
}
//Copy and add
char* arrString = malloc(sizeof(char)*(strlen(str)+1));
if(!arrString) return 1;
strcpy(arrString, str);
vec->arrP[pos] = arrString;
vec->size++;
return 0;
}
//Deletes the last element
void strVec_pop_back(strVec* vec){
//assert(vec);
//assert(vec->arrP);
//assert(vec->size != 0);
//free(vec->arrP[--vec->size]);
free(vec->arrP[vec->size]);
}
//Removes string at index, deleting it.
void strVec_remove(strVec* vec, size_t pos){
//assert(vec);
//assert(vec->arrP);
//assert(pos < vec->size);
if(vec->size - 1 == pos){ //End of vec
strVec_pop_back(vec);
return;
}
free(vec->arrP[pos]);
memmove(&(vec->arrP[pos]), &(vec->arrP[pos+1]), sizeof(char*) * (vec->size-1 - pos));
vec->size--;
}
//Deletes and returns element at index
char* strVec_remove_return(strVec* vec, size_t pos){
//assert(vec);
//assert(vec->arrP);
//assert(pos < vec->size);
char* rval = strVec_get_copy(vec, pos);
strVec_remove(vec, pos);
return rval;
}
//Returns a copy of string at the given index
char* strVec_get_copy(const strVec* vec, size_t pos){
//assert(vec);
//assert(vec->arrP);
//assert(pos < vec->size);
char* result = malloc(sizeof(char)*(strlen(vec->arrP[pos])+1));
if(!result) return NULL;
strcpy(result, vec->arrP[pos]);
return result;
}
//Deletes vec and the strings it contains. Using vec after calling this method is UB. frees vec itself.
void strVec_delete(strVec* vec){
//assert(vec);
//assert(vec->arrP);
for(size_t i=0; i < vec->size; ++i){
free(vec->arrP[i]);
}
free(vec->arrP);
vec->arrP = NULL;
free(vec);
vec = NULL;
}
//Counts the number of strings in vec that match str exactly
size_t strVec_count(const strVec* vec, const char* str){
//assert(vec);
//assert(vec->arrP);
size_t count = 0;
for(size_t i= 0; i < vec->size; ++i){
if(0 == strcmp(str, vec->arrP[i])) { ++count; }
}
return count;
}
//Returns index where str was found, or SIZE_MAX if not found
size_t strVec_findStr(const strVec* vec, const char* str){
//assert(vec);
//assert(vec->arrP);
for(size_t i = 0; i < vec->size; ++i){
if(0 == strcmp(str, vec->arrP[i])) { return i; }
}
return SIZE_MAX;
}
void strVec_print(const strVec* vec){
//assert(vec);
//assert(vec->arrP);
for(unsigned long i=0; i < vec->size; ++i){
printf("%lu%s\n", i, vec->arrP[i]);
}
}