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 |
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 * |
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 * |
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. |
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 |