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 #ifndef UCX_MAP_H
38 #define UCX_MAP_H
39
40 #include "common.h"
41 #include "collection.h"
42 #include "string.h"
43 #include "hash_key.h"
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49
50 typedef struct cx_map_s CxMap;
51
52
53 typedef struct cx_map_entry_s CxMapEntry;
54
55
56 typedef struct cx_map_class_s cx_map_class;
57
58
59 struct cx_map_s {
60 CX_COLLECTION_MEMBERS
61
62 cx_map_class *cl;
63 };
64
65
66
67
68 enum cx_map_iterator_type {
69
70
71
72 CX_MAP_ITERATOR_PAIRS,
73
74
75
76 CX_MAP_ITERATOR_KEYS,
77
78
79
80 CX_MAP_ITERATOR_VALUES
81 };
82
83
84
85
86 struct cx_map_class_s {
87
88
89
90 __attribute__((__nonnull__))
91 void (*destructor)(
struct cx_map_s *map);
92
93
94
95
96 __attribute__((__nonnull__))
97 void (*clear)(
struct cx_map_s *map);
98
99
100
101
102 __attribute__((__nonnull__))
103 int (*put)(
104 CxMap *map,
105 CxHashKey key,
106 void *value
107 );
108
109
110
111
112 __attribute__((__nonnull__, __warn_unused_result__))
113 void *(*get)(
114 CxMap
const *map,
115 CxHashKey key
116 );
117
118
119
120
121 __attribute__((__nonnull__))
122 void *(*remove)(
123 CxMap *map,
124 CxHashKey key,
125 bool destroy
126 );
127
128
129
130
131 __attribute__((__nonnull__, __warn_unused_result__))
132 CxIterator (*iterator)(CxMap
const *map,
enum cx_map_iterator_type type);
133 };
134
135
136
137
138 struct cx_map_entry_s {
139
140
141
142 CxHashKey
const *key;
143
144
145
146 void *value;
147 };
148
149
150
151
152
153
154 extern CxMap *
const cxEmptyMap;
155
156
157
158
159
160
161
162
163
164
165 __attribute__((__nonnull__))
166 static inline
void cxMapStoreObjects(CxMap *map) {
167 map->store_pointer = false;
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 __attribute__((__nonnull__))
183 static inline
void cxMapStorePointers(CxMap *map) {
184 map->store_pointer = true;
185 map->item_size =
sizeof(
void *);
186 }
187
188
189
190
191
192
193
194 __attribute__((__nonnull__))
195 static inline
void cxMapDestroy(CxMap *map) {
196 map->cl->destructor(map);
197 }
198
199
200
201
202
203
204
205 __attribute__((__nonnull__))
206 static inline
void cxMapClear(CxMap *map) {
207 map->cl->clear(map);
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222 __attribute__((__nonnull__, __warn_unused_result__))
223 static inline CxIterator cxMapIteratorValues(CxMap
const *map) {
224 return map->cl->iterator(map,
CX_MAP_ITERATOR_VALUES);
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238 __attribute__((__nonnull__, __warn_unused_result__))
239 static inline CxIterator cxMapIteratorKeys(CxMap
const *map) {
240 return map->cl->iterator(map,
CX_MAP_ITERATOR_KEYS);
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 __attribute__((__nonnull__, __warn_unused_result__))
257 static inline CxIterator cxMapIterator(CxMap
const *map) {
258 return map->cl->iterator(map,
CX_MAP_ITERATOR_PAIRS);
259 }
260
261
262
263
264
265
266
267
268
269
270
271 __attribute__((__nonnull__, __warn_unused_result__))
272 CxMutIterator cxMapMutIteratorValues(CxMap *map);
273
274
275
276
277
278
279
280
281
282
283
284
285 __attribute__((__nonnull__, __warn_unused_result__))
286 CxMutIterator cxMapMutIteratorKeys(CxMap *map);
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 __attribute__((__nonnull__, __warn_unused_result__))
302 CxMutIterator cxMapMutIterator(CxMap *map);
303
304 #ifdef __cplusplus
305 }
306
307
308
309
310
311
312
313
314
315 __attribute__((__nonnull__))
316 static inline
int cxMapPut(
317 CxMap *map,
318 CxHashKey
const &key,
319 void *value
320 ) {
321 return map->cl->put(map, key, value);
322 }
323
324
325
326
327
328
329
330
331
332
333 __attribute__((__nonnull__))
334 static inline
int cxMapPut(
335 CxMap *map,
336 cxstring
const &key,
337 void *value
338 ) {
339 return map->cl->put(map, cx_hash_key_cxstr(key), value);
340 }
341
342
343
344
345
346
347
348
349
350 __attribute__((__nonnull__))
351 static inline
int cxMapPut(
352 CxMap *map,
353 cxmutstr
const &key,
354 void *value
355 ) {
356 return map->cl->put(map, cx_hash_key_cxstr(key), value);
357 }
358
359
360
361
362
363
364
365
366
367 __attribute__((__nonnull__))
368 static inline
int cxMapPut(
369 CxMap *map,
370 char const *key,
371 void *value
372 ) {
373 return map->cl->put(map, cx_hash_key_str(key), value);
374 }
375
376
377
378
379
380
381
382
383 __attribute__((__nonnull__, __warn_unused_result__))
384 static inline
void *cxMapGet(
385 CxMap
const *map,
386 CxHashKey
const &key
387 ) {
388 return map->cl->get(map, key);
389 }
390
391
392
393
394
395
396
397
398 __attribute__((__nonnull__, __warn_unused_result__))
399 static inline
void *cxMapGet(
400 CxMap
const *map,
401 cxstring
const &key
402 ) {
403 return map->cl->get(map, cx_hash_key_cxstr(key));
404 }
405
406
407
408
409
410
411
412
413 __attribute__((__nonnull__, __warn_unused_result__))
414 static inline
void *cxMapGet(
415 CxMap
const *map,
416 cxmutstr
const &key
417 ) {
418 return map->cl->get(map, cx_hash_key_cxstr(key));
419 }
420
421
422
423
424
425
426
427
428 __attribute__((__nonnull__, __warn_unused_result__))
429 static inline
void *cxMapGet(
430 CxMap
const *map,
431 char const *key
432 ) {
433 return map->cl->get(map, cx_hash_key_str(key));
434 }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450 __attribute__((__nonnull__))
451 static inline
void cxMapRemove(
452 CxMap *map,
453 CxHashKey
const &key
454 ) {
455 (
void) map->cl->remove(map, key, true);
456 }
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 __attribute__((__nonnull__))
473 static inline
void cxMapRemove(
474 CxMap *map,
475 cxstring
const &key
476 ) {
477 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
478 }
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 __attribute__((__nonnull__))
495 static inline
void cxMapRemove(
496 CxMap *map,
497 cxmutstr
const &key
498 ) {
499 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516 __attribute__((__nonnull__))
517 static inline
void cxMapRemove(
518 CxMap *map,
519 char const *key
520 ) {
521 (
void) map->cl->remove(map, cx_hash_key_str(key), true);
522 }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538 __attribute__((__nonnull__))
539 static inline
void cxMapDetach(
540 CxMap *map,
541 CxHashKey
const &key
542 ) {
543 (
void) map->cl->remove(map, key, false);
544 }
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 __attribute__((__nonnull__))
561 static inline
void cxMapDetach(
562 CxMap *map,
563 cxstring
const &key
564 ) {
565 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
566 }
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582 __attribute__((__nonnull__))
583 static inline
void cxMapDetach(
584 CxMap *map,
585 cxmutstr
const &key
586 ) {
587 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604 __attribute__((__nonnull__))
605 static inline
void cxMapDetach(
606 CxMap *map,
607 char const *key
608 ) {
609 (
void) map->cl->remove(map, cx_hash_key_str(key), false);
610 }
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631 __attribute__((__nonnull__, __warn_unused_result__))
632 static inline
void *cxMapRemoveAndGet(
633 CxMap *map,
634 CxHashKey key
635 ) {
636 return map->cl->remove(map, key, !map->store_pointer);
637 }
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658 __attribute__((__nonnull__, __warn_unused_result__))
659 static inline
void *cxMapRemoveAndGet(
660 CxMap *map,
661 cxstring key
662 ) {
663 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
664 }
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685 __attribute__((__nonnull__, __warn_unused_result__))
686 static inline
void *cxMapRemoveAndGet(
687 CxMap *map,
688 cxmutstr key
689 ) {
690 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
691 }
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712 __attribute__((__nonnull__, __warn_unused_result__))
713 static inline
void *cxMapRemoveAndGet(
714 CxMap *map,
715 char const *key
716 ) {
717 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
718 }
719
720 #else
721
722
723
724
725
726
727
728
729
730 __attribute__((__nonnull__))
731 static inline
int cx_map_put(
732 CxMap *map,
733 CxHashKey key,
734 void *value
735 ) {
736 return map->cl->put(map, key, value);
737 }
738
739
740
741
742
743
744
745
746
747 __attribute__((__nonnull__))
748 static inline
int cx_map_put_cxstr(
749 CxMap *map,
750 cxstring key,
751 void *value
752 ) {
753 return map->cl->put(map, cx_hash_key_cxstr(key), value);
754 }
755
756
757
758
759
760
761
762
763
764 __attribute__((__nonnull__))
765 static inline
int cx_map_put_mustr(
766 CxMap *map,
767 cxmutstr key,
768 void *value
769 ) {
770 return map->cl->put(map, cx_hash_key_cxstr(key), value);
771 }
772
773
774
775
776
777
778
779
780
781 __attribute__((__nonnull__))
782 static inline
int cx_map_put_str(
783 CxMap *map,
784 char const *key,
785 void *value
786 ) {
787 return map->cl->put(map, cx_hash_key_str(key), value);
788 }
789
790
791
792
793
794
795
796
797
798 #define cxMapPut(map, key, value) _Generic((key), \
799 CxHashKey: cx_map_put, \
800 cxstring: cx_map_put_cxstr, \
801 cxmutstr: cx_map_put_mustr, \
802 char*: cx_map_put_str, \
803 char const*: cx_map_put_str) \
804 (map, key, value)
805
806
807
808
809
810
811
812
813 __attribute__((__nonnull__, __warn_unused_result__))
814 static inline
void *cx_map_get(
815 CxMap
const *map,
816 CxHashKey key
817 ) {
818 return map->cl->get(map, key);
819 }
820
821
822
823
824
825
826
827
828 __attribute__((__nonnull__, __warn_unused_result__))
829 static inline
void *cx_map_get_cxstr(
830 CxMap
const *map,
831 cxstring key
832 ) {
833 return map->cl->get(map, cx_hash_key_cxstr(key));
834 }
835
836
837
838
839
840
841
842
843 __attribute__((__nonnull__, __warn_unused_result__))
844 static inline
void *cx_map_get_mustr(
845 CxMap
const *map,
846 cxmutstr key
847 ) {
848 return map->cl->get(map, cx_hash_key_cxstr(key));
849 }
850
851
852
853
854
855
856
857
858 __attribute__((__nonnull__, __warn_unused_result__))
859 static inline
void *cx_map_get_str(
860 CxMap
const *map,
861 char const *key
862 ) {
863 return map->cl->get(map, cx_hash_key_str(key));
864 }
865
866
867
868
869
870
871
872
873 #define cxMapGet(map, key) _Generic((key), \
874 CxHashKey: cx_map_get, \
875 cxstring: cx_map_get_cxstr, \
876 cxmutstr: cx_map_get_mustr, \
877 char*: cx_map_get_str, \
878 char const*: cx_map_get_str) \
879 (map, key)
880
881
882
883
884
885
886
887 __attribute__((__nonnull__))
888 static inline
void cx_map_remove(
889 CxMap *map,
890 CxHashKey key
891 ) {
892 (
void) map->cl->remove(map, key, true);
893 }
894
895
896
897
898
899
900
901 __attribute__((__nonnull__))
902 static inline
void cx_map_remove_cxstr(
903 CxMap *map,
904 cxstring key
905 ) {
906 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
907 }
908
909
910
911
912
913
914
915 __attribute__((__nonnull__))
916 static inline
void cx_map_remove_mustr(
917 CxMap *map,
918 cxmutstr key
919 ) {
920 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
921 }
922
923
924
925
926
927
928
929 __attribute__((__nonnull__))
930 static inline
void cx_map_remove_str(
931 CxMap *map,
932 char const *key
933 ) {
934 (
void) map->cl->remove(map, cx_hash_key_str(key), true);
935 }
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951 #define cxMapRemove(map, key) _Generic((key), \
952 CxHashKey: cx_map_remove, \
953 cxstring: cx_map_remove_cxstr, \
954 cxmutstr: cx_map_remove_mustr, \
955 char*: cx_map_remove_str, \
956 char const*: cx_map_remove_str) \
957 (map, key)
958
959
960
961
962
963
964
965
966 __attribute__((__nonnull__))
967 static inline
void cx_map_detach(
968 CxMap *map,
969 CxHashKey key
970 ) {
971 (
void) map->cl->remove(map, key, false);
972 }
973
974
975
976
977
978
979
980
981 __attribute__((__nonnull__))
982 static inline
void cx_map_detach_cxstr(
983 CxMap *map,
984 cxstring key
985 ) {
986 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
987 }
988
989
990
991
992
993
994
995
996 __attribute__((__nonnull__))
997 static inline
void cx_map_detach_mustr(
998 CxMap *map,
999 cxmutstr key
1000 ) {
1001 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
1002 }
1003
1004
1005
1006
1007
1008
1009
1010
1011 __attribute__((__nonnull__))
1012 static inline
void cx_map_detach_str(
1013 CxMap *map,
1014 char const *key
1015 ) {
1016 (
void) map->cl->remove(map, cx_hash_key_str(key), false);
1017 }
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 #define cxMapDetach(map, key) _Generic((key), \
1034 CxHashKey: cx_map_detach, \
1035 cxstring: cx_map_detach_cxstr, \
1036 cxmutstr: cx_map_detach_mustr, \
1037 char*: cx_map_detach_str, \
1038 char const*: cx_map_detach_str) \
1039 (map, key)
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049 __attribute__((__nonnull__, __warn_unused_result__))
1050 static inline
void *cx_map_remove_and_get(
1051 CxMap *map,
1052 CxHashKey key
1053 ) {
1054 return map->cl->remove(map, key, !map->store_pointer);
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065 __attribute__((__nonnull__, __warn_unused_result__))
1066 static inline
void *cx_map_remove_and_get_cxstr(
1067 CxMap *map,
1068 cxstring key
1069 ) {
1070 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081 __attribute__((__nonnull__, __warn_unused_result__))
1082 static inline
void *cx_map_remove_and_get_mustr(
1083 CxMap *map,
1084 cxmutstr key
1085 ) {
1086 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
1087 }
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 __attribute__((__nonnull__, __warn_unused_result__))
1098 static inline
void *cx_map_remove_and_get_str(
1099 CxMap *map,
1100 char const *key
1101 ) {
1102 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
1103 }
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124 #define cxMapRemoveAndGet(map, key) _Generic((key), \
1125 CxHashKey: cx_map_remove_and_get, \
1126 cxstring: cx_map_remove_and_get_cxstr, \
1127 cxmutstr: cx_map_remove_and_get_mustr, \
1128 char*: cx_map_remove_and_get_str, \
1129 char const*: cx_map_remove_and_get_str) \
1130 (map, key)
1131
1132 #endif
1133
1134 #endif
1135