-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.h
More file actions
351 lines (273 loc) · 7.98 KB
/
vec.h
File metadata and controls
351 lines (273 loc) · 7.98 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// vec.h - A vector implementation that stores generic pointers.
//
// Library macros:
// - STRUCTYPES_IMPLEMENTATION: defines all of structype's implementations.
// - STRUCTYPES_DEBUG: if defined, prints error messages.
//
// File macros:
// - VEC_CAPACITY_STEP: used to initialize and realloc the Vec's capacity.
// By default, `4`.
//
// Function macros:
// - vec_from(arr)
#ifndef VEC_H_
#define VEC_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
/* Dynamic array that holds generic pointers. */
typedef struct Vec {
/* Array of generic pointers.*/
void **items;
/* How many items the Vec is holding.*/
size_t size;
/* How many items the Vec can hold.*/
size_t capacity;
} Vec;
/**
* Creates a new `Vec`.
*
* Returns `NULL` when:
* - `VEC_CAPACITY_STEP < 1`.
* - `malloc` fails to allocate a new `Vec`.
* - `malloc` fails to initialize `Vec.items`.
*/
Vec *vec_new();
/**
* Creates a new `Vec` from an existing array and `size`.
*
* Return `NULL` when:
* - `arr` evaluates to false.
* - `vec_new` or `vec_push` fail.
*/
Vec *vec_from_arr(void **arr, size_t size);
/**
* Frees `v->items` and `v`.
* If you need to free each element, use `vec_free` instead.
*
* Returns `false` when:
* - `v` evaluates to false.
*/
bool vec_free_struct(Vec *v);
/**
* Frees `v->items` (iterating over each element) and `v`.
* Uses `free`, so use this to free data like `char*` or `void*`.
*
* Returns `false` when:
* - `v` evaluates to false.
*/
bool vec_free(Vec *v);
/**
* Adds an `item` at the end of `v->items`.
*
* Returns `false` when:
* - `VEC_CAPACITY_STEP < 1`.
* - `realloc` for `v->items` fails.
*/
bool vec_push(Vec *v, void *item);
/**
* Adds `src->items` at the end of `dst->items`.
*
* Returns `false` when:
* - `size->size == 0`.
* - `realloc` for `dst->items` fails.
*/
bool vec_extend(Vec *dst, Vec *src);
/**
* Returns a pointer to the element at `n`. Supports negative indexing.
*
* Returns `NULL` when:
* - `n` is out of bounds.
* - `v->size == 0`.
*/
void *vec_get(Vec *v, int n);
/**
* Returns the value at the last element of `v` and assigns it to `NULL`.
*
* Returns `NULL` when:
* - `v` evaluates to false.
* - `v->size == 0`.
*/
void *vec_pop(Vec *v);
/**
* Sorts the elements of `v->items`.
*
* Returns `false` when:
* - `v` evaluates to false.
*/
bool vec_sort(Vec *v);
/**
* Compares `v1->items` with `v2->items`.
*
* Both arrays must be ordered to be considered equal (see `vec_sort`).
*
* Returns `false` when:
* - `v1` or `v2` evaluate to false.
* - `v1->size` or `v2->size` are different.
* - The compared elements are different.
*/
bool vec_eq(Vec *v1, Vec *v2);
/**
* Prints each item in `v`.
*
* Returns `false` when:
* - `v->size == 0`.
*/
bool vec_print(Vec *v);
/**
* Prints the values of `size` and `capacity` of `v`.
*
* Returns `false` when:
* - `v` evaluates to false.
*/
bool vec_info(Vec *v);
#endif // VEC_H_
// This allows the user to use an implementation in more than one file without linker errors
#if !defined(__STRUCTYPES_VEC_IMPLEMENTED) && (defined(STRUCTYPES_IMPLEMENTATION))
#define __STRUCTYPES_VEC_IMPLEMENTED
#ifndef VEC_CAPACITY_STEP
#define VEC_CAPACITY_STEP 4
#endif // VEC_CAPACITY_STEP
#ifdef STRUCTYPES_DEBUG
static void _vec_err(const char *file, int line, const char *func, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
fprintf(stderr, "[%s:%d] %s: ", file, line, func);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
#define VEC_THROW(ret, fmt, ...) ({ _vec_err(__FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); return ret; })
#else
#define VEC_THROW(ret, fmt, ...) ({ return ret; })
#endif // STRUCTYPES_DEBUG
static int _vec_comp(const void *a, const void *b) {
const char *s1 = *(const char **)a;
const char *s2 = *(const char **)b;
while (*s1 && *s2 && *s1 == *s2) s1++, s2++;
int diff = (unsigned char)*s1 - (unsigned char)*s2;
return diff;
}
/**
* Creates a new `Vec` from an existing buffer.
* This is a conveniency macro that calculates the size of `arr`.
*
* Return `NULL` when:
* - `arr` evaluates to false.
* - `vec_new` or `vec_push` fail.
*/
#define vec_from(arr) \
vec_from_arr((void**)arr, sizeof(arr)/sizeof(arr[0]))
Vec *vec_new() {
if (VEC_CAPACITY_STEP < 1)
VEC_THROW(NULL, "VEC_CAPACITY_STEP must be 1 or greater");
Vec *v = malloc(sizeof(Vec));
if (!v) VEC_THROW(NULL, "malloc error for vector");
v->size = 0;
v->capacity = VEC_CAPACITY_STEP;
v->items = malloc(v->capacity * sizeof(void *));
if (!v->items) VEC_THROW(NULL, "malloc error for vector items");
return v;
}
Vec *vec_from_arr(void **arr, size_t size) {
if (!arr) VEC_THROW(NULL, "arr evaluates to false");
Vec *v = vec_new();
if (!v) VEC_THROW(NULL, "vec_new failed");
for (size_t i = 0; i < size; i++)
if (!vec_push(v, arr[i])) VEC_THROW(NULL, "vec_new failed");
return v;
}
bool vec_free_struct(Vec *v) {
if (!v) VEC_THROW(false, "v evaluates to false");
free(v->items);
free(v);
v = NULL;
return true;
}
bool vec_free(Vec *v) {
if (!v) VEC_THROW(false, "v evaluates to false");
if (v->items) {
for (size_t i = 0; i < v->size; i++)
if (v->items[i]) free(v->items[i]);
free(v->items);
v->items = NULL;
}
free(v);
v = NULL;
return true;
}
bool vec_push(Vec *v, void *item) {
if (!v) VEC_THROW(false, "v evaluates to false");
if (v->size == v->capacity) {
if (VEC_CAPACITY_STEP < 1)
VEC_THROW(false, "VEC_CAPACITY_STEP must be 1 or greater");
v->capacity += VEC_CAPACITY_STEP;
void *tmp = realloc(v->items, v->capacity * sizeof(void *));
if (!tmp) VEC_THROW(false, "realloc error");
v->items = tmp;
}
v->items[v->size++] = item;
return true;
}
bool vec_extend(Vec *dst, Vec *src) {
if (!dst) VEC_THROW(false, "dst evaluates to false");
if (!src->size) VEC_THROW(false, "src vector is uninitialized");
size_t totalSize = dst->size + src->size;
if (totalSize > dst->capacity) {
void *tmp = realloc(dst->items, totalSize * sizeof(void *));
if (!tmp) VEC_THROW(false, "realloc error");
dst->items = tmp;
dst->capacity = totalSize;
}
for (size_t i = 0; i < src->size; i++)
dst->items[dst->size + i] = src->items[i];
dst->size = totalSize;
return true;
}
void *vec_get(Vec *v, int n) {
if (!v) VEC_THROW(false, "v evaluates to false");
if (!v->size) VEC_THROW(NULL, "vector is uninitialized");
int len = (int)v->size;
if (abs(n) > len)
VEC_THROW(NULL, "index %d out of bounds (size %d)", n, len);
return (n < 0) ? v->items[n + len] : v->items[n];
}
void *vec_pop(Vec *v) {
if (!v) VEC_THROW(NULL, "v evaluates to false");
if (!v->size) VEC_THROW(NULL, "v has no elements");
v->size--;
void *value = v->items[v->size];
v->items[v->size] = NULL;
return value;
}
bool vec_sort(Vec *v) {
if (!v) VEC_THROW(false, "v evaluates to false");
qsort(v->items, v->size, sizeof(void *), _vec_comp);
return true;
}
bool vec_eq(Vec *v1, Vec *v2) {
if (!v1) VEC_THROW(false, "v1 evaluates to false");
if (!v2) VEC_THROW(false, "v2 evaluates to false");
if (v1->size != v2->size) VEC_THROW(false, "sizes are different");
for (size_t i = 0; i < v1->size; i++) {
char *s1 = v1->items[i];
char *s2 = v2->items[i];
while (*s1 && *s2) if (*s1++ != *s2++) return false;
}
return true;
}
bool vec_print(Vec *v) {
if (!v) VEC_THROW(false, "v evaluates to false");
if (!v->size) VEC_THROW(false, "v is uninitialized");
for (size_t i = 0; i < v->size; i++)
printf("%zu %s\n", i, (char *)vec_get(v, i));
return true;
}
bool vec_info(Vec *v) {
if (!v) VEC_THROW(false, "v evaluates to false");
printf("size: %zu\n", v->size);
printf("capacity: %zu\n", v->capacity);
return true;
}
#endif // STRUCTYPES_IMPLEMENTATION