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