src/cx/map.h

changeset 994
3603bdf4a78b
parent 993
b642eca4b956
equal deleted inserted replaced
993:b642eca4b956 994:3603bdf4a78b
118 CxHashKey key 118 CxHashKey key
119 ); 119 );
120 120
121 /** 121 /**
122 * Removes an element. 122 * Removes an element.
123 */ 123 *
124 cx_attr_nonnull 124 * Implementations SHALL check if \p targetbuf is set and copy the elements
125 void *(*remove)( 125 * to the buffer without invoking any destructor.
126 * When \p targetbuf is not set, the destructors SHALL be invoked.
127 *
128 * The function SHALL return zero when the \p key was found and
129 * non-zero, otherwise.
130 */
131 cx_attr_nonnull_arg(1)
132 cx_attr_access_w(3)
133 int (*remove)(
126 CxMap *map, 134 CxMap *map,
127 CxHashKey key, 135 CxHashKey key,
128 bool destroy 136 void *targetbuf
129 ); 137 );
130 138
131 /** 139 /**
132 * Creates an iterator for this map. 140 * Creates an iterator for this map.
133 */ 141 */
472 } 480 }
473 481
474 /** 482 /**
475 * Removes a key/value-pair from the map by using the key. 483 * Removes a key/value-pair from the map by using the key.
476 * 484 *
477 * Always invokes the destructor function, if any, on the removed element. 485 * Always invokes the destructors functions, if any, on the removed element.
478 * If this map is storing pointers and you just want to retrieve the pointer 486 *
479 * without invoking the destructor, use cxMapRemoveAndGet(). 487 * @param map the map
480 * If you just want to detach the element from the map without invoking the 488 * @param key the key
481 * destructor or returning the element, use cxMapDetach(). 489 * @return zero on success, non-zero if the key was not found
482 * 490 *
483 * @param map the map
484 * @param key the key
485 * @see cxMapRemoveAndGet() 491 * @see cxMapRemoveAndGet()
486 * @see cxMapDetach() 492 */
487 */ 493 cx_attr_nonnull
488 cx_attr_nonnull 494 static inline int cxMapRemove(
489 static inline void cxMapRemove(
490 CxMap *map, 495 CxMap *map,
491 CxHashKey const &key 496 CxHashKey const &key
492 ) { 497 ) {
493 (void) map->cl->remove(map, key, true); 498 return map->cl->remove(map, key, nullptr);
494 } 499 }
495 500
496 /** 501 /**
497 * Removes a key/value-pair from the map by using the key. 502 * Removes a key/value-pair from the map by using the key.
498 * 503 *
499 * Always invokes the destructor function, if any, on the removed element. 504 * Always invokes the destructors functions, if any, on the removed element.
500 * If this map is storing pointers and you just want to retrieve the pointer 505 *
501 * without invoking the destructor, use cxMapRemoveAndGet(). 506 * @param map the map
502 * If you just want to detach the element from the map without invoking the 507 * @param key the key
503 * destructor or returning the element, use cxMapDetach(). 508 * @return zero on success, non-zero if the key was not found
504 * 509 *
505 * @param map the map
506 * @param key the key
507 * @see cxMapRemoveAndGet() 510 * @see cxMapRemoveAndGet()
508 * @see cxMapDetach() 511 */
509 */ 512 cx_attr_nonnull
510 cx_attr_nonnull 513 static inline int cxMapRemove(
511 static inline void cxMapRemove(
512 CxMap *map, 514 CxMap *map,
513 cxstring const &key 515 cxstring const &key
514 ) { 516 ) {
515 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 517 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
516 } 518 }
517 519
518 /** 520 /**
519 * Removes a key/value-pair from the map by using the key. 521 * Removes a key/value-pair from the map by using the key.
520 * 522 *
521 * Always invokes the destructor function, if any, on the removed element. 523 * Always invokes the destructors functions, if any, on the removed element.
522 * If this map is storing pointers and you just want to retrieve the pointer 524 *
523 * without invoking the destructor, use cxMapRemoveAndGet(). 525 * @param map the map
524 * If you just want to detach the element from the map without invoking the 526 * @param key the key
525 * destructor or returning the element, use cxMapDetach(). 527 * @return zero on success, non-zero if the key was not found
526 * 528 *
527 * @param map the map
528 * @param key the key
529 * @see cxMapRemoveAndGet() 529 * @see cxMapRemoveAndGet()
530 * @see cxMapDetach() 530 */
531 */ 531 cx_attr_nonnull
532 cx_attr_nonnull 532 static inline int cxMapRemove(
533 static inline void cxMapRemove(
534 CxMap *map, 533 CxMap *map,
535 cxmutstr const &key 534 cxmutstr const &key
536 ) { 535 ) {
537 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 536 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
538 } 537 }
539 538
540 /** 539 /**
541 * Removes a key/value-pair from the map by using the key. 540 * Removes a key/value-pair from the map by using the key.
542 * 541 *
543 * Always invokes the destructor function, if any, on the removed element. 542 * Always invokes the destructors functions, if any, on the removed element.
544 * If this map is storing pointers and you just want to retrieve the pointer 543 *
545 * without invoking the destructor, use cxMapRemoveAndGet(). 544 * @param map the map
546 * If you just want to detach the element from the map without invoking the 545 * @param key the key
547 * destructor or returning the element, use cxMapDetach(). 546 * @return zero on success, non-zero if the key was not found
548 * 547 *
549 * @param map the map
550 * @param key the key
551 * @see cxMapRemoveAndGet() 548 * @see cxMapRemoveAndGet()
552 * @see cxMapDetach()
553 */ 549 */
554 cx_attr_nonnull 550 cx_attr_nonnull
555 cx_attr_cstr_arg(2) 551 cx_attr_cstr_arg(2)
556 static inline void cxMapRemove( 552 static inline int cxMapRemove(
557 CxMap *map, 553 CxMap *map,
558 const char *key 554 const char *key
559 ) { 555 ) {
560 (void) map->cl->remove(map, cx_hash_key_str(key), true); 556 return map->cl->remove(map, cx_hash_key_str(key), nullptr);
561 } 557 }
562 558
563 /** 559 /**
564 * Detaches a key/value-pair from the map by using the key 560 * Removes a key/value-pair from the map by using the key.
565 * without invoking the destructor. 561 *
566 * 562 * This function will copy the contents to the target buffer
567 * In general, you should only use this function if the map does not own 563 * which must be guaranteed to be large enough to hold the element.
568 * the data and there is a valid reference to the data somewhere else 564 * The destructor functions, if any, will \em not be called.
569 * in the program. In all other cases it is preferable to use 565 *
570 * cxMapRemove() or cxMapRemoveAndGet(). 566 * If this map is storing pointers, the element is the pointer itself
571 * 567 * and not the object it points to.
572 * @param map the map 568 *
573 * @param key the key 569 * @param map the map
570 * @param key the key
571 * @param targetbuf the buffer where the element shall be copied to
572 * @return zero on success, non-zero if the key was not found
573 *
574 * @see cxMapStorePointers()
574 * @see cxMapRemove() 575 * @see cxMapRemove()
575 * @see cxMapRemoveAndGet() 576 */
576 */ 577 cx_attr_nonnull
577 cx_attr_nonnull 578 cx_attr_access_w(3)
578 static inline void cxMapDetach( 579 static inline int cxMapRemoveAndGet(
579 CxMap *map, 580 CxMap *map,
580 CxHashKey const &key 581 CxHashKey key,
581 ) { 582 void *targetbuf
582 (void) map->cl->remove(map, key, false); 583 ) {
583 } 584 return map->cl->remove(map, key, targetbuf);
584 585 }
585 /** 586
586 * Detaches a key/value-pair from the map by using the key 587 /**
587 * without invoking the destructor. 588 * Removes a key/value-pair from the map by using the key.
588 * 589 *
589 * In general, you should only use this function if the map does not own 590 * This function will copy the contents to the target buffer
590 * the data and there is a valid reference to the data somewhere else 591 * which must be guaranteed to be large enough to hold the element.
591 * in the program. In all other cases it is preferable to use 592 * The destructor functions, if any, will \em not be called.
592 * cxMapRemove() or cxMapRemoveAndGet(). 593 *
593 * 594 * If this map is storing pointers, the element is the pointer itself
594 * @param map the map 595 * and not the object it points to.
595 * @param key the key 596 *
597 * @param map the map
598 * @param key the key
599 * @param targetbuf the buffer where the element shall be copied to
600 * @return zero on success, non-zero if the key was not found
601 *
602 * @see cxMapStorePointers()
596 * @see cxMapRemove() 603 * @see cxMapRemove()
597 * @see cxMapRemoveAndGet() 604 */
598 */ 605 cx_attr_nonnull
599 cx_attr_nonnull 606 cx_attr_access_w(3)
600 static inline void cxMapDetach( 607 static inline int cxMapRemoveAndGet(
601 CxMap *map, 608 CxMap *map,
602 cxstring const &key 609 cxstring key,
603 ) { 610 void *targetbuf
604 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 611 ) {
605 } 612 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
606 613 }
607 /** 614
608 * Detaches a key/value-pair from the map by using the key 615 /**
609 * without invoking the destructor. 616 * Removes a key/value-pair from the map by using the key.
610 * 617 *
611 * In general, you should only use this function if the map does not own 618 * This function will copy the contents to the target buffer
612 * the data and there is a valid reference to the data somewhere else 619 * which must be guaranteed to be large enough to hold the element.
613 * in the program. In all other cases it is preferable to use 620 * The destructor functions, if any, will \em not be called.
614 * cxMapRemove() or cxMapRemoveAndGet(). 621 *
615 * 622 * If this map is storing pointers, the element is the pointer itself
616 * @param map the map 623 * and not the object it points to.
617 * @param key the key 624 *
625 * @param map the map
626 * @param key the key
627 * @param targetbuf the buffer where the element shall be copied to
628 * @return zero on success, non-zero if the key was not found
629 *
630 * @see cxMapStorePointers()
618 * @see cxMapRemove() 631 * @see cxMapRemove()
619 * @see cxMapRemoveAndGet() 632 */
620 */ 633 cx_attr_nonnull
621 cx_attr_nonnull 634 cx_attr_access_w(3)
622 static inline void cxMapDetach( 635 static inline int cxMapRemoveAndGet(
623 CxMap *map, 636 CxMap *map,
624 cxmutstr const &key 637 cxmutstr key,
625 ) { 638 void *targetbuf
626 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 639 ) {
627 } 640 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
628 641 }
629 /** 642
630 * Detaches a key/value-pair from the map by using the key 643 /**
631 * without invoking the destructor. 644 * Removes a key/value-pair from the map by using the key.
632 * 645 *
633 * In general, you should only use this function if the map does not own 646 * This function will copy the contents to the target buffer
634 * the data and there is a valid reference to the data somewhere else 647 * which must be guaranteed to be large enough to hold the element.
635 * in the program. In all other cases it is preferable to use 648 * The destructor functions, if any, will \em not be called.
636 * cxMapRemove() or cxMapRemoveAndGet(). 649 *
637 * 650 * If this map is storing pointers, the element is the pointer itself
638 * @param map the map 651 * and not the object it points to.
639 * @param key the key 652 *
653 * @param map the map
654 * @param key the key
655 * @param targetbuf the buffer where the element shall be copied to
656 * @return zero on success, non-zero if the key was not found
657 *
658 * @see cxMapStorePointers()
640 * @see cxMapRemove() 659 * @see cxMapRemove()
641 * @see cxMapRemoveAndGet() 660 */
642 */ 661 cx_attr_nonnull
643 cx_attr_nonnull 662 cx_attr_access_w(3)
644 cx_attr_cstr_arg(2) 663 cx_attr_cstr_arg(2)
645 static inline void cxMapDetach( 664 static inline int cxMapRemoveAndGet(
646 CxMap *map, 665 CxMap *map,
647 const char *key 666 const char *key,
648 ) { 667 void *targetbuf
649 (void) map->cl->remove(map, cx_hash_key_str(key), false); 668 ) {
650 } 669 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
651
652 /**
653 * Removes a key/value-pair from the map by using the key.
654 *
655 * This function can be used when the map is storing pointers,
656 * in order to retrieve the pointer from the map without invoking
657 * any destructor function. Sometimes you do not want the pointer
658 * to be returned - in that case (instead of suppressing the "unused
659 * result" warning) you can use cxMapDetach().
660 *
661 * If this map is not storing pointers, this function behaves like
662 * cxMapRemove() and returns \c NULL.
663 *
664 * @param map the map
665 * @param key the key
666 * @return the stored pointer or \c NULL if either the key is not present
667 * in the map or the map is not storing pointers
668 * @see cxMapStorePointers()
669 * @see cxMapDetach()
670 */
671 cx_attr_nonnull
672 cx_attr_nodiscard
673 static inline void *cxMapRemoveAndGet(
674 CxMap *map,
675 CxHashKey key
676 ) {
677 return map->cl->remove(map, key, !map->collection.store_pointer);
678 }
679
680 /**
681 * Removes a key/value-pair from the map by using the key.
682 *
683 * This function can be used when the map is storing pointers,
684 * in order to retrieve the pointer from the map without invoking
685 * any destructor function. Sometimes you do not want the pointer
686 * to be returned - in that case (instead of suppressing the "unused
687 * result" warning) you can use cxMapDetach().
688 *
689 * If this map is not storing pointers, this function behaves like
690 * cxMapRemove() and returns \c NULL.
691 *
692 * @param map the map
693 * @param key the key
694 * @return the stored pointer or \c NULL if either the key is not present
695 * in the map or the map is not storing pointers
696 * @see cxMapStorePointers()
697 * @see cxMapDetach()
698 */
699 cx_attr_nonnull
700 cx_attr_nodiscard
701 static inline void *cxMapRemoveAndGet(
702 CxMap *map,
703 cxstring key
704 ) {
705 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
706 }
707
708 /**
709 * Removes a key/value-pair from the map by using the key.
710 *
711 * This function can be used when the map is storing pointers,
712 * in order to retrieve the pointer from the map without invoking
713 * any destructor function. Sometimes you do not want the pointer
714 * to be returned - in that case (instead of suppressing the "unused
715 * result" warning) you can use cxMapDetach().
716 *
717 * If this map is not storing pointers, this function behaves like
718 * cxMapRemove() and returns \c NULL.
719 *
720 * @param map the map
721 * @param key the key
722 * @return the stored pointer or \c NULL if either the key is not present
723 * in the map or the map is not storing pointers
724 * @see cxMapStorePointers()
725 * @see cxMapDetach()
726 */
727 cx_attr_nonnull
728 cx_attr_nodiscard
729 static inline void *cxMapRemoveAndGet(
730 CxMap *map,
731 cxmutstr key
732 ) {
733 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
734 }
735
736 /**
737 * Removes a key/value-pair from the map by using the key.
738 *
739 * This function can be used when the map is storing pointers,
740 * in order to retrieve the pointer from the map without invoking
741 * any destructor function. Sometimes you do not want the pointer
742 * to be returned - in that case (instead of suppressing the "unused
743 * result" warning) you can use cxMapDetach().
744 *
745 * If this map is not storing pointers, this function behaves like
746 * cxMapRemove() and returns \c NULL.
747 *
748 * @param map the map
749 * @param key the key
750 * @return the stored pointer or \c NULL if either the key is not present
751 * in the map or the map is not storing pointers
752 * @see cxMapStorePointers()
753 * @see cxMapDetach()
754 */
755 cx_attr_nonnull
756 cx_attr_nodiscard
757 cx_attr_cstr_arg(2)
758 static inline void *cxMapRemoveAndGet(
759 CxMap *map,
760 const char *key
761 ) {
762 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer);
763 } 670 }
764 671
765 #else // __cplusplus 672 #else // __cplusplus
766 673
767 /** 674 /**
930 (map, key) 837 (map, key)
931 838
932 /** 839 /**
933 * Removes a key/value-pair from the map by using the key. 840 * Removes a key/value-pair from the map by using the key.
934 * 841 *
935 * @param map the map 842 * Always invokes the destructors functions, if any, on the removed element.
936 * @param key the key 843 *
937 */ 844 * @param map the map
938 cx_attr_nonnull 845 * @param key the key
939 static inline void cx_map_remove( 846 * @return zero on success, non-zero if the key was not found
847 */
848 cx_attr_nonnull
849 static inline int cx_map_remove(
940 CxMap *map, 850 CxMap *map,
941 CxHashKey key 851 CxHashKey key
942 ) { 852 ) {
943 (void) map->cl->remove(map, key, true); 853 return map->cl->remove(map, key, NULL);
944 } 854 }
945 855
946 /** 856 /**
947 * Removes a key/value-pair from the map by using the key. 857 * Removes a key/value-pair from the map by using the key.
948 * 858 *
949 * @param map the map 859 * Always invokes the destructors functions, if any, on the removed element.
950 * @param key the key 860 *
951 */ 861 * @param map the map
952 cx_attr_nonnull 862 * @param key the key
953 static inline void cx_map_remove_cxstr( 863 * @return zero on success, non-zero if the key was not found
864 */
865 cx_attr_nonnull
866 static inline int cx_map_remove_cxstr(
954 CxMap *map, 867 CxMap *map,
955 cxstring key 868 cxstring key
956 ) { 869 ) {
957 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 870 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
958 } 871 }
959 872
960 /** 873 /**
961 * Removes a key/value-pair from the map by using the key. 874 * Removes a key/value-pair from the map by using the key.
962 * 875 *
963 * @param map the map 876 * Always invokes the destructors functions, if any, on the removed element.
964 * @param key the key 877 *
965 */ 878 * @param map the map
966 cx_attr_nonnull 879 * @param key the key
967 static inline void cx_map_remove_mustr( 880 * @return zero on success, non-zero if the key was not found
881 */
882 cx_attr_nonnull
883 static inline int cx_map_remove_mustr(
968 CxMap *map, 884 CxMap *map,
969 cxmutstr key 885 cxmutstr key
970 ) { 886 ) {
971 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 887 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
972 } 888 }
973 889
974 /** 890 /**
975 * Removes a key/value-pair from the map by using the key. 891 * Removes a key/value-pair from the map by using the key.
976 * 892 *
977 * @param map the map 893 * Always invokes the destructors functions, if any, on the removed element.
978 * @param key the key 894 *
895 * @param map the map
896 * @param key the key
897 * @return zero on success, non-zero if the key was not found
979 */ 898 */
980 cx_attr_nonnull 899 cx_attr_nonnull
981 cx_attr_cstr_arg(2) 900 cx_attr_cstr_arg(2)
982 static inline void cx_map_remove_str( 901 static inline int cx_map_remove_str(
983 CxMap *map, 902 CxMap *map,
984 const char *key 903 const char *key
985 ) { 904 ) {
986 (void) map->cl->remove(map, cx_hash_key_str(key), true); 905 return map->cl->remove(map, cx_hash_key_str(key), NULL);
987 } 906 }
988 907
989 /** 908 /**
990 * Removes a key/value-pair from the map by using the key. 909 * Removes a key/value-pair from the map by using the key.
991 * 910 *
992 * Always invokes the destructor function, if any, on the removed element. 911 * Always invokes the destructors functions, if any, on the removed element.
993 * If this map is storing pointers and you just want to retrieve the pointer 912 *
994 * without invoking the destructor, use cxMapRemoveAndGet(). 913 * @param map the map
995 * If you just want to detach the element from the map without invoking the 914 * @param key the key
996 * destructor or returning the element, use cxMapDetach(). 915 * @return zero on success, non-zero if the key was not found
997 * 916 *
998 * @param map the map
999 * @param key the key
1000 * @see cxMapRemoveAndGet() 917 * @see cxMapRemoveAndGet()
1001 * @see cxMapDetach()
1002 */ 918 */
1003 #define cxMapRemove(map, key) _Generic((key), \ 919 #define cxMapRemove(map, key) _Generic((key), \
1004 CxHashKey: cx_map_remove, \ 920 CxHashKey: cx_map_remove, \
1005 cxstring: cx_map_remove_cxstr, \ 921 cxstring: cx_map_remove_cxstr, \
1006 cxmutstr: cx_map_remove_mustr, \ 922 cxmutstr: cx_map_remove_mustr, \
1007 char*: cx_map_remove_str, \ 923 char*: cx_map_remove_str, \
1008 const char*: cx_map_remove_str) \ 924 const char*: cx_map_remove_str) \
1009 (map, key) 925 (map, key)
1010 926
1011 /** 927 /**
1012 * Detaches a key/value-pair from the map by using the key 928 * Removes a key/value-pair from the map by using the key.
1013 * without invoking the destructor. 929 *
1014 * 930 * This function will copy the contents to the target buffer
1015 * @param map the map 931 * which must be guaranteed to be large enough to hold the element.
1016 * @param key the key 932 * The destructor functions, if any, will \em not be called.
1017 */ 933 *
1018 cx_attr_nonnull 934 * If this map is storing pointers, the element is the pointer itself
1019 static inline void cx_map_detach( 935 * and not the object it points to.
1020 CxMap *map, 936 *
1021 CxHashKey key 937 * @param map the map
1022 ) { 938 * @param key the key
1023 (void) map->cl->remove(map, key, false); 939 * @param targetbuf the buffer where the element shall be copied to
1024 } 940 * @return zero on success, non-zero if the key was not found
1025 941 */
1026 /** 942 cx_attr_nonnull
1027 * Detaches a key/value-pair from the map by using the key 943 cx_attr_access_w(3)
1028 * without invoking the destructor. 944 static inline int cx_map_remove_and_get(
1029 * 945 CxMap *map,
1030 * @param map the map 946 CxHashKey key,
1031 * @param key the key 947 void *targetbuf
1032 */ 948 ) {
1033 cx_attr_nonnull 949 return map->cl->remove(map, key, targetbuf);
1034 static inline void cx_map_detach_cxstr( 950 }
1035 CxMap *map, 951
1036 cxstring key 952 /**
1037 ) { 953 * Removes a key/value-pair from the map by using the key.
1038 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 954 *
1039 } 955 * This function will copy the contents to the target buffer
1040 956 * which must be guaranteed to be large enough to hold the element.
1041 /** 957 * The destructor functions, if any, will \em not be called.
1042 * Detaches a key/value-pair from the map by using the key 958 *
1043 * without invoking the destructor. 959 * If this map is storing pointers, the element is the pointer itself
1044 * 960 * and not the object it points to.
1045 * @param map the map 961 *
1046 * @param key the key 962 * @param map the map
1047 */ 963 * @param key the key
1048 cx_attr_nonnull 964 * @param targetbuf the buffer where the element shall be copied to
1049 static inline void cx_map_detach_mustr( 965 * @return zero on success, non-zero if the key was not found
1050 CxMap *map, 966 */
1051 cxmutstr key 967 cx_attr_nonnull
1052 ) { 968 cx_attr_access_w(3)
1053 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 969 static inline int cx_map_remove_and_get_cxstr(
1054 } 970 CxMap *map,
1055 971 cxstring key,
1056 /** 972 void *targetbuf
1057 * Detaches a key/value-pair from the map by using the key 973 ) {
1058 * without invoking the destructor. 974 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
1059 * 975 }
1060 * @param map the map 976
1061 * @param key the key 977 /**
1062 */ 978 * Removes a key/value-pair from the map by using the key.
1063 cx_attr_nonnull 979 *
980 * This function will copy the contents to the target buffer
981 * which must be guaranteed to be large enough to hold the element.
982 * The destructor functions, if any, will \em not be called.
983 *
984 * If this map is storing pointers, the element is the pointer itself
985 * and not the object it points to.
986 *
987 * @param map the map
988 * @param key the key
989 * @param targetbuf the buffer where the element shall be copied to
990 * @return zero on success, non-zero if the key was not found
991 */
992 cx_attr_nonnull
993 cx_attr_access_w(3)
994 static inline int cx_map_remove_and_get_mustr(
995 CxMap *map,
996 cxmutstr key,
997 void *targetbuf
998 ) {
999 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
1000 }
1001
1002 /**
1003 * Removes a key/value-pair from the map by using the key.
1004 *
1005 * This function will copy the contents to the target buffer
1006 * which must be guaranteed to be large enough to hold the element.
1007 * The destructor functions, if any, will \em not be called.
1008 *
1009 * If this map is storing pointers, the element is the pointer itself
1010 * and not the object it points to.
1011 *
1012 * @param map the map
1013 * @param key the key
1014 * @param targetbuf the buffer where the element shall be copied to
1015 * @return zero on success, non-zero if the key was not found
1016 */
1017 cx_attr_nonnull
1018 cx_attr_access_w(3)
1064 cx_attr_cstr_arg(2) 1019 cx_attr_cstr_arg(2)
1065 static inline void cx_map_detach_str( 1020 static inline int cx_map_remove_and_get_str(
1066 CxMap *map, 1021 CxMap *map,
1067 const char *key 1022 const char *key,
1068 ) { 1023 void *targetbuf
1069 (void) map->cl->remove(map, cx_hash_key_str(key), false); 1024 ) {
1070 } 1025 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
1071 1026 }
1072 /** 1027
1073 * Detaches a key/value-pair from the map by using the key 1028 /**
1074 * without invoking the destructor. 1029 * Removes a key/value-pair from the map by using the key.
1075 * 1030 *
1076 * In general, you should only use this function if the map does not own 1031 * This function will copy the contents to the target buffer
1077 * the data and there is a valid reference to the data somewhere else 1032 * which must be guaranteed to be large enough to hold the element.
1078 * in the program. In all other cases it is preferable to use 1033 * The destructor functions, if any, will \em not be called.
1079 * cxMapRemove() or cxMapRemoveAndGet(). 1034 *
1080 * 1035 * If this map is storing pointers, the element is the pointer itself
1081 * @param map the map 1036 * and not the object it points to.
1082 * @param key the key 1037 *
1038 * @param map the map
1039 * @param key the key
1040 * @param targetbuf the buffer where the element shall be copied to
1041 * @return zero on success, non-zero if the key was not found
1042 *
1043 * @see cxMapStorePointers()
1083 * @see cxMapRemove() 1044 * @see cxMapRemove()
1084 * @see cxMapRemoveAndGet() 1045 */
1085 */ 1046 #define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \
1086 #define cxMapDetach(map, key) _Generic((key), \
1087 CxHashKey: cx_map_detach, \
1088 cxstring: cx_map_detach_cxstr, \
1089 cxmutstr: cx_map_detach_mustr, \
1090 char*: cx_map_detach_str, \
1091 const char*: cx_map_detach_str) \
1092 (map, key)
1093
1094 /**
1095 * Removes a key/value-pair from the map by using the key.
1096 *
1097 * @param map the map
1098 * @param key the key
1099 * @return the stored pointer or \c NULL if either the key is not present
1100 * in the map or the map is not storing pointers
1101 */
1102 cx_attr_nonnull
1103 cx_attr_nodiscard
1104 static inline void *cx_map_remove_and_get(
1105 CxMap *map,
1106 CxHashKey key
1107 ) {
1108 return map->cl->remove(map, key, !map->collection.store_pointer);
1109 }
1110
1111 /**
1112 * Removes a key/value-pair from the map by using the key.
1113 *
1114 * @param map the map
1115 * @param key the key
1116 * @return the stored pointer or \c NULL if either the key is not present
1117 * in the map or the map is not storing pointers
1118 */
1119 cx_attr_nonnull
1120 cx_attr_nodiscard
1121 static inline void *cx_map_remove_and_get_cxstr(
1122 CxMap *map,
1123 cxstring key
1124 ) {
1125 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
1126 }
1127
1128 /**
1129 * Removes a key/value-pair from the map by using the key.
1130 *
1131 * @param map the map
1132 * @param key the key
1133 * @return the stored pointer or \c NULL if either the key is not present
1134 * in the map or the map is not storing pointers
1135 */
1136 cx_attr_nonnull
1137 cx_attr_nodiscard
1138 static inline void *cx_map_remove_and_get_mustr(
1139 CxMap *map,
1140 cxmutstr key
1141 ) {
1142 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
1143 }
1144
1145 /**
1146 * Removes a key/value-pair from the map by using the key.
1147 *
1148 * @param map the map
1149 * @param key the key
1150 * @return the stored pointer or \c NULL if either the key is not present
1151 * in the map or the map is not storing pointers
1152 */
1153 cx_attr_nonnull
1154 cx_attr_nodiscard
1155 cx_attr_cstr_arg(2)
1156 static inline void *cx_map_remove_and_get_str(
1157 CxMap *map,
1158 const char *key
1159 ) {
1160 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer);
1161 }
1162
1163 /**
1164 * Removes a key/value-pair from the map by using the key.
1165 *
1166 * This function can be used when the map is storing pointers,
1167 * in order to retrieve the pointer from the map without invoking
1168 * any destructor function. Sometimes you do not want the pointer
1169 * to be returned - in that case (instead of suppressing the "unused
1170 * result" warning) you can use cxMapDetach().
1171 *
1172 * If this map is not storing pointers, this function behaves like
1173 * cxMapRemove() and returns \c NULL.
1174 *
1175 * @param map the map
1176 * @param key the key
1177 * @return the stored pointer or \c NULL if either the key is not present
1178 * in the map or the map is not storing pointers
1179 * @see cxMapStorePointers()
1180 * @see cxMapDetach()
1181 */
1182 #define cxMapRemoveAndGet(map, key) _Generic((key), \
1183 CxHashKey: cx_map_remove_and_get, \ 1047 CxHashKey: cx_map_remove_and_get, \
1184 cxstring: cx_map_remove_and_get_cxstr, \ 1048 cxstring: cx_map_remove_and_get_cxstr, \
1185 cxmutstr: cx_map_remove_and_get_mustr, \ 1049 cxmutstr: cx_map_remove_and_get_mustr, \
1186 char*: cx_map_remove_and_get_str, \ 1050 char*: cx_map_remove_and_get_str, \
1187 const char*: cx_map_remove_and_get_str) \ 1051 const char*: cx_map_remove_and_get_str) \
1188 (map, key) 1052 (map, key, targetbuf)
1189 1053
1190 #endif // __cplusplus 1054 #endif // __cplusplus
1191 1055
1192 #endif // UCX_MAP_H 1056 #endif // UCX_MAP_H

mercurial