Sun, 16 Apr 2023 20:50:19 +0200
fix common.h include problems - fixes #255
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@549 | 59 | /** The map class definition. */ |
universe@549 | 60 | cx_map_class *cl; |
universe@549 | 61 | /** An allocator that is used for the map elements. */ |
universe@549 | 62 | CxAllocator *allocator; |
universe@549 | 63 | /** The number of elements currently stored. */ |
universe@549 | 64 | size_t size; |
universe@658 | 65 | /** |
universe@658 | 66 | * The size of an element. |
universe@658 | 67 | */ |
universe@677 | 68 | size_t item_size; |
universe@658 | 69 | /** |
universe@658 | 70 | * True, if this map shall store pointers instead |
universe@658 | 71 | * of copies of objects. |
universe@658 | 72 | */ |
universe@658 | 73 | bool store_pointers; |
universe@549 | 74 | }; |
universe@549 | 75 | |
universe@549 | 76 | /** |
universe@549 | 77 | * The class definition for arbitrary maps. |
universe@549 | 78 | */ |
universe@549 | 79 | struct cx_map_class_s { |
universe@549 | 80 | /** |
universe@549 | 81 | * Deallocates the entire memory. |
universe@549 | 82 | */ |
universe@549 | 83 | __attribute__((__nonnull__)) |
universe@549 | 84 | void (*destructor)(struct cx_map_s *map); |
universe@549 | 85 | |
universe@549 | 86 | /** |
universe@549 | 87 | * Removes all elements. |
universe@549 | 88 | */ |
universe@549 | 89 | __attribute__((__nonnull__)) |
universe@549 | 90 | void (*clear)(struct cx_map_s *map); |
universe@549 | 91 | |
universe@549 | 92 | /** |
universe@549 | 93 | * Add or overwrite an element. |
universe@549 | 94 | */ |
universe@549 | 95 | __attribute__((__nonnull__)) |
universe@549 | 96 | int (*put)( |
universe@549 | 97 | CxMap *map, |
universe@563 | 98 | CxHashKey key, |
universe@550 | 99 | void *value |
universe@549 | 100 | ); |
universe@549 | 101 | |
universe@549 | 102 | /** |
universe@549 | 103 | * Returns an element. |
universe@549 | 104 | */ |
universe@549 | 105 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@549 | 106 | void *(*get)( |
universe@549 | 107 | CxMap const *map, |
universe@563 | 108 | CxHashKey key |
universe@549 | 109 | ); |
universe@549 | 110 | |
universe@549 | 111 | /** |
universe@549 | 112 | * Removes an element. |
universe@549 | 113 | */ |
universe@659 | 114 | __attribute__((__nonnull__)) |
universe@549 | 115 | void *(*remove)( |
universe@550 | 116 | CxMap *map, |
universe@563 | 117 | CxHashKey key |
universe@549 | 118 | ); |
universe@549 | 119 | |
universe@549 | 120 | /** |
universe@549 | 121 | * Iterator over the key/value pairs. |
universe@549 | 122 | */ |
universe@549 | 123 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 124 | CxIterator (*iterator)(CxMap const *map); |
universe@549 | 125 | |
universe@549 | 126 | /** |
universe@549 | 127 | * Iterator over the keys. |
universe@549 | 128 | */ |
universe@549 | 129 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 130 | CxIterator (*iterator_keys)(CxMap const *map); |
universe@549 | 131 | |
universe@549 | 132 | /** |
universe@549 | 133 | * Iterator over the values. |
universe@549 | 134 | */ |
universe@549 | 135 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 136 | CxIterator (*iterator_values)(CxMap const *map); |
universe@630 | 137 | |
universe@630 | 138 | /** |
universe@630 | 139 | * Mutating iterator over the key/value pairs. |
universe@630 | 140 | */ |
universe@630 | 141 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 142 | CxMutIterator (*mut_iterator)(CxMap *map); |
universe@630 | 143 | |
universe@630 | 144 | /** |
universe@630 | 145 | * Mutating iterator over the keys. |
universe@630 | 146 | */ |
universe@630 | 147 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 148 | CxMutIterator (*mut_iterator_keys)(CxMap *map); |
universe@630 | 149 | |
universe@630 | 150 | /** |
universe@630 | 151 | * Mutating iterator over the values. |
universe@630 | 152 | */ |
universe@630 | 153 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 154 | CxMutIterator (*mut_iterator_values)(CxMap *map); |
universe@549 | 155 | }; |
universe@549 | 156 | |
universe@549 | 157 | /** |
universe@549 | 158 | * A map entry. |
universe@549 | 159 | */ |
universe@549 | 160 | struct cx_map_entry_s { |
universe@549 | 161 | /** |
universe@551 | 162 | * A pointer to the key. |
universe@549 | 163 | */ |
universe@563 | 164 | CxHashKey const *key; |
universe@549 | 165 | /** |
universe@551 | 166 | * A pointer to the value. |
universe@549 | 167 | */ |
universe@551 | 168 | void *value; |
universe@549 | 169 | }; |
universe@549 | 170 | |
universe@658 | 171 | /** |
universe@658 | 172 | * Advises the map to store copies of the objects (default mode of operation). |
universe@658 | 173 | * |
universe@658 | 174 | * Retrieving objects from this map will yield pointers to the copies stored |
universe@658 | 175 | * within this list. |
universe@658 | 176 | * |
universe@658 | 177 | * @param map the map |
universe@658 | 178 | * @see cxMapStorePointers() |
universe@658 | 179 | */ |
universe@658 | 180 | __attribute__((__nonnull__)) |
universe@658 | 181 | static inline void cxMapStoreObjects(CxMap *map) { |
universe@658 | 182 | map->store_pointers = false; |
universe@658 | 183 | } |
universe@658 | 184 | |
universe@658 | 185 | /** |
universe@658 | 186 | * Advises the map to only store pointers to the objects. |
universe@658 | 187 | * |
universe@658 | 188 | * Retrieving objects from this list will yield the original pointers stored. |
universe@658 | 189 | * |
universe@658 | 190 | * @note This function forcibly sets the element size to the size of a pointer. |
universe@658 | 191 | * Invoking this function on a non-empty map that already stores copies of |
universe@658 | 192 | * objects is undefined. |
universe@658 | 193 | * |
universe@658 | 194 | * @param map the map |
universe@658 | 195 | * @see cxMapStoreObjects() |
universe@658 | 196 | */ |
universe@658 | 197 | __attribute__((__nonnull__)) |
universe@658 | 198 | static inline void cxMapStorePointers(CxMap *map) { |
universe@658 | 199 | map->store_pointers = true; |
universe@677 | 200 | map->item_size = sizeof(void *); |
universe@658 | 201 | } |
universe@658 | 202 | |
universe@549 | 203 | |
universe@549 | 204 | /** |
universe@549 | 205 | * Deallocates the memory of the specified map. |
universe@549 | 206 | * |
universe@549 | 207 | * @param map the map to be destroyed |
universe@549 | 208 | */ |
universe@549 | 209 | __attribute__((__nonnull__)) |
universe@549 | 210 | static inline void cxMapDestroy(CxMap *map) { |
universe@549 | 211 | // TODO: likely to add auto-free feature for contents in the future |
universe@549 | 212 | map->cl->destructor(map); |
universe@549 | 213 | } |
universe@549 | 214 | |
universe@549 | 215 | |
universe@549 | 216 | /** |
universe@549 | 217 | * Clears a map by removing all elements. |
universe@549 | 218 | * |
universe@549 | 219 | * @param map the map to be cleared |
universe@549 | 220 | */ |
universe@549 | 221 | __attribute__((__nonnull__)) |
universe@549 | 222 | static inline void cxMapClear(CxMap *map) { |
universe@549 | 223 | map->cl->clear(map); |
universe@549 | 224 | } |
universe@549 | 225 | |
universe@549 | 226 | /** |
universe@549 | 227 | * Puts a key/value-pair into the map. |
universe@549 | 228 | * |
universe@549 | 229 | * @param map the map |
universe@549 | 230 | * @param key the key |
universe@549 | 231 | * @param value the value |
universe@549 | 232 | * @return 0 on success, non-zero value on failure |
universe@549 | 233 | */ |
universe@549 | 234 | __attribute__((__nonnull__)) |
universe@549 | 235 | static inline int cxMapPut( |
universe@549 | 236 | CxMap *map, |
universe@563 | 237 | CxHashKey key, |
universe@550 | 238 | void *value |
universe@549 | 239 | ) { |
universe@549 | 240 | return map->cl->put(map, key, value); |
universe@549 | 241 | } |
universe@549 | 242 | |
universe@549 | 243 | /** |
universe@549 | 244 | * Retrieves a value by using a key. |
universe@549 | 245 | * |
universe@549 | 246 | * @param map the map |
universe@549 | 247 | * @param key the key |
universe@549 | 248 | * @return the value |
universe@549 | 249 | */ |
universe@549 | 250 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@553 | 251 | static inline void *cxMapGet( |
universe@549 | 252 | CxMap const *map, |
universe@563 | 253 | CxHashKey key |
universe@549 | 254 | ) { |
universe@549 | 255 | return map->cl->get(map, key); |
universe@549 | 256 | } |
universe@549 | 257 | |
universe@549 | 258 | /** |
universe@549 | 259 | * Removes a key/value-pair from the map by using the key. |
universe@549 | 260 | * |
universe@659 | 261 | * If this map is storing pointers, you should make sure that the map |
universe@659 | 262 | * is not the last location where this pointer is stored. |
universe@659 | 263 | * Otherwise, use cxMapRemoveAndGet() to retrieve the pointer while |
universe@659 | 264 | * removing it from the map. |
universe@659 | 265 | * |
universe@549 | 266 | * @param map the map |
universe@549 | 267 | * @param key the key |
universe@659 | 268 | * @see cxMapRemoveAndGet() |
universe@659 | 269 | */ |
universe@659 | 270 | __attribute__((__nonnull__)) |
universe@659 | 271 | static inline void cxMapRemove( |
universe@659 | 272 | CxMap *map, |
universe@659 | 273 | CxHashKey key |
universe@659 | 274 | ) { |
universe@659 | 275 | (void) map->cl->remove(map, key); |
universe@659 | 276 | } |
universe@659 | 277 | |
universe@659 | 278 | /** |
universe@659 | 279 | * Removes a key/value-pair from the map by using the key. |
universe@659 | 280 | * |
universe@659 | 281 | * This function should only be used when the map is storing pointers, |
universe@659 | 282 | * in order to retrieve the pointer you are about to remove. |
universe@659 | 283 | * In any other case, cxMapRemove() is sufficient. |
universe@659 | 284 | * |
universe@659 | 285 | * @param map the map |
universe@659 | 286 | * @param key the key |
universe@659 | 287 | * @return the stored pointer or \c NULL if either the key is not present |
universe@659 | 288 | * in the map or the map is not storing pointers |
universe@658 | 289 | * @see cxMapStorePointers() |
universe@549 | 290 | */ |
universe@549 | 291 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@659 | 292 | static inline void *cxMapRemoveAndGet( |
universe@549 | 293 | CxMap *map, |
universe@563 | 294 | CxHashKey key |
universe@549 | 295 | ) { |
universe@549 | 296 | return map->cl->remove(map, key); |
universe@549 | 297 | } |
universe@549 | 298 | |
universe@549 | 299 | // TODO: set-like map operations (union, intersect, difference) |
universe@549 | 300 | |
universe@549 | 301 | /** |
universe@549 | 302 | * Creates a value iterator for a map. |
universe@549 | 303 | * |
universe@549 | 304 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 305 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 306 | * |
universe@549 | 307 | * @param map the map to create the iterator for |
universe@549 | 308 | * @return an iterator for the currently stored values |
universe@549 | 309 | */ |
universe@549 | 310 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 311 | static inline CxIterator cxMapIteratorValues(CxMap *map) { |
universe@549 | 312 | return map->cl->iterator_values(map); |
universe@549 | 313 | } |
universe@549 | 314 | |
universe@549 | 315 | /** |
universe@549 | 316 | * Creates a key iterator for a map. |
universe@549 | 317 | * |
universe@564 | 318 | * The elements of the iterator are keys of type CxHashKey. |
universe@555 | 319 | * |
universe@549 | 320 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 321 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 322 | * |
universe@549 | 323 | * @param map the map to create the iterator for |
universe@549 | 324 | * @return an iterator for the currently stored keys |
universe@549 | 325 | */ |
universe@549 | 326 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 327 | static inline CxIterator cxMapIteratorKeys(CxMap *map) { |
universe@549 | 328 | return map->cl->iterator_keys(map); |
universe@549 | 329 | } |
universe@549 | 330 | |
universe@549 | 331 | /** |
universe@549 | 332 | * Creates an iterator for a map. |
universe@549 | 333 | * |
universe@555 | 334 | * The elements of the iterator are key/value pairs of type CxMapEntry. |
universe@549 | 335 | * |
universe@549 | 336 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 337 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 338 | * |
universe@549 | 339 | * @param map the map to create the iterator for |
universe@555 | 340 | * @return an iterator for the currently stored entries |
universe@549 | 341 | * @see cxMapIteratorKeys() |
universe@549 | 342 | * @see cxMapIteratorValues() |
universe@549 | 343 | */ |
universe@549 | 344 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 345 | static inline CxIterator cxMapIterator(CxMap *map) { |
universe@549 | 346 | return map->cl->iterator(map); |
universe@549 | 347 | } |
universe@549 | 348 | |
universe@630 | 349 | |
universe@630 | 350 | /** |
universe@630 | 351 | * Creates a mutating iterator over the values of a map. |
universe@630 | 352 | * |
universe@630 | 353 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@630 | 354 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@630 | 355 | * |
universe@630 | 356 | * @param map the map to create the iterator for |
universe@630 | 357 | * @return an iterator for the currently stored values |
universe@630 | 358 | */ |
universe@630 | 359 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 360 | static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) { |
universe@630 | 361 | return map->cl->mut_iterator_values(map); |
universe@630 | 362 | } |
universe@630 | 363 | |
universe@630 | 364 | /** |
universe@630 | 365 | * Creates a mutating iterator over the keys of a map. |
universe@630 | 366 | * |
universe@630 | 367 | * The elements of the iterator are keys of type CxHashKey. |
universe@630 | 368 | * |
universe@630 | 369 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@630 | 370 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@630 | 371 | * |
universe@630 | 372 | * @param map the map to create the iterator for |
universe@630 | 373 | * @return an iterator for the currently stored keys |
universe@630 | 374 | */ |
universe@630 | 375 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 376 | static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) { |
universe@630 | 377 | return map->cl->mut_iterator_keys(map); |
universe@630 | 378 | } |
universe@630 | 379 | |
universe@630 | 380 | /** |
universe@630 | 381 | * Creates a mutating iterator for a map. |
universe@630 | 382 | * |
universe@630 | 383 | * The elements of the iterator are key/value pairs of type CxMapEntry. |
universe@630 | 384 | * |
universe@630 | 385 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@630 | 386 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@630 | 387 | * |
universe@630 | 388 | * @param map the map to create the iterator for |
universe@630 | 389 | * @return an iterator for the currently stored entries |
universe@630 | 390 | * @see cxMapMutIteratorKeys() |
universe@630 | 391 | * @see cxMapMutIteratorValues() |
universe@630 | 392 | */ |
universe@630 | 393 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@630 | 394 | static inline CxMutIterator cxMapMutIterator(CxMap *map) { |
universe@630 | 395 | return map->cl->mut_iterator(map); |
universe@630 | 396 | } |
universe@630 | 397 | |
universe@549 | 398 | #ifdef __cplusplus |
universe@549 | 399 | } |
universe@549 | 400 | #endif |
universe@549 | 401 | |
universe@549 | 402 | #endif // UCX_MAP_H |