ucx/cx/map.h

changeset 112
c3f2f16fa4b8
parent 102
64ded9f6a6c6
child 113
dde28a806552
equal deleted inserted replaced
111:81c4f73236a4 112:c3f2f16fa4b8
183 */ 183 */
184 void (*clear)(struct cx_map_s *map); 184 void (*clear)(struct cx_map_s *map);
185 185
186 /** 186 /**
187 * Add or overwrite an element. 187 * Add or overwrite an element.
188 */ 188 * If the @p value is @c NULL, the implementation
189 int (*put)( 189 * shall only allocate memory instead of adding an existing value to the map.
190 * Returns a pointer to the allocated memory or @c NULL if allocation fails.
191 */
192 void *(*put)(
190 CxMap *map, 193 CxMap *map,
191 CxHashKey key, 194 CxHashKey key,
192 void *value 195 void *value
193 ); 196 );
194 197
225 /** 228 /**
226 * A shared instance of an empty map. 229 * A shared instance of an empty map.
227 * 230 *
228 * Writing to that map is not allowed. 231 * Writing to that map is not allowed.
229 * 232 *
230 * You can use this is a placeholder for initializing CxMap pointers 233 * You can use this as a placeholder for initializing CxMap pointers
231 * for which you do not want to reserve memory right from the beginning. 234 * for which you do not want to reserve memory right from the beginning.
232 */ 235 */
233 cx_attr_export 236 cx_attr_export
234 extern CxMap *const cxEmptyMap; 237 extern CxMap *const cxEmptyMap;
235 238
275 * respective elements are stored. 278 * respective elements are stored.
276 * 279 *
277 * @note An iterator iterates over all elements successively. Therefore, the order 280 * @note An iterator iterates over all elements successively. Therefore, the order
278 * highly depends on the map implementation and may change arbitrarily when the contents change. 281 * highly depends on the map implementation and may change arbitrarily when the contents change.
279 * 282 *
280 * @param map the map to create the iterator for 283 * @param map the map to create the iterator for (can be @c NULL)
281 * @return an iterator for the currently stored values 284 * @return an iterator for the currently stored values
282 */ 285 */
283 cx_attr_nonnull
284 cx_attr_nodiscard 286 cx_attr_nodiscard
285 static inline CxMapIterator cxMapIteratorValues(const CxMap *map) { 287 static inline CxMapIterator cxMapIteratorValues(const CxMap *map) {
288 if (map == NULL) map = cxEmptyMap;
286 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); 289 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
287 } 290 }
288 291
289 /** 292 /**
290 * Creates a key iterator for a map. 293 * Creates a key iterator for a map.
291 * 294 *
292 * The elements of the iterator are keys of type CxHashKey and the pointer returned 295 * The elements of the iterator are keys of type CxHashKey, and the pointer returned
293 * during iterator shall be treated as @c const @c CxHashKey* . 296 * during iterator shall be treated as @c const @c CxHashKey* .
294 * 297 *
295 * @note An iterator iterates over all elements successively. Therefore, the order 298 * @note An iterator iterates over all elements successively. Therefore, the order
296 * highly depends on the map implementation and may change arbitrarily when the contents change. 299 * highly depends on the map implementation and may change arbitrarily when the contents change.
297 * 300 *
298 * @param map the map to create the iterator for 301 * @param map the map to create the iterator for (can be @c NULL)
299 * @return an iterator for the currently stored keys 302 * @return an iterator for the currently stored keys
300 */ 303 */
301 cx_attr_nonnull
302 cx_attr_nodiscard 304 cx_attr_nodiscard
303 static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) { 305 static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) {
306 if (map == NULL) map = cxEmptyMap;
304 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); 307 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
305 } 308 }
306 309
307 /** 310 /**
308 * Creates an iterator for a map. 311 * Creates an iterator for a map.
309 * 312 *
310 * The elements of the iterator are key/value pairs of type CxMapEntry and the pointer returned 313 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned
311 * during iterator shall be treated as @c const @c CxMapEntry* . 314 * during iterator shall be treated as @c const @c CxMapEntry* .
312 * 315 *
313 * @note An iterator iterates over all elements successively. Therefore, the order 316 * @note An iterator iterates over all elements successively. Therefore, the order
314 * highly depends on the map implementation and may change arbitrarily when the contents change. 317 * highly depends on the map implementation and may change arbitrarily when the contents change.
315 * 318 *
316 * @param map the map to create the iterator for 319 * @param map the map to create the iterator for (can be @c NULL)
317 * @return an iterator for the currently stored entries 320 * @return an iterator for the currently stored entries
318 * @see cxMapIteratorKeys() 321 * @see cxMapIteratorKeys()
319 * @see cxMapIteratorValues() 322 * @see cxMapIteratorValues()
320 */ 323 */
321 cx_attr_nonnull
322 cx_attr_nodiscard 324 cx_attr_nodiscard
323 static inline CxMapIterator cxMapIterator(const CxMap *map) { 325 static inline CxMapIterator cxMapIterator(const CxMap *map) {
326 if (map == NULL) map = cxEmptyMap;
324 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); 327 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
325 } 328 }
326 329
327 330
328 /** 331 /**
333 * respective elements are stored. 336 * respective elements are stored.
334 * 337 *
335 * @note An iterator iterates over all elements successively. Therefore, the order 338 * @note An iterator iterates over all elements successively. Therefore, the order
336 * highly depends on the map implementation and may change arbitrarily when the contents change. 339 * highly depends on the map implementation and may change arbitrarily when the contents change.
337 * 340 *
338 * @param map the map to create the iterator for 341 * @param map the map to create the iterator for (can be @c NULL)
339 * @return an iterator for the currently stored values 342 * @return an iterator for the currently stored values
340 */ 343 */
341 cx_attr_nonnull
342 cx_attr_nodiscard 344 cx_attr_nodiscard
343 cx_attr_export 345 cx_attr_export
344 CxMapIterator cxMapMutIteratorValues(CxMap *map); 346 CxMapIterator cxMapMutIteratorValues(CxMap *map);
345 347
346 /** 348 /**
347 * Creates a mutating iterator over the keys of a map. 349 * Creates a mutating iterator over the keys of a map.
348 * 350 *
349 * The elements of the iterator are keys of type CxHashKey and the pointer returned 351 * The elements of the iterator are keys of type CxHashKey, and the pointer returned
350 * during iterator shall be treated as @c const @c CxHashKey* . 352 * during iterator shall be treated as @c const @c CxHashKey* .
351 * 353 *
352 * @note An iterator iterates over all elements successively. Therefore, the order 354 * @note An iterator iterates over all elements successively. Therefore, the order
353 * highly depends on the map implementation and may change arbitrarily when the contents change. 355 * highly depends on the map implementation and may change arbitrarily when the contents change.
354 * 356 *
355 * @param map the map to create the iterator for 357 * @param map the map to create the iterator for (can be @c NULL)
356 * @return an iterator for the currently stored keys 358 * @return an iterator for the currently stored keys
357 */ 359 */
358 cx_attr_nonnull
359 cx_attr_nodiscard 360 cx_attr_nodiscard
360 cx_attr_export 361 cx_attr_export
361 CxMapIterator cxMapMutIteratorKeys(CxMap *map); 362 CxMapIterator cxMapMutIteratorKeys(CxMap *map);
362 363
363 /** 364 /**
364 * Creates a mutating iterator for a map. 365 * Creates a mutating iterator for a map.
365 * 366 *
366 * The elements of the iterator are key/value pairs of type CxMapEntry and the pointer returned 367 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned
367 * during iterator shall be treated as @c const @c CxMapEntry* . 368 * during iterator shall be treated as @c const @c CxMapEntry* .
368 * 369 *
369 * @note An iterator iterates over all elements successively. Therefore, the order 370 * @note An iterator iterates over all elements successively. Therefore, the order
370 * highly depends on the map implementation and may change arbitrarily when the contents change. 371 * highly depends on the map implementation and may change arbitrarily when the contents change.
371 * 372 *
372 * @param map the map to create the iterator for 373 * @param map the map to create the iterator for (can be @c NULL)
373 * @return an iterator for the currently stored entries 374 * @return an iterator for the currently stored entries
374 * @see cxMapMutIteratorKeys() 375 * @see cxMapMutIteratorKeys()
375 * @see cxMapMutIteratorValues() 376 * @see cxMapMutIteratorValues()
376 */ 377 */
377 cx_attr_nonnull
378 cx_attr_nodiscard 378 cx_attr_nodiscard
379 cx_attr_export 379 cx_attr_export
380 CxMapIterator cxMapMutIterator(CxMap *map); 380 CxMapIterator cxMapMutIterator(CxMap *map);
381 381
382 #ifdef __cplusplus 382 /**
383 } // end the extern "C" block here, because we want to start overloading 383 * Puts a key/value-pair into the map.
384 cx_attr_nonnull 384 *
385 static inline int cxMapPut( 385 * A possible existing value will be overwritten.
386 CxMap *map, 386 * If destructor functions are specified, they are called for
387 CxHashKey const &key, 387 * the overwritten element.
388 void *value 388 *
389 ) { 389 * If this map is storing pointers, the @p value pointer is written
390 return map->cl->put(map, key, value); 390 * to the map. Otherwise, the memory is copied from @p value with
391 } 391 * memcpy().
392 392 *
393 cx_attr_nonnull 393 * The @p key is always copied.
394 static inline int cxMapPut( 394 *
395 CxMap *map, 395 * @param map the map
396 cxstring const &key, 396 * @param key the key
397 void *value 397 * @param value the value
398 ) { 398 * @retval zero success
399 return map->cl->put(map, cx_hash_key_cxstr(key), value); 399 * @retval non-zero value on memory allocation failure
400 } 400 * @see cxMapPut()
401
402 cx_attr_nonnull
403 static inline int cxMapPut(
404 CxMap *map,
405 cxmutstr const &key,
406 void *value
407 ) {
408 return map->cl->put(map, cx_hash_key_cxstr(key), value);
409 }
410
411 cx_attr_nonnull
412 cx_attr_cstr_arg(2)
413 static inline int cxMapPut(
414 CxMap *map,
415 const char *key,
416 void *value
417 ) {
418 return map->cl->put(map, cx_hash_key_str(key), value);
419 }
420
421 cx_attr_nonnull
422 cx_attr_nodiscard
423 static inline void *cxMapGet(
424 const CxMap *map,
425 CxHashKey const &key
426 ) {
427 return map->cl->get(map, key);
428 }
429
430 cx_attr_nonnull
431 cx_attr_nodiscard
432 static inline void *cxMapGet(
433 const CxMap *map,
434 cxstring const &key
435 ) {
436 return map->cl->get(map, cx_hash_key_cxstr(key));
437 }
438
439 cx_attr_nonnull
440 cx_attr_nodiscard
441 static inline void *cxMapGet(
442 const CxMap *map,
443 cxmutstr const &key
444 ) {
445 return map->cl->get(map, cx_hash_key_cxstr(key));
446 }
447
448 cx_attr_nonnull
449 cx_attr_nodiscard
450 cx_attr_cstr_arg(2)
451 static inline void *cxMapGet(
452 const CxMap *map,
453 const char *key
454 ) {
455 return map->cl->get(map, cx_hash_key_str(key));
456 }
457
458 cx_attr_nonnull
459 static inline int cxMapRemove(
460 CxMap *map,
461 CxHashKey const &key
462 ) {
463 return map->cl->remove(map, key, nullptr);
464 }
465
466 cx_attr_nonnull
467 static inline int cxMapRemove(
468 CxMap *map,
469 cxstring const &key
470 ) {
471 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
472 }
473
474 cx_attr_nonnull
475 static inline int cxMapRemove(
476 CxMap *map,
477 cxmutstr const &key
478 ) {
479 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
480 }
481
482 cx_attr_nonnull
483 cx_attr_cstr_arg(2)
484 static inline int cxMapRemove(
485 CxMap *map,
486 const char *key
487 ) {
488 return map->cl->remove(map, cx_hash_key_str(key), nullptr);
489 }
490
491 cx_attr_nonnull
492 cx_attr_access_w(3)
493 static inline int cxMapRemoveAndGet(
494 CxMap *map,
495 CxHashKey key,
496 void *targetbuf
497 ) {
498 return map->cl->remove(map, key, targetbuf);
499 }
500
501 cx_attr_nonnull
502 cx_attr_access_w(3)
503 static inline int cxMapRemoveAndGet(
504 CxMap *map,
505 cxstring key,
506 void *targetbuf
507 ) {
508 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
509 }
510
511 cx_attr_nonnull
512 cx_attr_access_w(3)
513 static inline int cxMapRemoveAndGet(
514 CxMap *map,
515 cxmutstr key,
516 void *targetbuf
517 ) {
518 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
519 }
520
521 cx_attr_nonnull
522 cx_attr_access_w(3)
523 cx_attr_cstr_arg(2)
524 static inline int cxMapRemoveAndGet(
525 CxMap *map,
526 const char *key,
527 void *targetbuf
528 ) {
529 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
530 }
531
532 #else // __cplusplus
533
534 /**
535 * @copydoc cxMapPut()
536 */ 401 */
537 cx_attr_nonnull 402 cx_attr_nonnull
538 static inline int cx_map_put( 403 static inline int cx_map_put(
539 CxMap *map, 404 CxMap *map,
540 CxHashKey key, 405 CxHashKey key,
541 void *value 406 void *value
542 ) { 407 ) {
543 return map->cl->put(map, key, value); 408 return map->cl->put(map, key, value) == NULL;
544 }
545
546 /**
547 * @copydoc cxMapPut()
548 */
549 cx_attr_nonnull
550 static inline int cx_map_put_cxstr(
551 CxMap *map,
552 cxstring key,
553 void *value
554 ) {
555 return map->cl->put(map, cx_hash_key_cxstr(key), value);
556 }
557
558 /**
559 * @copydoc cxMapPut()
560 */
561 cx_attr_nonnull
562 static inline int cx_map_put_mustr(
563 CxMap *map,
564 cxmutstr key,
565 void *value
566 ) {
567 return map->cl->put(map, cx_hash_key_cxstr(key), value);
568 }
569
570 /**
571 * @copydoc cxMapPut()
572 */
573 cx_attr_nonnull
574 cx_attr_cstr_arg(2)
575 static inline int cx_map_put_str(
576 CxMap *map,
577 const char *key,
578 void *value
579 ) {
580 return map->cl->put(map, cx_hash_key_str(key), value);
581 } 409 }
582 410
583 /** 411 /**
584 * Puts a key/value-pair into the map. 412 * Puts a key/value-pair into the map.
585 * 413 *
592 * memcpy(). 420 * memcpy().
593 * 421 *
594 * The @p key is always copied. 422 * The @p key is always copied.
595 * 423 *
596 * @param map (@c CxMap*) the map 424 * @param map (@c CxMap*) the map
597 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key 425 * @param key (any supported key type) the key
598 * @param value (@c void*) the value 426 * @param value (@c void*) the value
599 * @retval zero success 427 * @retval zero success
600 * @retval non-zero value on memory allocation failure 428 * @retval non-zero value on memory allocation failure
601 */ 429 * @see CX_HASH_KEY()
602 #define cxMapPut(map, key, value) _Generic((key), \ 430 */
603 CxHashKey: cx_map_put, \ 431 #define cxMapPut(map, key, value) cx_map_put(map, CX_HASH_KEY(key), value)
604 cxstring: cx_map_put_cxstr, \ 432
605 cxmutstr: cx_map_put_mustr, \ 433 /**
606 char*: cx_map_put_str, \ 434 * Allocates memory for a value in the map associated with the specified key.
607 const char*: cx_map_put_str) \ 435 *
608 (map, key, value) 436 * A possible existing value will be overwritten.
609 437 * If destructor functions are specified, they are called for
610 /** 438 * the overwritten element.
611 * @copydoc cxMapGet() 439 *
440 * If the map is storing pointers, this function returns a @c void** pointer,
441 * meaning a pointer to that pointer.
442 *
443 * The @p key is always copied.
444 *
445 * @param map the map
446 * @param key the key
447 * @return the pointer to the allocated memory or @c NULL if allocation fails
448 * @retval zero success
449 * @retval non-zero value on memory allocation failure
450 * @see cxMapEmplace()
451 */
452 cx_attr_nonnull
453 static inline void *cx_map_emplace(
454 CxMap *map,
455 CxHashKey key
456 ) {
457 return map->cl->put(map, key, NULL);
458 }
459
460 /**
461 * Allocates memory for a value in the map associated with the specified key.
462 *
463 * A possible existing value will be overwritten.
464 * If destructor functions are specified, they are called for
465 * the overwritten element.
466 *
467 * If the map is storing pointers, this function returns a @c void** pointer,
468 * meaning a pointer to that pointer.
469 *
470 * The @p key is always copied.
471 *
472 * @param map (@c CxMap*) the map
473 * @param key (any supported key type) the key
474 * @return the pointer to the allocated memory or @c NULL if allocation fails
475 * @retval zero success
476 * @retval non-zero value on memory allocation failure
477 * @see CX_HASH_KEY()
478 */
479 #define cxMapEmplace(map, key) cx_map_emplace(map, CX_HASH_KEY(key))
480
481 /**
482 * Retrieves a value by using a key.
483 *
484 * If this map is storing pointers, the stored pointer is returned.
485 * Otherwise, a pointer to the element within the map's memory
486 * is returned (which is valid as long as the element stays in the map).
487 *
488 * @param map the map
489 * @param key the key
490 * @return the value
491 * @see cxMapGet()
612 */ 492 */
613 cx_attr_nonnull 493 cx_attr_nonnull
614 cx_attr_nodiscard 494 cx_attr_nodiscard
615 static inline void *cx_map_get( 495 static inline void *cx_map_get(
616 const CxMap *map, 496 const CxMap *map,
618 ) { 498 ) {
619 return map->cl->get(map, key); 499 return map->cl->get(map, key);
620 } 500 }
621 501
622 /** 502 /**
623 * @copydoc cxMapGet()
624 */
625 cx_attr_nonnull
626 cx_attr_nodiscard
627 static inline void *cx_map_get_cxstr(
628 const CxMap *map,
629 cxstring key
630 ) {
631 return map->cl->get(map, cx_hash_key_cxstr(key));
632 }
633
634 /**
635 * @copydoc cxMapGet()
636 */
637 cx_attr_nonnull
638 cx_attr_nodiscard
639 static inline void *cx_map_get_mustr(
640 const CxMap *map,
641 cxmutstr key
642 ) {
643 return map->cl->get(map, cx_hash_key_cxstr(key));
644 }
645
646 /**
647 * @copydoc cxMapGet()
648 */
649 cx_attr_nonnull
650 cx_attr_nodiscard
651 cx_attr_cstr_arg(2)
652 static inline void *cx_map_get_str(
653 const CxMap *map,
654 const char *key
655 ) {
656 return map->cl->get(map, cx_hash_key_str(key));
657 }
658
659 /**
660 * Retrieves a value by using a key. 503 * Retrieves a value by using a key.
661 * 504 *
662 * If this map is storing pointers, the stored pointer is returned. 505 * If this map is storing pointers, the stored pointer is returned.
663 * Otherwise, a pointer to the element within the map's memory 506 * Otherwise, a pointer to the element within the map's memory
664 * is returned (which is valid as long as the element stays in the map). 507 * is returned (which is valid as long as the element stays in the map).
665 * 508 *
666 * @param map (@c CxMap*) the map 509 * @param map (@c CxMap*) the map
667 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key 510 * @param key (any supported key type) the key
668 * @return (@c void*) the value 511 * @return (@c void*) the value
669 */ 512 * @see CX_HASH_KEY()
670 #define cxMapGet(map, key) _Generic((key), \ 513 */
671 CxHashKey: cx_map_get, \ 514 #define cxMapGet(map, key) cx_map_get(map, CX_HASH_KEY(key))
672 cxstring: cx_map_get_cxstr, \ 515
673 cxmutstr: cx_map_get_mustr, \ 516 /**
674 char*: cx_map_get_str, \ 517 * Removes a key/value-pair from the map by using the key.
675 const char*: cx_map_get_str) \ 518 *
676 (map, key) 519 * Invokes the destructor functions, if any, on the removed element if and only if the
677 520 * @p targetbuf is @c NULL.
678 /** 521 *
679 * @copydoc cxMapRemove() 522 * @param map the map
680 */ 523 * @param key the key
681 cx_attr_nonnull 524 * @param targetbuf the optional buffer where the removed element shall be copied to
525 * @retval zero success
526 * @retval non-zero the key was not found
527 *
528 * @see cxMapRemove()
529 * @see cxMapRemoveAndGet()
530 */
531 cx_attr_nonnull_arg(1)
682 static inline int cx_map_remove( 532 static inline int cx_map_remove(
683 CxMap *map,
684 CxHashKey key
685 ) {
686 return map->cl->remove(map, key, NULL);
687 }
688
689 /**
690 * @copydoc cxMapRemove()
691 */
692 cx_attr_nonnull
693 static inline int cx_map_remove_cxstr(
694 CxMap *map,
695 cxstring key
696 ) {
697 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
698 }
699
700 /**
701 * @copydoc cxMapRemove()
702 */
703 cx_attr_nonnull
704 static inline int cx_map_remove_mustr(
705 CxMap *map,
706 cxmutstr key
707 ) {
708 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
709 }
710
711 /**
712 * @copydoc cxMapRemove()
713 */
714 cx_attr_nonnull
715 cx_attr_cstr_arg(2)
716 static inline int cx_map_remove_str(
717 CxMap *map,
718 const char *key
719 ) {
720 return map->cl->remove(map, cx_hash_key_str(key), NULL);
721 }
722
723 /**
724 * Removes a key/value-pair from the map by using the key.
725 *
726 * Always invokes the destructors functions, if any, on the removed element.
727 *
728 * @param map (@c CxMap*) the map
729 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
730 * @retval zero success
731 * @retval non-zero the key was not found
732 *
733 * @see cxMapRemoveAndGet()
734 */
735 #define cxMapRemove(map, key) _Generic((key), \
736 CxHashKey: cx_map_remove, \
737 cxstring: cx_map_remove_cxstr, \
738 cxmutstr: cx_map_remove_mustr, \
739 char*: cx_map_remove_str, \
740 const char*: cx_map_remove_str) \
741 (map, key)
742
743 /**
744 * @copydoc cxMapRemoveAndGet()
745 */
746 cx_attr_nonnull
747 cx_attr_access_w(3)
748 static inline int cx_map_remove_and_get(
749 CxMap *map, 533 CxMap *map,
750 CxHashKey key, 534 CxHashKey key,
751 void *targetbuf 535 void *targetbuf
752 ) { 536 ) {
753 return map->cl->remove(map, key, targetbuf); 537 return map->cl->remove(map, key, targetbuf);
754 } 538 }
755 539
756 /** 540 /**
757 * @copydoc cxMapRemoveAndGet() 541 * Removes a key/value-pair from the map by using the key.
758 */ 542 *
759 cx_attr_nonnull 543 * Always invokes the destructor functions, if any, on the removed element.
760 cx_attr_access_w(3) 544 *
761 static inline int cx_map_remove_and_get_cxstr( 545 * @param map (@c CxMap*) the map
762 CxMap *map, 546 * @param key (any supported key type) the key
763 cxstring key, 547 * @retval zero success
764 void *targetbuf 548 * @retval non-zero the key was not found
765 ) { 549 *
766 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf); 550 * @see cxMapRemoveAndGet()
767 } 551 * @see CX_HASH_KEY()
768 552 */
769 /** 553 #define cxMapRemove(map, key) cx_map_remove(map, CX_HASH_KEY(key), NULL)
770 * @copydoc cxMapRemoveAndGet()
771 */
772 cx_attr_nonnull
773 cx_attr_access_w(3)
774 static inline int cx_map_remove_and_get_mustr(
775 CxMap *map,
776 cxmutstr key,
777 void *targetbuf
778 ) {
779 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
780 }
781
782 /**
783 * @copydoc cxMapRemoveAndGet()
784 */
785 cx_attr_nonnull
786 cx_attr_access_w(3)
787 cx_attr_cstr_arg(2)
788 static inline int cx_map_remove_and_get_str(
789 CxMap *map,
790 const char *key,
791 void *targetbuf
792 ) {
793 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
794 }
795 554
796 /** 555 /**
797 * Removes a key/value-pair from the map by using the key. 556 * Removes a key/value-pair from the map by using the key.
798 * 557 *
799 * This function will copy the contents of the removed element 558 * This function will copy the contents of the removed element
803 * 562 *
804 * If this map is storing pointers, the element is the pointer itself 563 * If this map is storing pointers, the element is the pointer itself
805 * and not the object it points to. 564 * and not the object it points to.
806 * 565 *
807 * @param map (@c CxMap*) the map 566 * @param map (@c CxMap*) the map
808 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key 567 * @param key (any supported key type) the key
809 * @param targetbuf (@c void*) the buffer where the element shall be copied to 568 * @param targetbuf (@c void*) the buffer where the element shall be copied to
810 * @retval zero success 569 * @retval zero success
811 * @retval non-zero the key was not found 570 * @retval non-zero the key was not found
812 * 571 *
813 * @see cxMapRemove() 572 * @see cxMapRemove()
814 */ 573 * @see CX_HASH_KEY()
815 #define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \ 574 */
816 CxHashKey: cx_map_remove_and_get, \ 575 #define cxMapRemoveAndGet(map, key, targetbuf) cx_map_remove(map, CX_HASH_KEY(key), targetbuf)
817 cxstring: cx_map_remove_and_get_cxstr, \ 576
818 cxmutstr: cx_map_remove_and_get_mustr, \ 577 #ifdef __cplusplus
819 char*: cx_map_remove_and_get_str, \ 578 } // extern "C"
820 const char*: cx_map_remove_and_get_str) \ 579 #endif
821 (map, key, targetbuf)
822
823 #endif // __cplusplus
824 580
825 #endif // UCX_MAP_H 581 #endif // UCX_MAP_H

mercurial