add cxmutstr to the map generics

Fri, 21 Apr 2023 20:17:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Apr 2023 20:17:09 +0200
changeset 692
6ac92936cd44
parent 691
65baf7f45ac8
child 693
494d9b20b99e

add cxmutstr to the map generics

src/cx/map.h file | annotate | diff | comparison | revisions
tests/test_map_generics.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/cx/map.h	Fri Apr 21 19:50:43 2023 +0200
     1.2 +++ b/src/cx/map.h	Fri Apr 21 20:17:09 2023 +0200
     1.3 @@ -361,6 +361,23 @@
     1.4  __attribute__((__nonnull__))
     1.5  static inline int cxMapPut(
     1.6          CxMap *map,
     1.7 +        cxmutstr const &key,
     1.8 +        void *value
     1.9 +) {
    1.10 +    return map->cl->put(map, cx_hash_key_cxstr(key), value);
    1.11 +}
    1.12 +
    1.13 +/**
    1.14 + * Puts a key/value-pair into the map.
    1.15 + *
    1.16 + * @param map the map
    1.17 + * @param key the key
    1.18 + * @param value the value
    1.19 + * @return 0 on success, non-zero value on failure
    1.20 + */
    1.21 +__attribute__((__nonnull__))
    1.22 +static inline int cxMapPut(
    1.23 +        CxMap *map,
    1.24          char const *key,
    1.25          void *value
    1.26  ) {
    1.27 @@ -407,6 +424,21 @@
    1.28  __attribute__((__nonnull__, __warn_unused_result__))
    1.29  static inline void *cxMapGet(
    1.30          CxMap const *map,
    1.31 +        cxmutstr const &key
    1.32 +) {
    1.33 +    return map->cl->get(map, cx_hash_key_cxstr(key));
    1.34 +}
    1.35 +
    1.36 +/**
    1.37 + * Retrieves a value by using a key.
    1.38 + *
    1.39 + * @param map the map
    1.40 + * @param key the key
    1.41 + * @return the value
    1.42 + */
    1.43 +__attribute__((__nonnull__, __warn_unused_result__))
    1.44 +static inline void *cxMapGet(
    1.45 +        CxMap const *map,
    1.46          char const *key
    1.47  ) {
    1.48      return map->cl->get(map, cx_hash_key_str(key));
    1.49 @@ -473,6 +505,28 @@
    1.50  __attribute__((__nonnull__))
    1.51  static inline void cxMapRemove(
    1.52          CxMap *map,
    1.53 +        cxmutstr const &key
    1.54 +) {
    1.55 +    (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
    1.56 +}
    1.57 +
    1.58 +/**
    1.59 + * Removes a key/value-pair from the map by using the key.
    1.60 + *
    1.61 + * Always invokes the destructor function, if any, on the removed element.
    1.62 + * If this map is storing pointers and you just want to retrieve the pointer
    1.63 + * without invoking the destructor, use cxMapRemoveAndGet().
    1.64 + * If you just want to detach the element from the map without invoking the
    1.65 + * destructor or returning the element, use cxMapDetach().
    1.66 + *
    1.67 + * @param map the map
    1.68 + * @param key the key
    1.69 + * @see cxMapRemoveAndGet()
    1.70 + * @see cxMapDetach()
    1.71 + */
    1.72 +__attribute__((__nonnull__))
    1.73 +static inline void cxMapRemove(
    1.74 +        CxMap *map,
    1.75          char const *key
    1.76  ) {
    1.77      (void) map->cl->remove(map, cx_hash_key_str(key), true);
    1.78 @@ -539,6 +593,28 @@
    1.79  __attribute__((__nonnull__))
    1.80  static inline void cxMapDetach(
    1.81          CxMap *map,
    1.82 +        cxmutstr const &key
    1.83 +) {
    1.84 +    (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
    1.85 +}
    1.86 +
    1.87 +/**
    1.88 + * Detaches a key/value-pair from the map by using the key
    1.89 + * without invoking the destructor.
    1.90 + *
    1.91 + * In general, you should only use this function if the map does not own
    1.92 + * the data and there is a valid reference to the data somewhere else
    1.93 + * in the program. In all other cases it is preferable to use
    1.94 + * cxMapRemove() or cxMapRemoveAndGet().
    1.95 + *
    1.96 + * @param map the map
    1.97 + * @param key the key
    1.98 + * @see cxMapRemove()
    1.99 + * @see cxMapRemoveAndGet()
   1.100 + */
   1.101 +__attribute__((__nonnull__))
   1.102 +static inline void cxMapDetach(
   1.103 +        CxMap *map,
   1.104          char const *key
   1.105  ) {
   1.106      (void) map->cl->remove(map, cx_hash_key_str(key), false);
   1.107 @@ -620,6 +696,33 @@
   1.108  __attribute__((__nonnull__, __warn_unused_result__))
   1.109  static inline void *cxMapRemoveAndGet(
   1.110          CxMap *map,
   1.111 +        cxmutstr key
   1.112 +) {
   1.113 +    return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
   1.114 +}
   1.115 +
   1.116 +/**
   1.117 + * Removes a key/value-pair from the map by using the key.
   1.118 + *
   1.119 + * This function can be used when the map is storing pointers,
   1.120 + * in order to retrieve the pointer from the map without invoking
   1.121 + * any destructor function. Sometimes you do not want the pointer
   1.122 + * to be returned - in that case (instead of suppressing the "unused
   1.123 + * result" warning) you can use cxMapDetach().
   1.124 + *
   1.125 + * If this map is not storing pointers, this function behaves like
   1.126 + * cxMapRemove() and returns \c NULL.
   1.127 + *
   1.128 + * @param map the map
   1.129 + * @param key the key
   1.130 + * @return the stored pointer or \c NULL if either the key is not present
   1.131 + * in the map or the map is not storing pointers
   1.132 + * @see cxMapStorePointers()
   1.133 + * @see cxMapDetach()
   1.134 + */
   1.135 +__attribute__((__nonnull__, __warn_unused_result__))
   1.136 +static inline void *cxMapRemoveAndGet(
   1.137 +        CxMap *map,
   1.138          char const *key
   1.139  ) {
   1.140      return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
   1.141 @@ -670,6 +773,23 @@
   1.142   * @return 0 on success, non-zero value on failure
   1.143   */
   1.144  __attribute__((__nonnull__))
   1.145 +static inline int cx_map_put_mustr(
   1.146 +        CxMap *map,
   1.147 +        cxmutstr key,
   1.148 +        void *value
   1.149 +) {
   1.150 +    return map->cl->put(map, cx_hash_key_cxstr(key), value);
   1.151 +}
   1.152 +
   1.153 +/**
   1.154 + * Puts a key/value-pair into the map.
   1.155 + *
   1.156 + * @param map the map
   1.157 + * @param key the key
   1.158 + * @param value the value
   1.159 + * @return 0 on success, non-zero value on failure
   1.160 + */
   1.161 +__attribute__((__nonnull__))
   1.162  static inline int cx_map_put_str(
   1.163          CxMap *map,
   1.164          char const *key,
   1.165 @@ -689,6 +809,7 @@
   1.166  #define cxMapPut(map, key, value) _Generic((key), \
   1.167      CxHashKey: cx_map_put,                        \
   1.168      cxstring: cx_map_put_cxstr,                   \
   1.169 +    cxmutstr: cx_map_put_mustr,                   \
   1.170      char*: cx_map_put_str)                        \
   1.171      (map, key, value)
   1.172  
   1.173 @@ -730,6 +851,21 @@
   1.174   * @return the value
   1.175   */
   1.176  __attribute__((__nonnull__, __warn_unused_result__))
   1.177 +static inline void *cx_map_get_mustr(
   1.178 +        CxMap const *map,
   1.179 +        cxmutstr key
   1.180 +) {
   1.181 +    return map->cl->get(map, cx_hash_key_cxstr(key));
   1.182 +}
   1.183 +
   1.184 +/**
   1.185 + * Retrieves a value by using a key.
   1.186 + *
   1.187 + * @param map the map
   1.188 + * @param key the key
   1.189 + * @return the value
   1.190 + */
   1.191 +__attribute__((__nonnull__, __warn_unused_result__))
   1.192  static inline void *cx_map_get_str(
   1.193          CxMap const *map,
   1.194          char const *key
   1.195 @@ -747,6 +883,7 @@
   1.196  #define cxMapGet(map, key) _Generic((key), \
   1.197      CxHashKey: cx_map_get,                 \
   1.198      cxstring: cx_map_get_cxstr,            \
   1.199 +    cxmutstr: cx_map_get_mustr,            \
   1.200      char*: cx_map_get_str)                 \
   1.201      (map, key)
   1.202  
   1.203 @@ -785,6 +922,20 @@
   1.204   * @param key the key
   1.205   */
   1.206  __attribute__((__nonnull__))
   1.207 +static inline void cx_map_remove_mustr(
   1.208 +        CxMap *map,
   1.209 +        cxmutstr key
   1.210 +) {
   1.211 +    (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
   1.212 +}
   1.213 +
   1.214 +/**
   1.215 + * Removes a key/value-pair from the map by using the key.
   1.216 + *
   1.217 + * @param map the map
   1.218 + * @param key the key
   1.219 + */
   1.220 +__attribute__((__nonnull__))
   1.221  static inline void cx_map_remove_str(
   1.222          CxMap *map,
   1.223          char const *key
   1.224 @@ -809,6 +960,7 @@
   1.225  #define cxMapRemove(map, key) _Generic((key), \
   1.226      CxHashKey: cx_map_remove,                 \
   1.227      cxstring: cx_map_remove_cxstr,            \
   1.228 +    cxmutstr: cx_map_remove_mustr,            \
   1.229      char*: cx_map_remove_str)                 \
   1.230      (map, key)
   1.231  
   1.232 @@ -850,6 +1002,21 @@
   1.233   * @param key the key
   1.234   */
   1.235  __attribute__((__nonnull__))
   1.236 +static inline void cx_map_detach_mustr(
   1.237 +        CxMap *map,
   1.238 +        cxmutstr key
   1.239 +) {
   1.240 +    (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
   1.241 +}
   1.242 +
   1.243 +/**
   1.244 + * Detaches a key/value-pair from the map by using the key
   1.245 + * without invoking the destructor.
   1.246 + *
   1.247 + * @param map the map
   1.248 + * @param key the key
   1.249 + */
   1.250 +__attribute__((__nonnull__))
   1.251  static inline void cx_map_detach_str(
   1.252          CxMap *map,
   1.253          char const *key
   1.254 @@ -874,6 +1041,7 @@
   1.255  #define cxMapDetach(map, key) _Generic((key), \
   1.256      CxHashKey: cx_map_detach,                 \
   1.257      cxstring: cx_map_detach_cxstr,            \
   1.258 +    cxmutstr: cx_map_detach_mustr,            \
   1.259      char*: cx_map_detach_str)                 \
   1.260      (map, key)
   1.261  
   1.262 @@ -918,6 +1086,22 @@
   1.263   * in the map or the map is not storing pointers
   1.264   */
   1.265  __attribute__((__nonnull__, __warn_unused_result__))
   1.266 +static inline void *cx_map_remove_and_get_mustr(
   1.267 +        CxMap *map,
   1.268 +        cxmutstr key
   1.269 +) {
   1.270 +    return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
   1.271 +}
   1.272 +
   1.273 +/**
   1.274 + * Removes a key/value-pair from the map by using the key.
   1.275 + *
   1.276 + * @param map the map
   1.277 + * @param key the key
   1.278 + * @return the stored pointer or \c NULL if either the key is not present
   1.279 + * in the map or the map is not storing pointers
   1.280 + */
   1.281 +__attribute__((__nonnull__, __warn_unused_result__))
   1.282  static inline void *cx_map_remove_and_get_str(
   1.283          CxMap *map,
   1.284          char const *key
   1.285 @@ -947,6 +1131,7 @@
   1.286  #define cxMapRemoveAndGet(map, key) _Generic((key), \
   1.287      CxHashKey: cx_map_remove_and_get,               \
   1.288      cxstring: cx_map_remove_and_get_cxstr,          \
   1.289 +    cxmutstr: cx_map_remove_and_get_mustr,          \
   1.290      char*: cx_map_remove_and_get_str)               \
   1.291      (map, key)
   1.292  
     2.1 --- a/tests/test_map_generics.c	Fri Apr 21 19:50:43 2023 +0200
     2.2 +++ b/tests/test_map_generics.c	Fri Apr 21 20:17:09 2023 +0200
     2.3 @@ -33,7 +33,7 @@
     2.4      CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0);
     2.5  
     2.6      cxMapPut(map, "test", "test");
     2.7 -    cxMapPut(map, CX_STR("foo"), "bar");
     2.8 +    cxMapPut(map, cx_mutstr("foo"), "bar");
     2.9      cxMapPut(map, cx_str("hallo"), "welt");
    2.10  
    2.11      return map;

mercurial