src/cx/map.h

Fri, 21 Apr 2023 19:50:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Apr 2023 19:50:43 +0200
changeset 691
65baf7f45ac8
parent 689
5d0244c6fa3e
child 692
6ac92936cd44
permissions
-rw-r--r--

bring a generic interface to CxMap

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

mercurial