-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
997 lines (916 loc) · 31.4 KB
/
vector.h
File metadata and controls
997 lines (916 loc) · 31.4 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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*
* vector.h - Dynamic Array Library
* Copyright (C) 2025 Stefan Froberg <stefan.froberg@protonmail.com>
*
* Overview:
* A thread-safe, dynamic array implementation with support for custom allocators,
* serialization, and sorting. Dual-licensed under GPLv3 (open-source) and BSD 3-Clause
* (commercial, contact author).
*
* Key Features:
* - O(1) access, O(1) amortized append, O(n) insert/remove.
* - Thread-safe with read-write locks (Windows SRWLOCK, Linux pthread_rwlock_t).
* - Customizable error handling via vector_set_error_callback.
*
* Usage Example:
* vector* v = vector_create(int, 3, 1, 2, 3); // Creates [1, 2, 3]
* vector_append(v, int, 4); // Appends 4
* int* val = vector_at(int, v, 0); // Accesses first element
* vector_free(v); // Frees vector
*
* Thread Safety:
* Safe for concurrent reads and exclusive writes. Ensure proper locking in custom code.
*
* Notes:
* - Uninitialized memory is allocated; initialize elements before use.
* - Requires C99 or later.
*/
#ifndef __VECTOR_H__
#define __VECTOR_H__
/* Includes */
#include <stddef.h> /* size_t, max_align_t */
#include <stdio.h> /* fprintf, FILE */
#include <stdlib.h> /* malloc, free, realloc, qsort, aligned_alloc */
#include <string.h> /* memcpy, memmove, memset */
#include <stdarg.h> /* va_list, vsnprintf */
#include <stdint.h> /* SIZE_MAX, uint64_t, ssize_t */
#include <stdbool.h>
#include "align.h" /* alignof, alignas */
#if defined(_WIN32)
#include <windows.h> /* SRWLOCK */
#elif defined(__linux__)
#include <pthread.h> /* pthread_rwlock_t */
#endif
/* Enforce C99 or later */
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#error "This library requires C99 or later."
#endif
/* Default alignment */
#define VECTOR_DEFAULT_ALIGNMENT 16
/* Vector struct definition */
typedef struct {
void* data alignas(VECTOR_DEFAULT_ALIGNMENT); /* Pointer to data array */
size_t length; /* Current number of elements */
size_t capacity; /* Total allocated capacity */
size_t element_size; /* Size of each element in bytes */
struct {
void* (*alloc)(size_t); /* Allocator function */
void* (*realloc)(void*, size_t); /* Reallocator function */
void (*free)(void*); /* Deallocator function */
} allocator; /* Custom allocator functions */
#if defined(_WIN32)
SRWLOCK rwlock; /* Windows read-write lock */
#elif defined(__linux__)
pthread_rwlock_t rwlock; /* POSIX read-write lock */
#endif
} vector;
/* Thread-local storage for sorting */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
_Thread_local static vector* _sort_context;
_Thread_local static int (*_sort_compar)(const void*, const void*, void*);
#elif defined(__GNUC__) || defined(__clang__)
__thread static vector* _sort_context;
__thread static int (*_sort_compar)(const void*, const void*, void*);
#else
static vector* _sort_context;
static int (*_sort_compar)(const void*, const void*, void*);
#warning "vector_sort not thread-safe in pre-C11 without GCC/Clang"
#endif
/* Forward declarations */
static void _vector_error(const char* format, ...);
static void* _vector_at(vector* vec, size_t index);
static vector* _vector_create_base(size_t element_size, size_t num_elements);
static int _vector_append_internal(vector* vec, size_t num_values,
const void* values);
static int _vector_insert_internal(vector* vec, size_t index, size_t num_values,
const void* values);
static int _vector_prepend_internal(vector* vec, size_t num_values,
const void* values);
static void* _vector_pop_internal(vector* vec);
static int _vector_remove_internal(vector* vec, size_t index,
size_t num_elements);
static int _vector_reserve_internal(vector* vec, size_t new_capacity);
static int _vector_resize_internal(vector* vec, size_t new_length);
static int _vector_serialize_internal(const vector* vec, FILE* fp);
static vector* _vector_deserialize_internal(FILE* fp, size_t element_size);
static int _vector_shrink_to_fit_internal(vector* vec);
static int _vector_swap_internal(vector* vec, size_t idx1, size_t idx2);
static void vector_rdlock(vector* vec);
static void vector_wrlock(vector* vec);
static void vector_unlock(vector* vec);
static int _safe_add(size_t a, size_t b, size_t* result);
static int _safe_mul(size_t a, size_t b, size_t* result);
static void* default_alloc(size_t size);
static void* default_realloc(void* ptr, size_t size);
static void default_free(void* ptr);
/* Public API Macros and Functions */
/* Macro to append values to the vector */
/* Args: vec - vector pointer, type - element type, ... - values to append */
/* Returns: 0 on success, -1 on failure */
#define vector_append(vec, type, ...) \
({ \
int _ret; \
if (!vec) { \
_vector_error("NULL vector"); \
_ret = -1; \
} else { \
vector_wrlock(vec); \
_ret = _vector_append_internal(vec, ARG_COUNT(__VA_ARGS__), \
(const type[]){__VA_ARGS__}); \
if (_ret == -1) _vector_error("Failed to append to vector"); \
vector_unlock(vec); \
} \
_ret; \
})
/* Macro to access an element at an index */
/* Args: type - element type, vec - vector pointer, index - element index */
/* Returns: pointer to element, NULL if invalid */
#define vector_at(type, vec, index) \
({ \
vector_rdlock(vec); \
type* _ptr = (type*)_vector_at(vec, index); \
if (!_ptr) _vector_error("Invalid vector or index %zu out of bounds " \
"(length: %zu)", index, vec ? vec->length : 0); \
vector_unlock(vec); \
_ptr; \
})
/* Macro to get pointer to element at index */
/* Args: type - element type, vec - vector pointer, index - element index */
/* Returns: pointer to element, NULL if invalid */
#define vector_at_ptr(type, vec, index) ((type*)_vector_at(vec, index))
/* Macro to get vector capacity */
/* Args: vec - vector pointer */
/* Returns: capacity of vector, 0 if NULL */
#define vector_capacity(vec) ((vec) ? (vec)->capacity : 0)
/* Clears vector by setting length to 0 */
/* Args: vec - vector pointer */
/* Returns: 0 on success, -1 if NULL */
static int vector_clear(vector* vec)
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_wrlock(vec);
vec->length = 0;
vector_unlock(vec);
return 0;
}
/* Creates a deep copy of the vector */
/* Args: src - source vector pointer (read-only) */
/* Returns: new vector pointer, NULL on failure */
static vector* vector_copy(const vector* src)
{
if (!src)
{
_vector_error("NULL source vector");
return NULL;
}
vector_rdlock((vector*)src);
vector* dst = _vector_create_base(src->element_size, src->length);
if (dst)
{
memcpy(dst->data, src->data, src->length * src->element_size);
dst->length = src->length;
}
vector_unlock((vector*)src);
return dst;
}
/* Macro to create vector with type and elements */
/* Args: type - element type, ... - num_elements and optional values */
/* Returns: new vector pointer, NULL on failure */
#define vector_create(type, ...) \
_vector_create_dispatch(type, __VA_ARGS__)
#define _vector_create_dispatch(type, num_elements_or_first, ...) \
_vector_create_with_values(sizeof(type), num_elements_or_first, \
ARG_COUNT(__VA_ARGS__), (const type[]){__VA_ARGS__})
/* Macro to iterate over vector elements */
/* Args: type - element type, vec - vector pointer, ptr - iterator variable */
#define vector_foreach(type, vec, ptr) \
do { \
if (vec) { \
vector_rdlock(vec); \
for (type* ptr = (type*)(vec)->data; \
ptr < (type*)((char*)(vec)->data + (vec)->length * (vec)->element_size); \
++ptr) { \
/* User code here */ \
} \
vector_unlock(vec); \
} \
} while (0)
/* Macro to find element in vector */
/* Args: type - element type, vec - vector pointer, value - value to find, */
/* compar - comparison function */
/* Returns: index of element, -1 if not found */
#define vector_find(type, vec, value, compar) \
_vector_find_internal((vec), (const void*)&(type){(value)}, sizeof(type), (compar))
/* Frees vector and its data */
/* Args: vec - vector pointer to free */
static void vector_free(vector* vec)
{
if (vec)
{
vector_wrlock(vec);
if (vec->data)
vec->allocator.free(vec->data);
vector_unlock(vec);
#if defined(_WIN32)
/* SRWLOCK does not require destruction */
#elif defined(__linux__)
pthread_rwlock_destroy(&vec->rwlock);
#endif
free(vec);
}
}
/* Macro to insert values at index */
/* Args: vec - vector pointer, type - element type, index - insertion point, */
/* ... - values to insert */
/* Returns: 0 on success, -1 on failure */
#define vector_insert(vec, type, index, ...) \
({ \
int _ret; \
vector_wrlock(vec); \
_ret = _vector_insert_internal(vec, index, ARG_COUNT(__VA_ARGS__), \
(const type[]){__VA_ARGS__}); \
if (_ret == -1) _vector_error("Failed to insert into vector"); \
vector_unlock(vec); \
_ret; \
})
/* Checks if vector is empty */
/* Args: vec - vector pointer */
/* Returns: true if empty or NULL, false otherwise */
#define vector_is_empty(vec) ((vec) ? (vec)->length == 0 : true)
/* Returns current length of vector */
/* Args: vec - vector pointer */
/* Returns: length of vector, 0 if NULL */
#define vector_length(vec) ((vec) ? (vec)->length : 0)
/* Removes and returns last element */
/* Args: type - element type, vec - vector pointer */
/* Returns: pointer to popped element, NULL on failure */
#define vector_pop(type, vec) ((type*)_vector_pop_internal(vec))
/* Macro to prepend values to vector */
/* Args: vec - vector pointer, type - element type, ... - values to prepend */
/* Returns: 0 on success, -1 on failure */
#define vector_prepend(vec, type, ...) \
({ \
int _ret; \
vector_wrlock(vec); \
_ret = _vector_prepend_internal(vec, ARG_COUNT(__VA_ARGS__), \
(const type[]){__VA_ARGS__}); \
if (_ret == -1) _vector_error("Failed to prepend to vector"); \
vector_unlock(vec); \
_ret; \
})
#define vector_push vector_append /* Alias for append */
/* Removes elements from index */
/* Args: vec - vector pointer, index - start index, num_elements - count */
/* Returns: 0 on success, -1 on failure */
static int vector_remove(vector* vec, size_t index, size_t num_elements)
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_wrlock(vec);
int result = _vector_remove_internal(vec, index, num_elements);
vector_unlock(vec);
return result;
}
/* Reserves capacity for vector */
/* Args: vec - vector pointer, new_capacity - desired capacity */
/* Returns: 0 on success, -1 on failure */
static int vector_reserve(vector* vec, size_t new_capacity)
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_wrlock(vec);
int result = _vector_reserve_internal(vec, new_capacity);
vector_unlock(vec);
return result;
}
/* Resizes vector to new length */
/* Args: vec - vector pointer, new_length - desired length */
/* Returns: 0 on success, -1 on failure */
static int vector_resize(vector* vec, size_t new_length)
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_wrlock(vec);
int result = _vector_resize_internal(vec, new_length);
vector_unlock(vec);
return result;
}
/* Serializes vector to file */
/* Args: vec - vector pointer (read-only), fp - file pointer */
/* Returns: 0 on success, -1 on failure */
static int vector_serialize(const vector* vec, FILE* fp)
{
if (!vec || !fp)
{
_vector_error("NULL vector or file pointer");
return -1;
}
vector_rdlock((vector*)vec);
int result = _vector_serialize_internal(vec, fp);
vector_unlock((vector*)vec);
return result;
}
/* Deserializes vector from file */
/* Args: fp - file pointer, element_size - size of each element */
/* Returns: new vector pointer, NULL on failure */
static vector* vector_deserialize(FILE* fp, size_t element_size)
{
if (!fp)
{
_vector_error("NULL file pointer");
return NULL;
}
return _vector_deserialize_internal(fp, element_size);
}
/* Macro to set value at index */
/* Args: type - element type, vec - vector pointer, index - position, */
/* value - value to set */
#define vector_set(type, vec, index, value) do { \
vector_wrlock(vec); \
type* _ptr = (type*)_vector_at(vec, index); \
if (_ptr) *_ptr = (value); \
vector_unlock(vec); \
} while (0)
/* Shrinks vector capacity to length */
/* Args: vec - vector pointer */
/* Returns: 0 on success, -1 on failure */
static int vector_shrink_to_fit(vector* vec)
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_wrlock(vec);
int result = _vector_shrink_to_fit_internal(vec);
vector_unlock(vec);
return result;
}
/* Macro to sort vector */
/* Args: vec - vector pointer, type - element type, compar - comparison fn */
/* Note: See vector.h header for thread safety and comparison details */
#define vector_sort(vec, type, compar) \
do { \
vector_wrlock(vec); \
_vector_sort_internal(vec, compar); \
vector_unlock(vec); \
} while (0)
/* Swaps two elements in vector */
/* Args: vec - vector pointer, idx1 - first index, idx2 - second index */
/* Returns: 0 on success, -1 on failure */
static int vector_swap(vector* vec, size_t idx1, size_t idx2)
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_wrlock(vec);
int result = _vector_swap_internal(vec, idx1, idx2);
vector_unlock(vec);
return result;
}
/* Comparison macros for sorting */
#define compare_asc _vector_compare_asc
#define compare_desc _vector_compare_desc
#define compare_eq _vector_compare_eq
/* Error callback type */
typedef void (*vector_error_callback)(const char* message);
/* Sets error callback function */
/* Args: callback - function to handle errors */
void vector_set_error_callback(vector_error_callback callback);
/* Internal Macros and Functions */
/* Counts variable arguments */
#define ARG_COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define ARG_COUNT(...) ARG_COUNT_N(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
/* Gets pointer to element at index */
/* Args: vec - vector pointer, index - element index */
/* Returns: pointer to element, NULL if invalid */
static void* _vector_at(vector* vec, size_t index)
{
if (!vec || index >= vec->length)
return NULL;
return (char*)vec->data + index * vec->element_size;
}
/* Appends values to vector */
/* Args: vec - vector pointer, num_values - count, values - data to append */
/* Returns: 0 on success, -1 on failure */
static int _vector_append_internal(vector* vec, size_t num_values,
const void* values)
{
if (!vec)
return -1;
if (num_values == 0)
return 0;
size_t total_elements;
if (_safe_add(vec->length, num_values, &total_elements) == -1)
return -1;
if (total_elements > vec->capacity)
{
size_t new_capacity = vec->capacity == 0 ? num_values :
vec->capacity + vec->capacity / 2;
if (new_capacity < total_elements)
new_capacity = total_elements;
size_t new_size;
if (_safe_mul(new_capacity, vec->element_size, &new_size) == -1)
return -1;
void* new_data = vec->allocator.realloc(vec->data, new_size);
if (!new_data)
return -1;
vec->data = new_data;
vec->capacity = new_capacity;
}
memcpy((char*)vec->data + vec->length * vec->element_size, values,
num_values * vec->element_size);
vec->length = total_elements;
return 0;
}
/* Compares elements ascending */
/* Args: a - first element, b - second element, context - vector pointer */
/* Returns: -1 if a < b, 1 if a > b, 0 if equal */
static int _vector_compare_asc(const void* a, const void* b, void* context)
{
vector* vec = (vector*)context;
const char* ptr_a = (const char*)a;
const char* ptr_b = (const char*)b;
for (size_t i = 0; i < vec->element_size; ++i)
{
if (ptr_a[i] < ptr_b[i])
return -1;
if (ptr_a[i] > ptr_b[i])
return 1;
}
return 0;
}
/* Compares elements descending */
/* Args: a - first element, b - second element, context - vector pointer */
/* Returns: -1 if b < a, 1 if b > a, 0 if equal */
static int _vector_compare_desc(const void* a, const void* b, void* context)
{
vector* vec = (vector*)context;
const char* ptr_a = (const char*)a;
const char* ptr_b = (const char*)b;
for (size_t i = 0; i < vec->element_size; ++i)
{
if (ptr_b[i] < ptr_a[i])
return -1;
if (ptr_b[i] > ptr_a[i])
return 1;
}
return 0;
}
/* Checks element equality */
/* Args: a - first element, b - second element, context - vector pointer */
/* Returns: 0 if equal, 1 if not equal */
static int _vector_compare_eq(const void* a, const void* b, void* context)
{
vector* vec = (vector*)context;
const char* ptr_a = (const char*)a;
const char* ptr_b = (const char*)b;
for (size_t i = 0; i < vec->element_size; ++i)
{
if (ptr_a[i] != ptr_b[i])
return 1;
}
return 0;
}
/* Creates vector with base settings */
/* Args: element_size - size of each element, num_elements - initial count */
/* Returns: new vector pointer, NULL on failure */
static vector* _vector_create_base(size_t element_size, size_t num_elements)
{
size_t alloc_size;
if (_safe_mul(element_size, num_elements, &alloc_size) == -1)
{
_vector_error("Overflow in allocation: element_size %zu * num_elements %zu",
element_size, num_elements);
return NULL;
}
vector* vec = malloc(sizeof(vector));
if (!vec)
{
_vector_error("Failed to allocate vector structure");
return NULL;
}
vec->allocator.alloc = default_alloc;
vec->allocator.realloc = default_realloc;
vec->allocator.free = default_free;
vec->data = alloc_size ? calloc(num_elements, element_size) : NULL;
if (!vec->data && alloc_size > 0)
{
_vector_error("Failed to allocate vector data for %zu bytes", alloc_size);
free(vec);
return NULL;
}
vec->length = num_elements;
vec->capacity = num_elements;
vec->element_size = element_size;
#if defined(_WIN32)
InitializeSRWLock(&vec->rwlock);
#elif defined(__linux__)
if (pthread_rwlock_init(&vec->rwlock, NULL) != 0)
{
vec->allocator.free(vec->data);
free(vec);
_vector_error("Failed to initialize rwlock");
return NULL;
}
#endif
return vec;
}
/* Creates vector with values */
/* Args: element_size - size of each element, num_elements - count, */
/* arg_count - number of values, values - initial values */
/* Returns: new vector pointer, NULL on failure */
static vector* _vector_create_with_values(size_t element_size, size_t num_elements,
size_t arg_count, const void* values)
{
vector* vec = _vector_create_base(element_size, num_elements);
if (!vec)
return NULL;
if (arg_count == 0)
{
/* Already zeroed by calloc */
}
else if (arg_count == 1)
{
for (size_t i = 0; i < num_elements; ++i)
memcpy((char*)vec->data + i * element_size, values, element_size);
}
else if (arg_count <= num_elements)
{
memcpy(vec->data, values, arg_count * element_size);
/* Remaining space already zeroed by calloc */
}
else
{
_vector_error("Argument count %zu exceeds num_elements %zu",
arg_count, num_elements);
vector_free(vec);
return NULL;
}
return vec;
}
/* Finds element in vector */
/* Args: vec - vector pointer, value - value to find, element_size - size, */
/* compar - comparison function */
/* Returns: index of element, -1 if not found */
static ssize_t _vector_find_internal(vector* vec, const void* value,
size_t element_size,
int (*compar)(const void*, const void*, void*))
{
if (!vec)
{
_vector_error("NULL vector");
return -1;
}
vector_rdlock(vec);
for (size_t i = 0; i < vec->length; ++i)
{
void* elem = (char*)vec->data + i * element_size;
if (compar(elem, value, vec) == 0)
{
vector_unlock(vec);
return (ssize_t)i;
}
}
vector_unlock(vec);
return -1;
}
/* Inserts values at index */
/* Args: vec - vector pointer, index - insertion point, num_values - count, */
/* values - data to insert */
/* Returns: 0 on success, -1 on failure */
static int _vector_insert_internal(vector* vec, size_t index, size_t num_values,
const void* values)
{
if (!vec)
return -1;
if (num_values == 0)
return 0;
if (index > vec->length)
return -1;
size_t total_elements;
if (_safe_add(vec->length, num_values, &total_elements) == -1)
return -1;
if (total_elements > vec->capacity)
{
size_t new_capacity = vec->capacity == 0 ? num_values :
vec->capacity + vec->capacity / 2;
if (new_capacity < total_elements)
new_capacity = total_elements;
if (_vector_reserve_internal(vec, new_capacity) == -1)
return -1;
}
if (index < vec->length)
{
memmove((char*)vec->data + (index + num_values) * vec->element_size,
(char*)vec->data + index * vec->element_size,
(vec->length - index) * vec->element_size);
}
memcpy((char*)vec->data + index * vec->element_size, values,
num_values * vec->element_size);
vec->length = total_elements;
return 0;
}
/* Pops last element */
/* Args: vec - vector pointer */
/* Returns: pointer to popped element, NULL on failure */
static void* _vector_pop_internal(vector* vec)
{
if (!vec || vec->length == 0)
{
_vector_error("NULL or empty vector");
return NULL;
}
vector_wrlock(vec);
void* popped_data = vec->allocator.alloc(vec->element_size);
if (!popped_data)
{
vector_unlock(vec);
return NULL;
}
void* last_element = (char*)vec->data + (vec->length - 1) * vec->element_size;
memcpy(popped_data, last_element, vec->element_size);
vec->length--;
vector_unlock(vec);
return popped_data;
}
/* Prepends values to vector */
/* Args: vec - vector pointer, num_values - count, values - data to prepend */
/* Returns: 0 on success, -1 on failure */
static int _vector_prepend_internal(vector* vec, size_t num_values,
const void* values)
{
return _vector_insert_internal(vec, 0, num_values, values);
}
/* Wrapper for qsort */
/* Args: a - first element, b - second element */
/* Returns: comparison result */
static int _vector_qsort_wrapper(const void* a, const void* b)
{
return _sort_compar(a, b, _sort_context);
}
/* Sorts vector with comparison function */
/* Args: vec - vector pointer, compar - comparison function */
static void _vector_sort_internal(vector* vec,
int (*compar)(const void*, const void*, void*))
{
if (!vec || vec->length <= 1)
return;
_sort_context = vec;
_sort_compar = compar;
qsort(vec->data, vec->length, vec->element_size, _vector_qsort_wrapper);
_sort_context = NULL;
_sort_compar = NULL;
}
/* Removes elements from index */
/* Args: vec - vector pointer, index - start index, num_elements - count */
/* Returns: 0 on success, -1 on failure */
static int _vector_remove_internal(vector* vec, size_t index,
size_t num_elements)
{
if (index >= vec->length || index + num_elements > vec->length)
{
_vector_error("Index out of bounds: index %zu, num_elements %zu, length %zu",
index, num_elements, vec->length);
return -1;
}
if (num_elements == 0)
return 0;
size_t elements_to_move = vec->length - index - num_elements;
if (elements_to_move > 0)
{
size_t bytes_to_move = elements_to_move * vec->element_size;
memmove((char*)vec->data + index * vec->element_size,
(char*)vec->data + (index + num_elements) * vec->element_size,
bytes_to_move);
}
vec->length -= num_elements;
return 0;
}
/* Reserves capacity for vector */
/* Args: vec - vector pointer, new_capacity - desired capacity */
/* Returns: 0 on success, -1 on failure */
static int _vector_reserve_internal(vector* vec, size_t new_capacity)
{
if (new_capacity <= vec->capacity)
return 0;
size_t new_size;
if (_safe_mul(new_capacity, vec->element_size, &new_size) == -1)
return -1;
void* new_data = vec->allocator.realloc(vec->data, new_size);
if (!new_data && new_size > 0)
return -1;
vec->data = new_data;
vec->capacity = new_capacity;
return 0;
}
/* Resizes vector to new length */
/* Args: vec - vector pointer, new_length - desired length */
/* Returns: 0 on success, -1 on failure */
static int _vector_resize_internal(vector* vec, size_t new_length)
{
if (new_length > vec->capacity)
{
size_t new_capacity = vec->capacity ? vec->capacity * 2 : new_length;
if (new_capacity < new_length)
new_capacity = new_length;
if (_vector_reserve_internal(vec, new_capacity) == -1)
return -1;
}
if (new_length > vec->length)
{
memset((char*)vec->data + vec->length * vec->element_size, 0,
(new_length - vec->length) * vec->element_size);
}
vec->length = new_length;
return 0;
}
/* Serializes vector to file */
/* Args: vec - vector pointer (read-only), fp - file pointer */
/* Returns: 0 on success, -1 on failure */
static int _vector_serialize_internal(const vector* vec, FILE* fp)
{
if (fwrite(&vec->length, sizeof(size_t), 1, fp) != 1 ||
fwrite(&vec->element_size, sizeof(size_t), 1, fp) != 1 ||
fwrite(vec->data, vec->element_size, vec->length, fp) != vec->length)
return -1;
return 0;
}
/* Deserializes vector from file */
/* Args: fp - file pointer, element_size - size of each element */
/* Returns: new vector pointer, NULL on failure */
static vector* _vector_deserialize_internal(FILE* fp, size_t element_size)
{
size_t length, read_element_size;
if (fread(&length, sizeof(size_t), 1, fp) != 1 ||
fread(&read_element_size, sizeof(size_t), 1, fp) != 1 ||
read_element_size != element_size)
return NULL;
vector* vec = _vector_create_base(element_size, length);
if (!vec || fread(vec->data, element_size, length, fp) != length)
{
vector_free(vec);
return NULL;
}
return vec;
}
/* Shrinks capacity to fit length */
/* Args: vec - vector pointer */
/* Returns: 0 on success, -1 on failure */
static int _vector_shrink_to_fit_internal(vector* vec)
{
if (vec->capacity == vec->length)
return 0;
size_t new_size;
if (_safe_mul(vec->length, vec->element_size, &new_size) == -1)
return -1;
void* new_data = vec->length ? vec->allocator.realloc(vec->data, new_size) : NULL;
if (vec->length && !new_data)
return -1;
if (!vec->length && vec->data)
vec->allocator.free(vec->data);
vec->data = new_data;
vec->capacity = vec->length;
return 0;
}
/* Swaps two elements in vector */
/* Args: vec - vector pointer, idx1 - first index, idx2 - second index */
/* Returns: 0 on success, -1 on failure */
static int _vector_swap_internal(vector* vec, size_t idx1, size_t idx2)
{
if (idx1 >= vec->length || idx2 >= vec->length)
return -1;
if (idx1 == idx2)
return 0;
char temp[vec->element_size];
void* ptr1 = (char*)vec->data + idx1 * vec->element_size;
void* ptr2 = (char*)vec->data + idx2 * vec->element_size;
memcpy(temp, ptr1, vec->element_size);
memcpy(ptr1, ptr2, vec->element_size);
memcpy(ptr2, temp, vec->element_size);
return 0;
}
/* Default allocator: allocates aligned memory */
/* Args: size - bytes to allocate */
/* Returns: pointer to allocated memory, NULL on failure */
static void* default_alloc(size_t size)
{
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
return aligned_alloc(VECTOR_DEFAULT_ALIGNMENT, size); /* C11 */
#else
return malloc(size); /* C99 */
#endif
}
/* Default realloc: resizes memory */
/* Args: ptr - pointer to resize, size - new size */
/* Returns: pointer to reallocated memory, NULL on failure */
static void* default_realloc(void* ptr, size_t size)
{
return realloc(ptr, size);
}
/* Default free: deallocates memory */
/* Args: ptr - pointer to free */
static void default_free(void* ptr)
{
free(ptr);
}
/* Default error callback: prints to stderr */
/* Args: message - error message to print */
static void default_error_callback(const char* message)
{
fprintf(stderr, "%s\n", message);
}
/* Static error callback variable */
static vector_error_callback error_callback = default_error_callback;
/* Sets error callback function */
/* Args: callback - function to handle errors */
void vector_set_error_callback(vector_error_callback callback)
{
error_callback = callback ? callback : default_error_callback;
}
/* Formats and dispatches error messages */
/* Args: format - format string, ... - variable arguments */
static void _vector_error(const char* format, ...)
{
if (error_callback)
{
char msg[256];
va_list args;
va_start(args, format);
vsnprintf(msg, sizeof(msg), format, args);
va_end(args);
error_callback(msg);
}
}
/* Locks vector for reading */
/* Args: vec - vector pointer */
static void vector_rdlock(vector* vec)
{
if (!vec)
return;
#if defined(_WIN32)
AcquireSRWLockShared(&vec->rwlock);
#elif defined(__linux__)
pthread_rwlock_rdlock(&vec->rwlock);
#endif
}
/* Locks vector for writing */
/* Args: vec - vector pointer */
static void vector_wrlock(vector* vec)
{
if (!vec)
return;
#if defined(_WIN32)
AcquireSRWLockExclusive(&vec->rwlock);
#elif defined(__linux__)
pthread_rwlock_wrlock(&vec->rwlock);
#endif
}
/* Unlocks vector */
/* Args: vec - vector pointer */
static void vector_unlock(vector* vec)
{
if (!vec)
return;
#if defined(_WIN32)
ReleaseSRWLockExclusive(&vec->rwlock); /* Assumes write lock; adjust if needed */
#elif defined(__linux__)
pthread_rwlock_unlock(&vec->rwlock);
#endif
}
/* Safe addition */
/* Args: a - first number, b - second number, result - sum */
/* Returns: 0 on success, -1 on overflow */
static int _safe_add(size_t a, size_t b, size_t* result)
{
if (b > SIZE_MAX - a)
return -1;
*result = a + b;
return 0;
}
/* Safe multiplication */
/* Args: a - first number, b - second number, result - product */
/* Returns: 0 on success, -1 on overflow */
static int _safe_mul(size_t a, size_t b, size_t* result)
{
if (a && b > SIZE_MAX / a)
return -1;
*result = a * b;
return 0;
}
#endif /* __VECTOR_H__ */