src/cx/map.h

Sun, 21 May 2023 16:22:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 16:22:09 +0200
changeset 710
2dd409ed056f
parent 709
1e8ba59e7911
child 714
34565d898f1f
permissions
-rw-r--r--

fix const-ness of non-mutating iterator creation for maps

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@691 42 #include "string.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@685 60 CX_COLLECTION_MEMBERS
universe@549 61 /** The map class definition. */
universe@549 62 cx_map_class *cl;
universe@549 63 };
universe@549 64
universe@549 65 /**
universe@709 66 * The type of iterator for a map.
universe@709 67 */
universe@709 68 enum cx_map_iterator_type {
universe@709 69 /**
universe@709 70 * Iterates over key/value pairs.
universe@709 71 */
universe@709 72 CX_MAP_ITERATOR_PAIRS,
universe@709 73 /**
universe@709 74 * Iterates over keys only.
universe@709 75 */
universe@709 76 CX_MAP_ITERATOR_KEYS,
universe@709 77 /**
universe@709 78 * Iterates over values only.
universe@709 79 */
universe@709 80 CX_MAP_ITERATOR_VALUES
universe@709 81 };
universe@709 82
universe@709 83 /**
universe@549 84 * The class definition for arbitrary maps.
universe@549 85 */
universe@549 86 struct cx_map_class_s {
universe@549 87 /**
universe@549 88 * Deallocates the entire memory.
universe@549 89 */
universe@549 90 __attribute__((__nonnull__))
universe@549 91 void (*destructor)(struct cx_map_s *map);
universe@549 92
universe@549 93 /**
universe@549 94 * Removes all elements.
universe@549 95 */
universe@549 96 __attribute__((__nonnull__))
universe@549 97 void (*clear)(struct cx_map_s *map);
universe@549 98
universe@549 99 /**
universe@549 100 * Add or overwrite an element.
universe@549 101 */
universe@549 102 __attribute__((__nonnull__))
universe@549 103 int (*put)(
universe@549 104 CxMap *map,
universe@563 105 CxHashKey key,
universe@550 106 void *value
universe@549 107 );
universe@549 108
universe@549 109 /**
universe@549 110 * Returns an element.
universe@549 111 */
universe@549 112 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 113 void *(*get)(
universe@549 114 CxMap const *map,
universe@563 115 CxHashKey key
universe@549 116 );
universe@549 117
universe@549 118 /**
universe@549 119 * Removes an element.
universe@549 120 */
universe@659 121 __attribute__((__nonnull__))
universe@549 122 void *(*remove)(
universe@550 123 CxMap *map,
universe@686 124 CxHashKey key,
universe@686 125 bool destroy
universe@549 126 );
universe@549 127
universe@549 128 /**
universe@709 129 * Creates an iterator for this map.
universe@549 130 */
universe@549 131 __attribute__((__nonnull__, __warn_unused_result__))
universe@709 132 CxIterator (*iterator)(CxMap const *map, enum cx_map_iterator_type type);
universe@549 133 };
universe@549 134
universe@549 135 /**
universe@549 136 * A map entry.
universe@549 137 */
universe@549 138 struct cx_map_entry_s {
universe@549 139 /**
universe@551 140 * A pointer to the key.
universe@549 141 */
universe@563 142 CxHashKey const *key;
universe@549 143 /**
universe@551 144 * A pointer to the value.
universe@549 145 */
universe@551 146 void *value;
universe@549 147 };
universe@549 148
universe@706 149 extern CxMap *const cxEmptyMap;
universe@706 150
universe@658 151 /**
universe@658 152 * Advises the map to store copies of the objects (default mode of operation).
universe@658 153 *
universe@658 154 * Retrieving objects from this map will yield pointers to the copies stored
universe@658 155 * within this list.
universe@658 156 *
universe@658 157 * @param map the map
universe@658 158 * @see cxMapStorePointers()
universe@658 159 */
universe@658 160 __attribute__((__nonnull__))
universe@658 161 static inline void cxMapStoreObjects(CxMap *map) {
universe@685 162 map->store_pointer = false;
universe@658 163 }
universe@658 164
universe@658 165 /**
universe@658 166 * Advises the map to only store pointers to the objects.
universe@658 167 *
universe@658 168 * Retrieving objects from this list will yield the original pointers stored.
universe@658 169 *
universe@658 170 * @note This function forcibly sets the element size to the size of a pointer.
universe@658 171 * Invoking this function on a non-empty map that already stores copies of
universe@658 172 * objects is undefined.
universe@658 173 *
universe@658 174 * @param map the map
universe@658 175 * @see cxMapStoreObjects()
universe@658 176 */
universe@658 177 __attribute__((__nonnull__))
universe@658 178 static inline void cxMapStorePointers(CxMap *map) {
universe@685 179 map->store_pointer = true;
universe@677 180 map->item_size = sizeof(void *);
universe@658 181 }
universe@658 182
universe@549 183
universe@549 184 /**
universe@549 185 * Deallocates the memory of the specified map.
universe@549 186 *
universe@549 187 * @param map the map to be destroyed
universe@549 188 */
universe@549 189 __attribute__((__nonnull__))
universe@549 190 static inline void cxMapDestroy(CxMap *map) {
universe@549 191 map->cl->destructor(map);
universe@549 192 }
universe@549 193
universe@549 194
universe@549 195 /**
universe@549 196 * Clears a map by removing all elements.
universe@549 197 *
universe@549 198 * @param map the map to be cleared
universe@549 199 */
universe@549 200 __attribute__((__nonnull__))
universe@549 201 static inline void cxMapClear(CxMap *map) {
universe@549 202 map->cl->clear(map);
universe@549 203 }
universe@549 204
universe@549 205
universe@549 206 // TODO: set-like map operations (union, intersect, difference)
universe@549 207
universe@549 208 /**
universe@549 209 * Creates a value iterator for a map.
universe@549 210 *
universe@549 211 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 212 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 213 *
universe@549 214 * @param map the map to create the iterator for
universe@549 215 * @return an iterator for the currently stored values
universe@549 216 */
universe@549 217 __attribute__((__nonnull__, __warn_unused_result__))
universe@710 218 static inline CxIterator cxMapIteratorValues(CxMap const *map) {
universe@709 219 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
universe@549 220 }
universe@549 221
universe@549 222 /**
universe@549 223 * Creates a key iterator for a map.
universe@549 224 *
universe@564 225 * The elements of the iterator are keys of type CxHashKey.
universe@555 226 *
universe@549 227 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 228 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 229 *
universe@549 230 * @param map the map to create the iterator for
universe@549 231 * @return an iterator for the currently stored keys
universe@549 232 */
universe@549 233 __attribute__((__nonnull__, __warn_unused_result__))
universe@710 234 static inline CxIterator cxMapIteratorKeys(CxMap const *map) {
universe@709 235 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
universe@549 236 }
universe@549 237
universe@549 238 /**
universe@549 239 * Creates an iterator for a map.
universe@549 240 *
universe@555 241 * The elements of the iterator are key/value pairs of type CxMapEntry.
universe@549 242 *
universe@549 243 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 244 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 245 *
universe@549 246 * @param map the map to create the iterator for
universe@555 247 * @return an iterator for the currently stored entries
universe@549 248 * @see cxMapIteratorKeys()
universe@549 249 * @see cxMapIteratorValues()
universe@549 250 */
universe@549 251 __attribute__((__nonnull__, __warn_unused_result__))
universe@710 252 static inline CxIterator cxMapIterator(CxMap const *map) {
universe@709 253 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
universe@549 254 }
universe@549 255
universe@630 256
universe@630 257 /**
universe@630 258 * Creates a mutating iterator over the values of a map.
universe@630 259 *
universe@630 260 * \note An iterator iterates over all elements successively. Therefore the order
universe@630 261 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@630 262 *
universe@630 263 * @param map the map to create the iterator for
universe@630 264 * @return an iterator for the currently stored values
universe@630 265 */
universe@630 266 __attribute__((__nonnull__, __warn_unused_result__))
universe@709 267 CxMutIterator cxMapMutIteratorValues(CxMap *map);
universe@630 268
universe@630 269 /**
universe@630 270 * Creates a mutating iterator over the keys of a map.
universe@630 271 *
universe@630 272 * The elements of the iterator are keys of type CxHashKey.
universe@630 273 *
universe@630 274 * \note An iterator iterates over all elements successively. Therefore the order
universe@630 275 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@630 276 *
universe@630 277 * @param map the map to create the iterator for
universe@630 278 * @return an iterator for the currently stored keys
universe@630 279 */
universe@630 280 __attribute__((__nonnull__, __warn_unused_result__))
universe@709 281 CxMutIterator cxMapMutIteratorKeys(CxMap *map);
universe@630 282
universe@630 283 /**
universe@630 284 * Creates a mutating iterator for a map.
universe@630 285 *
universe@630 286 * The elements of the iterator are key/value pairs of type CxMapEntry.
universe@630 287 *
universe@630 288 * \note An iterator iterates over all elements successively. Therefore the order
universe@630 289 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@630 290 *
universe@630 291 * @param map the map to create the iterator for
universe@630 292 * @return an iterator for the currently stored entries
universe@630 293 * @see cxMapMutIteratorKeys()
universe@630 294 * @see cxMapMutIteratorValues()
universe@630 295 */
universe@630 296 __attribute__((__nonnull__, __warn_unused_result__))
universe@709 297 CxMutIterator cxMapMutIterator(CxMap *map);
universe@630 298
universe@691 299 #ifdef __cplusplus
universe@691 300 } // end the extern "C" block here, because we want to start overloading
universe@691 301
universe@691 302 /**
universe@691 303 * Puts a key/value-pair into the map.
universe@691 304 *
universe@691 305 * @param map the map
universe@691 306 * @param key the key
universe@691 307 * @param value the value
universe@691 308 * @return 0 on success, non-zero value on failure
universe@691 309 */
universe@691 310 __attribute__((__nonnull__))
universe@691 311 static inline int cxMapPut(
universe@691 312 CxMap *map,
universe@691 313 CxHashKey const &key,
universe@691 314 void *value
universe@691 315 ) {
universe@691 316 return map->cl->put(map, key, value);
universe@549 317 }
universe@549 318
universe@691 319
universe@691 320 /**
universe@691 321 * Puts a key/value-pair into the map.
universe@691 322 *
universe@691 323 * @param map the map
universe@691 324 * @param key the key
universe@691 325 * @param value the value
universe@691 326 * @return 0 on success, non-zero value on failure
universe@691 327 */
universe@691 328 __attribute__((__nonnull__))
universe@691 329 static inline int cxMapPut(
universe@691 330 CxMap *map,
universe@691 331 cxstring const &key,
universe@691 332 void *value
universe@691 333 ) {
universe@691 334 return map->cl->put(map, cx_hash_key_cxstr(key), value);
universe@691 335 }
universe@691 336
universe@691 337 /**
universe@691 338 * Puts a key/value-pair into the map.
universe@691 339 *
universe@691 340 * @param map the map
universe@691 341 * @param key the key
universe@691 342 * @param value the value
universe@691 343 * @return 0 on success, non-zero value on failure
universe@691 344 */
universe@691 345 __attribute__((__nonnull__))
universe@691 346 static inline int cxMapPut(
universe@691 347 CxMap *map,
universe@692 348 cxmutstr const &key,
universe@692 349 void *value
universe@692 350 ) {
universe@692 351 return map->cl->put(map, cx_hash_key_cxstr(key), value);
universe@692 352 }
universe@692 353
universe@692 354 /**
universe@692 355 * Puts a key/value-pair into the map.
universe@692 356 *
universe@692 357 * @param map the map
universe@692 358 * @param key the key
universe@692 359 * @param value the value
universe@692 360 * @return 0 on success, non-zero value on failure
universe@692 361 */
universe@692 362 __attribute__((__nonnull__))
universe@692 363 static inline int cxMapPut(
universe@692 364 CxMap *map,
universe@691 365 char const *key,
universe@691 366 void *value
universe@691 367 ) {
universe@691 368 return map->cl->put(map, cx_hash_key_str(key), value);
universe@691 369 }
universe@691 370
universe@691 371 /**
universe@691 372 * Retrieves a value by using a key.
universe@691 373 *
universe@691 374 * @param map the map
universe@691 375 * @param key the key
universe@691 376 * @return the value
universe@691 377 */
universe@691 378 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 379 static inline void *cxMapGet(
universe@691 380 CxMap const *map,
universe@691 381 CxHashKey const &key
universe@691 382 ) {
universe@691 383 return map->cl->get(map, key);
universe@691 384 }
universe@691 385
universe@691 386 /**
universe@691 387 * Retrieves a value by using a key.
universe@691 388 *
universe@691 389 * @param map the map
universe@691 390 * @param key the key
universe@691 391 * @return the value
universe@691 392 */
universe@691 393 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 394 static inline void *cxMapGet(
universe@691 395 CxMap const *map,
universe@691 396 cxstring const &key
universe@691 397 ) {
universe@691 398 return map->cl->get(map, cx_hash_key_cxstr(key));
universe@691 399 }
universe@691 400
universe@691 401 /**
universe@691 402 * Retrieves a value by using a key.
universe@691 403 *
universe@691 404 * @param map the map
universe@691 405 * @param key the key
universe@691 406 * @return the value
universe@691 407 */
universe@691 408 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 409 static inline void *cxMapGet(
universe@691 410 CxMap const *map,
universe@692 411 cxmutstr const &key
universe@692 412 ) {
universe@692 413 return map->cl->get(map, cx_hash_key_cxstr(key));
universe@692 414 }
universe@692 415
universe@692 416 /**
universe@692 417 * Retrieves a value by using a key.
universe@692 418 *
universe@692 419 * @param map the map
universe@692 420 * @param key the key
universe@692 421 * @return the value
universe@692 422 */
universe@692 423 __attribute__((__nonnull__, __warn_unused_result__))
universe@692 424 static inline void *cxMapGet(
universe@692 425 CxMap const *map,
universe@691 426 char const *key
universe@691 427 ) {
universe@691 428 return map->cl->get(map, cx_hash_key_str(key));
universe@691 429 }
universe@691 430
universe@691 431 /**
universe@691 432 * Removes a key/value-pair from the map by using the key.
universe@691 433 *
universe@691 434 * Always invokes the destructor function, if any, on the removed element.
universe@691 435 * If this map is storing pointers and you just want to retrieve the pointer
universe@691 436 * without invoking the destructor, use cxMapRemoveAndGet().
universe@691 437 * If you just want to detach the element from the map without invoking the
universe@691 438 * destructor or returning the element, use cxMapDetach().
universe@691 439 *
universe@691 440 * @param map the map
universe@691 441 * @param key the key
universe@691 442 * @see cxMapRemoveAndGet()
universe@691 443 * @see cxMapDetach()
universe@691 444 */
universe@691 445 __attribute__((__nonnull__))
universe@691 446 static inline void cxMapRemove(
universe@691 447 CxMap *map,
universe@691 448 CxHashKey const &key
universe@691 449 ) {
universe@691 450 (void) map->cl->remove(map, key, true);
universe@691 451 }
universe@691 452
universe@691 453 /**
universe@691 454 * Removes a key/value-pair from the map by using the key.
universe@691 455 *
universe@691 456 * Always invokes the destructor function, if any, on the removed element.
universe@691 457 * If this map is storing pointers and you just want to retrieve the pointer
universe@691 458 * without invoking the destructor, use cxMapRemoveAndGet().
universe@691 459 * If you just want to detach the element from the map without invoking the
universe@691 460 * destructor or returning the element, use cxMapDetach().
universe@691 461 *
universe@691 462 * @param map the map
universe@691 463 * @param key the key
universe@691 464 * @see cxMapRemoveAndGet()
universe@691 465 * @see cxMapDetach()
universe@691 466 */
universe@691 467 __attribute__((__nonnull__))
universe@691 468 static inline void cxMapRemove(
universe@691 469 CxMap *map,
universe@691 470 cxstring const &key
universe@691 471 ) {
universe@691 472 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
universe@691 473 }
universe@691 474
universe@691 475 /**
universe@691 476 * Removes a key/value-pair from the map by using the key.
universe@691 477 *
universe@691 478 * Always invokes the destructor function, if any, on the removed element.
universe@691 479 * If this map is storing pointers and you just want to retrieve the pointer
universe@691 480 * without invoking the destructor, use cxMapRemoveAndGet().
universe@691 481 * If you just want to detach the element from the map without invoking the
universe@691 482 * destructor or returning the element, use cxMapDetach().
universe@691 483 *
universe@691 484 * @param map the map
universe@691 485 * @param key the key
universe@691 486 * @see cxMapRemoveAndGet()
universe@691 487 * @see cxMapDetach()
universe@691 488 */
universe@691 489 __attribute__((__nonnull__))
universe@691 490 static inline void cxMapRemove(
universe@691 491 CxMap *map,
universe@692 492 cxmutstr const &key
universe@692 493 ) {
universe@692 494 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
universe@692 495 }
universe@692 496
universe@692 497 /**
universe@692 498 * Removes a key/value-pair from the map by using the key.
universe@692 499 *
universe@692 500 * Always invokes the destructor function, if any, on the removed element.
universe@692 501 * If this map is storing pointers and you just want to retrieve the pointer
universe@692 502 * without invoking the destructor, use cxMapRemoveAndGet().
universe@692 503 * If you just want to detach the element from the map without invoking the
universe@692 504 * destructor or returning the element, use cxMapDetach().
universe@692 505 *
universe@692 506 * @param map the map
universe@692 507 * @param key the key
universe@692 508 * @see cxMapRemoveAndGet()
universe@692 509 * @see cxMapDetach()
universe@692 510 */
universe@692 511 __attribute__((__nonnull__))
universe@692 512 static inline void cxMapRemove(
universe@692 513 CxMap *map,
universe@691 514 char const *key
universe@691 515 ) {
universe@691 516 (void) map->cl->remove(map, cx_hash_key_str(key), true);
universe@691 517 }
universe@691 518
universe@691 519 /**
universe@691 520 * Detaches a key/value-pair from the map by using the key
universe@691 521 * without invoking the destructor.
universe@691 522 *
universe@691 523 * In general, you should only use this function if the map does not own
universe@691 524 * the data and there is a valid reference to the data somewhere else
universe@691 525 * in the program. In all other cases it is preferable to use
universe@691 526 * cxMapRemove() or cxMapRemoveAndGet().
universe@691 527 *
universe@691 528 * @param map the map
universe@691 529 * @param key the key
universe@691 530 * @see cxMapRemove()
universe@691 531 * @see cxMapRemoveAndGet()
universe@691 532 */
universe@691 533 __attribute__((__nonnull__))
universe@691 534 static inline void cxMapDetach(
universe@691 535 CxMap *map,
universe@691 536 CxHashKey const &key
universe@691 537 ) {
universe@691 538 (void) map->cl->remove(map, key, false);
universe@691 539 }
universe@691 540
universe@691 541 /**
universe@691 542 * Detaches a key/value-pair from the map by using the key
universe@691 543 * without invoking the destructor.
universe@691 544 *
universe@691 545 * In general, you should only use this function if the map does not own
universe@691 546 * the data and there is a valid reference to the data somewhere else
universe@691 547 * in the program. In all other cases it is preferable to use
universe@691 548 * cxMapRemove() or cxMapRemoveAndGet().
universe@691 549 *
universe@691 550 * @param map the map
universe@691 551 * @param key the key
universe@691 552 * @see cxMapRemove()
universe@691 553 * @see cxMapRemoveAndGet()
universe@691 554 */
universe@691 555 __attribute__((__nonnull__))
universe@691 556 static inline void cxMapDetach(
universe@691 557 CxMap *map,
universe@691 558 cxstring const &key
universe@691 559 ) {
universe@691 560 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
universe@691 561 }
universe@691 562
universe@691 563 /**
universe@691 564 * Detaches a key/value-pair from the map by using the key
universe@691 565 * without invoking the destructor.
universe@691 566 *
universe@691 567 * In general, you should only use this function if the map does not own
universe@691 568 * the data and there is a valid reference to the data somewhere else
universe@691 569 * in the program. In all other cases it is preferable to use
universe@691 570 * cxMapRemove() or cxMapRemoveAndGet().
universe@691 571 *
universe@691 572 * @param map the map
universe@691 573 * @param key the key
universe@691 574 * @see cxMapRemove()
universe@691 575 * @see cxMapRemoveAndGet()
universe@691 576 */
universe@691 577 __attribute__((__nonnull__))
universe@691 578 static inline void cxMapDetach(
universe@691 579 CxMap *map,
universe@692 580 cxmutstr const &key
universe@692 581 ) {
universe@692 582 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
universe@692 583 }
universe@692 584
universe@692 585 /**
universe@692 586 * Detaches a key/value-pair from the map by using the key
universe@692 587 * without invoking the destructor.
universe@692 588 *
universe@692 589 * In general, you should only use this function if the map does not own
universe@692 590 * the data and there is a valid reference to the data somewhere else
universe@692 591 * in the program. In all other cases it is preferable to use
universe@692 592 * cxMapRemove() or cxMapRemoveAndGet().
universe@692 593 *
universe@692 594 * @param map the map
universe@692 595 * @param key the key
universe@692 596 * @see cxMapRemove()
universe@692 597 * @see cxMapRemoveAndGet()
universe@692 598 */
universe@692 599 __attribute__((__nonnull__))
universe@692 600 static inline void cxMapDetach(
universe@692 601 CxMap *map,
universe@691 602 char const *key
universe@691 603 ) {
universe@691 604 (void) map->cl->remove(map, cx_hash_key_str(key), false);
universe@691 605 }
universe@691 606
universe@691 607 /**
universe@691 608 * Removes a key/value-pair from the map by using the key.
universe@691 609 *
universe@691 610 * This function can be used when the map is storing pointers,
universe@691 611 * in order to retrieve the pointer from the map without invoking
universe@691 612 * any destructor function. Sometimes you do not want the pointer
universe@691 613 * to be returned - in that case (instead of suppressing the "unused
universe@691 614 * result" warning) you can use cxMapDetach().
universe@691 615 *
universe@691 616 * If this map is not storing pointers, this function behaves like
universe@691 617 * cxMapRemove() and returns \c NULL.
universe@691 618 *
universe@691 619 * @param map the map
universe@691 620 * @param key the key
universe@691 621 * @return the stored pointer or \c NULL if either the key is not present
universe@691 622 * in the map or the map is not storing pointers
universe@691 623 * @see cxMapStorePointers()
universe@691 624 * @see cxMapDetach()
universe@691 625 */
universe@691 626 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 627 static inline void *cxMapRemoveAndGet(
universe@691 628 CxMap *map,
universe@691 629 CxHashKey key
universe@691 630 ) {
universe@691 631 return map->cl->remove(map, key, !map->store_pointer);
universe@691 632 }
universe@691 633
universe@691 634 /**
universe@691 635 * Removes a key/value-pair from the map by using the key.
universe@691 636 *
universe@691 637 * This function can be used when the map is storing pointers,
universe@691 638 * in order to retrieve the pointer from the map without invoking
universe@691 639 * any destructor function. Sometimes you do not want the pointer
universe@691 640 * to be returned - in that case (instead of suppressing the "unused
universe@691 641 * result" warning) you can use cxMapDetach().
universe@691 642 *
universe@691 643 * If this map is not storing pointers, this function behaves like
universe@691 644 * cxMapRemove() and returns \c NULL.
universe@691 645 *
universe@691 646 * @param map the map
universe@691 647 * @param key the key
universe@691 648 * @return the stored pointer or \c NULL if either the key is not present
universe@691 649 * in the map or the map is not storing pointers
universe@691 650 * @see cxMapStorePointers()
universe@691 651 * @see cxMapDetach()
universe@691 652 */
universe@691 653 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 654 static inline void *cxMapRemoveAndGet(
universe@691 655 CxMap *map,
universe@691 656 cxstring key
universe@691 657 ) {
universe@691 658 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
universe@691 659 }
universe@691 660
universe@691 661 /**
universe@691 662 * Removes a key/value-pair from the map by using the key.
universe@691 663 *
universe@691 664 * This function can be used when the map is storing pointers,
universe@691 665 * in order to retrieve the pointer from the map without invoking
universe@691 666 * any destructor function. Sometimes you do not want the pointer
universe@691 667 * to be returned - in that case (instead of suppressing the "unused
universe@691 668 * result" warning) you can use cxMapDetach().
universe@691 669 *
universe@691 670 * If this map is not storing pointers, this function behaves like
universe@691 671 * cxMapRemove() and returns \c NULL.
universe@691 672 *
universe@691 673 * @param map the map
universe@691 674 * @param key the key
universe@691 675 * @return the stored pointer or \c NULL if either the key is not present
universe@691 676 * in the map or the map is not storing pointers
universe@691 677 * @see cxMapStorePointers()
universe@691 678 * @see cxMapDetach()
universe@691 679 */
universe@691 680 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 681 static inline void *cxMapRemoveAndGet(
universe@691 682 CxMap *map,
universe@692 683 cxmutstr key
universe@692 684 ) {
universe@692 685 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
universe@692 686 }
universe@692 687
universe@692 688 /**
universe@692 689 * Removes a key/value-pair from the map by using the key.
universe@692 690 *
universe@692 691 * This function can be used when the map is storing pointers,
universe@692 692 * in order to retrieve the pointer from the map without invoking
universe@692 693 * any destructor function. Sometimes you do not want the pointer
universe@692 694 * to be returned - in that case (instead of suppressing the "unused
universe@692 695 * result" warning) you can use cxMapDetach().
universe@692 696 *
universe@692 697 * If this map is not storing pointers, this function behaves like
universe@692 698 * cxMapRemove() and returns \c NULL.
universe@692 699 *
universe@692 700 * @param map the map
universe@692 701 * @param key the key
universe@692 702 * @return the stored pointer or \c NULL if either the key is not present
universe@692 703 * in the map or the map is not storing pointers
universe@692 704 * @see cxMapStorePointers()
universe@692 705 * @see cxMapDetach()
universe@692 706 */
universe@692 707 __attribute__((__nonnull__, __warn_unused_result__))
universe@692 708 static inline void *cxMapRemoveAndGet(
universe@692 709 CxMap *map,
universe@691 710 char const *key
universe@691 711 ) {
universe@691 712 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
universe@691 713 }
universe@691 714
universe@691 715 #else // __cplusplus
universe@691 716
universe@691 717 /**
universe@691 718 * Puts a key/value-pair into the map.
universe@691 719 *
universe@691 720 * @param map the map
universe@691 721 * @param key the key
universe@691 722 * @param value the value
universe@691 723 * @return 0 on success, non-zero value on failure
universe@691 724 */
universe@691 725 __attribute__((__nonnull__))
universe@691 726 static inline int cx_map_put(
universe@691 727 CxMap *map,
universe@691 728 CxHashKey key,
universe@691 729 void *value
universe@691 730 ) {
universe@691 731 return map->cl->put(map, key, value);
universe@691 732 }
universe@691 733
universe@691 734 /**
universe@691 735 * Puts a key/value-pair into the map.
universe@691 736 *
universe@691 737 * @param map the map
universe@691 738 * @param key the key
universe@691 739 * @param value the value
universe@691 740 * @return 0 on success, non-zero value on failure
universe@691 741 */
universe@691 742 __attribute__((__nonnull__))
universe@691 743 static inline int cx_map_put_cxstr(
universe@691 744 CxMap *map,
universe@691 745 cxstring key,
universe@691 746 void *value
universe@691 747 ) {
universe@691 748 return map->cl->put(map, cx_hash_key_cxstr(key), value);
universe@691 749 }
universe@691 750
universe@691 751 /**
universe@691 752 * Puts a key/value-pair into the map.
universe@691 753 *
universe@691 754 * @param map the map
universe@691 755 * @param key the key
universe@691 756 * @param value the value
universe@691 757 * @return 0 on success, non-zero value on failure
universe@691 758 */
universe@691 759 __attribute__((__nonnull__))
universe@692 760 static inline int cx_map_put_mustr(
universe@692 761 CxMap *map,
universe@692 762 cxmutstr key,
universe@692 763 void *value
universe@692 764 ) {
universe@692 765 return map->cl->put(map, cx_hash_key_cxstr(key), value);
universe@692 766 }
universe@692 767
universe@692 768 /**
universe@692 769 * Puts a key/value-pair into the map.
universe@692 770 *
universe@692 771 * @param map the map
universe@692 772 * @param key the key
universe@692 773 * @param value the value
universe@692 774 * @return 0 on success, non-zero value on failure
universe@692 775 */
universe@692 776 __attribute__((__nonnull__))
universe@691 777 static inline int cx_map_put_str(
universe@691 778 CxMap *map,
universe@691 779 char const *key,
universe@691 780 void *value
universe@691 781 ) {
universe@691 782 return map->cl->put(map, cx_hash_key_str(key), value);
universe@691 783 }
universe@691 784
universe@691 785 /**
universe@691 786 * Puts a key/value-pair into the map.
universe@691 787 *
universe@691 788 * @param map the map
universe@691 789 * @param key the key
universe@691 790 * @param value the value
universe@691 791 * @return 0 on success, non-zero value on failure
universe@691 792 */
universe@691 793 #define cxMapPut(map, key, value) _Generic((key), \
universe@691 794 CxHashKey: cx_map_put, \
universe@691 795 cxstring: cx_map_put_cxstr, \
universe@692 796 cxmutstr: cx_map_put_mustr, \
universe@694 797 char*: cx_map_put_str, \
universe@694 798 char const*: cx_map_put_str) \
universe@691 799 (map, key, value)
universe@691 800
universe@691 801 /**
universe@691 802 * Retrieves a value by using a key.
universe@691 803 *
universe@691 804 * @param map the map
universe@691 805 * @param key the key
universe@691 806 * @return the value
universe@691 807 */
universe@691 808 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 809 static inline void *cx_map_get(
universe@691 810 CxMap const *map,
universe@691 811 CxHashKey key
universe@691 812 ) {
universe@691 813 return map->cl->get(map, key);
universe@691 814 }
universe@691 815
universe@691 816 /**
universe@691 817 * Retrieves a value by using a key.
universe@691 818 *
universe@691 819 * @param map the map
universe@691 820 * @param key the key
universe@691 821 * @return the value
universe@691 822 */
universe@691 823 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 824 static inline void *cx_map_get_cxstr(
universe@691 825 CxMap const *map,
universe@691 826 cxstring key
universe@691 827 ) {
universe@691 828 return map->cl->get(map, cx_hash_key_cxstr(key));
universe@691 829 }
universe@691 830
universe@691 831 /**
universe@691 832 * Retrieves a value by using a key.
universe@691 833 *
universe@691 834 * @param map the map
universe@691 835 * @param key the key
universe@691 836 * @return the value
universe@691 837 */
universe@691 838 __attribute__((__nonnull__, __warn_unused_result__))
universe@692 839 static inline void *cx_map_get_mustr(
universe@692 840 CxMap const *map,
universe@692 841 cxmutstr key
universe@692 842 ) {
universe@692 843 return map->cl->get(map, cx_hash_key_cxstr(key));
universe@692 844 }
universe@692 845
universe@692 846 /**
universe@692 847 * Retrieves a value by using a key.
universe@692 848 *
universe@692 849 * @param map the map
universe@692 850 * @param key the key
universe@692 851 * @return the value
universe@692 852 */
universe@692 853 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 854 static inline void *cx_map_get_str(
universe@691 855 CxMap const *map,
universe@691 856 char const *key
universe@691 857 ) {
universe@691 858 return map->cl->get(map, cx_hash_key_str(key));
universe@691 859 }
universe@691 860
universe@691 861 /**
universe@691 862 * Retrieves a value by using a key.
universe@691 863 *
universe@691 864 * @param map the map
universe@691 865 * @param key the key
universe@691 866 * @return the value
universe@691 867 */
universe@691 868 #define cxMapGet(map, key) _Generic((key), \
universe@691 869 CxHashKey: cx_map_get, \
universe@691 870 cxstring: cx_map_get_cxstr, \
universe@692 871 cxmutstr: cx_map_get_mustr, \
universe@694 872 char*: cx_map_get_str, \
universe@694 873 char const*: cx_map_get_str) \
universe@691 874 (map, key)
universe@691 875
universe@691 876 /**
universe@691 877 * Removes a key/value-pair from the map by using the key.
universe@691 878 *
universe@691 879 * @param map the map
universe@691 880 * @param key the key
universe@691 881 */
universe@691 882 __attribute__((__nonnull__))
universe@691 883 static inline void cx_map_remove(
universe@691 884 CxMap *map,
universe@691 885 CxHashKey key
universe@691 886 ) {
universe@691 887 (void) map->cl->remove(map, key, true);
universe@691 888 }
universe@691 889
universe@691 890 /**
universe@691 891 * Removes a key/value-pair from the map by using the key.
universe@691 892 *
universe@691 893 * @param map the map
universe@691 894 * @param key the key
universe@691 895 */
universe@691 896 __attribute__((__nonnull__))
universe@691 897 static inline void cx_map_remove_cxstr(
universe@691 898 CxMap *map,
universe@691 899 cxstring key
universe@691 900 ) {
universe@691 901 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
universe@691 902 }
universe@691 903
universe@691 904 /**
universe@691 905 * Removes a key/value-pair from the map by using the key.
universe@691 906 *
universe@691 907 * @param map the map
universe@691 908 * @param key the key
universe@691 909 */
universe@691 910 __attribute__((__nonnull__))
universe@692 911 static inline void cx_map_remove_mustr(
universe@692 912 CxMap *map,
universe@692 913 cxmutstr key
universe@692 914 ) {
universe@692 915 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
universe@692 916 }
universe@692 917
universe@692 918 /**
universe@692 919 * Removes a key/value-pair from the map by using the key.
universe@692 920 *
universe@692 921 * @param map the map
universe@692 922 * @param key the key
universe@692 923 */
universe@692 924 __attribute__((__nonnull__))
universe@691 925 static inline void cx_map_remove_str(
universe@691 926 CxMap *map,
universe@691 927 char const *key
universe@691 928 ) {
universe@691 929 (void) map->cl->remove(map, cx_hash_key_str(key), true);
universe@691 930 }
universe@691 931
universe@691 932 /**
universe@691 933 * Removes a key/value-pair from the map by using the key.
universe@691 934 *
universe@691 935 * Always invokes the destructor function, if any, on the removed element.
universe@691 936 * If this map is storing pointers and you just want to retrieve the pointer
universe@691 937 * without invoking the destructor, use cxMapRemoveAndGet().
universe@691 938 * If you just want to detach the element from the map without invoking the
universe@691 939 * destructor or returning the element, use cxMapDetach().
universe@691 940 *
universe@691 941 * @param map the map
universe@691 942 * @param key the key
universe@691 943 * @see cxMapRemoveAndGet()
universe@691 944 * @see cxMapDetach()
universe@691 945 */
universe@691 946 #define cxMapRemove(map, key) _Generic((key), \
universe@691 947 CxHashKey: cx_map_remove, \
universe@691 948 cxstring: cx_map_remove_cxstr, \
universe@692 949 cxmutstr: cx_map_remove_mustr, \
universe@694 950 char*: cx_map_remove_str, \
universe@694 951 char const*: cx_map_remove_str) \
universe@691 952 (map, key)
universe@691 953
universe@691 954 /**
universe@691 955 * Detaches a key/value-pair from the map by using the key
universe@691 956 * without invoking the destructor.
universe@691 957 *
universe@691 958 * @param map the map
universe@691 959 * @param key the key
universe@691 960 */
universe@691 961 __attribute__((__nonnull__))
universe@691 962 static inline void cx_map_detach(
universe@691 963 CxMap *map,
universe@691 964 CxHashKey key
universe@691 965 ) {
universe@691 966 (void) map->cl->remove(map, key, false);
universe@691 967 }
universe@691 968
universe@691 969 /**
universe@691 970 * Detaches a key/value-pair from the map by using the key
universe@691 971 * without invoking the destructor.
universe@691 972 *
universe@691 973 * @param map the map
universe@691 974 * @param key the key
universe@691 975 */
universe@691 976 __attribute__((__nonnull__))
universe@691 977 static inline void cx_map_detach_cxstr(
universe@691 978 CxMap *map,
universe@691 979 cxstring key
universe@691 980 ) {
universe@691 981 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
universe@691 982 }
universe@691 983
universe@691 984 /**
universe@691 985 * Detaches a key/value-pair from the map by using the key
universe@691 986 * without invoking the destructor.
universe@691 987 *
universe@691 988 * @param map the map
universe@691 989 * @param key the key
universe@691 990 */
universe@691 991 __attribute__((__nonnull__))
universe@692 992 static inline void cx_map_detach_mustr(
universe@692 993 CxMap *map,
universe@692 994 cxmutstr key
universe@692 995 ) {
universe@692 996 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
universe@692 997 }
universe@692 998
universe@692 999 /**
universe@692 1000 * Detaches a key/value-pair from the map by using the key
universe@692 1001 * without invoking the destructor.
universe@692 1002 *
universe@692 1003 * @param map the map
universe@692 1004 * @param key the key
universe@692 1005 */
universe@692 1006 __attribute__((__nonnull__))
universe@691 1007 static inline void cx_map_detach_str(
universe@691 1008 CxMap *map,
universe@691 1009 char const *key
universe@691 1010 ) {
universe@691 1011 (void) map->cl->remove(map, cx_hash_key_str(key), false);
universe@691 1012 }
universe@691 1013
universe@691 1014 /**
universe@691 1015 * Detaches a key/value-pair from the map by using the key
universe@691 1016 * without invoking the destructor.
universe@691 1017 *
universe@691 1018 * In general, you should only use this function if the map does not own
universe@691 1019 * the data and there is a valid reference to the data somewhere else
universe@691 1020 * in the program. In all other cases it is preferable to use
universe@691 1021 * cxMapRemove() or cxMapRemoveAndGet().
universe@691 1022 *
universe@691 1023 * @param map the map
universe@691 1024 * @param key the key
universe@691 1025 * @see cxMapRemove()
universe@691 1026 * @see cxMapRemoveAndGet()
universe@691 1027 */
universe@691 1028 #define cxMapDetach(map, key) _Generic((key), \
universe@691 1029 CxHashKey: cx_map_detach, \
universe@691 1030 cxstring: cx_map_detach_cxstr, \
universe@692 1031 cxmutstr: cx_map_detach_mustr, \
universe@694 1032 char*: cx_map_detach_str, \
universe@694 1033 char const*: cx_map_detach_str) \
universe@691 1034 (map, key)
universe@691 1035
universe@691 1036 /**
universe@691 1037 * Removes a key/value-pair from the map by using the key.
universe@691 1038 *
universe@691 1039 * @param map the map
universe@691 1040 * @param key the key
universe@691 1041 * @return the stored pointer or \c NULL if either the key is not present
universe@691 1042 * in the map or the map is not storing pointers
universe@691 1043 */
universe@691 1044 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 1045 static inline void *cx_map_remove_and_get(
universe@691 1046 CxMap *map,
universe@691 1047 CxHashKey key
universe@691 1048 ) {
universe@691 1049 return map->cl->remove(map, key, !map->store_pointer);
universe@691 1050 }
universe@691 1051
universe@691 1052 /**
universe@691 1053 * Removes a key/value-pair from the map by using the key.
universe@691 1054 *
universe@691 1055 * @param map the map
universe@691 1056 * @param key the key
universe@691 1057 * @return the stored pointer or \c NULL if either the key is not present
universe@691 1058 * in the map or the map is not storing pointers
universe@691 1059 */
universe@691 1060 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 1061 static inline void *cx_map_remove_and_get_cxstr(
universe@691 1062 CxMap *map,
universe@691 1063 cxstring key
universe@691 1064 ) {
universe@691 1065 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
universe@691 1066 }
universe@691 1067
universe@691 1068 /**
universe@691 1069 * Removes a key/value-pair from the map by using the key.
universe@691 1070 *
universe@691 1071 * @param map the map
universe@691 1072 * @param key the key
universe@691 1073 * @return the stored pointer or \c NULL if either the key is not present
universe@691 1074 * in the map or the map is not storing pointers
universe@691 1075 */
universe@691 1076 __attribute__((__nonnull__, __warn_unused_result__))
universe@692 1077 static inline void *cx_map_remove_and_get_mustr(
universe@692 1078 CxMap *map,
universe@692 1079 cxmutstr key
universe@692 1080 ) {
universe@692 1081 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
universe@692 1082 }
universe@692 1083
universe@692 1084 /**
universe@692 1085 * Removes a key/value-pair from the map by using the key.
universe@692 1086 *
universe@692 1087 * @param map the map
universe@692 1088 * @param key the key
universe@692 1089 * @return the stored pointer or \c NULL if either the key is not present
universe@692 1090 * in the map or the map is not storing pointers
universe@692 1091 */
universe@692 1092 __attribute__((__nonnull__, __warn_unused_result__))
universe@691 1093 static inline void *cx_map_remove_and_get_str(
universe@691 1094 CxMap *map,
universe@691 1095 char const *key
universe@691 1096 ) {
universe@691 1097 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
universe@691 1098 }
universe@691 1099
universe@691 1100 /**
universe@691 1101 * Removes a key/value-pair from the map by using the key.
universe@691 1102 *
universe@691 1103 * This function can be used when the map is storing pointers,
universe@691 1104 * in order to retrieve the pointer from the map without invoking
universe@691 1105 * any destructor function. Sometimes you do not want the pointer
universe@691 1106 * to be returned - in that case (instead of suppressing the "unused
universe@691 1107 * result" warning) you can use cxMapDetach().
universe@691 1108 *
universe@691 1109 * If this map is not storing pointers, this function behaves like
universe@691 1110 * cxMapRemove() and returns \c NULL.
universe@691 1111 *
universe@691 1112 * @param map the map
universe@691 1113 * @param key the key
universe@691 1114 * @return the stored pointer or \c NULL if either the key is not present
universe@691 1115 * in the map or the map is not storing pointers
universe@691 1116 * @see cxMapStorePointers()
universe@691 1117 * @see cxMapDetach()
universe@691 1118 */
universe@691 1119 #define cxMapRemoveAndGet(map, key) _Generic((key), \
universe@691 1120 CxHashKey: cx_map_remove_and_get, \
universe@691 1121 cxstring: cx_map_remove_and_get_cxstr, \
universe@692 1122 cxmutstr: cx_map_remove_and_get_mustr, \
universe@694 1123 char*: cx_map_remove_and_get_str, \
universe@694 1124 char const*: cx_map_remove_and_get_str) \
universe@691 1125 (map, key)
universe@691 1126
universe@691 1127 #endif // __cplusplus
universe@691 1128
universe@691 1129 #endif // UCX_MAP_H

mercurial