# HG changeset patch # User Mike Becker # Date 1682101029 -7200 # Node ID 6ac92936cd44acbc87403cd1570e3fab5030eed8 # Parent 65baf7f45ac80786566f0636a4750672ae9edfc9 add cxmutstr to the map generics diff -r 65baf7f45ac8 -r 6ac92936cd44 src/cx/map.h --- a/src/cx/map.h Fri Apr 21 19:50:43 2023 +0200 +++ b/src/cx/map.h Fri Apr 21 20:17:09 2023 +0200 @@ -361,6 +361,23 @@ __attribute__((__nonnull__)) static inline int cxMapPut( CxMap *map, + cxmutstr const &key, + void *value +) { + return map->cl->put(map, cx_hash_key_cxstr(key), value); +} + +/** + * Puts a key/value-pair into the map. + * + * @param map the map + * @param key the key + * @param value the value + * @return 0 on success, non-zero value on failure + */ +__attribute__((__nonnull__)) +static inline int cxMapPut( + CxMap *map, char const *key, void *value ) { @@ -407,6 +424,21 @@ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapGet( CxMap const *map, + cxmutstr const &key +) { + return map->cl->get(map, cx_hash_key_cxstr(key)); +} + +/** + * Retrieves a value by using a key. + * + * @param map the map + * @param key the key + * @return the value + */ +__attribute__((__nonnull__, __warn_unused_result__)) +static inline void *cxMapGet( + CxMap const *map, char const *key ) { return map->cl->get(map, cx_hash_key_str(key)); @@ -473,6 +505,28 @@ __attribute__((__nonnull__)) static inline void cxMapRemove( CxMap *map, + cxmutstr const &key +) { + (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); +} + +/** + * Removes a key/value-pair from the map by using the key. + * + * Always invokes the destructor function, if any, on the removed element. + * If this map is storing pointers and you just want to retrieve the pointer + * without invoking the destructor, use cxMapRemoveAndGet(). + * If you just want to detach the element from the map without invoking the + * destructor or returning the element, use cxMapDetach(). + * + * @param map the map + * @param key the key + * @see cxMapRemoveAndGet() + * @see cxMapDetach() + */ +__attribute__((__nonnull__)) +static inline void cxMapRemove( + CxMap *map, char const *key ) { (void) map->cl->remove(map, cx_hash_key_str(key), true); @@ -539,6 +593,28 @@ __attribute__((__nonnull__)) static inline void cxMapDetach( CxMap *map, + cxmutstr const &key +) { + (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); +} + +/** + * Detaches a key/value-pair from the map by using the key + * without invoking the destructor. + * + * In general, you should only use this function if the map does not own + * the data and there is a valid reference to the data somewhere else + * in the program. In all other cases it is preferable to use + * cxMapRemove() or cxMapRemoveAndGet(). + * + * @param map the map + * @param key the key + * @see cxMapRemove() + * @see cxMapRemoveAndGet() + */ +__attribute__((__nonnull__)) +static inline void cxMapDetach( + CxMap *map, char const *key ) { (void) map->cl->remove(map, cx_hash_key_str(key), false); @@ -620,6 +696,33 @@ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapRemoveAndGet( CxMap *map, + cxmutstr key +) { + return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); +} + +/** + * Removes a key/value-pair from the map by using the key. + * + * This function can be used when the map is storing pointers, + * in order to retrieve the pointer from the map without invoking + * any destructor function. Sometimes you do not want the pointer + * to be returned - in that case (instead of suppressing the "unused + * result" warning) you can use cxMapDetach(). + * + * If this map is not storing pointers, this function behaves like + * cxMapRemove() and returns \c NULL. + * + * @param map the map + * @param key the key + * @return the stored pointer or \c NULL if either the key is not present + * in the map or the map is not storing pointers + * @see cxMapStorePointers() + * @see cxMapDetach() + */ +__attribute__((__nonnull__, __warn_unused_result__)) +static inline void *cxMapRemoveAndGet( + CxMap *map, char const *key ) { return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); @@ -670,6 +773,23 @@ * @return 0 on success, non-zero value on failure */ __attribute__((__nonnull__)) +static inline int cx_map_put_mustr( + CxMap *map, + cxmutstr key, + void *value +) { + return map->cl->put(map, cx_hash_key_cxstr(key), value); +} + +/** + * Puts a key/value-pair into the map. + * + * @param map the map + * @param key the key + * @param value the value + * @return 0 on success, non-zero value on failure + */ +__attribute__((__nonnull__)) static inline int cx_map_put_str( CxMap *map, char const *key, @@ -689,6 +809,7 @@ #define cxMapPut(map, key, value) _Generic((key), \ CxHashKey: cx_map_put, \ cxstring: cx_map_put_cxstr, \ + cxmutstr: cx_map_put_mustr, \ char*: cx_map_put_str) \ (map, key, value) @@ -730,6 +851,21 @@ * @return the value */ __attribute__((__nonnull__, __warn_unused_result__)) +static inline void *cx_map_get_mustr( + CxMap const *map, + cxmutstr key +) { + return map->cl->get(map, cx_hash_key_cxstr(key)); +} + +/** + * Retrieves a value by using a key. + * + * @param map the map + * @param key the key + * @return the value + */ +__attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_get_str( CxMap const *map, char const *key @@ -747,6 +883,7 @@ #define cxMapGet(map, key) _Generic((key), \ CxHashKey: cx_map_get, \ cxstring: cx_map_get_cxstr, \ + cxmutstr: cx_map_get_mustr, \ char*: cx_map_get_str) \ (map, key) @@ -785,6 +922,20 @@ * @param key the key */ __attribute__((__nonnull__)) +static inline void cx_map_remove_mustr( + CxMap *map, + cxmutstr key +) { + (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); +} + +/** + * Removes a key/value-pair from the map by using the key. + * + * @param map the map + * @param key the key + */ +__attribute__((__nonnull__)) static inline void cx_map_remove_str( CxMap *map, char const *key @@ -809,6 +960,7 @@ #define cxMapRemove(map, key) _Generic((key), \ CxHashKey: cx_map_remove, \ cxstring: cx_map_remove_cxstr, \ + cxmutstr: cx_map_remove_mustr, \ char*: cx_map_remove_str) \ (map, key) @@ -850,6 +1002,21 @@ * @param key the key */ __attribute__((__nonnull__)) +static inline void cx_map_detach_mustr( + CxMap *map, + cxmutstr key +) { + (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); +} + +/** + * Detaches a key/value-pair from the map by using the key + * without invoking the destructor. + * + * @param map the map + * @param key the key + */ +__attribute__((__nonnull__)) static inline void cx_map_detach_str( CxMap *map, char const *key @@ -874,6 +1041,7 @@ #define cxMapDetach(map, key) _Generic((key), \ CxHashKey: cx_map_detach, \ cxstring: cx_map_detach_cxstr, \ + cxmutstr: cx_map_detach_mustr, \ char*: cx_map_detach_str) \ (map, key) @@ -918,6 +1086,22 @@ * in the map or the map is not storing pointers */ __attribute__((__nonnull__, __warn_unused_result__)) +static inline void *cx_map_remove_and_get_mustr( + CxMap *map, + cxmutstr key +) { + return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); +} + +/** + * Removes a key/value-pair from the map by using the key. + * + * @param map the map + * @param key the key + * @return the stored pointer or \c NULL if either the key is not present + * in the map or the map is not storing pointers + */ +__attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_remove_and_get_str( CxMap *map, char const *key @@ -947,6 +1131,7 @@ #define cxMapRemoveAndGet(map, key) _Generic((key), \ CxHashKey: cx_map_remove_and_get, \ cxstring: cx_map_remove_and_get_cxstr, \ + cxmutstr: cx_map_remove_and_get_mustr, \ char*: cx_map_remove_and_get_str) \ (map, key) diff -r 65baf7f45ac8 -r 6ac92936cd44 tests/test_map_generics.c --- a/tests/test_map_generics.c Fri Apr 21 19:50:43 2023 +0200 +++ b/tests/test_map_generics.c Fri Apr 21 20:17:09 2023 +0200 @@ -33,7 +33,7 @@ CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0); cxMapPut(map, "test", "test"); - cxMapPut(map, CX_STR("foo"), "bar"); + cxMapPut(map, cx_mutstr("foo"), "bar"); cxMapPut(map, cx_str("hallo"), "welt"); return map;