forked from skogorev/vc_vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvc_vector.c
329 lines (252 loc) · 8.02 KB
/
vc_vector.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
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
#include "vc_vector.h"
#include <stdlib.h>
#include <string.h>
#define GROWTH_FACTOR 1.5
#define DEFAULT_COUNT_OF_ELEMENTS 8
#define MINIMUM_COUNT_OF_ELEMENTS 2
// ----------------------------------------------------------------------------
// vc_vector structure
struct vc_vector {
size_t count;
size_t element_size;
size_t reserved_size;
char* data;
vc_vector_deleter* deleter;
};
// ----------------------------------------------------------------------------
// Auxiliary methods
bool vc_vector_realloc(vc_vector* vector, size_t new_count) {
const size_t new_size = new_count * vector->element_size;
char* new_data = (char*)realloc(vector->data, new_size);
if (!new_data) {
return false;
}
vector->reserved_size = new_size;
vector->data = new_data;
return true;
}
// [first_index, last_index)
void vc_vector_call_deleter(vc_vector* vector, size_t first_index, size_t last_index) {
for (size_t i = first_index; i < last_index; ++i) {
vector->deleter(vc_vector_at(vector, i));
}
}
void vc_vector_call_deleter_all(vc_vector* vector) {
vc_vector_call_deleter(vector, 0, vc_vector_count(vector));
}
// ----------------------------------------------------------------------------
// Control
vc_vector* vc_vector_create(size_t count_elements, size_t size_of_element, vc_vector_deleter* deleter) {
vc_vector* v = (vc_vector*)malloc(sizeof(vc_vector));
if (v != NULL) {
v->data = NULL;
v->count = 0;
v->element_size = size_of_element;
v->deleter = deleter;
if (count_elements < MINIMUM_COUNT_OF_ELEMENTS) {
count_elements = DEFAULT_COUNT_OF_ELEMENTS;
}
if (size_of_element < 1 ||
!vc_vector_realloc(v, count_elements)) {
free(v);
v = NULL;
}
}
return v;
}
vc_vector* vc_vector_create_copy(const vc_vector* vector) {
vc_vector* new_vector = vc_vector_create(vector->reserved_size / vector->count,
vector->element_size,
vector->deleter);
if (!new_vector) {
return new_vector;
}
if (memcpy(vector->data,
new_vector->data,
new_vector->element_size * vector->count) == NULL) {
vc_vector_release(new_vector);
new_vector = NULL;
return new_vector;
}
new_vector->count = vector->count;
return new_vector;
}
void vc_vector_release(vc_vector* vector) {
if (vector->deleter != NULL) {
vc_vector_call_deleter_all(vector);
}
if (vector->reserved_size != 0) {
free(vector->data);
}
free(vector);
}
bool vc_vector_is_equals(vc_vector* vector1, vc_vector* vector2) {
const size_t size_vector1 = vc_vector_size(vector1);
if (size_vector1 != vc_vector_size(vector2)) {
return false;
}
return memcmp(vector1->data, vector2->data, size_vector1) == 0;
}
float vc_vector_get_growth_factor() {
return GROWTH_FACTOR;
}
size_t vc_vector_get_default_count_of_elements() {
return DEFAULT_COUNT_OF_ELEMENTS;
}
size_t vc_vector_struct_size() {
return sizeof(vc_vector);
}
// ----------------------------------------------------------------------------
// Element access
void* vc_vector_at(vc_vector* vector, size_t index) {
return vector->data + index * vector->element_size;
}
void* vc_vector_front(vc_vector* vector) {
return vector->data;
}
void* vc_vector_back(vc_vector* vector) {
return vector->data + (vector->count - 1) * vector->element_size;
}
void* vc_vector_data(vc_vector* vector) {
return vector->data;
}
// ----------------------------------------------------------------------------
// Iterators
void* vc_vector_begin(vc_vector* vector) {
return vector->data;
}
void* vc_vector_end(vc_vector* vector) {
return vector->data + vector->element_size * vector->count;
}
void* vc_vector_next(vc_vector* vector, void* i) {
return (char *)i + vector->element_size;
}
// ----------------------------------------------------------------------------
// Capacity
bool vc_vector_empty(vc_vector* vector) {
return vector->count == 0;
}
size_t vc_vector_count(const vc_vector* vector) {
return vector->count;
}
size_t vc_vector_size(const vc_vector* vector) {
return vector->count * vector->element_size;
}
size_t vc_vector_max_count(const vc_vector* vector) {
return vector->reserved_size / vector->element_size;
}
size_t vc_vector_max_size(const vc_vector* vector) {
return vector->reserved_size;
}
bool vc_vector_reserve_count(vc_vector* vector, size_t new_count) {
if (new_count < vector->count) {
return false;
}
size_t new_size = vector->element_size * new_count;
if (new_size == vector->reserved_size) {
return true;
}
return vc_vector_realloc(vector, new_count);
}
bool vc_vector_reserve_size(vc_vector* vector, size_t new_size) {
return vc_vector_reserve_count(vector, new_size / vector->element_size);
}
// ----------------------------------------------------------------------------
// Modifiers
void vc_vector_clear(vc_vector* vector) {
if (vector->deleter != NULL) {
vc_vector_call_deleter_all(vector);
}
vector->count = 0;
}
bool vc_vector_insert(vc_vector* vector, size_t index, const void* value) {
if (vc_vector_max_count(vector) < vector->count + 1) {
if (!vc_vector_realloc(vector, vc_vector_max_count(vector) * GROWTH_FACTOR)) {
return false;
}
}
if (!memmove(vc_vector_at(vector, index + 1),
vc_vector_at(vector, index),
vector->element_size * (vector->count - index))) {
return false;
}
if (memcpy(vc_vector_at(vector, index),
value,
vector->element_size) == NULL) {
return false;
}
++vector->count;
return true;
}
bool vc_vector_erase(vc_vector* vector, size_t index) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_at(vector, index));
}
if (!memmove(vc_vector_at(vector, index),
vc_vector_at(vector, index + 1),
vector->element_size * (vector->count - index))) {
return false;
}
vector->count--;
return true;
}
bool vc_vector_erase_range(vc_vector* vector, size_t first_index, size_t last_index) {
if (vector->deleter != NULL) {
vc_vector_call_deleter(vector, first_index, last_index);
}
if (!memmove(vc_vector_at(vector, first_index),
vc_vector_at(vector, last_index),
vector->element_size * (vector->count - last_index))) {
return false;
}
vector->count -= last_index - first_index;
return true;
}
bool vc_vector_append(vc_vector* vector, const void* values, size_t count) {
const size_t count_new = count + vc_vector_count(vector);
if (vc_vector_max_count(vector) < count_new) {
size_t max_count_to_reserved = vc_vector_max_count(vector) * GROWTH_FACTOR;
while (count_new > max_count_to_reserved) {
max_count_to_reserved *= GROWTH_FACTOR;
}
if (!vc_vector_realloc(vector, max_count_to_reserved)) {
return false;
}
}
if (memcpy(vector->data + vector->count * vector->element_size,
values,
vector->element_size * count) == NULL) {
return false;
}
vector->count = count_new;
return true;
}
bool vc_vector_push_back(vc_vector* vector, const void* value) {
if (!vc_vector_append(vector, value, 1)) {
return false;
}
return true;
}
bool vc_vector_pop_back(vc_vector* vector) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_back(vector));
}
vector->count--;
return true;
}
bool vc_vector_replace(vc_vector* vector, size_t index, const void* value) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_at(vector, index));
}
return memcpy(vc_vector_at(vector, index),
value,
vector->element_size) != NULL;
}
bool vc_vector_replace_multiple(vc_vector* vector, size_t index, const void* values, size_t count) {
if (vector->deleter != NULL) {
vc_vector_call_deleter(vector, index, index + count);
}
return memcpy(vc_vector_at(vector, index),
values,
vector->element_size * count) != NULL;
}