-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
242 lines (238 loc) · 6.86 KB
/
vector.h
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
#ifndef VECTOR_H
#define VECTOR_H
#include <stdio.h>
#include <stdlib.h>
#define DEFINE_VECTOR(type) \
struct Vector_##type { \
size_t size; \
size_t capacity; \
size_t element_size; \
void* vectorType; \
void (*resize)(struct Vector_##type*, size_t); \
int (*empty)(const struct Vector_##type*); \
void (*shrink_to_fit)(struct Vector_##type*); \
type* (*data)(const struct Vector_##type*); \
type (*at)(const struct Vector_##type*, size_t); \
type (*front)(const struct Vector_##type*); \
type (*back)(const struct Vector_##type*); \
void (*push_back)(struct Vector_##type*, type); \
void (*pop_back)(struct Vector_##type*); \
void (*insert)(struct Vector_##type*, size_t, type); \
void (*erase)(struct Vector_##type*, size_t); \
void (*clear)(struct Vector_##type*); \
void (*swap)(struct Vector_##type*, struct Vector_##type*); \
void (*free)(struct Vector_##type*); \
}; \
\
typedef struct { \
type* data; \
} Vector_##type##_inner; \
\
/* Initialising and resizing */ \
\
void initialiseValues_##type(type* data, const size_t from, const size_t to) { \
size_t i = 0; \
for (i = from; i < to; i++) { \
data[i] = 0; \
} \
} \
\
void vector_resize_##type(struct Vector_##type* v, size_t newSize) { \
Vector_##type##_inner* temp = (Vector_##type##_inner*)v->vectorType; \
v->capacity = newSize; \
\
if (newSize >= v->capacity) { \
temp->data = realloc(temp->data, newSize * sizeof(type)); \
if (!(temp->data)) { \
fprintf(stderr, "Error: reallocate failed\n"); \
exit(1); \
} \
initialiseValues_##type(temp->data, v->capacity, newSize); \
v->capacity = newSize; \
} else { \
initialiseValues_##type(temp->data, v->capacity, newSize); \
v->size = newSize; \
} \
} \
\
int vector_empty_##type(const struct Vector_##type* v) { \
if (v->size) { \
return 0; \
} else { \
return 1; \
} \
} \
\
void vector_shrink_to_fit_##type(struct Vector_##type* v) { \
Vector_##type##_inner* temp = (Vector_##type##_inner*)v->vectorType; \
temp->data = realloc(temp->data, v->size * sizeof(type)); \
if (!(temp->data)) { \
fprintf(stderr, "Error: reallocate failed\n"); \
exit(1); \
} \
\
v->capacity = v->size; \
} \
\
/* Element access */ \
type* vector_data_##type(const struct Vector_##type* v) { \
Vector_##type##_inner* temp = (Vector_##type##_inner*)v->vectorType; \
return temp->data; \
} \
\
type vector_at_##type(const struct Vector_##type* v, size_t index) { \
Vector_##type##_inner* temp = (Vector_##type##_inner*)v->vectorType; \
\
if (index >= v->size) { \
fprintf(stderr, "Error: Index %d is out of bounds! (Size: %d)\n", (int)index, (int)v->size); \
exit(1); \
} \
return temp->data[index]; \
} \
\
type vector_front_##type(const struct Vector_##type* v) { \
return vector_at_##type(v, 0); \
} \
\
type vector_back_##type(const struct Vector_##type* v) { \
return vector_at_##type(v, (v->size - 1)); \
} \
\
/* Adding and removing */ \
void vector_push_back_##type(struct Vector_##type* v, type input) { \
Vector_##type##_inner* temp = (Vector_##type##_inner*)v->vectorType; \
if (v->vectorType == NULL) { \
fprintf(stderr, "Error: Vector type not initialized.\n"); \
exit(1); \
} \
\
if (v->size == v->capacity) { \
vector_resize_##type(v, (v->capacity * 2)); \
} \
\
temp->data[v->size] = input; \
v->size += 1; \
} \
\
void vector_pop_back_##type(struct Vector_##type* v) { \
initialiseValues_##type(vector_data_##type(v), (v->size - 1), v->size); \
v->size--; \
} \
\
void vector_insert_##type(struct Vector_##type* v, size_t pos, type insertValue) { \
type previous = insertValue; \
type next; \
type* data = vector_data_##type(v); \
size_t i = 0; \
\
if (pos > (v->size + 1)) { \
fprintf(stderr, "Error: Position %d is out of bounds! (Size: %d)\n", (int)pos, (int)v->size); \
exit(1); \
} \
\
if (v->size == v->capacity) { \
vector_resize_##type(v, (v->capacity * 2)); \
} \
\
v->size++; \
\
for (i = pos; i < v->size; i++) { \
next = vector_at_##type(v, i); \
data[i] = previous; \
previous = next; \
} \
} \
\
void vector_erase_##type(struct Vector_##type* v, size_t pos) { \
type previous = 0; \
type next; \
type* data = vector_data_##type(v); \
size_t i = 0; \
\
if (pos > (v->size)) { \
fprintf(stderr, "Error: Position %d is out of bounds! (Size: %d)\n", (int)pos, (int)v->size); \
exit(1); \
} \
\
for (i = (v->size - 1); i > pos; i--) { \
next = vector_at_##type(v, i); \
data[i] = previous; \
previous = next; \
} \
\
v->size--; \
} \
\
void vector_clear_##type(struct Vector_##type* v) { \
v->size = 0; \
initialiseValues_##type(vector_data_##type(v), v->size, v->capacity); \
} \
\
void vector_swap_##type(struct Vector_##type* a, struct Vector_##type* b) { \
struct Vector_##type temp; \
temp.vectorType = b->vectorType; \
b->vectorType = a->vectorType; \
a->vectorType = temp.vectorType; \
} \
\
void vector_free_##type(struct Vector_##type* v) {\
v->size=0;\
v->capacity=0;\
v->element_size=0;\
if (v) { \
Vector_##type##_inner* temp = (Vector_##type##_inner*)v->vectorType; \
if (temp) { \
if (temp->data) { \
free(temp->data); \
} \
free(temp); \
} \
free(v); \
} \
}\
\
struct Vector_##type* vectorInit_##type() { \
struct Vector_##type* v = malloc(sizeof(struct Vector_##type)); \
Vector_##type##_inner* temp = malloc(sizeof(Vector_##type##_inner)); \
\
if (!v) { \
fprintf(stderr, "Error: v is NULL, failed allocation\n"); \
exit(1); \
} \
if (!temp) { \
fprintf(stderr, "Error: temp is NULL, failed allocation\n"); \
exit(1); \
} \
\
v->size = 0; \
v->capacity = 4; \
temp->data = malloc(v->capacity * sizeof(type)); \
\
if (!temp->data) { \
fprintf(stderr, "Error: temp->data is NULL, failed allocation\n"); \
exit(1); \
} \
\
initialiseValues_##type(temp->data, v->size, v->capacity); \
\
v->element_size = sizeof(type); \
v->vectorType = temp; \
\
v->resize = vector_resize_##type; \
v->empty = vector_empty_##type; \
v->shrink_to_fit = vector_shrink_to_fit_##type; \
v->data = vector_data_##type; \
v->at = vector_at_##type; \
v->front = vector_front_##type; \
v->back = vector_back_##type; \
v->push_back = vector_push_back_##type; \
v->pop_back = vector_pop_back_##type; \
v->insert = vector_insert_##type; \
v->erase = vector_erase_##type; \
v->clear = vector_clear_##type; \
v->swap = vector_swap_##type; \
v->free = vector_free_##type;\
\
return v; \
}
#endif