Tue, 18 Apr 2023 19:10:45 +0200
add destructor functions for maps - fixes #253
universe@549 | 1 | /* |
universe@549 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@549 | 3 | * |
universe@549 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@549 | 5 | * |
universe@549 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@549 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@549 | 8 | * |
universe@549 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@549 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@549 | 11 | * |
universe@549 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@549 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@549 | 14 | * documentation and/or other materials provided with the distribution. |
universe@549 | 15 | * |
universe@549 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@549 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@549 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@549 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@549 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@549 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@549 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@549 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@549 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@549 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@549 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@549 | 27 | */ |
universe@549 | 28 | /** |
universe@549 | 29 | * \file map.h |
universe@549 | 30 | * \brief Interface for map implementations. |
universe@549 | 31 | * \author Mike Becker |
universe@549 | 32 | * \author Olaf Wintermann |
universe@549 | 33 | * \version 3.0 |
universe@549 | 34 | * \copyright 2-Clause BSD License |
universe@549 | 35 | */ |
universe@549 | 36 | |
universe@549 | 37 | #ifndef UCX_MAP_H |
universe@549 | 38 | #define UCX_MAP_H |
universe@549 | 39 | |
universe@681 | 40 | #include "common.h" |
universe@677 | 41 | #include "collection.h" |
universe@563 | 42 | #include "hash_key.h" |
universe@549 | 43 | |
universe@549 | 44 | #ifdef __cplusplus |
universe@549 | 45 | extern "C" { |
universe@549 | 46 | #endif |
universe@549 | 47 | |
universe@549 | 48 | /** Type for the UCX map. */ |
universe@549 | 49 | typedef struct cx_map_s CxMap; |
universe@549 | 50 | |
universe@549 | 51 | /** Type for a map entry. */ |
universe@549 | 52 | typedef struct cx_map_entry_s CxMapEntry; |
universe@549 | 53 | |
universe@549 | 54 | /** Type for map class definitions. */ |
universe@549 | 55 | typedef struct cx_map_class_s cx_map_class; |
universe@549 | 56 | |
universe@549 | 57 | /** Structure for the UCX map. */ |
universe@549 | 58 | struct cx_map_s { |
universe@685 | 59 | CX_COLLECTION_MEMBERS |
universe@549 | 60 | /** The map class definition. */ |
universe@549 | 61 | cx_map_class *cl; |
universe@549 | 62 | }; |
universe@549 | 63 | |
universe@549 | 64 | /** |
universe@549 | 65 | * The class definition for arbitrary maps. |
universe@549 | 66 | */ |
universe@549 | 67 | struct cx_map_class_s { |
universe@549 | 68 | /** |
universe@549 | 69 | * Deallocates the entire memory. |
universe@549 | 70 | */ |
universe@549 | 71 | __attribute__((__nonnull__)) |
universe@549 | 72 | void (*destructor)(struct cx_map_s *map); |
universe@549 | 73 | |
universe@549 | 74 | /** |
universe@549 | 75 | * Removes all elements. |
universe@549 | 76 | */ |
universe@549 | 77 | __attribute__((__nonnull__)) |
universe@549 | 78 | void (*clear)(struct cx_map_s *map); |
universe@549 | 79 | |
universe@549 | 80 | /** |
universe@549 | 81 | * Add or overwrite an element. |
universe@549 | 82 | */ |
universe@549 | 83 | __attribute__((__nonnull__)) |
universe@549 | 84 | int (*put)( |
universe@549 | 85 | CxMap *map, |
universe@563 | 86 | CxHashKey key, |
universe@550 | 87 | void *value |
universe@549 | 88 | ); |
universe@549 | 89 | |
universe@549 | 90 | /** |
universe@549 | 91 | * Returns an element. |
universe@549 | 92 | */ |
universe@549 | 93 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@549 | 94 | void *(*get)( |
universe@549 | 95 | CxMap const *map, |
universe@563 | 96 | CxHashKey key |
universe@549 | 97 | ); |
universe@549 | 98 | |
universe@549 | 99 | /** |
universe@549 | 100 | * Removes an element. |
universe@549 | 101 | */ |
universe@659 | 102 | __attribute__((__nonnull__)) |
universe@549 | 103 | void *(*remove)( |
universe@550 | 104 | CxMap *map, |
universe@686 | 105 | CxHashKey key, |
universe@686 | 106 | bool destroy |
universe@549 | 107 | ); |
universe@549 | 108 | |
universe@549 | 109 | /** |
universe@549 | 110 | * Iterator over the key/value pairs. |
universe@549 | 111 | */ |
universe@549 | 112 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 113 | CxIterator (*iterator)(CxMap const *map); |
universe@549 | 114 | |
universe@549 | 115 | /** |
universe@549 | 116 | * Iterator over the keys. |
universe@549 | 117 | */ |
universe@549 | 118 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 119 | CxIterator (*iterator_keys)(CxMap const *map); |
universe@549 | 120 | |
universe@549 | 121 | /** |
universe@549 | 122 | * Iterator over the values. |
universe@549 | 123 | */ |
universe@549 | 124 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 125 | CxIterator (*iterator_values)(CxMap const *map); |
universe@630 | 126 | |
universe@630 | 127 | /** |
universe@630 | 128 | * Mutating iterator over the key/value pairs. |
universe@630 | 129 | */ |
universe@630 | 130 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 131 | CxMutIterator (*mut_iterator)(CxMap *map); |
universe@630 | 132 | |
universe@630 | 133 | /** |
universe@630 | 134 | * Mutating iterator over the keys. |
universe@630 | 135 | */ |
universe@630 | 136 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 137 | CxMutIterator (*mut_iterator_keys)(CxMap *map); |
universe@630 | 138 | |
universe@630 | 139 | /** |
universe@630 | 140 | * Mutating iterator over the values. |
universe@630 | 141 | */ |
universe@630 | 142 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 143 | CxMutIterator (*mut_iterator_values)(CxMap *map); |
universe@549 | 144 | }; |
universe@549 | 145 | |
universe@549 | 146 | /** |
universe@549 | 147 | * A map entry. |
universe@549 | 148 | */ |
universe@549 | 149 | struct cx_map_entry_s { |
universe@549 | 150 | /** |
universe@551 | 151 | * A pointer to the key. |
universe@549 | 152 | */ |
universe@563 | 153 | CxHashKey const *key; |
universe@549 | 154 | /** |
universe@551 | 155 | * A pointer to the value. |
universe@549 | 156 | */ |
universe@551 | 157 | void *value; |
universe@549 | 158 | }; |
universe@549 | 159 | |
universe@658 | 160 | /** |
universe@658 | 161 | * Advises the map to store copies of the objects (default mode of operation). |
universe@658 | 162 | * |
universe@658 | 163 | * Retrieving objects from this map will yield pointers to the copies stored |
universe@658 | 164 | * within this list. |
universe@658 | 165 | * |
universe@658 | 166 | * @param map the map |
universe@658 | 167 | * @see cxMapStorePointers() |
universe@658 | 168 | */ |
universe@658 | 169 | __attribute__((__nonnull__)) |
universe@658 | 170 | static inline void cxMapStoreObjects(CxMap *map) { |
universe@685 | 171 | map->store_pointer = false; |
universe@658 | 172 | } |
universe@658 | 173 | |
universe@658 | 174 | /** |
universe@658 | 175 | * Advises the map to only store pointers to the objects. |
universe@658 | 176 | * |
universe@658 | 177 | * Retrieving objects from this list will yield the original pointers stored. |
universe@658 | 178 | * |
universe@658 | 179 | * @note This function forcibly sets the element size to the size of a pointer. |
universe@658 | 180 | * Invoking this function on a non-empty map that already stores copies of |
universe@658 | 181 | * objects is undefined. |
universe@658 | 182 | * |
universe@658 | 183 | * @param map the map |
universe@658 | 184 | * @see cxMapStoreObjects() |
universe@658 | 185 | */ |
universe@658 | 186 | __attribute__((__nonnull__)) |
universe@658 | 187 | static inline void cxMapStorePointers(CxMap *map) { |
universe@685 | 188 | map->store_pointer = true; |
universe@677 | 189 | map->item_size = sizeof(void *); |
universe@658 | 190 | } |
universe@658 | 191 | |
universe@549 | 192 | |
universe@549 | 193 | /** |
universe@549 | 194 | * Deallocates the memory of the specified map. |
universe@549 | 195 | * |
universe@549 | 196 | * @param map the map to be destroyed |
universe@549 | 197 | */ |
universe@549 | 198 | __attribute__((__nonnull__)) |
universe@549 | 199 | static inline void cxMapDestroy(CxMap *map) { |
universe@549 | 200 | map->cl->destructor(map); |
universe@549 | 201 | } |
universe@549 | 202 | |
universe@549 | 203 | |
universe@549 | 204 | /** |
universe@549 | 205 | * Clears a map by removing all elements. |
universe@549 | 206 | * |
universe@549 | 207 | * @param map the map to be cleared |
universe@549 | 208 | */ |
universe@549 | 209 | __attribute__((__nonnull__)) |
universe@549 | 210 | static inline void cxMapClear(CxMap *map) { |
universe@549 | 211 | map->cl->clear(map); |
universe@549 | 212 | } |
universe@549 | 213 | |
universe@549 | 214 | /** |
universe@549 | 215 | * Puts a key/value-pair into the map. |
universe@549 | 216 | * |
universe@549 | 217 | * @param map the map |
universe@549 | 218 | * @param key the key |
universe@549 | 219 | * @param value the value |
universe@549 | 220 | * @return 0 on success, non-zero value on failure |
universe@549 | 221 | */ |
universe@549 | 222 | __attribute__((__nonnull__)) |
universe@549 | 223 | static inline int cxMapPut( |
universe@549 | 224 | CxMap *map, |
universe@563 | 225 | CxHashKey key, |
universe@550 | 226 | void *value |
universe@549 | 227 | ) { |
universe@549 | 228 | return map->cl->put(map, key, value); |
universe@549 | 229 | } |
universe@549 | 230 | |
universe@549 | 231 | /** |
universe@549 | 232 | * Retrieves a value by using a key. |
universe@549 | 233 | * |
universe@549 | 234 | * @param map the map |
universe@549 | 235 | * @param key the key |
universe@549 | 236 | * @return the value |
universe@549 | 237 | */ |
universe@549 | 238 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@553 | 239 | static inline void *cxMapGet( |
universe@549 | 240 | CxMap const *map, |
universe@563 | 241 | CxHashKey key |
universe@549 | 242 | ) { |
universe@549 | 243 | return map->cl->get(map, key); |
universe@549 | 244 | } |
universe@549 | 245 | |
universe@549 | 246 | /** |
universe@549 | 247 | * Removes a key/value-pair from the map by using the key. |
universe@549 | 248 | * |
universe@686 | 249 | * Always invokes the destructor function, if any, on the removed element. |
universe@686 | 250 | * If this map is storing pointers and you just want to retrieve the pointer |
universe@686 | 251 | * without invoking the destructor, use cxMapRemoveAndGet(). |
universe@686 | 252 | * If you just want to detach the element from the map without invoking the |
universe@686 | 253 | * destructor or returning the element, use cxMapDetach(). |
universe@659 | 254 | * |
universe@549 | 255 | * @param map the map |
universe@549 | 256 | * @param key the key |
universe@659 | 257 | * @see cxMapRemoveAndGet() |
universe@686 | 258 | * @see cxMapDetach() |
universe@659 | 259 | */ |
universe@659 | 260 | __attribute__((__nonnull__)) |
universe@659 | 261 | static inline void cxMapRemove( |
universe@659 | 262 | CxMap *map, |
universe@659 | 263 | CxHashKey key |
universe@659 | 264 | ) { |
universe@686 | 265 | (void) map->cl->remove(map, key, true); |
universe@686 | 266 | } |
universe@686 | 267 | |
universe@686 | 268 | /** |
universe@686 | 269 | * Detaches a key/value-pair from the map by using the key |
universe@686 | 270 | * without invoking the destructor. |
universe@686 | 271 | * |
universe@686 | 272 | * In general, you should only use this function if the map does not own |
universe@686 | 273 | * the data and there is a valid reference to the data somewhere else |
universe@686 | 274 | * in the program. In all other cases it is prefarable to use |
universe@686 | 275 | * cxMapRemove() or cxMapRemoveAndGet(). |
universe@686 | 276 | * |
universe@686 | 277 | * @param map the map |
universe@686 | 278 | * @param key the key |
universe@686 | 279 | * @see cxMapRemove() |
universe@686 | 280 | * @see cxMapRemoveAndGet() |
universe@686 | 281 | */ |
universe@686 | 282 | __attribute__((__nonnull__)) |
universe@686 | 283 | static inline void cxMapDetach( |
universe@686 | 284 | CxMap *map, |
universe@686 | 285 | CxHashKey key |
universe@686 | 286 | ) { |
universe@686 | 287 | (void) map->cl->remove(map, key, false); |
universe@659 | 288 | } |
universe@659 | 289 | |
universe@659 | 290 | /** |
universe@659 | 291 | * Removes a key/value-pair from the map by using the key. |
universe@659 | 292 | * |
universe@686 | 293 | * This function can be used when the map is storing pointers, |
universe@686 | 294 | * in order to retrieve the pointer from the map without invoking |
universe@686 | 295 | * any destructor function. Sometimes you do not want the pointer |
universe@686 | 296 | * to be returned - in that case (instead of suppressing the "unused |
universe@686 | 297 | * result" warning) you can use cxMapDetach(). |
universe@686 | 298 | * |
universe@686 | 299 | * If this map is not storing pointers, this function behaves like |
universe@686 | 300 | * cxMapRemove() and returns \c NULL. |
universe@659 | 301 | * |
universe@659 | 302 | * @param map the map |
universe@659 | 303 | * @param key the key |
universe@659 | 304 | * @return the stored pointer or \c NULL if either the key is not present |
universe@659 | 305 | * in the map or the map is not storing pointers |
universe@658 | 306 | * @see cxMapStorePointers() |
universe@686 | 307 | * @see cxMapDetach() |
universe@549 | 308 | */ |
universe@549 | 309 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@659 | 310 | static inline void *cxMapRemoveAndGet( |
universe@549 | 311 | CxMap *map, |
universe@563 | 312 | CxHashKey key |
universe@549 | 313 | ) { |
universe@686 | 314 | return map->cl->remove(map, key, !map->store_pointer); |
universe@549 | 315 | } |
universe@549 | 316 | |
universe@549 | 317 | // TODO: set-like map operations (union, intersect, difference) |
universe@549 | 318 | |
universe@549 | 319 | /** |
universe@549 | 320 | * Creates a value iterator for a map. |
universe@549 | 321 | * |
universe@549 | 322 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 323 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 324 | * |
universe@549 | 325 | * @param map the map to create the iterator for |
universe@549 | 326 | * @return an iterator for the currently stored values |
universe@549 | 327 | */ |
universe@549 | 328 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 329 | static inline CxIterator cxMapIteratorValues(CxMap *map) { |
universe@549 | 330 | return map->cl->iterator_values(map); |
universe@549 | 331 | } |
universe@549 | 332 | |
universe@549 | 333 | /** |
universe@549 | 334 | * Creates a key iterator for a map. |
universe@549 | 335 | * |
universe@564 | 336 | * The elements of the iterator are keys of type CxHashKey. |
universe@555 | 337 | * |
universe@549 | 338 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 339 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 340 | * |
universe@549 | 341 | * @param map the map to create the iterator for |
universe@549 | 342 | * @return an iterator for the currently stored keys |
universe@549 | 343 | */ |
universe@549 | 344 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 345 | static inline CxIterator cxMapIteratorKeys(CxMap *map) { |
universe@549 | 346 | return map->cl->iterator_keys(map); |
universe@549 | 347 | } |
universe@549 | 348 | |
universe@549 | 349 | /** |
universe@549 | 350 | * Creates an iterator for a map. |
universe@549 | 351 | * |
universe@555 | 352 | * The elements of the iterator are key/value pairs of type CxMapEntry. |
universe@549 | 353 | * |
universe@549 | 354 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 355 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 356 | * |
universe@549 | 357 | * @param map the map to create the iterator for |
universe@555 | 358 | * @return an iterator for the currently stored entries |
universe@549 | 359 | * @see cxMapIteratorKeys() |
universe@549 | 360 | * @see cxMapIteratorValues() |
universe@549 | 361 | */ |
universe@549 | 362 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 363 | static inline CxIterator cxMapIterator(CxMap *map) { |
universe@549 | 364 | return map->cl->iterator(map); |
universe@549 | 365 | } |
universe@549 | 366 | |
universe@630 | 367 | |
universe@630 | 368 | /** |
universe@630 | 369 | * Creates a mutating iterator over the values of a map. |
universe@630 | 370 | * |
universe@630 | 371 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@630 | 372 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@630 | 373 | * |
universe@630 | 374 | * @param map the map to create the iterator for |
universe@630 | 375 | * @return an iterator for the currently stored values |
universe@630 | 376 | */ |
universe@630 | 377 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 378 | static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) { |
universe@630 | 379 | return map->cl->mut_iterator_values(map); |
universe@630 | 380 | } |
universe@630 | 381 | |
universe@630 | 382 | /** |
universe@630 | 383 | * Creates a mutating iterator over the keys of a map. |
universe@630 | 384 | * |
universe@630 | 385 | * The elements of the iterator are keys of type CxHashKey. |
universe@630 | 386 | * |
universe@630 | 387 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@630 | 388 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@630 | 389 | * |
universe@630 | 390 | * @param map the map to create the iterator for |
universe@630 | 391 | * @return an iterator for the currently stored keys |
universe@630 | 392 | */ |
universe@630 | 393 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 394 | static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) { |
universe@630 | 395 | return map->cl->mut_iterator_keys(map); |
universe@630 | 396 | } |
universe@630 | 397 | |
universe@630 | 398 | /** |
universe@630 | 399 | * Creates a mutating iterator for a map. |
universe@630 | 400 | * |
universe@630 | 401 | * The elements of the iterator are key/value pairs of type CxMapEntry. |
universe@630 | 402 | * |
universe@630 | 403 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@630 | 404 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@630 | 405 | * |
universe@630 | 406 | * @param map the map to create the iterator for |
universe@630 | 407 | * @return an iterator for the currently stored entries |
universe@630 | 408 | * @see cxMapMutIteratorKeys() |
universe@630 | 409 | * @see cxMapMutIteratorValues() |
universe@630 | 410 | */ |
universe@630 | 411 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 412 | static inline CxMutIterator cxMapMutIterator(CxMap *map) { |
universe@630 | 413 | return map->cl->mut_iterator(map); |
universe@630 | 414 | } |
universe@630 | 415 | |
universe@549 | 416 | #ifdef __cplusplus |
universe@549 | 417 | } |
universe@549 | 418 | #endif |
universe@549 | 419 | |
universe@549 | 420 | #endif // UCX_MAP_H |