Repository navigation
Expand file tree
/
Copy pathvector.h
More file actions
509 lines (457 loc) · 16.9 KB
/
Copy pathvector.h
File metadata and controls
509 lines (457 loc) · 16.9 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// SPDX-License-Identifier: MIT
// Copyright (c) 2021 Michael Bäck <mhcoma@gmail.com>
#ifndef __CCTL_VECTOR_H__
#define __CCTL_VECTOR_H__
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "cctl.h"
// Vector Iterator
/// `vector_iterator(T)` ⇒ `T_vector_iterator`
///
/// Generates the iterator type name for `vector(T)`.
#define vector_iterator(T) cctl_join(T, vector_iterator)
/// `vector_iterator_func(FUNC, T)` ⇒ `T_vector_iterator_FUNC`
///
/// Generates a mangled function identifier for `vector_iterator(T)`.
#define vector_iterator_func(FUNC, T) cctl_join(vector_iterator(T), FUNC)
/// `vector_iterator_struct(T)` ⇒ `struct T_vector_iterator_struct`
///
/// Generates the underlying structure tag for `vector_iterator(T)`.
#define vector_iterator_struct(T) cctl_join(vector_iterator(T), struct)
/// ```c
/// T* vector_iterator_get(T, vector_iterator(T)* p_it)
/// ```
/// Returns a pointer to the element currently referenced by the iterator.
///
/// `p_it` is a pointer to the iterator instance.
#define vector_iterator_get(T, p_it) ((p_it)->p_v->p_data + (p_it)->index)
/// ```c
/// bool vector_iterator_is_valid(T, vector_iterator(T)* p_it)
/// ```
/// Checks whether the iterator currently points to a valid element.
///
/// Returns `true` if the iterator is valid and within bounds, `false` otherwise.
#define vector_iterator_is_valid(T, p_it) ((p_it)->p_v && (p_it)->index < (p_it)->p_v->size)
/// ```c
/// bool vector_iterator_is_equal(T, vector_iterator(T)* p_it1, vector_iterator(T)* p_it2)
/// ```
/// Checks whether two iterators point to the same vector and index.
#define vector_iterator_is_equal(T, p_it1, p_it2) ((p_it1)->p_v == (p_it2)->p_v && (p_it1)->index == (p_it2)->index)
/// ```c
/// void vector_iterator_next(T, vector_iterator(T)* p_it)
/// ```
/// Advances the iterator forward to the next element.
#define vector_iterator_next(T, p_it) ((p_it)->index++)
/// ```c
/// void vector_iterator_prev(T, vector_iterator(T)* p_it)
/// ```
/// Moves the iterator backward to the previous element.
#define vector_iterator_prev(T, p_it) ((p_it)->index--)
/// ```c
/// bool vector_iterator_insert_after(T, vector_iterator(T)* p_it, T item)
/// ```
/// Inserts `item` immediately after the current iterator position.
///
/// Returns `true` on successful insertion, or `false` on allocation failure or invalid iterator.
#define vector_iterator_insert_after(T, p_it, item) vector_iterator_func(insert_after, T)(p_it, item)
/// ```c
/// bool vector_iterator_insert_before(T, vector_iterator(T)* p_it, T item)
/// ```
/// Inserts `item` immediately before the current iterator position.
///
/// Returns `true` on successful insertion, or `false` on allocation failure or invalid iterator.
#define vector_iterator_insert_before(T, p_it, item) vector_iterator_func(insert_before, T)(p_it, item)
/// ```c
/// bool vector_iterator_remove(T, vector_iterator(T)* p_it)
/// ```
/// Removes the element at the current iterator position.
///
/// Returns `true` on successful removal, or `false` if the iterator is invalid.
#define vector_iterator_remove(T, p_it) vector_iterator_func(remove, T)(p_it)
/// Declares iterator struct and function prototypes in a header file.
#define vector_iterator_imp_h(T) \
typedef struct vector_iterator_struct(T) vector_iterator(T); \
struct vector_iterator_struct(T) { \
vector(T)* p_v; \
size_t index; \
}; \
\
bool vector_iterator_func(insert_after, T)(vector_iterator(T)* p_it, T item); \
bool vector_iterator_func(insert_before, T)(vector_iterator(T)* p_it, T item); \
bool vector_iterator_func(remove, T)(vector_iterator(T)* p_it);
/// Implements iterator functions in a source file.
#define vector_iterator_imp_c(T) \
bool vector_iterator_func(insert_after, T)(vector_iterator(T)* p_it, T item) { \
if (!p_it || !vector_iterator_is_valid(T, p_it)) return false; \
if (!vector_func(resize, T)(p_it->p_v, p_it->p_v->size + 1)) return false; \
if (p_it->index + 1 < p_it->p_v->size - 1) { \
memmove( \
p_it->p_v->p_data + p_it->index + 2, \
p_it->p_v->p_data + p_it->index + 1, \
(p_it->p_v->size - 2 - p_it->index) * sizeof(T) \
); \
} \
*(p_it->p_v->p_data + p_it->index + 1) = item; \
return true; \
} \
\
bool vector_iterator_func(insert_before, T)(vector_iterator(T)* p_it, T item) { \
if (!p_it || !vector_iterator_is_valid(T, p_it)) return false; \
if (!vector_func(resize, T)(p_it->p_v, p_it->p_v->size + 1)) return false; \
memmove( \
p_it->p_v->p_data + p_it->index + 1, \
p_it->p_v->p_data + p_it->index, \
(p_it->p_v->size - 1 - p_it->index) * sizeof(T) \
); \
*(p_it->p_v->p_data + p_it->index) = item; \
p_it->index++; \
return true; \
} \
\
bool vector_iterator_func(remove, T)(vector_iterator(T)* p_it) { \
if (!p_it || !vector_iterator_is_valid(T, p_it)) return false; \
if (p_it->index < p_it->p_v->size - 1) { \
memmove( \
p_it->p_v->p_data + p_it->index, \
p_it->p_v->p_data + p_it->index + 1, \
(p_it->p_v->size - 1 - p_it->index) * sizeof(T) \
); \
} \
p_it->p_v->size--; \
return true; \
}
// Vector
/// `vector(T)` ⇒ `T_vector`
///
/// Generates the vector container type name for `T`.
#define vector(T) cctl_join(T, vector)
/// `vector_func(FUNC, T)` ⇒ `T_vector_FUNC`
///
/// Generates a mangled function identifier for `vector(T)`.
#define vector_func(FUNC, T) cctl_join(vector(T), FUNC)
/// `vector_struct(T)` ⇒ `struct T_vector_struct`
///
/// Generates the underlying structure tag for `vector(T)`.
#define vector_struct(T) cctl_join(vector(T), struct)
/// ```c
/// void vector_init(T, vector(T)* p_v)
/// ```
/// Initializes the vector to an empty state.
///
/// `p_v` is a pointer to the vector instance. Must be called before any other operations.
#define vector_init(T, p_v) vector_func(init, T)(p_v)
#define vector_new_1(T) vector_func(new, T)(0)
#define vector_new_2(T, size) vector_func(new, T)(size)
/// ```c
/// vector(T) vector_new(T, [size_t size])
/// ```
/// Constructs and returns an initialized vector, optionally pre-allocated and zero-initialized with `size` elements.
#define vector_new(...) cctl_dispatch(vector_new, __VA_ARGS__)(__VA_ARGS__)
/// ```c
/// vector(T) vector_from_items(T, ...)
/// ```
/// Constructs and returns an initialized vector containing all items provided in `...`.
#define vector_from_items(T, ...) \
vector_func(from_items, T)((const T[]) { __VA_ARGS__ }, sizeof((const T[]) { __VA_ARGS__ }) / sizeof(T))
/// ```c
/// bool vector_extend_items(T, vector(T)* p_v, ...)
/// ```
/// Appends multiple items provided in `...` to the end of the vector.
#define vector_extend_items(T, p_v, ...) \
vector_func(extend_items, T)(p_v, (const T[]) { __VA_ARGS__ }, sizeof((const T[]) { __VA_ARGS__ }) / sizeof(T))
/// ```c
/// void vector_free(T, vector(T)* p_v)
/// ```
/// Frees all dynamically allocated memory of the vector and reinitializes it.
#define vector_free(T, p_v) vector_func(free, T)(p_v)
/// ```c
/// void vector_clear(T, vector(T)* p_v)
/// ```
/// Clears all elements from the vector without deallocating internal buffer memory.
#define vector_clear(T, p_v) vector_func(clear, T)(p_v)
/// ```c
/// bool vector_reserve(T, vector(T)* p_v, size_t capacity)
/// ```
/// Ensures that the vector has enough capacity to hold at least `capacity` elements.
///
/// Returns `true` on success, or `false` on allocation failure.
#define vector_reserve(T, p_v, capacity) vector_func(reserve, T)(p_v, capacity)
/// ```c
/// bool vector_resize(T, vector(T)* p_v, size_t size)
/// ```
/// Resizes the vector to contain `size` elements.
///
/// Newly allocated memory is zero-initialized. Returns `true` on success, `false` on allocation failure.
#define vector_resize(T, p_v, size) vector_func(resize, T)(p_v, size)
/// ```c
/// bool vector_push_back(T, vector(T)* p_v, T item)
/// ```
/// Appends `item` to the end of the vector.
///
/// Automatically grows capacity if needed. Returns `true` on success, `false` on allocation failure.
#define vector_push_back(T, p_v, item) vector_func(push_back, T)(p_v, item)
/// ```c
/// bool vector_pop_back(T, vector(T)* p_v)
/// ```
/// Removes the last element from the vector.
///
/// Returns `true` on success, or `false` if the vector is already empty.
#define vector_pop_back(T, p_v) vector_func(pop_back, T)(p_v)
/// ```c
/// bool vector_swap_remove(T, vector(T)* p_v, size_t index)
/// ```
/// Removes the element at `index` in O(1) time by swapping it with the last element and popping back.
///
/// Returns `true` on success, or `false` if `index` is out of bounds.
#define vector_swap_remove(T, p_v, index) vector_func(swap_remove, T)(p_v, index)
/// ```c
/// bool vector_shrink_to_fit(T, vector(T)* p_v)
/// ```
/// Reduces vector memory capacity to fit its current size.
///
/// Returns `true` on success, or `false` on reallocation failure.
#define vector_shrink_to_fit(T, p_v) vector_func(shrink_to_fit, T)(p_v)
/// ```c
/// T* vector_at(T, vector(T)* p_v, size_t index)
/// ```
/// Returns a pointer to the element at the specified `index`, or `NULL` if out of bounds.
#define vector_at(T, p_v, index) \
(((index) < (p_v)->size) ? ((p_v)->p_data + (index)) : NULL)
/// ```c
/// T* vector_front(T, vector(T)* p_v)
/// ```
/// Returns a pointer to the first element in the vector, or `NULL` if empty.
#define vector_front(T, p_v) \
(((p_v)->size > 0) ? (p_v)->p_data : NULL)
/// ```c
/// T* vector_back(T, vector(T)* p_v)
/// ```
/// Returns a pointer to the last element in the vector, or `NULL` if empty.
#define vector_back(T, p_v) \
(((p_v)->size > 0) ? ((p_v)->p_data + (p_v)->size - 1) : NULL)
/// ```c
/// bool vector_is_empty(T, vector(T)* p_v)
/// ```
/// Checks whether the vector contains no elements.
#define vector_is_empty(T, p_v) ((p_v)->size == 0)
/// ```c
/// size_t vector_size(T, vector(T)* p_v)
/// ```
/// Returns the current number of elements in the vector as a read-only rvalue.
#define vector_size(T, p_v) ((size_t)((p_v)->size))
/// ```c
/// size_t vector_capacity(T, vector(T)* p_v)
/// ```
/// Returns the allocated capacity of the vector as a read-only rvalue.
#define vector_capacity(T, p_v) ((size_t)((p_v)->capacity))
/// ```c
/// vector_iterator(T) vector_begin(T, vector(T)* p_v)
/// ```
/// Returns an iterator pointing to the first element in the vector.
#define vector_begin(T, p_v) vector_func(begin, T)(p_v)
/// ```c
/// vector_iterator(T) vector_end(T, vector(T)* p_v)
/// ```
/// Returns an iterator pointing past the last element in the vector.
#define vector_end(T, p_v) vector_func(end, T)(p_v)
/// ```c
/// vector_iterator(T) vector_rbegin(T, vector(T)* p_v)
/// ```
/// Returns a reverse iterator pointing to the last element in the vector.
#define vector_rbegin(T, p_v) vector_func(rbegin, T)(p_v)
/// ```c
/// vector_iterator(T) vector_rend(T, vector(T)* p_v)
/// ```
/// Returns a reverse iterator pointing before the first element in the vector.
#define vector_rend(T, p_v) vector_func(rend, T)(p_v)
/// ```c
/// vector_iterator(T) vector_seek(T, vector(T)* p_v, size_t index)
/// ```
/// Returns an iterator pointing to the element at `index`.
#define vector_seek(T, p_v, index) vector_func(seek, T)(p_v, index)
/// ```c
/// vector_foreach(T, vector(T)* p_v, IT_NAME)
/// ```
/// Iterates over all elements in the vector in forward order.
#define vector_foreach(T, p_v, it) for (vector_iterator(T) it = vector_begin(T, p_v); vector_iterator_is_valid(T, &it); vector_iterator_next(T, &it))
/// ```c
/// vector_rforeach(T, vector(T)* p_v, IT_NAME)
/// ```
/// Iterates over all elements in the vector in reverse order.
#define vector_rforeach(T, p_v, it) for (vector_iterator(T) it = vector_rbegin(T, p_v); vector_iterator_is_valid(T, &it); vector_iterator_prev(T, &it))
/// Forward declares the `vector(T)` container type for prior references.
#define vector_fd(T) \
typedef struct vector_struct(T) vector(T);
/// Declares vector struct and function prototypes in a header file.
#define vector_imp_h(T) \
vector_iterator_imp_h(T); \
struct vector_struct(T) { \
T* p_data; \
size_t size; \
size_t capacity; \
}; \
void vector_func(init, T)(vector(T)* p_v); \
vector(T) vector_func(new, T)(size_t size); \
vector(T) vector_func(from_items, T)(const T* p_items, size_t count); \
bool vector_func(extend_items, T)(vector(T)* p_v, const T* p_items, size_t count); \
void vector_func(free, T)(vector(T)* p_v); \
void vector_func(clear, T)(vector(T)* p_v); \
bool vector_func(reserve, T)(vector(T)* p_v, size_t capacity); \
bool vector_func(resize, T)(vector(T)* p_v, size_t size); \
bool vector_func(push_back, T)(vector(T)* p_v, T item); \
bool vector_func(pop_back, T)(vector(T)* p_v); \
bool vector_func(swap_remove, T)(vector(T)* p_v, size_t index); \
bool vector_func(shrink_to_fit, T)(vector(T)* p_v); \
vector_iterator(T) vector_func(begin, T)(vector(T)* p_v); \
vector_iterator(T) vector_func(end, T)(vector(T)* p_v); \
vector_iterator(T) vector_func(rbegin, T)(vector(T)* p_v); \
vector_iterator(T) vector_func(rend, T)(vector(T)* p_v); \
vector_iterator(T) vector_func(seek, T)(vector(T)* p_v, size_t index);
/// Implements vector functions in a source file.
#define vector_imp_c(T) \
vector_iterator_imp_c(T); \
\
void vector_func(init, T)(vector(T)* p_v) { \
p_v->p_data = NULL; \
p_v->size = 0; \
p_v->capacity = 0; \
return; \
} \
\
vector(T) vector_func(new, T)(size_t size) { \
vector(T) v; \
vector_func(init, T)(&v); \
if (size > 0) { \
if (!vector_func(resize, T)(&v, size)) { \
vector_func(free, T)(&v); \
} \
} \
return v; \
} \
\
vector(T) vector_func(from_items, T)(const T* p_items, size_t count) { \
vector(T) v; \
vector_func(init, T)(&v); \
if (p_items && count > 0) { \
if (!vector_func(extend_items, T)(&v, p_items, count)) { \
vector_func(free, T)(&v); \
} \
} \
return v; \
} \
\
bool vector_func(extend_items, T)(vector(T)* p_v, const T* p_items, size_t count) { \
if (!p_v || !p_items || count == 0) return true; \
if (!vector_func(reserve, T)(p_v, p_v->size + count)) return false; \
for (size_t i = 0; i < count; i++) { \
*(p_v->p_data + p_v->size + i) = p_items[i]; \
} \
p_v->size += count; \
return true; \
} \
\
void vector_func(free, T)(vector(T)* p_v) { \
if (p_v) { \
vector_func(clear, T)(p_v); \
free(p_v->p_data); \
vector_func(init, T)(p_v); \
return; \
} \
} \
\
void vector_func(clear, T)(vector(T)* p_v) { \
memset(p_v->p_data, 0, p_v->size * sizeof(T)); \
p_v->size = 0; \
return; \
} \
\
bool vector_func(reserve, T)(vector(T)* p_v, size_t capacity) { \
if (p_v->capacity >= capacity) return true; \
size_t new_capacity = 1; \
if (capacity > 0) while (new_capacity <= capacity) new_capacity <<= 1; \
if (p_v->capacity < capacity) { \
if (p_v->capacity) { \
if (!(p_v->p_data = (T*) realloc(p_v->p_data, new_capacity * sizeof(T)))) return false; \
memset(p_v->p_data + p_v->size, 0, (new_capacity - p_v->size) * sizeof(T)); \
} \
else { \
if (!(p_v->p_data = (T*) calloc(new_capacity, sizeof(T)))) return false; \
} \
p_v->capacity = new_capacity; \
} \
return true; \
} \
\
bool vector_func(resize, T)(vector(T)* p_v, size_t size) { \
if (vector_func(reserve, T)(p_v, size)) { \
p_v->size = size; \
return true; \
} \
return false; \
} \
\
bool vector_func(push_back, T)(vector(T)* p_v, T item) { \
if (vector_func(resize, T)(p_v, p_v->size + 1)) { \
*(p_v->p_data + p_v->size - 1) = item; \
return true; \
} \
return false; \
} \
\
bool vector_func(pop_back, T)(vector(T)* p_v) { \
if (p_v->size == 0) return false; \
memset(p_v->p_data + p_v->size - 1, 0, sizeof(T)); \
p_v->size--; \
return true; \
} \
\
bool vector_func(swap_remove, T)(vector(T)* p_v, size_t index) { \
if (!p_v || index >= p_v->size) return false; \
if (index != p_v->size - 1) { \
*(p_v->p_data + index) = *(p_v->p_data + p_v->size - 1); \
} \
return vector_func(pop_back, T)(p_v); \
} \
\
bool vector_func(shrink_to_fit, T)(vector(T)* p_v) { \
if (!p_v) return false; \
if (p_v->size == p_v->capacity) return true; \
if (p_v->size == 0) { \
free(p_v->p_data); \
p_v->p_data = NULL; \
p_v->capacity = 0; \
return true; \
} \
T* p_new_data = (T*) realloc(p_v->p_data, p_v->size * sizeof(T)); \
if (!p_new_data) return false; \
p_v->p_data = p_new_data; \
p_v->capacity = p_v->size; \
return true; \
} \
\
vector_iterator(T) vector_func(begin, T)(vector(T)* p_v) { \
vector_iterator(T) it = { p_v, 0 }; \
return it; \
} \
\
vector_iterator(T) vector_func(end, T)(vector(T)* p_v) { \
vector_iterator(T) it = { p_v, p_v ? p_v->size : 0 }; \
return it; \
} \
\
vector_iterator(T) vector_func(rbegin, T)(vector(T)* p_v) { \
vector_iterator(T) it = { p_v, (p_v && p_v->size > 0) ? p_v->size - 1 : (size_t)-1 }; \
return it; \
} \
\
vector_iterator(T) vector_func(rend, T)(vector(T)* p_v) { \
vector_iterator(T) it = { p_v, (size_t)-1 }; \
return it; \
} \
\
vector_iterator(T) vector_func(seek, T)(vector(T)* p_v, size_t index) { \
vector_iterator(T) it = { p_v, (p_v && index < p_v->size) ? index : (size_t)-1 }; \
return it; \
}
#endif