src/cx/map.h

Thu, 23 Feb 2023 18:58:15 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Feb 2023 18:58:15 +0100
changeset 658
56c62780582e
parent 630
ac5e7f789048
child 659
4a06fd63909a
permissions
-rw-r--r--

make hashmap store objects instead of pointers by default - fixes #239

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

mercurial