ucx
UAP Common Extensions
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
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
46extern "C" {
47#endif
48
50typedef struct cx_map_s CxMap;
51
54
57
59struct cx_map_s {
63};
64
81};
82
90 __attribute__((__nonnull__))
91 void (*destructor)(struct cx_map_s *map);
92
96 __attribute__((__nonnull__))
97 void (*clear)(struct cx_map_s *map);
98
102 __attribute__((__nonnull__))
103 int (*put)(
104 CxMap *map,
105 CxHashKey key,
106 void *value
107 );
108
112 __attribute__((__nonnull__, __warn_unused_result__))
113 void *(*get)(
114 CxMap const *map,
115 CxHashKey key
116 );
117
121 __attribute__((__nonnull__))
122 void *(*remove)(
123 CxMap *map,
124 CxHashKey key,
125 bool destroy
126 );
127
131 __attribute__((__nonnull__, __warn_unused_result__))
133};
134
146 void *value;
147};
148
154extern CxMap *const cxEmptyMap;
155
165__attribute__((__nonnull__))
166static inline void cxMapStoreObjects(CxMap *map) {
167 map->store_pointer = false;
168}
169
182__attribute__((__nonnull__))
183static inline void cxMapStorePointers(CxMap *map) {
184 map->store_pointer = true;
185 map->item_size = sizeof(void *);
186}
187
188
194__attribute__((__nonnull__))
195static inline void cxMapDestroy(CxMap *map) {
196 map->cl->destructor(map);
197}
198
199
205__attribute__((__nonnull__))
206static inline void cxMapClear(CxMap *map) {
207 map->cl->clear(map);
208}
209
210
211// TODO: set-like map operations (union, intersect, difference)
212
222__attribute__((__nonnull__, __warn_unused_result__))
223static inline CxIterator cxMapIteratorValues(CxMap const *map) {
224 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
225}
226
238__attribute__((__nonnull__, __warn_unused_result__))
239static inline CxIterator cxMapIteratorKeys(CxMap const *map) {
240 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
241}
242
256__attribute__((__nonnull__, __warn_unused_result__))
257static inline CxIterator cxMapIterator(CxMap const *map) {
258 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
259}
260
261
271__attribute__((__nonnull__, __warn_unused_result__))
273
285__attribute__((__nonnull__, __warn_unused_result__))
287
301__attribute__((__nonnull__, __warn_unused_result__))
303
304#ifdef __cplusplus
305} // end the extern "C" block here, because we want to start overloading
306
315__attribute__((__nonnull__))
316static 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
333__attribute__((__nonnull__))
334static 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
350__attribute__((__nonnull__))
351static 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
367__attribute__((__nonnull__))
368static 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
383__attribute__((__nonnull__, __warn_unused_result__))
384static inline void *cxMapGet(
385 CxMap const *map,
386 CxHashKey const &key
387) {
388 return map->cl->get(map, key);
389}
390
398__attribute__((__nonnull__, __warn_unused_result__))
399static 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
413__attribute__((__nonnull__, __warn_unused_result__))
414static 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
428__attribute__((__nonnull__, __warn_unused_result__))
429static 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
450__attribute__((__nonnull__))
451static inline void cxMapRemove(
452 CxMap *map,
453 CxHashKey const &key
454) {
455 (void) map->cl->remove(map, key, true);
456}
457
472__attribute__((__nonnull__))
473static 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
494__attribute__((__nonnull__))
495static 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
516__attribute__((__nonnull__))
517static 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
538__attribute__((__nonnull__))
539static inline void cxMapDetach(
540 CxMap *map,
541 CxHashKey const &key
542) {
543 (void) map->cl->remove(map, key, false);
544}
545
560__attribute__((__nonnull__))
561static 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
582__attribute__((__nonnull__))
583static 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
604__attribute__((__nonnull__))
605static 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
631__attribute__((__nonnull__, __warn_unused_result__))
632static inline void *cxMapRemoveAndGet(
633 CxMap *map,
634 CxHashKey key
635) {
636 return map->cl->remove(map, key, !map->store_pointer);
637}
638
658__attribute__((__nonnull__, __warn_unused_result__))
659static 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
685__attribute__((__nonnull__, __warn_unused_result__))
686static 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
712__attribute__((__nonnull__, __warn_unused_result__))
713static 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 // __cplusplus
721
730__attribute__((__nonnull__))
731static 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
747__attribute__((__nonnull__))
748static 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
764__attribute__((__nonnull__))
765static 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
781__attribute__((__nonnull__))
782static 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
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
813__attribute__((__nonnull__, __warn_unused_result__))
814static inline void *cx_map_get(
815 CxMap const *map,
816 CxHashKey key
817) {
818 return map->cl->get(map, key);
819}
820
828__attribute__((__nonnull__, __warn_unused_result__))
829static 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
843__attribute__((__nonnull__, __warn_unused_result__))
844static 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
858__attribute__((__nonnull__, __warn_unused_result__))
859static 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
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
887__attribute__((__nonnull__))
888static inline void cx_map_remove(
889 CxMap *map,
890 CxHashKey key
891) {
892 (void) map->cl->remove(map, key, true);
893}
894
901__attribute__((__nonnull__))
902static 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
915__attribute__((__nonnull__))
916static 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
929__attribute__((__nonnull__))
930static 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
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
966__attribute__((__nonnull__))
967static inline void cx_map_detach(
968 CxMap *map,
969 CxHashKey key
970) {
971 (void) map->cl->remove(map, key, false);
972}
973
981__attribute__((__nonnull__))
982static 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
996__attribute__((__nonnull__))
997static 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
1011__attribute__((__nonnull__))
1012static 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
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
1049__attribute__((__nonnull__, __warn_unused_result__))
1050static 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
1065__attribute__((__nonnull__, __warn_unused_result__))
1067 CxMap *map,
1068 cxstring key
1069) {
1070 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
1071}
1072
1081__attribute__((__nonnull__, __warn_unused_result__))
1083 CxMap *map,
1084 cxmutstr key
1085) {
1086 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
1087}
1088
1097__attribute__((__nonnull__, __warn_unused_result__))
1098static 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
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 // __cplusplus
1133
1134#endif // UCX_MAP_H
Common definitions for various collection implementations.
#define CX_COLLECTION_MEMBERS
Use this macro to declare common members for a collection structure.
Definition: collection.h:63
Common definitions and feature checks.
#define __attribute__(x)
Removes GNU C attributes where they are not supported.
Definition: common.h:127
Interface for map implementations.
CxHashKey cx_hash_key_str(char const *str)
Computes a hash key from a string.
#define cx_hash_key_cxstr(str)
Computes a hash key from a UCX string.
Definition: hash_key.h:123
CxMutIterator cxMapMutIterator(CxMap *map)
Creates a mutating iterator for a map.
static int cx_map_put(CxMap *map, CxHashKey key, void *value)
Puts a key/value-pair into the map.
Definition: map.h:731
static void * cx_map_remove_and_get_str(CxMap *map, char const *key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:1098
#define cxMapRemoveAndGet(map, key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:1124
static int cx_map_put_cxstr(CxMap *map, cxstring key, void *value)
Puts a key/value-pair into the map.
Definition: map.h:748
static CxIterator cxMapIteratorValues(CxMap const *map)
Creates a value iterator for a map.
Definition: map.h:223
cx_map_iterator_type
The type of iterator for a map.
Definition: map.h:68
@ CX_MAP_ITERATOR_KEYS
Iterates over keys only.
Definition: map.h:76
@ CX_MAP_ITERATOR_PAIRS
Iterates over key/value pairs.
Definition: map.h:72
@ CX_MAP_ITERATOR_VALUES
Iterates over values only.
Definition: map.h:80
static CxIterator cxMapIteratorKeys(CxMap const *map)
Creates a key iterator for a map.
Definition: map.h:239
#define cxMapRemove(map, key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:951
static CxIterator cxMapIterator(CxMap const *map)
Creates an iterator for a map.
Definition: map.h:257
static void * cx_map_remove_and_get_cxstr(CxMap *map, cxstring key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:1066
CxMutIterator cxMapMutIteratorKeys(CxMap *map)
Creates a mutating iterator over the keys of a map.
static void * cx_map_get_mustr(CxMap const *map, cxmutstr key)
Retrieves a value by using a key.
Definition: map.h:844
static void cxMapStorePointers(CxMap *map)
Advises the map to only store pointers to the objects.
Definition: map.h:183
static void cx_map_detach_mustr(CxMap *map, cxmutstr key)
Detaches a key/value-pair from the map by using the key without invoking the destructor.
Definition: map.h:997
CxMutIterator cxMapMutIteratorValues(CxMap *map)
Creates a mutating iterator over the values of a map.
static void cxMapClear(CxMap *map)
Clears a map by removing all elements.
Definition: map.h:206
static void * cx_map_get(CxMap const *map, CxHashKey key)
Retrieves a value by using a key.
Definition: map.h:814
static void cx_map_detach_cxstr(CxMap *map, cxstring key)
Detaches a key/value-pair from the map by using the key without invoking the destructor.
Definition: map.h:982
static void * cx_map_remove_and_get_mustr(CxMap *map, cxmutstr key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:1082
static void cxMapDestroy(CxMap *map)
Deallocates the memory of the specified map.
Definition: map.h:195
static void cx_map_remove_cxstr(CxMap *map, cxstring key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:902
#define cxMapDetach(map, key)
Detaches a key/value-pair from the map by using the key without invoking the destructor.
Definition: map.h:1033
static void cx_map_remove(CxMap *map, CxHashKey key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:888
static void * cx_map_remove_and_get(CxMap *map, CxHashKey key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:1050
static void cx_map_detach_str(CxMap *map, char const *key)
Detaches a key/value-pair from the map by using the key without invoking the destructor.
Definition: map.h:1012
static void cx_map_remove_mustr(CxMap *map, cxmutstr key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:916
static void * cx_map_get_cxstr(CxMap const *map, cxstring key)
Retrieves a value by using a key.
Definition: map.h:829
#define cxMapPut(map, key, value)
Puts a key/value-pair into the map.
Definition: map.h:798
static void cx_map_detach(CxMap *map, CxHashKey key)
Detaches a key/value-pair from the map by using the key without invoking the destructor.
Definition: map.h:967
static int cx_map_put_str(CxMap *map, char const *key, void *value)
Puts a key/value-pair into the map.
Definition: map.h:782
CxMap *const cxEmptyMap
A shared instance of an empty map.
static void * cx_map_get_str(CxMap const *map, char const *key)
Retrieves a value by using a key.
Definition: map.h:859
static void cxMapStoreObjects(CxMap *map)
Advises the map to store copies of the objects (default mode of operation).
Definition: map.h:166
static void cx_map_remove_str(CxMap *map, char const *key)
Removes a key/value-pair from the map by using the key.
Definition: map.h:930
#define cxMapGet(map, key)
Retrieves a value by using a key.
Definition: map.h:873
static int cx_map_put_mustr(CxMap *map, cxmutstr key, void *value)
Puts a key/value-pair into the map.
Definition: map.h:765
Strings that know their length.
Internal structure for a key within a hash map.
Definition: hash_key.h:48
Internal iterator struct - use CxIterator.
Definition: iterator.h:160
The class definition for arbitrary maps.
Definition: map.h:86
void *(* get)(CxMap const *map, CxHashKey key)
Returns an element.
Definition: map.h:113
int(* put)(CxMap *map, CxHashKey key, void *value)
Add or overwrite an element.
Definition: map.h:103
void(* clear)(struct cx_map_s *map)
Removes all elements.
Definition: map.h:97
void *(* remove)(CxMap *map, CxHashKey key, bool destroy)
Removes an element.
Definition: map.h:122
void(* destructor)(struct cx_map_s *map)
Deallocates the entire memory.
Definition: map.h:91
CxIterator(* iterator)(CxMap const *map, enum cx_map_iterator_type type)
Creates an iterator for this map.
Definition: map.h:132
A map entry.
Definition: map.h:138
CxHashKey const * key
A pointer to the key.
Definition: map.h:142
void * value
A pointer to the value.
Definition: map.h:146
Structure for the UCX map.
Definition: map.h:59
CX_COLLECTION_MEMBERS cx_map_class * cl
The map class definition.
Definition: map.h:62
Internal iterator struct - use CxMutIterator.
Definition: iterator.h:96
The UCX string structure.
Definition: string.h:46
The UCX string structure for immutable (constant) strings.
Definition: string.h:65