-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcc.h
9591 lines (8386 loc) · 452 KB
/
cc.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
/*----------------------------------------- CC: CONVENIENT CONTAINERS v1.4.2 -------------------------------------------
This library provides ergonomic, high-performance generic containers (vectors, linked lists, unordered maps, unordered
sets, ordered maps, ordered sets, and null-terminated strings).
Features:
* Fully generic API.
* Type safety without boilerplate container-type definitions.
* User-defined destructor, comparison, and hash functions associated with element and key types.
* No assumption of successful memory allocation.
* Single header.
* Compiles in C and C++.
It requires C23, or C11 and support for typeof (available in every major compiler), or C++11.
It has been tested with GCC, Clang, MinGW, and MSVC.
Simple usage example (for more advanced examples, see https://github.com./JacksonAllan/CC):
+---------------------------------------------------------+----------------------------------------------------------+
| Vector: | List: |
|---------------------------------------------------------|----------------------------------------------------------|
| #include <stdio.h> | #include <stdio.h> |
| #include "cc.h" | #include "cc.h" |
| | |
| int main( void ) | int main( void ) |
| { | { |
| vec( int ) our_vec; | list( int ) our_list; |
| init( &our_vec ); | init( &our_list ); |
| | |
| // Adding elements to end. | // Adding elements to end. |
| for( int i = 0; i < 10; ++i ) | for( int i = 0; i < 10; ++i ) |
| if( !push( &our_vec, i ) ) | if( !push( &our_list, i ) ) |
| { | { |
| // Out of memory, so abort. | // Out of memory, so abort. |
| cleanup( &our_vec ); | cleanup( &our_list ); |
| return 1; | return 1; |
| } | } |
| | |
| // Inserting an element at an index. | // Inserting an element before another element. |
| for( int i = 0; i < 10; ++i ) | for( |
| if( !insert( &our_vec, i * 2, i ) ) | int *el = first( &our_list ); |
| { | el != end( &our_list ); |
| // Out of memory, so abort. | el = next( &our_list, el ) |
| cleanup( &our_vec ); | ) |
| return 1; | if( !insert( &our_list, el, *el ) ) |
| } | { |
| | // Out of memory, so abort. |
| // Retrieving and erasing elements. | cleanup( &our_list ); |
| for( int i = 0; i < size( &our_vec ); ) | return 1; |
| if( *get( &our_vec, i ) % 3 == 0 ) | } |
| erase( &our_vec, i ); | |
| else | // Erasing elements. |
| ++i; | for( |
| | int *el = first( &our_list ); |
| // Iteration #1. | el != end( &our_list ); |
| for_each( &our_vec, el ) | ) |
| printf( "%d ", *el ); | if( *el % 3 == 0 ) |
| // Printed: 1 1 2 2 4 4 5 5 7 7 8 8 | el = erase( &our_list, el ); |
| | else |
| // Iteration #2. | el = next( &our_list, el ); |
| for( | |
| int *el = first( &our_vec ); | // Iteration #1. |
| el != end( &our_vec ); | for_each( &our_list, el ) |
| el = next( &our_vec, el ) | printf( "%d ", *el ); |
| ) | // Printed: 1 1 2 2 4 4 5 5 7 7 8 8 |
| printf( "%d ", *el ); | |
| // Printed: Same as above. | // Iteration #2. |
| | for( |
| cleanup( &our_vec ); | int *el = first( &our_list ); |
| } | el != end( &our_list ); |
| | el = next( &our_list, el ) |
| | ) |
| | printf( "%d ", *el ); |
| | // Printed: Same as above. |
| | |
| | cleanup( &our_list ); |
| | } |
+---------------------------------------------------------+----------------------------------------------------------+
| Map: | Set: |
|---------------------------------------------------------|----------------------------------------------------------|
| #include <stdio.h> | #include <stdio.h> |
| #include "cc.h" | #include "cc.h" |
| | |
| int main( void ) | int main( void ) |
| { | { |
| // Declare a map with int keys and short elements. | set( int ) our_set; |
| map( int, short ) our_map; | init( &our_set ); |
| init( &our_map ); | |
| | // Inserting elements. |
| // Inserting elements. | for( int i = 0; i < 10; ++i ) |
| for( int i = 0; i < 10; ++i ) | if( !insert( &our_set, i ) ) |
| if( !insert( &our_map, i, i + 1 ) ) | { |
| { | // Out of memory, so abort. |
| // Out of memory, so abort. | cleanup( &our_set ); |
| cleanup( &our_map ); | return 1; |
| return 1; | } |
| } | |
| | // Erasing elements. |
| // Erasing elements. | for( int i = 0; i < 10; i += 3 ) |
| for( int i = 0; i < 10; i += 3 ) | erase( &our_set, i ); |
| erase( &our_map, i ); | |
| | // Retrieving elements. |
| // Retrieving elements. | for( int i = 0; i < 10; ++i ) |
| for( int i = 0; i < 10; ++i ) | { |
| { | int *el = get( &our_set, i ); |
| short *el = get( &our_map, i ); | if( el ) |
| if( el ) | printf( "%d ", *el ); |
| printf( "%d:%d ", i, *el ); | } |
| } | // Printed: 1 2 4 5 7 8 |
| // Printed: 1:2 2:3 4:5 5:6 7:8 8:9 | |
| | // Iteration #1. |
| // Iteration #1 (elements only). | for_each( &our_set, el ) |
| for_each( &our_map, el ) | printf( "%d ", *el ); |
| printf( "%d ", *el ); | // Printed: 2 4 7 1 5 8 |
| // Printed: 3 5 8 2 6 9 | |
| | // Iteration #2. |
| // Iteration #2 (elements and keys). | for( |
| for_each( &our_map, key, el ) | int *el = first( &our_set ); |
| printf( "%d:%d ", *key, *el ); | el != end( &our_set ); |
| // Printed: 2:3 4:5 7:8 1:2 5:6 8:9 | el = next( &our_set, el ) |
| | ) |
| // Iteration #3. | printf( "%d ", *el ); |
| for( | // Printed: Same as above. |
| short *el = first( &our_map ); | |
| el != end( &our_map ); | cleanup( &our_set ); |
| el = next( &our_map, el ) | } |
| ) | |
| printf( "%d:%d ", *key_for( &our_map, el ), *el ); | |
| // Printed: Same as above. | |
| | |
| cleanup( &our_map ); | |
| } | |
+---------------------------------------------------------+----------------------------------------------------------+
| Ordered map: | Ordered set: |
|---------------------------------------------------------|----------------------------------------------------------|
| #include <stdio.h> | #include <stdio.h> |
| #include "cc.h" | #include "cc.h" |
| | |
| int main( void ) | int main( void ) |
| { | { |
| // Declare an ordered map with int keys and short | oset( int ) our_oset; |
| // elements. | init( &our_oset ); |
| omap( int, short ) our_omap; | |
| init( &our_omap ); | // Inserting elements. |
| | for( int i = 0; i < 10; ++i ) |
| // Inserting elements. | if( !insert( &our_oset, i ) ) |
| for( int i = 0; i < 10; ++i ) | { |
| if( !insert( &our_omap, i, i + 1 ) ) | // Out of memory, so abort. |
| { | cleanup( &our_oset ); |
| // Out of memory, so abort. | return 1; |
| cleanup( &our_omap ); | } |
| return 1; | |
| } | // Erasing elements. |
| | for( int i = 0; i < 10; i += 3 ) |
| // Erasing elements. | erase( &our_oset, i ); |
| for( int i = 0; i < 10; i += 3 ) | |
| erase( &our_omap, i ); | // Retrieving elements. |
| | for( int i = 0; i < 10; ++i ) |
| // Retrieving elements. | { |
| for( int i = 0; i < 10; ++i ) | int *el = get( &our_oset, i ); |
| { | if( el ) |
| short *el = get( &our_omap, i ); | printf( "%d ", *el ); |
| if( el ) | } |
| printf( "%d:%d ", i, *el ); | // Printed: 1 2 4 5 7 8 |
| } | |
| // Printed: 1:2 2:3 4:5 5:6 7:8 8:9 | // Iteration #1. |
| | for_each( &our_oset, el ) |
| // Iteration #1 (elements only). | printf( "%d ", *el ); |
| for_each( &our_omap, el ) | // Printed: 1 2 4 5 7 8 |
| printf( "%d ", *el ); | |
| // Printed: 2 3 5 6 8 9 | // Iteration #2. |
| | for( |
| // Iteration #2 (elements and keys). | int *el = first( &our_oset ); |
| for_each( &our_omap, key, el ) | el != end( &our_oset ); |
| printf( "%d:%d ", *key, *el ); | el = next( &our_oset, el ) |
| // Printed: 1:2 2:3 4:5 5:6 7:8 8:9 | ) |
| | printf( "%d ", *el ); |
| // Iteration #3. | // Printed: Same as above. |
| for( | |
| short *el = first( &our_omap ); | // Iteration over an element range, namely from 2 |
| el != end( &our_omap ); | // (inclusive) to 7 (exclusive). |
| el = next( &our_omap, el ) | for( |
| ) | int *el = first( &our_oset, 2 ), |
| printf( "%d:%d ", *key_for( &our_omap, el ), *el ); | *range_end = first( &our_oset, 7 ); |
| // Printed: Same as above. | el != range_end; |
| | el = next( &our_oset, el ) |
| // Iteration over a key range, namely from 2 | ) |
| // (inclusive) to 7 (exclusive). | printf( "%d ", *el ); |
| for( | // Printed: 2 4 5 |
| short *el = first( &our_omap, 2 ), | |
| *range_end = first( &our_omap, 7 ); | cleanup( &our_oset ); |
| el != range_end; | } |
| el = next( &our_omap, el ) | |
| ) | |
| printf( "%d:%d ", *key_for( &our_omap, el ), *el ); | |
| // Printed: 2:3 4:5 5:6 | |
| | |
| cleanup( &our_omap ); | |
| } | |
+---------------------------------------------------------+----------------------------------------------------------+
| String: |
|---------------------------------------------------------|
| #include <stdio.h> |
| #include "cc.h" |
| |
| int main( void ) |
| { |
| str( char ) our_str; |
| init( &our_str ); |
| |
| // Appending formatted data. |
| const char model[] = "Hornet CB900F"; |
| const char manufacturer[] = "Honda"; |
| unsigned int year_introduced = 2002; |
| unsigned int year_discontinued = 2007; |
| double horsepower = 103.0; |
| double torque = 84.9; |
| if( |
| !push_fmt( |
| &our_str, "The ", model, |
| " is a motorcycle that was manufactured by ", |
| manufacturer, " from ", year_introduced, " to ", |
| year_discontinued, ".\nIt makes ", horsepower, |
| "hp and ", torque, "Nm of torque.\n" |
| ) |
| ) |
| { |
| // Out of memory, so abort. |
| cleanup( &our_str ); |
| return 1; |
| } |
| |
| // Inserting formatted data at an index. |
| const char alternative_model_name[] = "919"; |
| if( |
| !insert_fmt( |
| &our_str, 17, ", also known as the ", |
| alternative_model_name, "," |
| ) |
| ) |
| { |
| // Out of memory, so abort. |
| cleanup( &our_str ); |
| return 1; |
| } |
| |
| printf( first( &our_str ) ); |
| // Printed: |
| // The Hornet CB900F, also known as the 919, is a |
| // motorcycle that was manufactured by Honda from |
| // 2002 to 2007. |
| // It makes 103.00hp and 84.90Nm of torque. |
| |
| // Erasing elements. |
| erase_n( &our_str, 108, 41 ); |
| |
| printf( first( &our_str ) ); |
| // Printed: |
| // The Hornet CB900F, also known as the 919, is a |
| // motorcycle that was manufactured by Honda from |
| // 2002 to 2007. |
| |
| // Iteration #1. |
| for_each( &our_str, el ) |
| printf( "%c", *el ); |
| // Printed: Same as above. |
| |
| // Iteration #2. |
| for( |
| char *el = first( &our_str ); |
| el != end( &our_str ); |
| el = next( &our_str, el ) |
| ) |
| printf( "%c", *el ); |
| // Printed: Same as above. |
| |
| cleanup( &our_str ); |
| } |
+---------------------------------------------------------+
Including the library:
Place this at the top of your file/s:
#include "cc.h"
The following can be defined before including the library in any file:
#define CC_NO_SHORT_NAMES
By default, CC exposes API macros without the "cc_" prefix.
Define this flag to withhold the unprefixed names.
The following can be defined anywhere and affect all calls to API macros where the definition is visible:
#define CC_REALLOC our_realloc
Causes API macros to use a custom realloc function rather than the one in the standard library.
#define CC_FREE our_free
Causes API macros to use a custom free function rather than the one in the standard library.
API:
General notes:
* API macros may evaluate their first argument - the pointer to the container - multiple times, so never use
expressions with side effects (e.g. &our_containers[ ++i ] ) for that argument. In GCC and Clang, attempting to do
so will cause a compiler warning. All other arguments are only evaluated once.
* If CC_NO_SHORT_NAMES was declared, all API macros and functions are prefixed with "cc_".
* Duplicating a container handle via assignment and then operating on the duplicate will invalidate the original.
Hence, only create a duplicate via assignment (including through function parameters and return values) if you have
finished with the original.
* An iterator is a pointer to an element in the container or to the associated end (or r_end, if the container
supports it). In the documentation below, these pointers are referred to as "pointer-iterators".
* In the documentation below, el_ty is the container's element type and key_ty is the container's key type (where
applicable).
All containers:
The following function-like macros operate on all containers:
void init( <any container type> *cntr )
Initializes cntr for use.
This call cannot fail (it does not allocate memory).
<any container type> initialized( <same container type> *cntr )
Returns an initialized instance of the same type as cntr.
This call cannot fail (it does not allocate memory).
The call is a constant expression and can therefore be used to initialize global containers at the site of their
declaration, e.g.:
vec( int ) our_vec = initialized( &our_vec );
bool init_clone( <any container type> *cntr, <same container type> *src )
Initializes cntr as a shallow copy of src.
Returns true, or false if unsuccessful due to memory allocation failure.
size_t size( <any container type> *cntr )
Returns the number of elements.
void clear( <any container type> *cntr )
Erases all elements, calling the element and key types' destructors if they exist (unless the container is a
string).
void cleanup( <any container type> *cntr )
Erases all elements (calling the element and key types' destructors if they exist, unless the container is a
string), frees any other memory associated with the container, and initializes the container for reuse.
el_ty *first( <any container type> *cntr )
Returns a pointer-iterator to the first element, or an end pointer-iterator if the container is empty.
el_ty *next( <any container type> *cntr, el_ty *i )
Returns a pointer-iterator to the element after the one pointed to by i.
If i points to the last element, the return value is an end pointer-iterator.
If i points to r_end (for the containers that support reverse iteration), the return value is a pointer-iterator
to the first element, or an end pointer-iterator if the container is empty.
el_ty *end( <any container type> *cntr )
Returns an end pointer-iterator for the container.
for_each( <any container type> *cntr, i_name )
Creates a loop iterating over all elements from first to last.
This macro declares a pointer-iterator (el_ty *) named i_name.
It is equivalent to
for( el_ty *i_name = first( cntr ); i_name != end( cntr ); i_name = next( cntr, i_name ) )
and should be followed by the loop body.
Vector (a dynamic array that stores elements in contiguous memory):
vec( el_ty ) cntr
Declares an uninitialized vector named cntr.
size_t cap( vec( el_ty ) *cntr )
Returns the current capacity.
bool reserve( vec( el_ty ) *cntr, size_t n )
Ensures that the capacity is large enough to accommodate n elements.
Returns true, or false if unsuccessful due to memory allocation failure.
bool resize( vec( el_ty ) *cntr, size_t n )
Sets the number of elements to n.
If n is above the current size, the new elements are uninitialized.
If n is below the current size, the element type's destructor (if it exists) is called for each erased element.
Returns true, or false if unsuccessful due to memory allocation failure.
bool shrink( vec( el_ty ) *cntr )
Shrinks the capacity to the current size.
Returns true, or false if unsuccessful due to memory allocation failure.
el_ty *get( vec( el_ty ) *cntr, size_t i )
Returns a pointer-iterator to the element at index i.
el_ty *push( vec( el_ty ) *cntr, el_ty el )
Inserts el at the end of the vector.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *push_n( vec( el_ty ) *cntr, el_ty *els, size_t n )
Inserts n elements from array els at the end of the vector.
Returns a pointer-iterator to the first new element, or NULL in the case of memory allocation failure.
el_ty *insert( vec( el_ty ) *cntr, size_t i, el_ty el )
Inserts el at index i.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *insert_n( vec( el_ty ) *cntr, size_t i, el_ty *els, size_t n )
Inserts n elements from array els at index i.
Returns a pointer-iterator to the first new element, or NULL in the case of memory allocation failure.
el_ty *erase( vec( el_ty ) *cntr, size_t i )
Erases the element at index i, calling the element type's destructor if it exists.
Returns a pointer-iterator to the element after the erased element, or an end pointer-iterator if there is no
subsequent element.
el_ty *erase_n( vec( el_ty ) *cntr, size_t i, size_t n )
Erases n elements beginning at index i, calling the element type's destructor, if it exists, for each
erased element.
Returns a pointer-iterator to the element after the erased elements, or an end pointer-iterator if there is no
subsequent element.
el_ty *last( vec( el_ty ) *cntr )
Returns a pointer-iterator to the last element.
This call is synonymous with get( cntr, size( cntr ) - 1 ) and assumes that the vector is not empty.
Notes:
* Vector pointer-iterators (including end) are invalidated by any API calls that cause memory reallocation.
List (a doubly linked list):
list( el_ty ) cntr
Declares an uninitialized list named cntr.
el_ty *insert( list( el_ty ) *cntr, el_ty *i, el_ty el )
Inserts el before pointer-iterator i.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *push( list( el_ty ) *cntr, el_ty el )
Inserts el at the end of the list.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
This call is synonymous with insert( cntr, end( cntr ), el ).
el_ty *erase( list( el_ty ) *cntr, el_ty *i )
Erases the element pointed to by pointer-iterator i, calling the element type's destructor if it exists.
Returns a pointer-iterator to the element after i, or an end pointer-iterator if i was the last element.
bool splice( list( el_ty ) *cntr, el_ty *i, list( el_ty ) src, el_ty *src_i )
Removes the element pointed to by pointer-iterator src_i from src and inserts it before the element pointed to by
pointer-iterator i in cntr.
Returns true, or false if unsuccessful.
This call only allocates memory, and therefore can only fail, if the list has not had any element inserted,
pushed, or spliced into it since it was initialized.
el_ty *last( list( el_ty ) *cntr )
Returns a pointer-iterator to the last element, or an r_end pointer-iterator if the list is empty.
el_ty *prev( list( el_ty ) *cntr, el_ty *i )
Returns a pointer-iterator to the element before the one pointed to by i.
If i points to the first element, the return value is an r_end pointer-iterator.
If i points to end, then the return value is a pointer-iterator to the last element, or an r_end pointer-iterator
if the list is empty.
el_ty *r_end( list( el_ty ) *cntr )
Returns an r_end (reverse end) pointer-iterator for the list.
r_for_each( list( el_ty ) *cntr, i_name )
Creates a loop iterating over all elements from last to first.
This macro declares an el_ty * pointer-iterator named i_name.
It is equivalent to
for( el_ty *i_name = last( cntr ); i_name != r_end( cntr ); i_name = prev( cntr, i_name ) )
and should be followed by the body of the loop.
Notes:
* List pointer-iterators (including r_end and end) are not invalidated by any API calls besides init and cleanup,
unless they point to erased elements.
Map (an unordered associative container mapping elements to keys, implemented as a hybrid open-addressing, chained
hash table):
map( key_ty, el_ty ) cntr
Declares an uninitialized map named cntr.
key_ty must be a type, or alias for a type, for which comparison and hash functions have been defined (this
requirement is enforced internally such that neglecting it causes a compiler error).
For types with in-built comparison and hash functions, and for details on how to declare new comparison and hash
functions, see "Destructor, comparison, and hash functions and custom max load factors" below.
size_t cap( map( key_ty, el_ty ) *cntr )
Returns the current capacity, i.e. bucket count.
Note that the number of elements a map can accommodate without rehashing is not its capacity but its capacity
multiplied by the max load factor associated with its key type.
bool reserve( map( key_ty, el_ty ) *cntr, size_t n )
Ensures that the capacity is large enough to accommodate n elements without rehashing.
Returns true, or false if unsuccessful due to memory allocation failure.
bool shrink( map( key_ty, el_ty ) *cntr )
Shrinks the capacity to best accommodate the current size.
Returns true, or false if unsuccessful due to memory allocation failure.
el_ty *insert( map( key_ty, el_ty ) *cntr, key_ty key, el_ty el )
Inserts element el with the specified key.
If an element with the same key already exists, the existing element is replaced.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *get( map( key_ty, el_ty ) *cntr, key_ty key )
Returns a pointer-iterator to the element with the specified key, or NULL if no such element exists.
el_ty *get_or_insert( map( key_ty, el_ty ) *cntr, key_ty key, el_ty el )
Inserts element el if no element with the specified key already exist.
Returns a pointer-iterator to the new element if it was inserted, or a pointer-iterator to the existing element
with the same key, or NULL in the case of memory allocation failure.
Determine whether an element was inserted by comparing the map's size before and after the call.
const key_ty *key_for( map( key_ty, el_ty ) *cntr, el_ty *i )
Returns a const pointer to the key for the element pointed to by pointer-iterator i.
bool erase( map( key_ty, el_ty ) *cntr, key_ty key )
Erases the element with the specified key, if it exists.
Returns true if an element was erased, or false if no such element exists.
el_ty *erase_itr( map( key_ty, el_ty ) *cntr, el_ty *i )
Erases the element pointed to by pointer-iterator i.
Returns a pointer-iterator to the next element in the map, or an end pointer-iterator if the erased element was
the last one.
for_each( map( key_ty, el_ty ) *cntr, key_ptr_name, i_name )
Creates a loop iterating over all elements from first to last, with easy access to the corresponding keys.
This macro declares a pointer to the key (const key_ty *) named key_ptr_name and a pointer-iterator (el_ty *)
named i_name.
It should be followed by the body of the loop.
Notes:
* Map pointer-iterators (including end) may be invalidated by any API calls that cause memory reallocation.
Set (an unordered associative container for elements without a separate key, implemented as a hybrid open-addressing,
chained hash table):
set( el_ty ) cntr
Declares an uninitialized set named cntr.
el_ty must be a type, or alias for a type, for which comparison and hash functions have been defined (this
requirement is enforced internally such that neglecting it causes a compiler error).
For types with in-built comparison and hash functions, and for details on how to declare new comparison and hash
functions, see "Destructor, comparison, and hash functions and custom max load factors" below.
size_t cap( set( el_ty ) *cntr )
Returns the current capacity, i.e. bucket count.
Note that the number of elements a set can accommodate without rehashing is not its capacity but its capacity
multiplied by the max load factor associated with its key type.
bool reserve( set( el_ty ) *cntr, size_t n )
Ensures that the capacity is large enough to accommodate n elements without rehashing.
Returns true, or false if unsuccessful due to memory allocation failure.
bool shrink( set( el_ty ) *cntr )
Shrinks the capacity to best accommodate the current size.
Returns true, or false if unsuccessful due to memory allocation failure.
el_ty *insert( set( el_ty ) *cntr, el_ty el )
Inserts element el.
If the element already exists, the existing element is replaced.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *get( set( el_ty ) *cntr, el_ty el )
Returns a pointer-iterator to element el, or NULL if no such element exists.
el_ty *get_or_insert( set( el_ty ) *cntr, el_ty el )
Inserts element el if it does not already exist.
Returns a pointer-iterator to the new element if it was inserted, or a pointer-iterator to the existing element,
or NULL in the case of memory allocation failure.
Determine whether an element was inserted by comparing the set's size before and after the call.
bool erase( set( el_ty ) *cntr, el_ty el )
Erases the element el, if it exists.
Returns true if an element was erased, or false if no such element exists.
el_ty *erase_itr( set( el_ty ) *cntr, el_ty *i )
Erases the element pointed to by pointer-iterator i.
Returns a pointer-iterator to the next element in the set, or an end pointer-iterator if the erased element was
the last one.
Notes:
* Set pointer-iterators (including end) may be invalidated by any API calls that cause memory reallocation.
Ordered map (an ordered associative container mapping elements to keys, implemented as a red-black tree):
omap( key_ty, el_ty ) cntr
Declares an uninitialized ordered map named cntr.
key_ty must be a type, or alias for a type, for which a comparison function has been defined (this requirement is
enforced internally such that neglecting it causes a compiler error).
For types with in-built comparison functions, and for details on how to declare new comparison functions, see
"Destructor, comparison, and hash functions and custom max load factors" below.
el_ty *insert( omap( key_ty, el_ty ) *cntr, key_ty key, el_ty el )
Inserts element el with the specified key.
If an element with the same key already exists, the existing element is replaced.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *get( omap( key_ty, el_ty ) *cntr, key_ty key )
Returns a pointer-iterator to the element with the specified key, or NULL if no such element exists.
el_ty *get_or_insert( omap( key_ty, el_ty ) *cntr, key_ty key, el_ty el )
Inserts element el if no element with the specified key already exists.
Returns a pointer-iterator to the new element if it was inserted, or a pointer-iterator to the existing element
with the same key, or NULL in the case of memory allocation failure.
Determine whether an element was inserted by comparing the ordered map's size before and after the call.
const key_ty *key_for( omap( key_ty, el_ty ) *cntr, el_ty *i )
Returns a const pointer to the key for the element pointed to by pointer-iterator i.
bool erase( omap( key_ty, el_ty ) *cntr, key_ty key )
Erases the element with the specified key, if it exists.
Returns true if an element was erased, or false if no such element exists.
el_ty *erase_itr( omap( key_ty, el_ty ) *cntr, el_ty *i )
Erases the element pointed to by pointer-iterator i.
Returns a pointer-iterator to the next element in the ordered map, or an end pointer-iterator if the erased
element was the last one.
el_ty *first( omap( key_ty, el_ty ) *cntr, key_ty key )
Returns a pointer-iterator to the first element with a key greater than or equal to the specified key, or an end
pointer-iterator if no such element exists.
el_ty *last( omap( key_ty, el_ty ) *cntr )
Returns a pointer-iterator to the last element, or an r_end pointer-iterator if the ordered map is empty.
el_ty *last( omap( key_ty, el_ty ) *cntr, key_ty key )
Returns a pointer-iterator to the last element with a key less than or equal to the specified key, or an r_end
pointer-iterator if the ordered map is empty.
el_ty *prev( omap( key_ty, el_ty ) *cntr, el_ty *i )
Returns a pointer-iterator to the element before the one pointed to by i.
If i points to the first element, the value returned is an r_end pointer-iterator.
If i points to end, then the value returned points to the last element, or is an r_end pointer-iterator if the
ordered map is empty.
el_ty *r_end( omap( key_ty, el_ty ) *cntr )
Returns an r_end (reverse end) pointer-iterator for the ordered map.
for_each( omap( key_ty, el_ty ) *cntr, key_ptr_name, i_name )
Creates a loop iterating over all elements from first to last, with easy access to the corresponding keys.
This macro declares a pointer to the key (const key_ty *) named key_ptr_name and a pointer-iterator (el_ty *)
named i_name.
It should be followed by the body of the loop.
r_for_each( omap( key_ty, el_ty ) *cntr, i_name )
Creates a loop iterating over all elements from last to first.
This macro declares an el_ty * pointer-iterator named i_name.
It is equivalent to
for( el_ty *i_name = last( cntr ); i_name != r_end( cntr ); i_name = prev( cntr, i_name ) )
and should be followed by the body of the loop.
r_for_each( omap( key_ty, el_ty ) *cntr, key_ptr_name, i_name )
Creates a loop iterating over all elements from last to first, with easy access to the corresponding keys.
This macro declares a pointer to the key (const key_ty *) named key_ptr_name and a pointer-iterator (el_ty *)
named i_name.
It should be followed by the body of the loop.
Notes:
* Ordered map pointer-iterators (including r_end and end) are not invalidated by any API calls besides init and
cleanup, unless they point to erased elements.
Ordered set (an ordered associative container for elements without a separate key, implemented as a red-black tree):
oset( el_ty ) cntr
Declares an uninitialized ordered set named cntr.
el_ty must be a type, or alias for a type, for which a comparison function has been defined (this requirement is
enforced internally such that neglecting it causes a compiler error).
For types with in-built comparison functions, and for details on how to declare new comparison functions, see
"Destructor, comparison, and hash functions and custom max load factors" below.
el_ty *insert( oset( el_ty ) *cntr, el_ty el )
Inserts element el.
If the element already exists, the existing element is replaced.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *get( oset( el_ty ) *cntr, el_ty el )
Returns a pointer-iterator to element el, or NULL if no such element exists.
el_ty *get_or_insert( oset( el_ty ) *cntr, el_ty el )
Inserts element el if it does not already exist.
Returns a pointer-iterator to the new element if it was inserted, or a pointer-iterator to the existing element,
or NULL in the case of memory allocation failure.
Determine whether an element was inserted by comparing the ordered set's size before and after the call.
bool erase( oset( el_ty ) *cntr, el_ty el )
Erases the element el, if it exists.
Returns true if an element was erased, or false if no such element exists.
el_ty *erase_itr( oset( el_ty ) *cntr, el_ty *i )
Erases the element pointed to by pointer-iterator i.
Returns a pointer-iterator to the next element in the ordered set, or an end pointer-iterator if the erased
element was the last one.
el_ty *first( oset( key_ty, el_ty ) *cntr, el_ty el )
Returns a pointer-iterator to the first element greater than or equal to el, or an end pointer-iterator if no such
element exists.
el_ty *last( oset( el_ty ) *cntr )
Returns a pointer-iterator to the last element, or an r_end pointer-iterator if the ordered set is empty.
el_ty *last( oset( key_ty, el_ty ) *cntr, el_ty el )
Returns a pointer-iterator to the last element less than or equal to el, or an end pointer-iterator if no such
element exists.
el_ty *prev( oset( el_ty ) *cntr, el_ty *i )
Returns a pointer-iterator to the element before the one pointed to by i.
If i points to the first element, the return value is an r_end pointer-iterator.
If i points to end, then the pointer-iterator returned points to the last element, or is an r_end pointer-iterator
if the ordered set is empty.
el_ty *r_end( oset( el_ty ) *cntr )
Returns an r_end (reverse end) pointer-iterator for the ordered set.
r_for_each( oset( el_ty ) *cntr, i_name )
Creates a loop iterating over all elements from last to first.
This macro declares an el_ty * pointer-iterator named i_name.
It is equivalent to
for( el_ty *i_name = last( cntr ); i_name != r_end( cntr ); i_name = prev( cntr, i_name ) )
and should be followed by the body of the loop.
Notes:
* Ordered set pointer-iterators (including r_end and end) may be invalidated by any API calls that cause memory
reallocation.
String (a dynamic, null-terminated array representing a sequence of characters):
str( el_ty ) cntr
Declares an uninitialized string named cntr.
el_ty must be char, unsigned char, signed char, char8_t, char16_t, char32_t, or an alias for one of these types.
size_t cap( str( el_ty ) *cntr )
Returns the current capacity, i.e. the number of elements that the string can accommodate (not including the null
terminator) without reallocating its internal buffer.
bool reserve( str( el_ty ) *cntr, size_t n )
Ensures that the the capacity is large enough to accommodate n elements.
Returns true, or false if unsuccessful due to memory allocation failure.
bool resize( str( el_ty ) *cntr, size_t n, el_ty fill_character )
Sets the number of elements to n.
If n is above the current size, the new elements are initialized to fill_character.
If n is below the current size, no destructor is called for any erased element, even if a destructor for the
element type has been defined.
Returns true, or false if unsuccessful due to memory allocation failure.
bool shrink( str( el_ty ) *cntr )
Shrinks the capacity to the current size.
Returns true, or false if unsuccessful due to memory allocation failure.
el_ty *get( str( el_ty ) *cntr, size_t i )
Returns a pointer-iterator to the element at index i.
el_ty *push( str( el_ty ) *cntr, el_ty el )
Inserts el at the end of the string.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *push_fmt( str( el_ty ) *cntr, ... )
Inserts up to 32 formatted values, provided as variadic arguments, at the end of the string.
Returns a pointer-iterator to the first new element, or NULL in the case of memory allocation failure.
Each variadic argument must be one of the following:
* A null-terminated array of elements of the same type as el_ty (i.e. a C string).
* A CC string with the same element type.
* A fundamental integer type (bool, char, unsigned char, signed char, unsigned short, short, unsigned int, int,
unsigned long, long, unsigned long long, or long long) or alias for such a type.
* A fundamental floating-point type (float or double).
* A void pointer (to be formatted as a memory address).
* The return value of one of the following functions:
* integer_dec( int min_digits )
Causes subsequent integer arguments to be formatted as decimal integers.
min_digits specifies the minimum number of digits, and if the formatted integer is shorter than this number,
it is padded with leading zeros.
* integer_hex( int min_digits )
Causes subsequent integer arguments to be formatted as unsigned hexadecimal integers.
min_digits specifies the minimum number of digits, and if the formatted integer is shorter than this number,
it is padded with leading zeros.
* integer_oct( int min_digits )
Causes subsequent integer arguments to be formatted as unsigned octal integers.
min_digits specifies the minimum number of digits, and if the formatted integer is shorter than this number,
it is padded with leading zeros.
* float_dec( int precision )
Causes subsequent floating-point arguments to be formatted as decimal floating-point numbers.
precision specifies the number of decimal places to include.
* float_hex( int precision )
Causes subsequent floating-point arguments to be formatted as hexadecimal floating-point numbers.
precision specifies the number of decimal places to include.
* float_sci( int precision )
Causes subsequent floating-point arguments to be formatted using scientific notation.
precision specifies the number of decimal places to include.
* float_shortest( int significant_digits )
Causes subsequent floating-point arguments to be formatted as decimal floating-point numbers or using
scientific notation, depending on which representation is shorter.
significant_digits specifies the maximum number of significant digits to include.
Arguments are type-promoted as follows:
* bool, unsigned char, unsigned short, unsigned int, unsigned long -> unsigned long long.
* signed char, short, int, long, -> long long.
* char -> long long or unsigned long long, depending on whether char is signed.
* float -> double.
By default, integer arguments are formatted as decimal integers with a minimum of one digit, and floating-point
arguments are formatted as decimal floating-point numbers with two decimal places.
For formatting, C and CC strings of char16_t and char32_t elements are assumed to be encoded as UTF-16 and UTF-32,
respectively.
el_ty *push_n( str( el_ty ) *cntr, el_ty *els, size_t n )
Inserts n elements from array els at the end of the string.
Returns a pointer-iterator to the first new element, or NULL in the case of memory allocation failure.
el_ty *insert( str( el_ty ) *cntr, size_t i, el_ty el )
Inserts el at index i.
Returns a pointer-iterator to the new element, or NULL in the case of memory allocation failure.
el_ty *insert_fmt( str( el_ty ) *cntr, size_t i, ... )
Inserts up to 32 formatted values, provided as variadic arguments, at index i.
Each variadic argument must be one of the possibilities listed in the above documentation for push_fmt, and the
same type-promotions and encoding assumptions apply.
Returns a pointer-iterator to the first new element, or NULL in the case of memory allocation failure.
el_ty *insert_n( str( el_ty ) *cntr, size_t i, el_ty *els, size_t n )
Inserts n elements from array els at index i.
Returns a pointer-iterator to the first new element, or NULL in the case of memory allocation failure.
el_ty *erase( str( el_ty ) *cntr, size_t i )
Erases the element at index i.
No destructor is called for the erased element, even if a destructor for the element type has been defined.
Returns a pointer-iterator to the element after the erased element, or an end pointer-iterator if there is no
subsequent element.
el_ty *erase_n( str( el_ty ) *cntr, size_t i, size_t n )
Erases n elements beginning at index i.
No destructor is called for erased elements, even if a destructor for the element type has been defined.
Returns a pointer-iterator to the element after the erased elements, or an end pointer-iterator if there is no
subsequent element.
el_ty *last( str( el_ty ) *cntr )
Returns a pointer-iterator to the last element.
This call is synonymous with get( cntr, size( cntr ) - 1 ) and assumes that the string is not empty.
Notes:
* String pointer-iterators (including end) are invalidated by any API calls that cause memory reallocation.
Destructor, comparison, and hash functions and custom max load factors:
This part of the API allows the user to define custom destructor, comparison, and hash functions and max load
factors for a type.
Once these functions are defined, any container (except strings) using that type for its elements or keys will call
them automatically.
Once the max load factor is defined, any map using the type for its keys and any set using the type for its elements
will use the defined load factor to determine when rehashing is necessary.
#define CC_DTOR ty, { function body }
#include "cc.h"
Defines a destructor for type ty.
The signature of the function is void ( ty val ).
#define CC_CMPR ty, { function body }
#include "cc.h"
Defines a comparison function for type ty.
The signature of the function is int ( ty val_1, ty val_2 ).
The function should return 0 if val_1 and val_2 are equal, a negative integer if val_1 is less than val_2, and a
positive integer if val_1 is greater than val_2.
#define CC_HASH ty, { function body }
#include "cc.h"
Defines a hash function for type ty.
The signature of the function is size_t ( ty val ).
The function should return the hash of val.
#define CC_LOAD ty, max_load_factor
Defines the max load factor for type ty.
max_load_factor should be a float or double between 0.0 and 1.0.
The default max load factor is 0.9.
Trivial example:
typedef struct { int x; } our_type;
#define CC_DTOR our_type, { printf( "!%d\n", val.x ); }
#define CC_CMPR our_type, { return val_1.x < val_2.x ? -1 : val_1.x > val_2.x; }
#define CC_HASH our_type, { return val.x * 2654435761ull; }
#define CC_LOAD our_type, 0.5
#include "cc.h"
Notes:
* These functions are inline and have static scope, so you need to either redefine them in each translation unit
from which they should be called or (preferably) define them in a shared header. For structs or unions, a sensible
place to define them is immediately after the definition of the struct or union.
* Only one destructor, comparison, or hash function or max load factor should be defined by the user for each type.
* Including cc.h in these cases does not include the full header, so you still need to include it separately at the