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_iterator_s CxMapIterator;
56
57
58 typedef struct cx_map_class_s cx_map_class;
59
60
61 struct cx_map_s {
62
63
64
65 CX_COLLECTION_BASE;
66
67 cx_map_class *cl;
68 };
69
70
71
72
73 struct cx_map_entry_s {
74
75
76
77 const CxHashKey *key;
78
79
80
81 void *value;
82 };
83
84
85
86
87 enum cx_map_iterator_type {
88
89
90
91 CX_MAP_ITERATOR_PAIRS,
92
93
94
95 CX_MAP_ITERATOR_KEYS,
96
97
98
99 CX_MAP_ITERATOR_VALUES
100 };
101
102
103
104
105 struct cx_map_iterator_s {
106
107
108
109 CX_ITERATOR_BASE;
110
111
112
113
114 union {
115
116
117
118 CxMap *m;
119
120
121
122 const CxMap *c;
123 } map;
124
125
126
127
128
129
130 void *elem;
131
132
133
134
135
136
137
138 CxMapEntry entry;
139
140
141
142
143
144
145 size_t slot;
146
147
148
149
150
151 size_t index;
152
153
154
155
156 size_t elem_size;
157
158
159
160
161
162
163
164 size_t elem_count;
165
166
167
168
169 enum cx_map_iterator_type type;
170 };
171
172
173
174
175 struct cx_map_class_s {
176
177
178
179 void (*deallocate)(
struct cx_map_s *map);
180
181
182
183
184 void (*clear)(
struct cx_map_s *map);
185
186
187
188
189 int (*put)(
190 CxMap *map,
191 CxHashKey key,
192 void *value
193 );
194
195
196
197
198 void *(*get)(
199 const CxMap *map,
200 CxHashKey key
201 );
202
203
204
205
206
207
208
209
210
211
212
213 int (*remove)(
214 CxMap *map,
215 CxHashKey key,
216 void *targetbuf
217 );
218
219
220
221
222 CxMapIterator (*iterator)(
const CxMap *map,
enum cx_map_iterator_type type);
223 };
224
225
226
227
228
229
230
231
232
233 cx_attr_export
234 extern CxMap *
const cxEmptyMap;
235
236
237
238
239
240
241
242
243 cx_attr_export
244 void cxMapFree(CxMap *map);
245
246
247
248
249
250
251
252
253
254 cx_attr_nonnull
255 static inline
void cxMapClear(CxMap *map) {
256 map->cl->clear(map);
257 }
258
259
260
261
262
263
264
265 cx_attr_nonnull
266 static inline
size_t cxMapSize(
const CxMap *map) {
267 return map->collection.size;
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 cx_attr_nonnull
284 cx_attr_nodiscard
285 static inline CxMapIterator cxMapIteratorValues(
const CxMap *map) {
286 return map->cl->iterator(map,
CX_MAP_ITERATOR_VALUES);
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300
301 cx_attr_nonnull
302 cx_attr_nodiscard
303 static inline CxMapIterator cxMapIteratorKeys(
const CxMap *map) {
304 return map->cl->iterator(map,
CX_MAP_ITERATOR_KEYS);
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 cx_attr_nonnull
322 cx_attr_nodiscard
323 static inline CxMapIterator cxMapIterator(
const CxMap *map) {
324 return map->cl->iterator(map,
CX_MAP_ITERATOR_PAIRS);
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 cx_attr_nonnull
342 cx_attr_nodiscard
343 cx_attr_export
344 CxMapIterator cxMapMutIteratorValues(CxMap *map);
345
346
347
348
349
350
351
352
353
354
355
356
357
358 cx_attr_nonnull
359 cx_attr_nodiscard
360 cx_attr_export
361 CxMapIterator cxMapMutIteratorKeys(CxMap *map);
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 cx_attr_nonnull
378 cx_attr_nodiscard
379 cx_attr_export
380 CxMapIterator cxMapMutIterator(CxMap *map);
381
382 #ifdef __cplusplus
383 }
384 cx_attr_nonnull
385 static inline
int cxMapPut(
386 CxMap *map,
387 CxHashKey
const &key,
388 void *value
389 ) {
390 return map->cl->put(map, key, value);
391 }
392
393 cx_attr_nonnull
394 static inline
int cxMapPut(
395 CxMap *map,
396 cxstring
const &key,
397 void *value
398 ) {
399 return map->cl->put(map, cx_hash_key_cxstr(key), value);
400 }
401
402 cx_attr_nonnull
403 static inline
int cxMapPut(
404 CxMap *map,
405 cxmutstr
const &key,
406 void *value
407 ) {
408 return map->cl->put(map, cx_hash_key_cxstr(key), value);
409 }
410
411 cx_attr_nonnull
412 cx_attr_cstr_arg(
2)
413 static inline
int cxMapPut(
414 CxMap *map,
415 const char *key,
416 void *value
417 ) {
418 return map->cl->put(map, cx_hash_key_str(key), value);
419 }
420
421 cx_attr_nonnull
422 cx_attr_nodiscard
423 static inline
void *cxMapGet(
424 const CxMap *map,
425 CxHashKey
const &key
426 ) {
427 return map->cl->get(map, key);
428 }
429
430 cx_attr_nonnull
431 cx_attr_nodiscard
432 static inline
void *cxMapGet(
433 const CxMap *map,
434 cxstring
const &key
435 ) {
436 return map->cl->get(map, cx_hash_key_cxstr(key));
437 }
438
439 cx_attr_nonnull
440 cx_attr_nodiscard
441 static inline
void *cxMapGet(
442 const CxMap *map,
443 cxmutstr
const &key
444 ) {
445 return map->cl->get(map, cx_hash_key_cxstr(key));
446 }
447
448 cx_attr_nonnull
449 cx_attr_nodiscard
450 cx_attr_cstr_arg(
2)
451 static inline
void *cxMapGet(
452 const CxMap *map,
453 const char *key
454 ) {
455 return map->cl->get(map, cx_hash_key_str(key));
456 }
457
458 cx_attr_nonnull
459 static inline
int cxMapRemove(
460 CxMap *map,
461 CxHashKey
const &key
462 ) {
463 return map->cl->remove(map, key, nullptr);
464 }
465
466 cx_attr_nonnull
467 static inline
int cxMapRemove(
468 CxMap *map,
469 cxstring
const &key
470 ) {
471 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
472 }
473
474 cx_attr_nonnull
475 static inline
int cxMapRemove(
476 CxMap *map,
477 cxmutstr
const &key
478 ) {
479 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
480 }
481
482 cx_attr_nonnull
483 cx_attr_cstr_arg(
2)
484 static inline
int cxMapRemove(
485 CxMap *map,
486 const char *key
487 ) {
488 return map->cl->remove(map, cx_hash_key_str(key), nullptr);
489 }
490
491 cx_attr_nonnull
492 cx_attr_access_w(
3)
493 static inline
int cxMapRemoveAndGet(
494 CxMap *map,
495 CxHashKey key,
496 void *targetbuf
497 ) {
498 return map->cl->remove(map, key, targetbuf);
499 }
500
501 cx_attr_nonnull
502 cx_attr_access_w(
3)
503 static inline
int cxMapRemoveAndGet(
504 CxMap *map,
505 cxstring key,
506 void *targetbuf
507 ) {
508 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
509 }
510
511 cx_attr_nonnull
512 cx_attr_access_w(
3)
513 static inline
int cxMapRemoveAndGet(
514 CxMap *map,
515 cxmutstr key,
516 void *targetbuf
517 ) {
518 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
519 }
520
521 cx_attr_nonnull
522 cx_attr_access_w(
3)
523 cx_attr_cstr_arg(
2)
524 static inline
int cxMapRemoveAndGet(
525 CxMap *map,
526 const char *key,
527 void *targetbuf
528 ) {
529 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
530 }
531
532 #else
533
534
535
536
537 cx_attr_nonnull
538 static inline
int cx_map_put(
539 CxMap *map,
540 CxHashKey key,
541 void *value
542 ) {
543 return map->cl->put(map, key, value);
544 }
545
546
547
548
549 cx_attr_nonnull
550 static inline
int cx_map_put_cxstr(
551 CxMap *map,
552 cxstring key,
553 void *value
554 ) {
555 return map->cl->put(map, cx_hash_key_cxstr(key), value);
556 }
557
558
559
560
561 cx_attr_nonnull
562 static inline
int cx_map_put_mustr(
563 CxMap *map,
564 cxmutstr key,
565 void *value
566 ) {
567 return map->cl->put(map, cx_hash_key_cxstr(key), value);
568 }
569
570
571
572
573 cx_attr_nonnull
574 cx_attr_cstr_arg(
2)
575 static inline
int cx_map_put_str(
576 CxMap *map,
577 const char *key,
578 void *value
579 ) {
580 return map->cl->put(map, cx_hash_key_str(key), value);
581 }
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602 #define cxMapPut(map, key, value) _Generic((key), \
603 CxHashKey: cx_map_put, \
604 cxstring: cx_map_put_cxstr, \
605 cxmutstr: cx_map_put_mustr, \
606 char*: cx_map_put_str, \
607 const char*: cx_map_put_str) \
608 (map, key, value)
609
610
611
612
613 cx_attr_nonnull
614 cx_attr_nodiscard
615 static inline
void *cx_map_get(
616 const CxMap *map,
617 CxHashKey key
618 ) {
619 return map->cl->get(map, key);
620 }
621
622
623
624
625 cx_attr_nonnull
626 cx_attr_nodiscard
627 static inline
void *cx_map_get_cxstr(
628 const CxMap *map,
629 cxstring key
630 ) {
631 return map->cl->get(map, cx_hash_key_cxstr(key));
632 }
633
634
635
636
637 cx_attr_nonnull
638 cx_attr_nodiscard
639 static inline
void *cx_map_get_mustr(
640 const CxMap *map,
641 cxmutstr key
642 ) {
643 return map->cl->get(map, cx_hash_key_cxstr(key));
644 }
645
646
647
648
649 cx_attr_nonnull
650 cx_attr_nodiscard
651 cx_attr_cstr_arg(
2)
652 static inline
void *cx_map_get_str(
653 const CxMap *map,
654 const char *key
655 ) {
656 return map->cl->get(map, cx_hash_key_str(key));
657 }
658
659
660
661
662
663
664
665
666
667
668
669
670 #define cxMapGet(map, key) _Generic((key), \
671 CxHashKey: cx_map_get, \
672 cxstring: cx_map_get_cxstr, \
673 cxmutstr: cx_map_get_mustr, \
674 char*: cx_map_get_str, \
675 const char*: cx_map_get_str) \
676 (map, key)
677
678
679
680
681 cx_attr_nonnull
682 static inline
int cx_map_remove(
683 CxMap *map,
684 CxHashKey key
685 ) {
686 return map->cl->remove(map, key,
NULL);
687 }
688
689
690
691
692 cx_attr_nonnull
693 static inline
int cx_map_remove_cxstr(
694 CxMap *map,
695 cxstring key
696 ) {
697 return map->cl->remove(map, cx_hash_key_cxstr(key),
NULL);
698 }
699
700
701
702
703 cx_attr_nonnull
704 static inline
int cx_map_remove_mustr(
705 CxMap *map,
706 cxmutstr key
707 ) {
708 return map->cl->remove(map, cx_hash_key_cxstr(key),
NULL);
709 }
710
711
712
713
714 cx_attr_nonnull
715 cx_attr_cstr_arg(
2)
716 static inline
int cx_map_remove_str(
717 CxMap *map,
718 const char *key
719 ) {
720 return map->cl->remove(map, cx_hash_key_str(key),
NULL);
721 }
722
723
724
725
726
727
728
729
730
731
732
733
734
735 #define cxMapRemove(map, key) _Generic((key), \
736 CxHashKey: cx_map_remove, \
737 cxstring: cx_map_remove_cxstr, \
738 cxmutstr: cx_map_remove_mustr, \
739 char*: cx_map_remove_str, \
740 const char*: cx_map_remove_str) \
741 (map, key)
742
743
744
745
746 cx_attr_nonnull
747 cx_attr_access_w(
3)
748 static inline
int cx_map_remove_and_get(
749 CxMap *map,
750 CxHashKey key,
751 void *targetbuf
752 ) {
753 return map->cl->remove(map, key, targetbuf);
754 }
755
756
757
758
759 cx_attr_nonnull
760 cx_attr_access_w(
3)
761 static inline
int cx_map_remove_and_get_cxstr(
762 CxMap *map,
763 cxstring key,
764 void *targetbuf
765 ) {
766 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
767 }
768
769
770
771
772 cx_attr_nonnull
773 cx_attr_access_w(
3)
774 static inline
int cx_map_remove_and_get_mustr(
775 CxMap *map,
776 cxmutstr key,
777 void *targetbuf
778 ) {
779 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
780 }
781
782
783
784
785 cx_attr_nonnull
786 cx_attr_access_w(
3)
787 cx_attr_cstr_arg(
2)
788 static inline
int cx_map_remove_and_get_str(
789 CxMap *map,
790 const char *key,
791 void *targetbuf
792 ) {
793 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
794 }
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815 #define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \
816 CxHashKey: cx_map_remove_and_get, \
817 cxstring: cx_map_remove_and_get_cxstr, \
818 cxmutstr: cx_map_remove_and_get_mustr, \
819 char*: cx_map_remove_and_get_str, \
820 const char*: cx_map_remove_and_get_str) \
821 (map, key, targetbuf)
822
823 #endif
824
825 #endif
826