-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.c
177 lines (124 loc) · 4.28 KB
/
vec.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
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
#include <execinfo.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "vec.h"
#define VEC_INITIAL_CAPACITY 6
#define VEC_FATAL_ERR(condition, msg, ...) \
do { \
if (condition) { \
fprintf(stderr, "%s: " msg, __func__, ##__VA_ARGS__); \
vec_print_trace(); \
exit(EXIT_FAILURE); \
} \
} while (false)
static void vec_print_trace() {
void *array[40];
size_t size = backtrace(array, 40);
char **strings = backtrace_symbols(array, size);
if (strings == NULL) return;
printf("\t\e[31mobtained %ld stack frames.\n", size);
for (int i = 0; i < size; i++) {
printf("\t\t%d: %s\n", i, strings[i]);
}
printf("\e[0m\n");
free(strings);
}
void vec_init(struct vec_t *const vec, size_t size) {
VEC_FATAL_ERR(vec == NULL, "invalid argument.");
VEC_FATAL_ERR(size == 0, "size cannot be \033[1m0\033[0m.");
vec->capacity = 6;
vec->length = 0;
vec->items = calloc(sizeof(void *), vec->capacity);
vec->size = size;
VEC_FATAL_ERR(vec->items == NULL, "failed to calloc.");
}
void vec_push(struct vec_t *const vec, void const *const item) {
VEC_FATAL_ERR(vec == NULL, "invalid argument.");
VEC_FATAL_ERR(item == NULL, "invalid argument.");
VEC_FATAL_ERR(vec->items == NULL, "vec is not initialized");
if (vec->capacity <= vec->length + 1) {
vec->capacity *= 2;
vec->items = realloc(vec->items, sizeof(void *) * vec->capacity);
VEC_FATAL_ERR(vec->items == NULL, "failed to realloc");
}
void *item_copy = malloc(vec->size);
VEC_FATAL_ERR(item_copy == NULL, "failed to malloc.");
memcpy(item_copy, item, vec->size);
vec->items[vec->length] = item_copy;
vec->length += 1;
}
void vec_insert(struct vec_t *const vec, void const *const restrict item, size_t index) {
VEC_FATAL_ERR(vec == NULL, "invalid argument.");
VEC_FATAL_ERR(item == NULL, "invalid argument.");
VEC_FATAL_ERR(vec->items == NULL, "vec is not initialized");
if (vec->capacity <= vec->length + 1) {
vec->capacity *= 2;
vec->items = realloc(vec->items, sizeof(void *) * vec->capacity);
VEC_FATAL_ERR(vec->items == NULL, "failed to realloc");
}
void *item_copy = malloc(vec->size);
VEC_FATAL_ERR(item_copy == NULL, "failed to malloc.");
memcpy(item_copy, item, vec->size);
VEC_FATAL_ERR(index < 0 || index > vec->length, "index out of bounds.");
for (int i = index; i < vec->length; i++) {
vec->items[i + 1] = vec->items[i];
}
vec->items[index] = item_copy;
vec->length += 1;
}
void vec_remove(struct vec_t *const vec, size_t index) {
VEC_FATAL_ERR(vec == NULL, "invalid argument.");
VEC_FATAL_ERR(vec->items == NULL, "vec is not initialized.");
VEC_FATAL_ERR(index >= vec->length, "index overlap.");
free(vec->items[index]);
for (uint i = index; i + 1 < vec->length; i++) {
vec->items[i] = vec->items[i + 1];
}
vec->length -= 1;
}
void *vec_get(struct vec_t const *const vec, size_t index) {
VEC_FATAL_ERR(vec == NULL, "invalid argument.");
VEC_FATAL_ERR(vec->items == NULL, "vec is not initialized.");
VEC_FATAL_ERR(index >= vec->length, "index overlap.");
return vec->items[index];
}
void vec_concat(struct vec_t *const dest, struct vec_t const *const src) {
VEC_FATAL_ERR(dest == NULL, "invalid argument.");
VEC_FATAL_ERR(src == NULL, "invalid argument.");
VEC_FATAL_ERR(dest->items == NULL, "vec is not initialized");
VEC_FATAL_ERR(src->items == NULL, "vec is not initialized.");
VEC_FATAL_ERR(
dest->size != src->size,
"destination vec doesnt share the same type with "
"source vec."
);
for (uint i = 0; i < src->length; i++) {
vec_push(dest, vec_get(src, i));
}
}
void vec_copy(struct vec_t *dest, struct vec_t const *const src) {
VEC_FATAL_ERR(dest == NULL, "invalid argument.");
VEC_FATAL_ERR(src == NULL, "invalid argument.");
VEC_FATAL_ERR(dest->items == NULL, "vec is not initialized");
VEC_FATAL_ERR(src->items == NULL, "vec is not initialized.");
if (dest->length > 0) {
vec_clear(dest);
}
vec_concat(dest, src);
}
void vec_clear(struct vec_t *const vec) {
VEC_FATAL_ERR(vec == NULL, "invalid argument.");
VEC_FATAL_ERR(vec->items == NULL, "vec is not initialized.");
for (uint i = 0; i < vec->length; i++) {
free(vec->items[i]);
vec->items[i] = NULL;
}
vec->length = 0;
}
void vec_drop(struct vec_t *const vec) {
vec_clear(vec);
free(vec->items);
vec->items = NULL;
}