-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cvector.h
3210 lines (2857 loc) · 149 KB
/
cvector.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
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
997
998
999
1000
/*
CVector 4.2.0 MIT Licensed vector (dynamic array) library in strict C89
http://www.robertwinkler.com/projects/cvector.html
http://www.robertwinkler.com/projects/cvector/
Besides the docs and all the Doxygen comments, see cvector_tests.c for
examples of how to use it or look at any of these other projects for
more practical examples:
https://github.com/rswinkle/C_Interpreter
https://github.com/rswinkle/CPIM2
https://github.com/rswinkle/spelling_game
https://github.com/rswinkle/c_bigint
http://portablegl.com/
The MIT License (MIT)
Copyright (c) 2011-2024 Robert Winkler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/* header starts */
#ifndef CVECTOR_H
#define CVECTOR_H
#if defined(CVEC_ONLY_INT) || defined(CVEC_ONLY_DOUBLE) || defined(CVEC_ONLY_STR) || defined(CVEC_ONLY_VOID)
#ifndef CVEC_ONLY_INT
#define CVEC_NO_INT
#endif
#ifndef CVEC_ONLY_DOUBLE
#define CVEC_NO_DOUBLE
#endif
#ifndef CVEC_ONLY_STR
#define CVEC_NO_STR
#endif
#ifndef CVEC_ONLY_VOID
#define CVEC_NO_VOID
#endif
#endif
#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
/* ok */
#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
/* ok */
#else
#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
#endif
#ifndef CVEC_MALLOC
#include <stdlib.h>
#define CVEC_MALLOC(sz) malloc(sz)
#define CVEC_REALLOC(p, sz) realloc(p, sz)
#define CVEC_FREE(p) free(p)
#endif
#ifndef CVEC_MEMMOVE
#include <string.h>
#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
#endif
#ifndef CVEC_ASSERT
#include <assert.h>
#define CVEC_ASSERT(x) assert(x)
#endif
#ifndef CVEC_SIZE_T
#include <stdlib.h>
#define CVEC_SIZE_T size_t
#endif
#ifndef CVEC_SZ
#define CVEC_SZ
typedef CVEC_SIZE_T cvec_sz;
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef CVEC_NO_INT
/** Data structure for int vector. */
typedef struct cvector_i
{
int* a; /**< Array. */
cvec_sz size; /**< Current size (amount you use when manipulating array directly). */
cvec_sz capacity; /**< Allocated size of array; always >= size. */
} cvector_i;
extern cvec_sz CVEC_I_START_SZ;
int cvec_i(cvector_i* vec, cvec_sz size, cvec_sz capacity);
int cvec_init_i(cvector_i* vec, int* vals, cvec_sz num);
cvector_i* cvec_i_heap(cvec_sz size, cvec_sz capacity);
cvector_i* cvec_init_i_heap(int* vals, cvec_sz num);
int cvec_copyc_i(void* dest, void* src);
int cvec_copy_i(cvector_i* dest, cvector_i* src);
int cvec_push_i(cvector_i* vec, int a);
int cvec_pop_i(cvector_i* vec);
int cvec_extend_i(cvector_i* vec, cvec_sz num);
int cvec_insert_i(cvector_i* vec, cvec_sz i, int a);
int cvec_insert_array_i(cvector_i* vec, cvec_sz i, int* a, cvec_sz num);
int cvec_replace_i(cvector_i* vec, cvec_sz i, int a);
void cvec_erase_i(cvector_i* vec, cvec_sz start, cvec_sz end);
int cvec_reserve_i(cvector_i* vec, cvec_sz size);
#define cvec_shrink_to_fit_i(vec) cvec_set_cap_i((vec), (vec)->size)
int cvec_set_cap_i(cvector_i* vec, cvec_sz size);
void cvec_set_val_sz_i(cvector_i* vec, int val);
void cvec_set_val_cap_i(cvector_i* vec, int val);
int* cvec_back_i(cvector_i* vec);
void cvec_clear_i(cvector_i* vec);
void cvec_free_i_heap(void* vec);
void cvec_free_i(void* vec);
#endif
#ifndef CVEC_NO_DOUBLE
/** Data structure for double vector. */
typedef struct cvector_d
{
double* a; /**< Array. */
cvec_sz size; /**< Current size (amount you use when manipulating array directly). */
cvec_sz capacity; /**< Allocated size of array; always >= size. */
} cvector_d;
extern cvec_sz CVEC_D_START_SZ;
int cvec_d(cvector_d* vec, cvec_sz size, cvec_sz capacity);
int cvec_init_d(cvector_d* vec, double* vals, cvec_sz num);
cvector_d* cvec_d_heap(cvec_sz size, cvec_sz capacity);
cvector_d* cvec_init_d_heap(double* vals, cvec_sz num);
int cvec_copyc_d(void* dest, void* src);
int cvec_copy_d(cvector_d* dest, cvector_d* src);
int cvec_push_d(cvector_d* vec, double a);
double cvec_pop_d(cvector_d* vec);
int cvec_extend_d(cvector_d* vec, cvec_sz num);
int cvec_insert_d(cvector_d* vec, cvec_sz i, double a);
int cvec_insert_array_d(cvector_d* vec, cvec_sz i, double* a, cvec_sz num);
double cvec_replace_d(cvector_d* vec, cvec_sz i, double a);
void cvec_erase_d(cvector_d* vec, cvec_sz start, cvec_sz end);
int cvec_reserve_d(cvector_d* vec, cvec_sz size);
#define cvec_shrink_to_fit_d(vec) cvec_set_cap_d((vec), (vec)->size)
int cvec_set_cap_d(cvector_d* vec, cvec_sz size);
void cvec_set_val_sz_d(cvector_d* vec, double val);
void cvec_set_val_cap_d(cvector_d* vec, double val);
double* cvec_back_d(cvector_d* vec);
void cvec_clear_d(cvector_d* vec);
void cvec_free_d_heap(void* vec);
void cvec_free_d(void* vec);
#endif
#ifndef CVEC_NO_STR
/** Data structure for string vector. */
typedef struct cvector_str
{
char** a; /**< Array. */
cvec_sz size; /**< Current size (amount you use when manipulating array directly). */
cvec_sz capacity; /**< Allocated size of array; always >= size. */
} cvector_str;
extern cvec_sz CVEC_STR_START_SZ;
#ifndef CVEC_STRDUP
#define CVEC_STRDUP cvec_strdup
char* cvec_strdup(const char* str);
#endif
int cvec_str(cvector_str* vec, cvec_sz size, cvec_sz capacity);
int cvec_init_str(cvector_str* vec, char** vals, cvec_sz num);
cvector_str* cvec_str_heap(cvec_sz size, cvec_sz capacity);
cvector_str* cvec_init_str_heap(char** vals, cvec_sz num);
int cvec_copyc_str(void* dest, void* src);
int cvec_copy_str(cvector_str* dest, cvector_str* src);
int cvec_push_str(cvector_str* vec, char* a);
void cvec_pop_str(cvector_str* vec, char* ret);
int cvec_pushm_str(cvector_str* vec, char* a);
#define cvec_popm_str(vec) (vec).a[--(vec).size]
int cvec_insertm_str(cvector_str* vec, cvec_sz i, char* a);
int cvec_insert_arraym_str(cvector_str* vec, cvec_sz i, char** a, cvec_sz num);
#define cvec_replacem_str(vec, i, s, ret) ((ret) = (vec).a[i], (vec).a[i] = (s))
int cvec_extend_str(cvector_str* vec, cvec_sz num);
int cvec_insert_str(cvector_str* vec, cvec_sz i, char* a);
int cvec_insert_array_str(cvector_str* vec, cvec_sz i, char** a, cvec_sz num);
void cvec_replace_str(cvector_str* vec, cvec_sz i, char* a, char* ret);
void cvec_erase_str(cvector_str* vec, cvec_sz start, cvec_sz end);
void cvec_remove_str(cvector_str* vec, cvec_sz start, cvec_sz end);
int cvec_reserve_str(cvector_str* vec, cvec_sz size);
#define cvec_shrink_to_fit_str(vec) cvec_set_cap_str((vec), (vec)->size)
int cvec_set_cap_str(cvector_str* vec, cvec_sz size);
void cvec_set_val_sz_str(cvector_str* vec, char* val);
void cvec_set_val_cap_str(cvector_str* vec, char* val);
char** cvec_back_str(cvector_str* vec);
void cvec_clear_str(cvector_str* vec);
void cvec_free_str_heap(void* vec);
void cvec_free_str(void* vec);
#endif
#ifndef CVEC_NO_VOID
typedef unsigned char cvec_u8;
/** Data structure for generic type (cast to void) vectors */
typedef struct cvector_void
{
cvec_u8* a; /**< Array. */
cvec_sz size; /**< Current size (amount you should use when manipulating array directly). */
cvec_sz capacity; /**< Allocated size of array; always >= size. */
cvec_sz elem_size; /**< Size in bytes of type stored (sizeof(T) where T is type). */
void (*elem_free)(void*);
int (*elem_init)(void*, void*);
} cvector_void;
extern cvec_sz CVEC_VOID_START_SZ;
#define CVEC_GET_VOID(VEC, TYPE, I) ((TYPE*)&(VEC)->a[(I)*(VEC)->elem_size])
int cvec_void(cvector_void* vec, cvec_sz size, cvec_sz capacity, cvec_sz elem_sz, void(*elem_free)(void*), int(*elem_init)(void*, void*));
int cvec_init_void(cvector_void* vec, void* vals, cvec_sz num, cvec_sz elem_sz, void(*elem_free)(void*), int(*elem_init)(void*, void*));
cvector_void* cvec_void_heap(cvec_sz size, cvec_sz capacity, cvec_sz elem_sz, void (*elem_free)(void*), int(*elem_init)(void*, void*));
cvector_void* cvec_init_void_heap(void* vals, cvec_sz num, cvec_sz elem_sz, void (*elem_free)(void*), int(*elem_init)(void*, void*));
int cvec_copyc_void(void* dest, void* src);
int cvec_copy_void(cvector_void* dest, cvector_void* src);
int cvec_push_void(cvector_void* vec, void* a);
void cvec_pop_void(cvector_void* vec, void* ret);
void* cvec_get_void(cvector_void* vec, cvec_sz i);
int cvec_pushm_void(cvector_void* vec, void* a);
void cvec_popm_void(cvector_void* vec, void* ret);
int cvec_insertm_void(cvector_void* vec, cvec_sz i, void* a);
int cvec_insert_arraym_void(cvector_void* vec, cvec_sz i, void* a, cvec_sz num);
void cvec_replacem_void(cvector_void* vec, cvec_sz i, void* a, void* ret);
int cvec_extend_void(cvector_void* vec, cvec_sz num);
int cvec_insert_void(cvector_void* vec, cvec_sz i, void* a);
int cvec_insert_array_void(cvector_void* vec, cvec_sz i, void* a, cvec_sz num);
int cvec_replace_void(cvector_void* vec, cvec_sz i, void* a, void* ret);
void cvec_erase_void(cvector_void* vec, cvec_sz start, cvec_sz end);
void cvec_remove_void(cvector_void* vec, cvec_sz start, cvec_sz end);
int cvec_reserve_void(cvector_void* vec, cvec_sz size);
#define cvec_shrink_to_fit_void(vec) cvec_set_cap_void((vec), (vec)->size)
int cvec_set_cap_void(cvector_void* vec, cvec_sz size);
int cvec_set_val_sz_void(cvector_void* vec, void* val);
int cvec_set_val_cap_void(cvector_void* vec, void* val);
void* cvec_back_void(cvector_void* vec);
void cvec_clear_void(cvector_void* vec);
void cvec_free_void_heap(void* vec);
void cvec_free_void(void* vec);
#endif
#ifdef __cplusplus
}
#endif
#define CVEC_NEW_DECLS(TYPE) \
typedef struct cvector_##TYPE { \
TYPE* a; \
cvec_sz size; \
cvec_sz capacity; \
} cvector_##TYPE; \
\
extern cvec_sz CVEC_##TYPE##_SZ; \
\
int cvec_##TYPE(cvector_##TYPE* vec, cvec_sz size, cvec_sz capacity); \
int cvec_init_##TYPE(cvector_##TYPE* vec, TYPE* vals, cvec_sz num); \
\
cvector_##TYPE* cvec_##TYPE##_heap(cvec_sz size, cvec_sz capacity); \
cvector_##TYPE* cvec_init_##TYPE##_heap(TYPE* vals, cvec_sz num); \
\
int cvec_copyc_##TYPE(void* dest, void* src); \
int cvec_copy_##TYPE(cvector_##TYPE* dest, cvector_##TYPE* src); \
\
int cvec_push_##TYPE(cvector_##TYPE* vec, TYPE a); \
TYPE cvec_pop_##TYPE(cvector_##TYPE* vec); \
\
int cvec_extend_##TYPE(cvector_##TYPE* vec, cvec_sz num); \
int cvec_insert_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE a); \
int cvec_insert_array_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, cvec_sz num); \
TYPE cvec_replace_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE a); \
void cvec_erase_##TYPE(cvector_##TYPE* vec, cvec_sz start, cvec_sz end); \
int cvec_reserve_##TYPE(cvector_##TYPE* vec, cvec_sz size); \
int cvec_set_cap_##TYPE(cvector_##TYPE* vec, cvec_sz size); \
void cvec_set_val_sz_##TYPE(cvector_##TYPE* vec, TYPE val); \
void cvec_set_val_cap_##TYPE(cvector_##TYPE* vec, TYPE val); \
\
TYPE* cvec_back_##TYPE(cvector_##TYPE* vec); \
\
void cvec_clear_##TYPE(cvector_##TYPE* vec); \
void cvec_free_##TYPE##_heap(void* vec); \
void cvec_free_##TYPE(void* vec);
#define CVEC_NEW_DEFS(TYPE, RESIZE_MACRO) \
cvec_sz CVEC_##TYPE##_SZ = 50; \
\
cvector_##TYPE* cvec_##TYPE##_heap(cvec_sz size, cvec_sz capacity) \
{ \
cvector_##TYPE* vec; \
if (!(vec = (cvector_##TYPE*)CVEC_MALLOC(sizeof(cvector_##TYPE)))) { \
CVEC_ASSERT(vec != NULL); \
return NULL; \
} \
\
vec->size = size; \
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) \
? capacity \
: vec->size + CVEC_##TYPE##_SZ; \
\
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
CVEC_FREE(vec); \
return NULL; \
} \
\
return vec; \
} \
\
cvector_##TYPE* cvec_init_##TYPE##_heap(TYPE* vals, cvec_sz num) \
{ \
cvector_##TYPE* vec; \
\
if (!(vec = (cvector_##TYPE*)CVEC_MALLOC(sizeof(cvector_##TYPE)))) { \
CVEC_ASSERT(vec != NULL); \
return NULL; \
} \
\
vec->capacity = num + CVEC_##TYPE##_SZ; \
vec->size = num; \
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
CVEC_FREE(vec); \
return NULL; \
} \
\
CVEC_MEMMOVE(vec->a, vals, sizeof(TYPE) * num); \
\
return vec; \
} \
\
int cvec_##TYPE(cvector_##TYPE* vec, cvec_sz size, cvec_sz capacity) \
{ \
vec->size = size; \
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) \
? capacity \
: vec->size + CVEC_##TYPE##_SZ; \
\
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
vec->size = vec->capacity = 0; \
return 0; \
} \
\
return 1; \
} \
\
int cvec_init_##TYPE(cvector_##TYPE* vec, TYPE* vals, cvec_sz num) \
{ \
vec->capacity = num + CVEC_##TYPE##_SZ; \
vec->size = num; \
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
vec->size = vec->capacity = 0; \
return 0; \
} \
\
CVEC_MEMMOVE(vec->a, vals, sizeof(TYPE) * num); \
\
return 1; \
} \
\
int cvec_copyc_##TYPE(void* dest, void* src) \
{ \
cvector_##TYPE* vec1 = (cvector_##TYPE*)dest; \
cvector_##TYPE* vec2 = (cvector_##TYPE*)src; \
\
vec1->a = NULL; \
vec1->size = 0; \
vec1->capacity = 0; \
\
return cvec_copy_##TYPE(vec1, vec2); \
} \
\
int cvec_copy_##TYPE(cvector_##TYPE* dest, cvector_##TYPE* src) \
{ \
TYPE* tmp = NULL; \
if (!(tmp = (TYPE*)CVEC_REALLOC(dest->a, src->capacity*sizeof(TYPE)))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
dest->a = tmp; \
\
CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(TYPE)); \
dest->size = src->size; \
dest->capacity = src->capacity; \
return 1; \
} \
\
int cvec_push_##TYPE(cvector_##TYPE* vec, TYPE a) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity > vec->size) { \
vec->a[vec->size++] = a; \
} else { \
tmp_sz = RESIZE_MACRO(vec->capacity); \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->a[vec->size++] = a; \
vec->capacity = tmp_sz; \
} \
return 1; \
} \
\
TYPE cvec_pop_##TYPE(cvector_##TYPE* vec) { return vec->a[--vec->size]; } \
\
TYPE* cvec_back_##TYPE(cvector_##TYPE* vec) { return &vec->a[vec->size - 1]; } \
\
int cvec_extend_##TYPE(cvector_##TYPE* vec, cvec_sz num) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity < vec->size + num) { \
tmp_sz = vec->capacity + num + CVEC_##TYPE##_SZ; \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
\
vec->size += num; \
return 1; \
} \
\
int cvec_insert_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE a) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity > vec->size) { \
CVEC_MEMMOVE(&vec->a[i + 1], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
vec->a[i] = a; \
} else { \
tmp_sz = RESIZE_MACRO(vec->capacity); \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
CVEC_MEMMOVE(&vec->a[i + 1], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
vec->a[i] = a; \
vec->capacity = tmp_sz; \
} \
\
vec->size++; \
return 1; \
} \
\
int cvec_insert_array_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, cvec_sz num) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity < vec->size + num) { \
tmp_sz = vec->capacity + num + CVEC_##TYPE##_SZ; \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
\
CVEC_MEMMOVE(&vec->a[i + num], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
CVEC_MEMMOVE(&vec->a[i], a, num * sizeof(TYPE)); \
vec->size += num; \
return 1; \
} \
\
TYPE cvec_replace_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE a) \
{ \
TYPE tmp = vec->a[i]; \
vec->a[i] = a; \
return tmp; \
} \
\
void cvec_erase_##TYPE(cvector_##TYPE* vec, cvec_sz start, cvec_sz end) \
{ \
cvec_sz d = end - start + 1; \
CVEC_MEMMOVE(&vec->a[start], &vec->a[end + 1], (vec->size - 1 - end) * sizeof(TYPE)); \
vec->size -= d; \
} \
\
int cvec_reserve_##TYPE(cvector_##TYPE* vec, cvec_sz size) \
{ \
TYPE* tmp; \
if (vec->capacity < size) { \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * (size + CVEC_##TYPE##_SZ)))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = size + CVEC_##TYPE##_SZ; \
} \
return 1; \
} \
\
int cvec_set_cap_##TYPE(cvector_##TYPE* vec, cvec_sz size) \
{ \
TYPE* tmp; \
if (size < vec->size) { \
vec->size = size; \
} \
\
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * size))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = size; \
return 1; \
} \
\
void cvec_set_val_sz_##TYPE(cvector_##TYPE* vec, TYPE val) \
{ \
cvec_sz i; \
for (i = 0; i < vec->size; i++) { \
vec->a[i] = val; \
} \
} \
\
void cvec_set_val_cap_##TYPE(cvector_##TYPE* vec, TYPE val) \
{ \
cvec_sz i; \
for (i = 0; i < vec->capacity; i++) { \
vec->a[i] = val; \
} \
} \
\
void cvec_clear_##TYPE(cvector_##TYPE* vec) { vec->size = 0; } \
\
void cvec_free_##TYPE##_heap(void* vec) \
{ \
cvector_##TYPE* tmp = (cvector_##TYPE*)vec; \
if (!tmp) return; \
CVEC_FREE(tmp->a); \
CVEC_FREE(tmp); \
} \
\
void cvec_free_##TYPE(void* vec) \
{ \
cvector_##TYPE* tmp = (cvector_##TYPE*)vec; \
CVEC_FREE(tmp->a); \
tmp->size = 0; \
tmp->capacity = 0; \
}
#define CVEC_NEW_DECLS2(TYPE) \
typedef struct cvector_##TYPE { \
TYPE* a; \
cvec_sz size; \
cvec_sz capacity; \
void (*elem_free)(void*); \
int (*elem_init)(void*, void*); \
} cvector_##TYPE; \
\
extern cvec_sz CVEC_##TYPE##_SZ; \
\
int cvec_##TYPE(cvector_##TYPE* vec, cvec_sz size, cvec_sz capacity, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)); \
int cvec_init_##TYPE(cvector_##TYPE* vec, TYPE* vals, cvec_sz num, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)); \
\
cvector_##TYPE* cvec_##TYPE##_heap(cvec_sz size, cvec_sz capacity, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)); \
cvector_##TYPE* cvec_init_##TYPE##_heap(TYPE* vals, cvec_sz num, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)); \
\
int cvec_copyc_##TYPE(void* dest, void* src); \
int cvec_copy_##TYPE(cvector_##TYPE* dest, cvector_##TYPE* src); \
\
int cvec_push_##TYPE(cvector_##TYPE* vec, TYPE* val); \
void cvec_pop_##TYPE(cvector_##TYPE* vec, TYPE* ret); \
\
int cvec_pushm_##TYPE(cvector_##TYPE* vec, TYPE* a); \
void cvec_popm_##TYPE(cvector_##TYPE* vec, TYPE* ret); \
int cvec_insertm_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a); \
int cvec_insert_arraym_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, cvec_sz num); \
void cvec_replacem_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, TYPE* ret); \
\
int cvec_extend_##TYPE(cvector_##TYPE* vec, cvec_sz num); \
int cvec_insert_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a); \
int cvec_insert_array_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, cvec_sz num); \
int cvec_replace_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, TYPE* ret); \
void cvec_erase_##TYPE(cvector_##TYPE* vec, cvec_sz start, cvec_sz end); \
void cvec_remove_##TYPE(cvector_##TYPE* vec, cvec_sz start, cvec_sz end); \
int cvec_reserve_##TYPE(cvector_##TYPE* vec, cvec_sz size); \
int cvec_set_cap_##TYPE(cvector_##TYPE* vec, cvec_sz size); \
int cvec_set_val_sz_##TYPE(cvector_##TYPE* vec, TYPE* val); \
int cvec_set_val_cap_##TYPE(cvector_##TYPE* vec, TYPE* val); \
\
TYPE* cvec_back_##TYPE(cvector_##TYPE* vec); \
\
void cvec_clear_##TYPE(cvector_##TYPE* vec); \
void cvec_free_##TYPE##_heap(void* vec); \
void cvec_free_##TYPE(void* vec);
#define CVEC_NEW_DEFS2(TYPE, RESIZE_MACRO) \
cvec_sz CVEC_##TYPE##_SZ = 20; \
\
cvector_##TYPE* cvec_##TYPE##_heap(cvec_sz size, cvec_sz capacity, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)) \
{ \
cvector_##TYPE* vec; \
if (!(vec = (cvector_##TYPE*)CVEC_MALLOC(sizeof(cvector_##TYPE)))) { \
CVEC_ASSERT(vec != NULL); \
return NULL; \
} \
\
vec->size = size; \
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) \
? capacity \
: vec->size + CVEC_##TYPE##_SZ; \
\
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
CVEC_FREE(vec); \
return NULL; \
} \
\
vec->elem_free = elem_free; \
vec->elem_init = elem_init; \
\
return vec; \
} \
\
cvector_##TYPE* cvec_init_##TYPE##_heap(TYPE* vals, cvec_sz num, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)) \
{ \
cvector_##TYPE* vec; \
cvec_sz i; \
\
if (!(vec = (cvector_##TYPE*)CVEC_MALLOC(sizeof(cvector_##TYPE)))) { \
CVEC_ASSERT(vec != NULL); \
return NULL; \
} \
\
vec->capacity = num + CVEC_##TYPE##_SZ; \
vec->size = num; \
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
CVEC_FREE(vec); \
return NULL; \
} \
\
if (elem_init) { \
for (i = 0; i < num; ++i) { \
if (!elem_init(&vec->a[i], &vals[i])) { \
CVEC_ASSERT(0); \
CVEC_FREE(vec->a); \
CVEC_FREE(vec); \
return NULL; \
} \
} \
} else { \
CVEC_MEMMOVE(vec->a, vals, sizeof(TYPE) * num); \
} \
\
vec->elem_free = elem_free; \
vec->elem_init = elem_init; \
\
return vec; \
} \
\
int cvec_##TYPE(cvector_##TYPE* vec, cvec_sz size, cvec_sz capacity, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)) \
{ \
vec->size = size; \
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) \
? capacity \
: vec->size + CVEC_##TYPE##_SZ; \
\
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
vec->size = vec->capacity = 0; \
return 0; \
} \
\
vec->elem_free = elem_free; \
vec->elem_init = elem_init; \
\
return 1; \
} \
\
int cvec_init_##TYPE(cvector_##TYPE* vec, TYPE* vals, cvec_sz num, void (*elem_free)(void*), \
int (*elem_init)(void*, void*)) \
{ \
cvec_sz i; \
\
vec->capacity = num + CVEC_##TYPE##_SZ; \
vec->size = num; \
if (!(vec->a = (TYPE*)CVEC_MALLOC(vec->capacity * sizeof(TYPE)))) { \
CVEC_ASSERT(vec->a != NULL); \
vec->size = vec->capacity = 0; \
return 0; \
} \
\
if (elem_init) { \
for (i = 0; i < num; ++i) { \
if (!elem_init(&vec->a[i], &vals[i])) { \
CVEC_ASSERT(0); \
return 0; \
} \
} \
} else { \
CVEC_MEMMOVE(vec->a, vals, sizeof(TYPE) * num); \
} \
\
vec->elem_free = elem_free; \
vec->elem_init = elem_init; \
\
return 1; \
} \
\
int cvec_copyc_##TYPE(void* dest, void* src) \
{ \
cvector_##TYPE* vec1 = (cvector_##TYPE*)dest; \
cvector_##TYPE* vec2 = (cvector_##TYPE*)src; \
\
vec1->a = NULL; \
vec1->size = 0; \
vec1->capacity = 0; \
\
return cvec_copy_##TYPE(vec1, vec2); \
} \
\
int cvec_copy_##TYPE(cvector_##TYPE* dest, cvector_##TYPE* src) \
{ \
int i; \
TYPE* tmp = NULL; \
if (!(tmp = (TYPE*)CVEC_REALLOC(dest->a, src->capacity*sizeof(TYPE)))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
dest->a = tmp; \
\
if (src->elem_init) { \
for (i=0; i<src->size; ++i) { \
if (!src->elem_init(&dest->a[i], &src->a[i])) { \
CVEC_ASSERT(0); \
return 0; \
} \
} \
} else { \
CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(TYPE)); \
} \
\
dest->size = src->size; \
dest->capacity = src->capacity; \
dest->elem_free = src->elem_free; \
dest->elem_init = src->elem_init; \
return 1; \
} \
\
int cvec_push_##TYPE(cvector_##TYPE* vec, TYPE* a) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity == vec->size) { \
tmp_sz = RESIZE_MACRO(vec->capacity); \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
if (vec->elem_init) { \
if (!vec->elem_init(&vec->a[vec->size], a)) { \
CVEC_ASSERT(0); \
return 0; \
} \
} else { \
CVEC_MEMMOVE(&vec->a[vec->size], a, sizeof(TYPE)); \
} \
\
vec->size++; \
return 1; \
} \
\
int cvec_pushm_##TYPE(cvector_##TYPE* vec, TYPE* a) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity == vec->size) { \
tmp_sz = RESIZE_MACRO(vec->capacity); \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
CVEC_MEMMOVE(&vec->a[vec->size], a, sizeof(TYPE)); \
\
vec->size++; \
return 1; \
} \
\
void cvec_pop_##TYPE(cvector_##TYPE* vec, TYPE* ret) \
{ \
if (ret) { \
CVEC_MEMMOVE(ret, &vec->a[--vec->size], sizeof(TYPE)); \
} else { \
vec->size--; \
} \
\
if (vec->elem_free) { \
vec->elem_free(&vec->a[vec->size]); \
} \
} \
\
void cvec_popm_##TYPE(cvector_##TYPE* vec, TYPE* ret) \
{ \
vec->size--; \
if (ret) { \
CVEC_MEMMOVE(ret, &vec->a[vec->size], sizeof(TYPE)); \
} \
} \
\
TYPE* cvec_back_##TYPE(cvector_##TYPE* vec) { return &vec->a[vec->size - 1]; } \
\
int cvec_extend_##TYPE(cvector_##TYPE* vec, cvec_sz num) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity < vec->size + num) { \
tmp_sz = vec->capacity + num + CVEC_##TYPE##_SZ; \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
\
vec->size += num; \
return 1; \
} \
\
int cvec_insert_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity == vec->size) { \
tmp_sz = RESIZE_MACRO(vec->capacity); \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
\
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
CVEC_MEMMOVE(&vec->a[i + 1], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
\
if (vec->elem_init) { \
if (!vec->elem_init(&vec->a[i], a)) { \
CVEC_ASSERT(0); \
return 0; \
} \
} else { \
CVEC_MEMMOVE(&vec->a[i], a, sizeof(TYPE)); \
} \
\
vec->size++; \
return 1; \
} \
\
int cvec_insertm_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity == vec->size) { \
tmp_sz = RESIZE_MACRO(vec->capacity); \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
\
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
CVEC_MEMMOVE(&vec->a[i + 1], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
\
CVEC_MEMMOVE(&vec->a[i], a, sizeof(TYPE)); \
\
vec->size++; \
return 1; \
} \
\
int cvec_insert_array_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, cvec_sz num) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz, j; \
if (vec->capacity < vec->size + num) { \
tmp_sz = vec->capacity + num + CVEC_##TYPE##_SZ; \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
\
CVEC_MEMMOVE(&vec->a[i + num], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
if (vec->elem_init) { \
for (j = 0; j < num; ++j) { \
if (!vec->elem_init(&vec->a[j + i], &a[j])) { \
CVEC_ASSERT(0); \
return 0; \
} \
} \
} else { \
CVEC_MEMMOVE(&vec->a[i], a, num * sizeof(TYPE)); \
} \
vec->size += num; \
return 1; \
} \
\
int cvec_insert_arraym_##TYPE(cvector_##TYPE* vec, cvec_sz i, TYPE* a, cvec_sz num) \
{ \
TYPE* tmp; \
cvec_sz tmp_sz; \
if (vec->capacity < vec->size + num) { \
tmp_sz = vec->capacity + num + CVEC_##TYPE##_SZ; \
if (!(tmp = (TYPE*)CVEC_REALLOC(vec->a, sizeof(TYPE) * tmp_sz))) { \
CVEC_ASSERT(tmp != NULL); \
return 0; \
} \
vec->a = tmp; \
vec->capacity = tmp_sz; \
} \
\
CVEC_MEMMOVE(&vec->a[i + num], &vec->a[i], (vec->size - i) * sizeof(TYPE)); \
\
CVEC_MEMMOVE(&vec->a[i], a, num * sizeof(TYPE)); \
vec->size += num; \
return 1; \