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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#include "../include/osm/utils.h"
#include <stdlib.h>
#include <string.h>
#define VECT_MAX_GROW 1024
#define VECT_INIT_CAP 4
/**
* Initialize a new vector
*/
Vector vect_init(unsigned int elsz)
{
Vector out = {
.elsz = elsz,
.size = VECT_INIT_CAP,
.count = 0
};
out.data = malloc(out.elsz * out.size);
return out;
}
/**
* Unexported function for auto-growing the vector
*/
void _vect_grow(Vector *vec)
{
if (vec->elsz == 0)
{
return;
}
if (vec->size == 0)
{
vec->size = VECT_INIT_CAP;
vec->data = realloc(vec->data, vec->size * vec->elsz);
return;
}
if (vec->size > VECT_MAX_GROW)
{
vec->size += VECT_MAX_GROW;
}
else
{
vec->size *= 2;
}
vec->data = realloc(vec->data, vec->size * vec->elsz);
}
/**
* Unexported function for auto-shrinking the vector
*/
void _vect_shrink(Vector *vec)
{
if (vec->size / 2 < VECT_INIT_CAP)
{
return;
}
vec->size /= 2;
vec->data = realloc(vec->data, vec->size * vec->elsz);
}
/**
* Push a new element to the end of the vector
* Returns false if the vector was invalid
*/
bool vect_push(Vector *vec, void *el)
{
if (vec->elsz == 0 || vec->size == 0)
return false;
memcpy(vec->data + (vec->count * vec->elsz), el, vec->elsz);
vec->count++;
if (vec->size == vec->count)
{
_vect_grow(vec);
}
return true;
}
/**
* Pop an element off of the end of the vector
* Returns true if an element was removed, false if there was no element to remove.
*/
bool vect_pop(Vector *vec)
{
if (vec->count < 1)
return false;
vec->count--;
if (vec->count < vec->size / 3)
{
_vect_shrink(vec);
}
return true;
}
/**
* Get an element from the vector
* Returns NULL if the index is invalid
*/
void *vect_get(Vector *vec, unsigned int index)
{
if (index >= vec->count)
return NULL;
return vec->data + (index * vec->elsz);
}
/**
* Set an element in the vector
* If the index is outside the vector, returns false.
*/
bool vect_set(Vector *vec, unsigned int index, void *el)
{
if (index >= vec->count)
return false;
memcpy(vec->data + (index * vec->elsz), el, vec->elsz);
return true;
}
/**
* Remove an element from the given position in the vector
* Returns false if the index was invalid
* O(n)
*/
bool vect_remove(Vector *vec, unsigned int index)
{
if (index >= vec->count)
return false;
if (index == vec->count - 1)
{
return vect_pop(vec);
}
char *curr = vec->data + (vec->elsz * index);
char *next = vec->data + (vec->elsz * (index + 1));
for(int i = 0; i < vec->elsz * (vec->count - (index + 1)); i++)
{
*(curr + i) = *(next + i);
}
return vect_pop(vec);
}
/**
* Add an element into the vector
* Returns false if the index is invalid
* O(n)
*/
bool vect_insert(Vector *vec, unsigned int index, void *el)
{
if (index > vec->count)
return false;
if (index == vec->count)
return vect_push(vec, el);
char *curr = vec->data + (vec->elsz * index);
char *next = vec->data + (vec->elsz * (index + 1));
for (int i = vec->elsz * (vec->count - (index + 1)); i > 0; i--)
{
*(next + i - 1) = *(curr + i - 1);
}
vec->count++;
if (vec->size == vec->count)
{
_vect_grow(vec);
}
return true;
}
/**
* Clear all data from the vector
*/
void vect_clear(Vector *vec)
{
int elsz = vec->elsz;
vect_end(vec);
*vec = vect_init(elsz);
}
/**
* Free associated data of the vector
*/
void vect_end(Vector *vec)
{
vec->size = 0;
vec->count = 0;
vec->elsz = 0;
free(vec->data);
vec->data = NULL;
}
|