126 |
129 |
127 /** |
130 /** |
128 * Creates an iterator for this map. |
131 * Creates an iterator for this map. |
129 */ |
132 */ |
130 __attribute__((__nonnull__, __warn_unused_result__)) |
133 __attribute__((__nonnull__, __warn_unused_result__)) |
131 CxIterator (*iterator)(CxMap const *map, enum cx_map_iterator_type type); |
134 CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); |
132 }; |
135 }; |
133 |
136 |
134 /** |
137 /** |
135 * A map entry. |
138 * A map entry. |
136 */ |
139 */ |
137 struct cx_map_entry_s { |
140 struct cx_map_entry_s { |
138 /** |
141 /** |
139 * A pointer to the key. |
142 * A pointer to the key. |
140 */ |
143 */ |
141 CxHashKey const *key; |
144 const CxHashKey *key; |
142 /** |
145 /** |
143 * A pointer to the value. |
146 * A pointer to the value. |
144 */ |
147 */ |
145 void *value; |
148 void *value; |
146 }; |
149 }; |
161 * @param map the map |
164 * @param map the map |
162 * @see cxMapStorePointers() |
165 * @see cxMapStorePointers() |
163 */ |
166 */ |
164 __attribute__((__nonnull__)) |
167 __attribute__((__nonnull__)) |
165 static inline void cxMapStoreObjects(CxMap *map) { |
168 static inline void cxMapStoreObjects(CxMap *map) { |
166 map->store_pointer = false; |
169 map->collection.store_pointer = false; |
167 } |
170 } |
168 |
171 |
169 /** |
172 /** |
170 * Advises the map to only store pointers to the objects. |
173 * Advises the map to only store pointers to the objects. |
171 * |
174 * |
178 * @param map the map |
181 * @param map the map |
179 * @see cxMapStoreObjects() |
182 * @see cxMapStoreObjects() |
180 */ |
183 */ |
181 __attribute__((__nonnull__)) |
184 __attribute__((__nonnull__)) |
182 static inline void cxMapStorePointers(CxMap *map) { |
185 static inline void cxMapStorePointers(CxMap *map) { |
183 map->store_pointer = true; |
186 map->collection.store_pointer = true; |
184 map->item_size = sizeof(void *); |
187 map->collection.elem_size = sizeof(void *); |
185 } |
188 } |
186 |
189 |
|
190 /** |
|
191 * Returns true, if this map is storing pointers instead of the actual data. |
|
192 * |
|
193 * @param map |
|
194 * @return true, if this map is storing pointers |
|
195 * @see cxMapStorePointers() |
|
196 */ |
|
197 __attribute__((__nonnull__)) |
|
198 static inline bool cxMapIsStoringPointers(const CxMap *map) { |
|
199 return map->collection.store_pointer; |
|
200 } |
187 |
201 |
188 /** |
202 /** |
189 * Deallocates the memory of the specified map. |
203 * Deallocates the memory of the specified map. |
190 * |
204 * |
191 * @param map the map to be destroyed |
205 * @param map the map to be destroyed |
204 __attribute__((__nonnull__)) |
218 __attribute__((__nonnull__)) |
205 static inline void cxMapClear(CxMap *map) { |
219 static inline void cxMapClear(CxMap *map) { |
206 map->cl->clear(map); |
220 map->cl->clear(map); |
207 } |
221 } |
208 |
222 |
|
223 /** |
|
224 * Returns the number of elements in this map. |
|
225 * |
|
226 * @param map the map |
|
227 * @return the number of stored elements |
|
228 */ |
|
229 __attribute__((__nonnull__)) |
|
230 static inline size_t cxMapSize(const CxMap *map) { |
|
231 return map->collection.size; |
|
232 } |
|
233 |
209 |
234 |
210 // TODO: set-like map operations (union, intersect, difference) |
235 // TODO: set-like map operations (union, intersect, difference) |
211 |
236 |
212 /** |
237 /** |
213 * Creates a value iterator for a map. |
238 * Creates a value iterator for a map. |
217 * |
242 * |
218 * @param map the map to create the iterator for |
243 * @param map the map to create the iterator for |
219 * @return an iterator for the currently stored values |
244 * @return an iterator for the currently stored values |
220 */ |
245 */ |
221 __attribute__((__nonnull__, __warn_unused_result__)) |
246 __attribute__((__nonnull__, __warn_unused_result__)) |
222 static inline CxIterator cxMapIteratorValues(CxMap const *map) { |
247 static inline CxIterator cxMapIteratorValues(const CxMap *map) { |
223 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
248 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
224 } |
249 } |
225 |
250 |
226 /** |
251 /** |
227 * Creates a key iterator for a map. |
252 * Creates a key iterator for a map. |
233 * |
258 * |
234 * @param map the map to create the iterator for |
259 * @param map the map to create the iterator for |
235 * @return an iterator for the currently stored keys |
260 * @return an iterator for the currently stored keys |
236 */ |
261 */ |
237 __attribute__((__nonnull__, __warn_unused_result__)) |
262 __attribute__((__nonnull__, __warn_unused_result__)) |
238 static inline CxIterator cxMapIteratorKeys(CxMap const *map) { |
263 static inline CxIterator cxMapIteratorKeys(const CxMap *map) { |
239 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); |
264 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); |
240 } |
265 } |
241 |
266 |
242 /** |
267 /** |
243 * Creates an iterator for a map. |
268 * Creates an iterator for a map. |
251 * @return an iterator for the currently stored entries |
276 * @return an iterator for the currently stored entries |
252 * @see cxMapIteratorKeys() |
277 * @see cxMapIteratorKeys() |
253 * @see cxMapIteratorValues() |
278 * @see cxMapIteratorValues() |
254 */ |
279 */ |
255 __attribute__((__nonnull__, __warn_unused_result__)) |
280 __attribute__((__nonnull__, __warn_unused_result__)) |
256 static inline CxIterator cxMapIterator(CxMap const *map) { |
281 static inline CxIterator cxMapIterator(const CxMap *map) { |
257 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
282 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
258 } |
283 } |
259 |
284 |
260 |
285 |
261 /** |
286 /** |
266 * |
291 * |
267 * @param map the map to create the iterator for |
292 * @param map the map to create the iterator for |
268 * @return an iterator for the currently stored values |
293 * @return an iterator for the currently stored values |
269 */ |
294 */ |
270 __attribute__((__nonnull__, __warn_unused_result__)) |
295 __attribute__((__nonnull__, __warn_unused_result__)) |
271 CxMutIterator cxMapMutIteratorValues(CxMap *map); |
296 CxIterator cxMapMutIteratorValues(CxMap *map); |
272 |
297 |
273 /** |
298 /** |
274 * Creates a mutating iterator over the keys of a map. |
299 * Creates a mutating iterator over the keys of a map. |
275 * |
300 * |
276 * The elements of the iterator are keys of type CxHashKey. |
301 * The elements of the iterator are keys of type CxHashKey. |
280 * |
305 * |
281 * @param map the map to create the iterator for |
306 * @param map the map to create the iterator for |
282 * @return an iterator for the currently stored keys |
307 * @return an iterator for the currently stored keys |
283 */ |
308 */ |
284 __attribute__((__nonnull__, __warn_unused_result__)) |
309 __attribute__((__nonnull__, __warn_unused_result__)) |
285 CxMutIterator cxMapMutIteratorKeys(CxMap *map); |
310 CxIterator cxMapMutIteratorKeys(CxMap *map); |
286 |
311 |
287 /** |
312 /** |
288 * Creates a mutating iterator for a map. |
313 * Creates a mutating iterator for a map. |
289 * |
314 * |
290 * The elements of the iterator are key/value pairs of type CxMapEntry. |
315 * The elements of the iterator are key/value pairs of type CxMapEntry. |
296 * @return an iterator for the currently stored entries |
321 * @return an iterator for the currently stored entries |
297 * @see cxMapMutIteratorKeys() |
322 * @see cxMapMutIteratorKeys() |
298 * @see cxMapMutIteratorValues() |
323 * @see cxMapMutIteratorValues() |
299 */ |
324 */ |
300 __attribute__((__nonnull__, __warn_unused_result__)) |
325 __attribute__((__nonnull__, __warn_unused_result__)) |
301 CxMutIterator cxMapMutIterator(CxMap *map); |
326 CxIterator cxMapMutIterator(CxMap *map); |
302 |
327 |
303 #ifdef __cplusplus |
328 #ifdef __cplusplus |
304 } // end the extern "C" block here, because we want to start overloading |
329 } // end the extern "C" block here, because we want to start overloading |
305 |
330 |
306 /** |
331 /** |
364 * @return 0 on success, non-zero value on failure |
389 * @return 0 on success, non-zero value on failure |
365 */ |
390 */ |
366 __attribute__((__nonnull__)) |
391 __attribute__((__nonnull__)) |
367 static inline int cxMapPut( |
392 static inline int cxMapPut( |
368 CxMap *map, |
393 CxMap *map, |
369 char const *key, |
394 const char *key, |
370 void *value |
395 void *value |
371 ) { |
396 ) { |
372 return map->cl->put(map, cx_hash_key_str(key), value); |
397 return map->cl->put(map, cx_hash_key_str(key), value); |
373 } |
398 } |
374 |
399 |
778 * @return 0 on success, non-zero value on failure |
803 * @return 0 on success, non-zero value on failure |
779 */ |
804 */ |
780 __attribute__((__nonnull__)) |
805 __attribute__((__nonnull__)) |
781 static inline int cx_map_put_str( |
806 static inline int cx_map_put_str( |
782 CxMap *map, |
807 CxMap *map, |
783 char const *key, |
808 const char *key, |
784 void *value |
809 void *value |
785 ) { |
810 ) { |
786 return map->cl->put(map, cx_hash_key_str(key), value); |
811 return map->cl->put(map, cx_hash_key_str(key), value); |
787 } |
812 } |
788 |
813 |
797 #define cxMapPut(map, key, value) _Generic((key), \ |
822 #define cxMapPut(map, key, value) _Generic((key), \ |
798 CxHashKey: cx_map_put, \ |
823 CxHashKey: cx_map_put, \ |
799 cxstring: cx_map_put_cxstr, \ |
824 cxstring: cx_map_put_cxstr, \ |
800 cxmutstr: cx_map_put_mustr, \ |
825 cxmutstr: cx_map_put_mustr, \ |
801 char*: cx_map_put_str, \ |
826 char*: cx_map_put_str, \ |
802 char const*: cx_map_put_str) \ |
827 const char*: cx_map_put_str) \ |
803 (map, key, value) |
828 (map, key, value) |
804 |
829 |
805 /** |
830 /** |
806 * Retrieves a value by using a key. |
831 * Retrieves a value by using a key. |
807 * |
832 * |
872 #define cxMapGet(map, key) _Generic((key), \ |
897 #define cxMapGet(map, key) _Generic((key), \ |
873 CxHashKey: cx_map_get, \ |
898 CxHashKey: cx_map_get, \ |
874 cxstring: cx_map_get_cxstr, \ |
899 cxstring: cx_map_get_cxstr, \ |
875 cxmutstr: cx_map_get_mustr, \ |
900 cxmutstr: cx_map_get_mustr, \ |
876 char*: cx_map_get_str, \ |
901 char*: cx_map_get_str, \ |
877 char const*: cx_map_get_str) \ |
902 const char*: cx_map_get_str) \ |
878 (map, key) |
903 (map, key) |
879 |
904 |
880 /** |
905 /** |
881 * 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. |
882 * |
907 * |
950 #define cxMapRemove(map, key) _Generic((key), \ |
975 #define cxMapRemove(map, key) _Generic((key), \ |
951 CxHashKey: cx_map_remove, \ |
976 CxHashKey: cx_map_remove, \ |
952 cxstring: cx_map_remove_cxstr, \ |
977 cxstring: cx_map_remove_cxstr, \ |
953 cxmutstr: cx_map_remove_mustr, \ |
978 cxmutstr: cx_map_remove_mustr, \ |
954 char*: cx_map_remove_str, \ |
979 char*: cx_map_remove_str, \ |
955 char const*: cx_map_remove_str) \ |
980 const char*: cx_map_remove_str) \ |
956 (map, key) |
981 (map, key) |
957 |
982 |
958 /** |
983 /** |
959 * 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 |
960 * without invoking the destructor. |
985 * without invoking the destructor. |
1032 #define cxMapDetach(map, key) _Generic((key), \ |
1057 #define cxMapDetach(map, key) _Generic((key), \ |
1033 CxHashKey: cx_map_detach, \ |
1058 CxHashKey: cx_map_detach, \ |
1034 cxstring: cx_map_detach_cxstr, \ |
1059 cxstring: cx_map_detach_cxstr, \ |
1035 cxmutstr: cx_map_detach_mustr, \ |
1060 cxmutstr: cx_map_detach_mustr, \ |
1036 char*: cx_map_detach_str, \ |
1061 char*: cx_map_detach_str, \ |
1037 char const*: cx_map_detach_str) \ |
1062 const char*: cx_map_detach_str) \ |
1038 (map, key) |
1063 (map, key) |
1039 |
1064 |
1040 /** |
1065 /** |
1041 * 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. |
1042 * |
1067 * |
1048 __attribute__((__nonnull__, __warn_unused_result__)) |
1073 __attribute__((__nonnull__, __warn_unused_result__)) |
1049 static inline void *cx_map_remove_and_get( |
1074 static inline void *cx_map_remove_and_get( |
1050 CxMap *map, |
1075 CxMap *map, |
1051 CxHashKey key |
1076 CxHashKey key |
1052 ) { |
1077 ) { |
1053 return map->cl->remove(map, key, !map->store_pointer); |
1078 return map->cl->remove(map, key, !map->collection.store_pointer); |
1054 } |
1079 } |
1055 |
1080 |
1056 /** |
1081 /** |
1057 * Removes a key/value-pair from the map by using the key. |
1082 * Removes a key/value-pair from the map by using the key. |
1058 * |
1083 * |
1064 __attribute__((__nonnull__, __warn_unused_result__)) |
1089 __attribute__((__nonnull__, __warn_unused_result__)) |
1065 static inline void *cx_map_remove_and_get_cxstr( |
1090 static inline void *cx_map_remove_and_get_cxstr( |
1066 CxMap *map, |
1091 CxMap *map, |
1067 cxstring key |
1092 cxstring key |
1068 ) { |
1093 ) { |
1069 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); |
1094 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
1070 } |
1095 } |
1071 |
1096 |
1072 /** |
1097 /** |
1073 * Removes a key/value-pair from the map by using the key. |
1098 * Removes a key/value-pair from the map by using the key. |
1074 * |
1099 * |
1080 __attribute__((__nonnull__, __warn_unused_result__)) |
1105 __attribute__((__nonnull__, __warn_unused_result__)) |
1081 static inline void *cx_map_remove_and_get_mustr( |
1106 static inline void *cx_map_remove_and_get_mustr( |
1082 CxMap *map, |
1107 CxMap *map, |
1083 cxmutstr key |
1108 cxmutstr key |
1084 ) { |
1109 ) { |
1085 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); |
1110 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
1086 } |
1111 } |
1087 |
1112 |
1088 /** |
1113 /** |
1089 * Removes a key/value-pair from the map by using the key. |
1114 * Removes a key/value-pair from the map by using the key. |
1090 * |
1115 * |
1094 * in the map or the map is not storing pointers |
1119 * in the map or the map is not storing pointers |
1095 */ |
1120 */ |
1096 __attribute__((__nonnull__, __warn_unused_result__)) |
1121 __attribute__((__nonnull__, __warn_unused_result__)) |
1097 static inline void *cx_map_remove_and_get_str( |
1122 static inline void *cx_map_remove_and_get_str( |
1098 CxMap *map, |
1123 CxMap *map, |
1099 char const *key |
1124 const char *key |
1100 ) { |
1125 ) { |
1101 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); |
1126 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); |
1102 } |
1127 } |
1103 |
1128 |
1104 /** |
1129 /** |
1105 * Removes a key/value-pair from the map by using the key. |
1130 * Removes a key/value-pair from the map by using the key. |
1106 * |
1131 * |
1123 #define cxMapRemoveAndGet(map, key) _Generic((key), \ |
1148 #define cxMapRemoveAndGet(map, key) _Generic((key), \ |
1124 CxHashKey: cx_map_remove_and_get, \ |
1149 CxHashKey: cx_map_remove_and_get, \ |
1125 cxstring: cx_map_remove_and_get_cxstr, \ |
1150 cxstring: cx_map_remove_and_get_cxstr, \ |
1126 cxmutstr: cx_map_remove_and_get_mustr, \ |
1151 cxmutstr: cx_map_remove_and_get_mustr, \ |
1127 char*: cx_map_remove_and_get_str, \ |
1152 char*: cx_map_remove_and_get_str, \ |
1128 char const*: cx_map_remove_and_get_str) \ |
1153 const char*: cx_map_remove_and_get_str) \ |
1129 (map, key) |
1154 (map, key) |
1130 |
1155 |
1131 #endif // __cplusplus |
1156 #endif // __cplusplus |
1132 |
1157 |
1133 #endif // UCX_MAP_H |
1158 #endif // UCX_MAP_H |