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 const CxMap *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)(
const CxMap *map,
enum cx_map_iterator_type type);
135 };
136
137
138
139
140 struct cx_map_entry_s {
141
142
143
144 const CxHashKey *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(
const CxMap *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(
const CxMap *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(
const CxMap *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(
const CxMap *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(
const CxMap *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 const char *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 const CxMap *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 const CxMap *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 const CxMap *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 const CxMap *map,
455 const char *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 const char *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 const char *key
632 ) {
633 (
void) map->cl->remove(map, cx_hash_key_str(key), false);
634 }
635
636
637
638 #else
639
640
641
642
643
644
645
646
647
648 __attribute__((__nonnull__))
649 static inline
int cx_map_put(
650 CxMap *map,
651 CxHashKey key,
652 void *value
653 ) {
654 return map->cl->put(map, key, value);
655 }
656
657
658
659
660
661
662
663
664
665 __attribute__((__nonnull__))
666 static inline
int cx_map_put_cxstr(
667 CxMap *map,
668 cxstring key,
669 void *value
670 ) {
671 return map->cl->put(map, cx_hash_key_cxstr(key), value);
672 }
673
674
675
676
677
678
679
680
681
682 __attribute__((__nonnull__))
683 static inline
int cx_map_put_mustr(
684 CxMap *map,
685 cxmutstr key,
686 void *value
687 ) {
688 return map->cl->put(map, cx_hash_key_cxstr(key), value);
689 }
690
691
692
693
694
695
696
697
698
699 __attribute__((__nonnull__))
700 static inline
int cx_map_put_str(
701 CxMap *map,
702 const char *key,
703 void *value
704 ) {
705 return map->cl->put(map, cx_hash_key_str(key), value);
706 }
707
708
709
710
711
712
713
714
715
716 #define cxMapPut(map, key, value) _Generic((key), \
717 CxHashKey: cx_map_put, \
718 cxstring: cx_map_put_cxstr, \
719 cxmutstr: cx_map_put_mustr, \
720 char*: cx_map_put_str, \
721 const char*: cx_map_put_str) \
722 (map, key, value)
723
724
725
726
727
728
729
730
731 __attribute__((__nonnull__, __warn_unused_result__))
732 static inline
void *cx_map_get(
733 const CxMap *map,
734 CxHashKey key
735 ) {
736 return map->cl->get(map, key);
737 }
738
739
740
741
742
743
744
745
746 __attribute__((__nonnull__, __warn_unused_result__))
747 static inline
void *cx_map_get_cxstr(
748 const CxMap *map,
749 cxstring key
750 ) {
751 return map->cl->get(map, cx_hash_key_cxstr(key));
752 }
753
754
755
756
757
758
759
760
761 __attribute__((__nonnull__, __warn_unused_result__))
762 static inline
void *cx_map_get_mustr(
763 const CxMap *map,
764 cxmutstr key
765 ) {
766 return map->cl->get(map, cx_hash_key_cxstr(key));
767 }
768
769
770
771
772
773
774
775
776 __attribute__((__nonnull__, __warn_unused_result__))
777 static inline
void *cx_map_get_str(
778 const CxMap *map,
779 const char *key
780 ) {
781 return map->cl->get(map, cx_hash_key_str(key));
782 }
783
784
785
786
787
788
789
790
791 #define cxMapGet(map, key) _Generic((key), \
792 CxHashKey: cx_map_get, \
793 cxstring: cx_map_get_cxstr, \
794 cxmutstr: cx_map_get_mustr, \
795 char*: cx_map_get_str, \
796 const char*: cx_map_get_str) \
797 (map, key)
798
799
800
801
802
803
804
805 __attribute__((__nonnull__))
806 static inline
void cx_map_remove(
807 CxMap *map,
808 CxHashKey key
809 ) {
810 (
void) map->cl->remove(map, key, true);
811 }
812
813
814
815
816
817
818
819 __attribute__((__nonnull__))
820 static inline
void cx_map_remove_cxstr(
821 CxMap *map,
822 cxstring key
823 ) {
824 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
825 }
826
827
828
829
830
831
832
833 __attribute__((__nonnull__))
834 static inline
void cx_map_remove_mustr(
835 CxMap *map,
836 cxmutstr key
837 ) {
838 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
839 }
840
841
842
843
844
845
846
847 __attribute__((__nonnull__))
848 static inline
void cx_map_remove_str(
849 CxMap *map,
850 const char *key
851 ) {
852 (
void) map->cl->remove(map, cx_hash_key_str(key), true);
853 }
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869 #define cxMapRemove(map, key) _Generic((key), \
870 CxHashKey: cx_map_remove, \
871 cxstring: cx_map_remove_cxstr, \
872 cxmutstr: cx_map_remove_mustr, \
873 char*: cx_map_remove_str, \
874 const char*: cx_map_remove_str) \
875 (map, key)
876
877
878
879
880
881
882
883
884 __attribute__((__nonnull__))
885 static inline
void cx_map_detach(
886 CxMap *map,
887 CxHashKey key
888 ) {
889 (
void) map->cl->remove(map, key, false);
890 }
891
892
893
894
895
896
897
898
899 __attribute__((__nonnull__))
900 static inline
void cx_map_detach_cxstr(
901 CxMap *map,
902 cxstring key
903 ) {
904 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
905 }
906
907
908
909
910
911
912
913
914 __attribute__((__nonnull__))
915 static inline
void cx_map_detach_mustr(
916 CxMap *map,
917 cxmutstr key
918 ) {
919 (
void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
920 }
921
922
923
924
925
926
927
928
929 __attribute__((__nonnull__))
930 static inline
void cx_map_detach_str(
931 CxMap *map,
932 const char *key
933 ) {
934 (
void) map->cl->remove(map, cx_hash_key_str(key), false);
935 }
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951 #define cxMapDetach(map, key) _Generic((key), \
952 CxHashKey: cx_map_detach, \
953 cxstring: cx_map_detach_cxstr, \
954 cxmutstr: cx_map_detach_mustr, \
955 char*: cx_map_detach_str, \
956 const char*: cx_map_detach_str) \
957 (map, key)
958
959
960
961
962
963
964
965
966
967 __attribute__((__nonnull__, __warn_unused_result__))
968 static inline
void *cx_map_remove_and_get(
969 CxMap *map,
970 CxHashKey key
971 ) {
972 return map->cl->remove(map, key, !map->collection.store_pointer);
973 }
974
975
976
977
978
979
980
981
982
983 __attribute__((__nonnull__, __warn_unused_result__))
984 static inline
void *cx_map_remove_and_get_cxstr(
985 CxMap *map,
986 cxstring key
987 ) {
988 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
989 }
990
991
992
993
994
995
996
997
998
999 __attribute__((__nonnull__, __warn_unused_result__))
1000 static inline
void *cx_map_remove_and_get_mustr(
1001 CxMap *map,
1002 cxmutstr key
1003 ) {
1004 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015 __attribute__((__nonnull__, __warn_unused_result__))
1016 static inline
void *cx_map_remove_and_get_str(
1017 CxMap *map,
1018 const char *key
1019 ) {
1020 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer);
1021 }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042 #define cxMapRemoveAndGet(map, key) _Generic((key), \
1043 CxHashKey: cx_map_remove_and_get, \
1044 cxstring: cx_map_remove_and_get_cxstr, \
1045 cxmutstr: cx_map_remove_and_get_mustr, \
1046 char*: cx_map_remove_and_get_str, \
1047 const char*: cx_map_remove_and_get_str) \
1048 (map, key)
1049
1050 #endif
1051
1052 #endif
1053