src/cx/map.h

Tue, 20 Jun 2023 18:30:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 20 Jun 2023 18:30:13 +0200
changeset 714
34565d898f1f
parent 710
2dd409ed056f
child 759
475335643af4
permissions
-rw-r--r--

add missing docs for cxEmptyMap

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

mercurial