src/cx/map.h

changeset 692
6ac92936cd44
parent 691
65baf7f45ac8
child 694
ac827d873c17
equal deleted inserted replaced
691:65baf7f45ac8 692:6ac92936cd44
359 * @return 0 on success, non-zero value on failure 359 * @return 0 on success, non-zero value on failure
360 */ 360 */
361 __attribute__((__nonnull__)) 361 __attribute__((__nonnull__))
362 static inline int cxMapPut( 362 static inline int cxMapPut(
363 CxMap *map, 363 CxMap *map,
364 cxmutstr const &key,
365 void *value
366 ) {
367 return map->cl->put(map, cx_hash_key_cxstr(key), value);
368 }
369
370 /**
371 * Puts a key/value-pair into the map.
372 *
373 * @param map the map
374 * @param key the key
375 * @param value the value
376 * @return 0 on success, non-zero value on failure
377 */
378 __attribute__((__nonnull__))
379 static inline int cxMapPut(
380 CxMap *map,
364 char const *key, 381 char const *key,
365 void *value 382 void *value
366 ) { 383 ) {
367 return map->cl->put(map, cx_hash_key_str(key), value); 384 return map->cl->put(map, cx_hash_key_str(key), value);
368 } 385 }
391 */ 408 */
392 __attribute__((__nonnull__, __warn_unused_result__)) 409 __attribute__((__nonnull__, __warn_unused_result__))
393 static inline void *cxMapGet( 410 static inline void *cxMapGet(
394 CxMap const *map, 411 CxMap const *map,
395 cxstring const &key 412 cxstring const &key
413 ) {
414 return map->cl->get(map, cx_hash_key_cxstr(key));
415 }
416
417 /**
418 * Retrieves a value by using a key.
419 *
420 * @param map the map
421 * @param key the key
422 * @return the value
423 */
424 __attribute__((__nonnull__, __warn_unused_result__))
425 static inline void *cxMapGet(
426 CxMap const *map,
427 cxmutstr const &key
396 ) { 428 ) {
397 return map->cl->get(map, cx_hash_key_cxstr(key)); 429 return map->cl->get(map, cx_hash_key_cxstr(key));
398 } 430 }
399 431
400 /** 432 /**
471 * @see cxMapDetach() 503 * @see cxMapDetach()
472 */ 504 */
473 __attribute__((__nonnull__)) 505 __attribute__((__nonnull__))
474 static inline void cxMapRemove( 506 static inline void cxMapRemove(
475 CxMap *map, 507 CxMap *map,
508 cxmutstr const &key
509 ) {
510 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
511 }
512
513 /**
514 * Removes a key/value-pair from the map by using the key.
515 *
516 * Always invokes the destructor function, if any, on the removed element.
517 * If this map is storing pointers and you just want to retrieve the pointer
518 * without invoking the destructor, use cxMapRemoveAndGet().
519 * If you just want to detach the element from the map without invoking the
520 * destructor or returning the element, use cxMapDetach().
521 *
522 * @param map the map
523 * @param key the key
524 * @see cxMapRemoveAndGet()
525 * @see cxMapDetach()
526 */
527 __attribute__((__nonnull__))
528 static inline void cxMapRemove(
529 CxMap *map,
476 char const *key 530 char const *key
477 ) { 531 ) {
478 (void) map->cl->remove(map, cx_hash_key_str(key), true); 532 (void) map->cl->remove(map, cx_hash_key_str(key), true);
479 } 533 }
480 534
516 */ 570 */
517 __attribute__((__nonnull__)) 571 __attribute__((__nonnull__))
518 static inline void cxMapDetach( 572 static inline void cxMapDetach(
519 CxMap *map, 573 CxMap *map,
520 cxstring const &key 574 cxstring const &key
575 ) {
576 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
577 }
578
579 /**
580 * Detaches a key/value-pair from the map by using the key
581 * without invoking the destructor.
582 *
583 * In general, you should only use this function if the map does not own
584 * the data and there is a valid reference to the data somewhere else
585 * in the program. In all other cases it is preferable to use
586 * cxMapRemove() or cxMapRemoveAndGet().
587 *
588 * @param map the map
589 * @param key the key
590 * @see cxMapRemove()
591 * @see cxMapRemoveAndGet()
592 */
593 __attribute__((__nonnull__))
594 static inline void cxMapDetach(
595 CxMap *map,
596 cxmutstr const &key
521 ) { 597 ) {
522 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 598 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
523 } 599 }
524 600
525 /** 601 /**
618 * @see cxMapDetach() 694 * @see cxMapDetach()
619 */ 695 */
620 __attribute__((__nonnull__, __warn_unused_result__)) 696 __attribute__((__nonnull__, __warn_unused_result__))
621 static inline void *cxMapRemoveAndGet( 697 static inline void *cxMapRemoveAndGet(
622 CxMap *map, 698 CxMap *map,
699 cxmutstr key
700 ) {
701 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
702 }
703
704 /**
705 * Removes a key/value-pair from the map by using the key.
706 *
707 * This function can be used when the map is storing pointers,
708 * in order to retrieve the pointer from the map without invoking
709 * any destructor function. Sometimes you do not want the pointer
710 * to be returned - in that case (instead of suppressing the "unused
711 * result" warning) you can use cxMapDetach().
712 *
713 * If this map is not storing pointers, this function behaves like
714 * cxMapRemove() and returns \c NULL.
715 *
716 * @param map the map
717 * @param key the key
718 * @return the stored pointer or \c NULL if either the key is not present
719 * in the map or the map is not storing pointers
720 * @see cxMapStorePointers()
721 * @see cxMapDetach()
722 */
723 __attribute__((__nonnull__, __warn_unused_result__))
724 static inline void *cxMapRemoveAndGet(
725 CxMap *map,
623 char const *key 726 char const *key
624 ) { 727 ) {
625 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); 728 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
626 } 729 }
627 730
654 */ 757 */
655 __attribute__((__nonnull__)) 758 __attribute__((__nonnull__))
656 static inline int cx_map_put_cxstr( 759 static inline int cx_map_put_cxstr(
657 CxMap *map, 760 CxMap *map,
658 cxstring key, 761 cxstring key,
762 void *value
763 ) {
764 return map->cl->put(map, cx_hash_key_cxstr(key), value);
765 }
766
767 /**
768 * Puts a key/value-pair into the map.
769 *
770 * @param map the map
771 * @param key the key
772 * @param value the value
773 * @return 0 on success, non-zero value on failure
774 */
775 __attribute__((__nonnull__))
776 static inline int cx_map_put_mustr(
777 CxMap *map,
778 cxmutstr key,
659 void *value 779 void *value
660 ) { 780 ) {
661 return map->cl->put(map, cx_hash_key_cxstr(key), value); 781 return map->cl->put(map, cx_hash_key_cxstr(key), value);
662 } 782 }
663 783
687 * @return 0 on success, non-zero value on failure 807 * @return 0 on success, non-zero value on failure
688 */ 808 */
689 #define cxMapPut(map, key, value) _Generic((key), \ 809 #define cxMapPut(map, key, value) _Generic((key), \
690 CxHashKey: cx_map_put, \ 810 CxHashKey: cx_map_put, \
691 cxstring: cx_map_put_cxstr, \ 811 cxstring: cx_map_put_cxstr, \
812 cxmutstr: cx_map_put_mustr, \
692 char*: cx_map_put_str) \ 813 char*: cx_map_put_str) \
693 (map, key, value) 814 (map, key, value)
694 815
695 /** 816 /**
696 * Retrieves a value by using a key. 817 * Retrieves a value by using a key.
728 * @param map the map 849 * @param map the map
729 * @param key the key 850 * @param key the key
730 * @return the value 851 * @return the value
731 */ 852 */
732 __attribute__((__nonnull__, __warn_unused_result__)) 853 __attribute__((__nonnull__, __warn_unused_result__))
854 static inline void *cx_map_get_mustr(
855 CxMap const *map,
856 cxmutstr key
857 ) {
858 return map->cl->get(map, cx_hash_key_cxstr(key));
859 }
860
861 /**
862 * Retrieves a value by using a key.
863 *
864 * @param map the map
865 * @param key the key
866 * @return the value
867 */
868 __attribute__((__nonnull__, __warn_unused_result__))
733 static inline void *cx_map_get_str( 869 static inline void *cx_map_get_str(
734 CxMap const *map, 870 CxMap const *map,
735 char const *key 871 char const *key
736 ) { 872 ) {
737 return map->cl->get(map, cx_hash_key_str(key)); 873 return map->cl->get(map, cx_hash_key_str(key));
745 * @return the value 881 * @return the value
746 */ 882 */
747 #define cxMapGet(map, key) _Generic((key), \ 883 #define cxMapGet(map, key) _Generic((key), \
748 CxHashKey: cx_map_get, \ 884 CxHashKey: cx_map_get, \
749 cxstring: cx_map_get_cxstr, \ 885 cxstring: cx_map_get_cxstr, \
886 cxmutstr: cx_map_get_mustr, \
750 char*: cx_map_get_str) \ 887 char*: cx_map_get_str) \
751 (map, key) 888 (map, key)
752 889
753 /** 890 /**
754 * 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.
772 */ 909 */
773 __attribute__((__nonnull__)) 910 __attribute__((__nonnull__))
774 static inline void cx_map_remove_cxstr( 911 static inline void cx_map_remove_cxstr(
775 CxMap *map, 912 CxMap *map,
776 cxstring key 913 cxstring key
914 ) {
915 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
916 }
917
918 /**
919 * Removes a key/value-pair from the map by using the key.
920 *
921 * @param map the map
922 * @param key the key
923 */
924 __attribute__((__nonnull__))
925 static inline void cx_map_remove_mustr(
926 CxMap *map,
927 cxmutstr key
777 ) { 928 ) {
778 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 929 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true);
779 } 930 }
780 931
781 /** 932 /**
807 * @see cxMapDetach() 958 * @see cxMapDetach()
808 */ 959 */
809 #define cxMapRemove(map, key) _Generic((key), \ 960 #define cxMapRemove(map, key) _Generic((key), \
810 CxHashKey: cx_map_remove, \ 961 CxHashKey: cx_map_remove, \
811 cxstring: cx_map_remove_cxstr, \ 962 cxstring: cx_map_remove_cxstr, \
963 cxmutstr: cx_map_remove_mustr, \
812 char*: cx_map_remove_str) \ 964 char*: cx_map_remove_str) \
813 (map, key) 965 (map, key)
814 966
815 /** 967 /**
816 * Detaches a key/value-pair from the map by using the key 968 * Detaches a key/value-pair from the map by using the key
836 */ 988 */
837 __attribute__((__nonnull__)) 989 __attribute__((__nonnull__))
838 static inline void cx_map_detach_cxstr( 990 static inline void cx_map_detach_cxstr(
839 CxMap *map, 991 CxMap *map,
840 cxstring key 992 cxstring key
993 ) {
994 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
995 }
996
997 /**
998 * Detaches a key/value-pair from the map by using the key
999 * without invoking the destructor.
1000 *
1001 * @param map the map
1002 * @param key the key
1003 */
1004 __attribute__((__nonnull__))
1005 static inline void cx_map_detach_mustr(
1006 CxMap *map,
1007 cxmutstr key
841 ) { 1008 ) {
842 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 1009 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false);
843 } 1010 }
844 1011
845 /** 1012 /**
872 * @see cxMapRemoveAndGet() 1039 * @see cxMapRemoveAndGet()
873 */ 1040 */
874 #define cxMapDetach(map, key) _Generic((key), \ 1041 #define cxMapDetach(map, key) _Generic((key), \
875 CxHashKey: cx_map_detach, \ 1042 CxHashKey: cx_map_detach, \
876 cxstring: cx_map_detach_cxstr, \ 1043 cxstring: cx_map_detach_cxstr, \
1044 cxmutstr: cx_map_detach_mustr, \
877 char*: cx_map_detach_str) \ 1045 char*: cx_map_detach_str) \
878 (map, key) 1046 (map, key)
879 1047
880 /** 1048 /**
881 * Removes a key/value-pair from the map by using the key. 1049 * Removes a key/value-pair from the map by using the key.
903 */ 1071 */
904 __attribute__((__nonnull__, __warn_unused_result__)) 1072 __attribute__((__nonnull__, __warn_unused_result__))
905 static inline void *cx_map_remove_and_get_cxstr( 1073 static inline void *cx_map_remove_and_get_cxstr(
906 CxMap *map, 1074 CxMap *map,
907 cxstring key 1075 cxstring key
1076 ) {
1077 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
1078 }
1079
1080 /**
1081 * Removes a key/value-pair from the map by using the key.
1082 *
1083 * @param map the map
1084 * @param key the key
1085 * @return the stored pointer or \c NULL if either the key is not present
1086 * in the map or the map is not storing pointers
1087 */
1088 __attribute__((__nonnull__, __warn_unused_result__))
1089 static inline void *cx_map_remove_and_get_mustr(
1090 CxMap *map,
1091 cxmutstr key
908 ) { 1092 ) {
909 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); 1093 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
910 } 1094 }
911 1095
912 /** 1096 /**
945 * @see cxMapDetach() 1129 * @see cxMapDetach()
946 */ 1130 */
947 #define cxMapRemoveAndGet(map, key) _Generic((key), \ 1131 #define cxMapRemoveAndGet(map, key) _Generic((key), \
948 CxHashKey: cx_map_remove_and_get, \ 1132 CxHashKey: cx_map_remove_and_get, \
949 cxstring: cx_map_remove_and_get_cxstr, \ 1133 cxstring: cx_map_remove_and_get_cxstr, \
1134 cxmutstr: cx_map_remove_and_get_mustr, \
950 char*: cx_map_remove_and_get_str) \ 1135 char*: cx_map_remove_and_get_str) \
951 (map, key) 1136 (map, key)
952 1137
953 #endif // __cplusplus 1138 #endif // __cplusplus
954 1139

mercurial