src/cx/map.h

changeset 890
54565fd74e74
parent 857
4d12e34bb130
equal deleted inserted replaced
889:f549fd9fbd8f 890:54565fd74e74
111 /** 111 /**
112 * Returns an element. 112 * Returns an element.
113 */ 113 */
114 __attribute__((__nonnull__, __warn_unused_result__)) 114 __attribute__((__nonnull__, __warn_unused_result__))
115 void *(*get)( 115 void *(*get)(
116 CxMap const *map, 116 const CxMap *map,
117 CxHashKey key 117 CxHashKey key
118 ); 118 );
119 119
120 /** 120 /**
121 * Removes an element. 121 * Removes an element.
129 129
130 /** 130 /**
131 * Creates an iterator for this map. 131 * Creates an iterator for this map.
132 */ 132 */
133 __attribute__((__nonnull__, __warn_unused_result__)) 133 __attribute__((__nonnull__, __warn_unused_result__))
134 CxIterator (*iterator)(CxMap const *map, enum cx_map_iterator_type type); 134 CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type);
135 }; 135 };
136 136
137 /** 137 /**
138 * A map entry. 138 * A map entry.
139 */ 139 */
140 struct cx_map_entry_s { 140 struct cx_map_entry_s {
141 /** 141 /**
142 * A pointer to the key. 142 * A pointer to the key.
143 */ 143 */
144 CxHashKey const *key; 144 const CxHashKey *key;
145 /** 145 /**
146 * A pointer to the value. 146 * A pointer to the value.
147 */ 147 */
148 void *value; 148 void *value;
149 }; 149 };
193 * @param map 193 * @param map
194 * @return true, if this map is storing pointers 194 * @return true, if this map is storing pointers
195 * @see cxMapStorePointers() 195 * @see cxMapStorePointers()
196 */ 196 */
197 __attribute__((__nonnull__)) 197 __attribute__((__nonnull__))
198 static inline bool cxMapIsStoringPointers(CxMap const *map) { 198 static inline bool cxMapIsStoringPointers(const CxMap *map) {
199 return map->collection.store_pointer; 199 return map->collection.store_pointer;
200 } 200 }
201 201
202 /** 202 /**
203 * Deallocates the memory of the specified map. 203 * Deallocates the memory of the specified map.
225 * 225 *
226 * @param map the map 226 * @param map the map
227 * @return the number of stored elements 227 * @return the number of stored elements
228 */ 228 */
229 __attribute__((__nonnull__)) 229 __attribute__((__nonnull__))
230 static inline size_t cxMapSize(CxMap const *map) { 230 static inline size_t cxMapSize(const CxMap *map) {
231 return map->collection.size; 231 return map->collection.size;
232 } 232 }
233 233
234 234
235 // TODO: set-like map operations (union, intersect, difference) 235 // TODO: set-like map operations (union, intersect, difference)
242 * 242 *
243 * @param map the map to create the iterator for 243 * @param map the map to create the iterator for
244 * @return an iterator for the currently stored values 244 * @return an iterator for the currently stored values
245 */ 245 */
246 __attribute__((__nonnull__, __warn_unused_result__)) 246 __attribute__((__nonnull__, __warn_unused_result__))
247 static inline CxIterator cxMapIteratorValues(CxMap const *map) { 247 static inline CxIterator cxMapIteratorValues(const CxMap *map) {
248 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); 248 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
249 } 249 }
250 250
251 /** 251 /**
252 * Creates a key iterator for a map. 252 * Creates a key iterator for a map.
258 * 258 *
259 * @param map the map to create the iterator for 259 * @param map the map to create the iterator for
260 * @return an iterator for the currently stored keys 260 * @return an iterator for the currently stored keys
261 */ 261 */
262 __attribute__((__nonnull__, __warn_unused_result__)) 262 __attribute__((__nonnull__, __warn_unused_result__))
263 static inline CxIterator cxMapIteratorKeys(CxMap const *map) { 263 static inline CxIterator cxMapIteratorKeys(const CxMap *map) {
264 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); 264 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
265 } 265 }
266 266
267 /** 267 /**
268 * Creates an iterator for a map. 268 * Creates an iterator for a map.
276 * @return an iterator for the currently stored entries 276 * @return an iterator for the currently stored entries
277 * @see cxMapIteratorKeys() 277 * @see cxMapIteratorKeys()
278 * @see cxMapIteratorValues() 278 * @see cxMapIteratorValues()
279 */ 279 */
280 __attribute__((__nonnull__, __warn_unused_result__)) 280 __attribute__((__nonnull__, __warn_unused_result__))
281 static inline CxIterator cxMapIterator(CxMap const *map) { 281 static inline CxIterator cxMapIterator(const CxMap *map) {
282 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); 282 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
283 } 283 }
284 284
285 285
286 /** 286 /**
389 * @return 0 on success, non-zero value on failure 389 * @return 0 on success, non-zero value on failure
390 */ 390 */
391 __attribute__((__nonnull__)) 391 __attribute__((__nonnull__))
392 static inline int cxMapPut( 392 static inline int cxMapPut(
393 CxMap *map, 393 CxMap *map,
394 char const *key, 394 const char *key,
395 void *value 395 void *value
396 ) { 396 ) {
397 return map->cl->put(map, cx_hash_key_str(key), value); 397 return map->cl->put(map, cx_hash_key_str(key), value);
398 } 398 }
399 399
404 * @param key the key 404 * @param key the key
405 * @return the value 405 * @return the value
406 */ 406 */
407 __attribute__((__nonnull__, __warn_unused_result__)) 407 __attribute__((__nonnull__, __warn_unused_result__))
408 static inline void *cxMapGet( 408 static inline void *cxMapGet(
409 CxMap const *map, 409 const CxMap *map,
410 CxHashKey const &key 410 CxHashKey const &key
411 ) { 411 ) {
412 return map->cl->get(map, key); 412 return map->cl->get(map, key);
413 } 413 }
414 414
419 * @param key the key 419 * @param key the key
420 * @return the value 420 * @return the value
421 */ 421 */
422 __attribute__((__nonnull__, __warn_unused_result__)) 422 __attribute__((__nonnull__, __warn_unused_result__))
423 static inline void *cxMapGet( 423 static inline void *cxMapGet(
424 CxMap const *map, 424 const CxMap *map,
425 cxstring const &key 425 cxstring const &key
426 ) { 426 ) {
427 return map->cl->get(map, cx_hash_key_cxstr(key)); 427 return map->cl->get(map, cx_hash_key_cxstr(key));
428 } 428 }
429 429
434 * @param key the key 434 * @param key the key
435 * @return the value 435 * @return the value
436 */ 436 */
437 __attribute__((__nonnull__, __warn_unused_result__)) 437 __attribute__((__nonnull__, __warn_unused_result__))
438 static inline void *cxMapGet( 438 static inline void *cxMapGet(
439 CxMap const *map, 439 const CxMap *map,
440 cxmutstr const &key 440 cxmutstr const &key
441 ) { 441 ) {
442 return map->cl->get(map, cx_hash_key_cxstr(key)); 442 return map->cl->get(map, cx_hash_key_cxstr(key));
443 } 443 }
444 444
449 * @param key the key 449 * @param key the key
450 * @return the value 450 * @return the value
451 */ 451 */
452 __attribute__((__nonnull__, __warn_unused_result__)) 452 __attribute__((__nonnull__, __warn_unused_result__))
453 static inline void *cxMapGet( 453 static inline void *cxMapGet(
454 CxMap const *map, 454 const CxMap *map,
455 char const *key 455 const char *key
456 ) { 456 ) {
457 return map->cl->get(map, cx_hash_key_str(key)); 457 return map->cl->get(map, cx_hash_key_str(key));
458 } 458 }
459 459
460 /** 460 /**
538 * @see cxMapDetach() 538 * @see cxMapDetach()
539 */ 539 */
540 __attribute__((__nonnull__)) 540 __attribute__((__nonnull__))
541 static inline void cxMapRemove( 541 static inline void cxMapRemove(
542 CxMap *map, 542 CxMap *map,
543 char const *key 543 const char *key
544 ) { 544 ) {
545 (void) map->cl->remove(map, cx_hash_key_str(key), true); 545 (void) map->cl->remove(map, cx_hash_key_str(key), true);
546 } 546 }
547 547
548 /** 548 /**
626 * @see cxMapRemoveAndGet() 626 * @see cxMapRemoveAndGet()
627 */ 627 */
628 __attribute__((__nonnull__)) 628 __attribute__((__nonnull__))
629 static inline void cxMapDetach( 629 static inline void cxMapDetach(
630 CxMap *map, 630 CxMap *map,
631 char const *key 631 const char *key
632 ) { 632 ) {
633 (void) map->cl->remove(map, cx_hash_key_str(key), false); 633 (void) map->cl->remove(map, cx_hash_key_str(key), false);
634 } 634 }
635 635
636 /** 636 /**
734 * @see cxMapDetach() 734 * @see cxMapDetach()
735 */ 735 */
736 __attribute__((__nonnull__, __warn_unused_result__)) 736 __attribute__((__nonnull__, __warn_unused_result__))
737 static inline void *cxMapRemoveAndGet( 737 static inline void *cxMapRemoveAndGet(
738 CxMap *map, 738 CxMap *map,
739 char const *key 739 const char *key
740 ) { 740 ) {
741 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); 741 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
742 } 742 }
743 743
744 #else // __cplusplus 744 #else // __cplusplus
803 * @return 0 on success, non-zero value on failure 803 * @return 0 on success, non-zero value on failure
804 */ 804 */
805 __attribute__((__nonnull__)) 805 __attribute__((__nonnull__))
806 static inline int cx_map_put_str( 806 static inline int cx_map_put_str(
807 CxMap *map, 807 CxMap *map,
808 char const *key, 808 const char *key,
809 void *value 809 void *value
810 ) { 810 ) {
811 return map->cl->put(map, cx_hash_key_str(key), value); 811 return map->cl->put(map, cx_hash_key_str(key), value);
812 } 812 }
813 813
822 #define cxMapPut(map, key, value) _Generic((key), \ 822 #define cxMapPut(map, key, value) _Generic((key), \
823 CxHashKey: cx_map_put, \ 823 CxHashKey: cx_map_put, \
824 cxstring: cx_map_put_cxstr, \ 824 cxstring: cx_map_put_cxstr, \
825 cxmutstr: cx_map_put_mustr, \ 825 cxmutstr: cx_map_put_mustr, \
826 char*: cx_map_put_str, \ 826 char*: cx_map_put_str, \
827 char const*: cx_map_put_str) \ 827 const char*: cx_map_put_str) \
828 (map, key, value) 828 (map, key, value)
829 829
830 /** 830 /**
831 * Retrieves a value by using a key. 831 * Retrieves a value by using a key.
832 * 832 *
834 * @param key the key 834 * @param key the key
835 * @return the value 835 * @return the value
836 */ 836 */
837 __attribute__((__nonnull__, __warn_unused_result__)) 837 __attribute__((__nonnull__, __warn_unused_result__))
838 static inline void *cx_map_get( 838 static inline void *cx_map_get(
839 CxMap const *map, 839 const CxMap *map,
840 CxHashKey key 840 CxHashKey key
841 ) { 841 ) {
842 return map->cl->get(map, key); 842 return map->cl->get(map, key);
843 } 843 }
844 844
849 * @param key the key 849 * @param key the key
850 * @return the value 850 * @return the value
851 */ 851 */
852 __attribute__((__nonnull__, __warn_unused_result__)) 852 __attribute__((__nonnull__, __warn_unused_result__))
853 static inline void *cx_map_get_cxstr( 853 static inline void *cx_map_get_cxstr(
854 CxMap const *map, 854 const CxMap *map,
855 cxstring key 855 cxstring key
856 ) { 856 ) {
857 return map->cl->get(map, cx_hash_key_cxstr(key)); 857 return map->cl->get(map, cx_hash_key_cxstr(key));
858 } 858 }
859 859
864 * @param key the key 864 * @param key the key
865 * @return the value 865 * @return the value
866 */ 866 */
867 __attribute__((__nonnull__, __warn_unused_result__)) 867 __attribute__((__nonnull__, __warn_unused_result__))
868 static inline void *cx_map_get_mustr( 868 static inline void *cx_map_get_mustr(
869 CxMap const *map, 869 const CxMap *map,
870 cxmutstr key 870 cxmutstr key
871 ) { 871 ) {
872 return map->cl->get(map, cx_hash_key_cxstr(key)); 872 return map->cl->get(map, cx_hash_key_cxstr(key));
873 } 873 }
874 874
879 * @param key the key 879 * @param key the key
880 * @return the value 880 * @return the value
881 */ 881 */
882 __attribute__((__nonnull__, __warn_unused_result__)) 882 __attribute__((__nonnull__, __warn_unused_result__))
883 static inline void *cx_map_get_str( 883 static inline void *cx_map_get_str(
884 CxMap const *map, 884 const CxMap *map,
885 char const *key 885 const char *key
886 ) { 886 ) {
887 return map->cl->get(map, cx_hash_key_str(key)); 887 return map->cl->get(map, cx_hash_key_str(key));
888 } 888 }
889 889
890 /** 890 /**
897 #define cxMapGet(map, key) _Generic((key), \ 897 #define cxMapGet(map, key) _Generic((key), \
898 CxHashKey: cx_map_get, \ 898 CxHashKey: cx_map_get, \
899 cxstring: cx_map_get_cxstr, \ 899 cxstring: cx_map_get_cxstr, \
900 cxmutstr: cx_map_get_mustr, \ 900 cxmutstr: cx_map_get_mustr, \
901 char*: cx_map_get_str, \ 901 char*: cx_map_get_str, \
902 char const*: cx_map_get_str) \ 902 const char*: cx_map_get_str) \
903 (map, key) 903 (map, key)
904 904
905 /** 905 /**
906 * Removes a key/value-pair from the map by using the key. 906 * Removes a key/value-pair from the map by using the key.
907 * 907 *
951 * @param key the key 951 * @param key the key
952 */ 952 */
953 __attribute__((__nonnull__)) 953 __attribute__((__nonnull__))
954 static inline void cx_map_remove_str( 954 static inline void cx_map_remove_str(
955 CxMap *map, 955 CxMap *map,
956 char const *key 956 const char *key
957 ) { 957 ) {
958 (void) map->cl->remove(map, cx_hash_key_str(key), true); 958 (void) map->cl->remove(map, cx_hash_key_str(key), true);
959 } 959 }
960 960
961 /** 961 /**
975 #define cxMapRemove(map, key) _Generic((key), \ 975 #define cxMapRemove(map, key) _Generic((key), \
976 CxHashKey: cx_map_remove, \ 976 CxHashKey: cx_map_remove, \
977 cxstring: cx_map_remove_cxstr, \ 977 cxstring: cx_map_remove_cxstr, \
978 cxmutstr: cx_map_remove_mustr, \ 978 cxmutstr: cx_map_remove_mustr, \
979 char*: cx_map_remove_str, \ 979 char*: cx_map_remove_str, \
980 char const*: cx_map_remove_str) \ 980 const char*: cx_map_remove_str) \
981 (map, key) 981 (map, key)
982 982
983 /** 983 /**
984 * Detaches a key/value-pair from the map by using the key 984 * Detaches a key/value-pair from the map by using the key
985 * without invoking the destructor. 985 * without invoking the destructor.
1033 * @param key the key 1033 * @param key the key
1034 */ 1034 */
1035 __attribute__((__nonnull__)) 1035 __attribute__((__nonnull__))
1036 static inline void cx_map_detach_str( 1036 static inline void cx_map_detach_str(
1037 CxMap *map, 1037 CxMap *map,
1038 char const *key 1038 const char *key
1039 ) { 1039 ) {
1040 (void) map->cl->remove(map, cx_hash_key_str(key), false); 1040 (void) map->cl->remove(map, cx_hash_key_str(key), false);
1041 } 1041 }
1042 1042
1043 /** 1043 /**
1057 #define cxMapDetach(map, key) _Generic((key), \ 1057 #define cxMapDetach(map, key) _Generic((key), \
1058 CxHashKey: cx_map_detach, \ 1058 CxHashKey: cx_map_detach, \
1059 cxstring: cx_map_detach_cxstr, \ 1059 cxstring: cx_map_detach_cxstr, \
1060 cxmutstr: cx_map_detach_mustr, \ 1060 cxmutstr: cx_map_detach_mustr, \
1061 char*: cx_map_detach_str, \ 1061 char*: cx_map_detach_str, \
1062 char const*: cx_map_detach_str) \ 1062 const char*: cx_map_detach_str) \
1063 (map, key) 1063 (map, key)
1064 1064
1065 /** 1065 /**
1066 * Removes a key/value-pair from the map by using the key. 1066 * Removes a key/value-pair from the map by using the key.
1067 * 1067 *
1119 * in the map or the map is not storing pointers 1119 * in the map or the map is not storing pointers
1120 */ 1120 */
1121 __attribute__((__nonnull__, __warn_unused_result__)) 1121 __attribute__((__nonnull__, __warn_unused_result__))
1122 static inline void *cx_map_remove_and_get_str( 1122 static inline void *cx_map_remove_and_get_str(
1123 CxMap *map, 1123 CxMap *map,
1124 char const *key 1124 const char *key
1125 ) { 1125 ) {
1126 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); 1126 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer);
1127 } 1127 }
1128 1128
1129 /** 1129 /**
1148 #define cxMapRemoveAndGet(map, key) _Generic((key), \ 1148 #define cxMapRemoveAndGet(map, key) _Generic((key), \
1149 CxHashKey: cx_map_remove_and_get, \ 1149 CxHashKey: cx_map_remove_and_get, \
1150 cxstring: cx_map_remove_and_get_cxstr, \ 1150 cxstring: cx_map_remove_and_get_cxstr, \
1151 cxmutstr: cx_map_remove_and_get_mustr, \ 1151 cxmutstr: cx_map_remove_and_get_mustr, \
1152 char*: cx_map_remove_and_get_str, \ 1152 char*: cx_map_remove_and_get_str, \
1153 char const*: cx_map_remove_and_get_str) \ 1153 const char*: cx_map_remove_and_get_str) \
1154 (map, key) 1154 (map, key)
1155 1155
1156 #endif // __cplusplus 1156 #endif // __cplusplus
1157 1157
1158 #endif // UCX_MAP_H 1158 #endif // UCX_MAP_H

mercurial