| 87 */ |
87 */ |
| 88 struct cx_map_class_s { |
88 struct cx_map_class_s { |
| 89 /** |
89 /** |
| 90 * Deallocates the entire memory. |
90 * Deallocates the entire memory. |
| 91 */ |
91 */ |
| 92 __attribute__((__nonnull__)) |
92 cx_attr_nonnull |
| 93 void (*destructor)(struct cx_map_s *map); |
93 void (*destructor)(struct cx_map_s *map); |
| 94 |
94 |
| 95 /** |
95 /** |
| 96 * Removes all elements. |
96 * Removes all elements. |
| 97 */ |
97 */ |
| 98 __attribute__((__nonnull__)) |
98 cx_attr_nonnull |
| 99 void (*clear)(struct cx_map_s *map); |
99 void (*clear)(struct cx_map_s *map); |
| 100 |
100 |
| 101 /** |
101 /** |
| 102 * Add or overwrite an element. |
102 * Add or overwrite an element. |
| 103 */ |
103 */ |
| 104 __attribute__((__nonnull__)) |
104 cx_attr_nonnull |
| 105 int (*put)( |
105 int (*put)( |
| 106 CxMap *map, |
106 CxMap *map, |
| 107 CxHashKey key, |
107 CxHashKey key, |
| 108 void *value |
108 void *value |
| 109 ); |
109 ); |
| 110 |
110 |
| 111 /** |
111 /** |
| 112 * Returns an element. |
112 * Returns an element. |
| 113 */ |
113 */ |
| 114 __attribute__((__nonnull__, __warn_unused_result__)) |
114 cx_attr_nonnull |
| |
115 cx_attr_nodiscard |
| 115 void *(*get)( |
116 void *(*get)( |
| 116 const CxMap *map, |
117 const CxMap *map, |
| 117 CxHashKey key |
118 CxHashKey key |
| 118 ); |
119 ); |
| 119 |
120 |
| 120 /** |
121 /** |
| 121 * Removes an element. |
122 * Removes an element. |
| 122 */ |
123 */ |
| 123 __attribute__((__nonnull__)) |
124 cx_attr_nonnull |
| 124 void *(*remove)( |
125 void *(*remove)( |
| 125 CxMap *map, |
126 CxMap *map, |
| 126 CxHashKey key, |
127 CxHashKey key, |
| 127 bool destroy |
128 bool destroy |
| 128 ); |
129 ); |
| 129 |
130 |
| 130 /** |
131 /** |
| 131 * Creates an iterator for this map. |
132 * Creates an iterator for this map. |
| 132 */ |
133 */ |
| 133 __attribute__((__nonnull__, __warn_unused_result__)) |
134 cx_attr_nonnull |
| |
135 cx_attr_nodiscard |
| 134 CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); |
136 CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); |
| 135 }; |
137 }; |
| 136 |
138 |
| 137 /** |
139 /** |
| 138 * A map entry. |
140 * A map entry. |
| 179 * objects is undefined. |
181 * objects is undefined. |
| 180 * |
182 * |
| 181 * @param map the map |
183 * @param map the map |
| 182 * @see cxMapStoreObjects() |
184 * @see cxMapStoreObjects() |
| 183 */ |
185 */ |
| 184 __attribute__((__nonnull__)) |
186 cx_attr_nonnull |
| 185 static inline void cxMapStorePointers(CxMap *map) { |
187 static inline void cxMapStorePointers(CxMap *map) { |
| 186 map->collection.store_pointer = true; |
188 map->collection.store_pointer = true; |
| 187 map->collection.elem_size = sizeof(void *); |
189 map->collection.elem_size = sizeof(void *); |
| 188 } |
190 } |
| 189 |
191 |
| 192 * |
194 * |
| 193 * @param map |
195 * @param map |
| 194 * @return true, if this map is storing pointers |
196 * @return true, if this map is storing pointers |
| 195 * @see cxMapStorePointers() |
197 * @see cxMapStorePointers() |
| 196 */ |
198 */ |
| 197 __attribute__((__nonnull__)) |
199 cx_attr_nonnull |
| 198 static inline bool cxMapIsStoringPointers(const CxMap *map) { |
200 static inline bool cxMapIsStoringPointers(const CxMap *map) { |
| 199 return map->collection.store_pointer; |
201 return map->collection.store_pointer; |
| 200 } |
202 } |
| 201 |
203 |
| 202 /** |
204 /** |
| 213 /** |
215 /** |
| 214 * Clears a map by removing all elements. |
216 * Clears a map by removing all elements. |
| 215 * |
217 * |
| 216 * @param map the map to be cleared |
218 * @param map the map to be cleared |
| 217 */ |
219 */ |
| 218 __attribute__((__nonnull__)) |
220 cx_attr_nonnull |
| 219 static inline void cxMapClear(CxMap *map) { |
221 static inline void cxMapClear(CxMap *map) { |
| 220 map->cl->clear(map); |
222 map->cl->clear(map); |
| 221 } |
223 } |
| 222 |
224 |
| 223 /** |
225 /** |
| 224 * Returns the number of elements in this map. |
226 * Returns the number of elements in this map. |
| 225 * |
227 * |
| 226 * @param map the map |
228 * @param map the map |
| 227 * @return the number of stored elements |
229 * @return the number of stored elements |
| 228 */ |
230 */ |
| 229 __attribute__((__nonnull__)) |
231 cx_attr_nonnull |
| 230 static inline size_t cxMapSize(const CxMap *map) { |
232 static inline size_t cxMapSize(const CxMap *map) { |
| 231 return map->collection.size; |
233 return map->collection.size; |
| 232 } |
234 } |
| 233 |
235 |
| 234 |
236 |
| 241 * highly depends on the map implementation and may change arbitrarily when the contents change. |
243 * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 242 * |
244 * |
| 243 * @param map the map to create the iterator for |
245 * @param map the map to create the iterator for |
| 244 * @return an iterator for the currently stored values |
246 * @return an iterator for the currently stored values |
| 245 */ |
247 */ |
| 246 __attribute__((__nonnull__, __warn_unused_result__)) |
248 cx_attr_nonnull |
| |
249 cx_attr_nodiscard |
| 247 static inline CxIterator cxMapIteratorValues(const CxMap *map) { |
250 static inline CxIterator cxMapIteratorValues(const CxMap *map) { |
| 248 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
251 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
| 249 } |
252 } |
| 250 |
253 |
| 251 /** |
254 /** |
| 257 * highly depends on the map implementation and may change arbitrarily when the contents change. |
260 * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 258 * |
261 * |
| 259 * @param map the map to create the iterator for |
262 * @param map the map to create the iterator for |
| 260 * @return an iterator for the currently stored keys |
263 * @return an iterator for the currently stored keys |
| 261 */ |
264 */ |
| 262 __attribute__((__nonnull__, __warn_unused_result__)) |
265 cx_attr_nonnull |
| |
266 cx_attr_nodiscard |
| 263 static inline CxIterator cxMapIteratorKeys(const CxMap *map) { |
267 static inline CxIterator cxMapIteratorKeys(const CxMap *map) { |
| 264 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); |
268 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); |
| 265 } |
269 } |
| 266 |
270 |
| 267 /** |
271 /** |
| 275 * @param map the map to create the iterator for |
279 * @param map the map to create the iterator for |
| 276 * @return an iterator for the currently stored entries |
280 * @return an iterator for the currently stored entries |
| 277 * @see cxMapIteratorKeys() |
281 * @see cxMapIteratorKeys() |
| 278 * @see cxMapIteratorValues() |
282 * @see cxMapIteratorValues() |
| 279 */ |
283 */ |
| 280 __attribute__((__nonnull__, __warn_unused_result__)) |
284 cx_attr_nonnull |
| |
285 cx_attr_nodiscard |
| 281 static inline CxIterator cxMapIterator(const CxMap *map) { |
286 static inline CxIterator cxMapIterator(const CxMap *map) { |
| 282 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
287 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
| 283 } |
288 } |
| 284 |
289 |
| 285 |
290 |
| 290 * highly depends on the map implementation and may change arbitrarily when the contents change. |
295 * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 291 * |
296 * |
| 292 * @param map the map to create the iterator for |
297 * @param map the map to create the iterator for |
| 293 * @return an iterator for the currently stored values |
298 * @return an iterator for the currently stored values |
| 294 */ |
299 */ |
| 295 __attribute__((__nonnull__, __warn_unused_result__)) |
300 cx_attr_nonnull |
| |
301 cx_attr_nodiscard |
| 296 CxIterator cxMapMutIteratorValues(CxMap *map); |
302 CxIterator cxMapMutIteratorValues(CxMap *map); |
| 297 |
303 |
| 298 /** |
304 /** |
| 299 * Creates a mutating iterator over the keys of a map. |
305 * Creates a mutating iterator over the keys of a map. |
| 300 * |
306 * |
| 304 * highly depends on the map implementation and may change arbitrarily when the contents change. |
310 * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 305 * |
311 * |
| 306 * @param map the map to create the iterator for |
312 * @param map the map to create the iterator for |
| 307 * @return an iterator for the currently stored keys |
313 * @return an iterator for the currently stored keys |
| 308 */ |
314 */ |
| 309 __attribute__((__nonnull__, __warn_unused_result__)) |
315 cx_attr_nonnull |
| |
316 cx_attr_nodiscard |
| 310 CxIterator cxMapMutIteratorKeys(CxMap *map); |
317 CxIterator cxMapMutIteratorKeys(CxMap *map); |
| 311 |
318 |
| 312 /** |
319 /** |
| 313 * Creates a mutating iterator for a map. |
320 * Creates a mutating iterator for a map. |
| 314 * |
321 * |
| 320 * @param map the map to create the iterator for |
327 * @param map the map to create the iterator for |
| 321 * @return an iterator for the currently stored entries |
328 * @return an iterator for the currently stored entries |
| 322 * @see cxMapMutIteratorKeys() |
329 * @see cxMapMutIteratorKeys() |
| 323 * @see cxMapMutIteratorValues() |
330 * @see cxMapMutIteratorValues() |
| 324 */ |
331 */ |
| 325 __attribute__((__nonnull__, __warn_unused_result__)) |
332 cx_attr_nonnull |
| |
333 cx_attr_nodiscard |
| 326 CxIterator cxMapMutIterator(CxMap *map); |
334 CxIterator cxMapMutIterator(CxMap *map); |
| 327 |
335 |
| 328 #ifdef __cplusplus |
336 #ifdef __cplusplus |
| 329 } // end the extern "C" block here, because we want to start overloading |
337 } // end the extern "C" block here, because we want to start overloading |
| 330 |
338 |
| 334 * @param map the map |
342 * @param map the map |
| 335 * @param key the key |
343 * @param key the key |
| 336 * @param value the value |
344 * @param value the value |
| 337 * @return 0 on success, non-zero value on failure |
345 * @return 0 on success, non-zero value on failure |
| 338 */ |
346 */ |
| 339 __attribute__((__nonnull__)) |
347 cx_attr_nonnull |
| 340 static inline int cxMapPut( |
348 static inline int cxMapPut( |
| 341 CxMap *map, |
349 CxMap *map, |
| 342 CxHashKey const &key, |
350 CxHashKey const &key, |
| 343 void *value |
351 void *value |
| 344 ) { |
352 ) { |
| 650 * @return the stored pointer or \c NULL if either the key is not present |
666 * @return the stored pointer or \c NULL if either the key is not present |
| 651 * in the map or the map is not storing pointers |
667 * in the map or the map is not storing pointers |
| 652 * @see cxMapStorePointers() |
668 * @see cxMapStorePointers() |
| 653 * @see cxMapDetach() |
669 * @see cxMapDetach() |
| 654 */ |
670 */ |
| 655 __attribute__((__nonnull__, __warn_unused_result__)) |
671 cx_attr_nonnull |
| |
672 cx_attr_nodiscard |
| 656 static inline void *cxMapRemoveAndGet( |
673 static inline void *cxMapRemoveAndGet( |
| 657 CxMap *map, |
674 CxMap *map, |
| 658 CxHashKey key |
675 CxHashKey key |
| 659 ) { |
676 ) { |
| 660 return map->cl->remove(map, key, !map->collection.store_pointer); |
677 return map->cl->remove(map, key, !map->collection.store_pointer); |
| 677 * @return the stored pointer or \c NULL if either the key is not present |
694 * @return the stored pointer or \c NULL if either the key is not present |
| 678 * in the map or the map is not storing pointers |
695 * in the map or the map is not storing pointers |
| 679 * @see cxMapStorePointers() |
696 * @see cxMapStorePointers() |
| 680 * @see cxMapDetach() |
697 * @see cxMapDetach() |
| 681 */ |
698 */ |
| 682 __attribute__((__nonnull__, __warn_unused_result__)) |
699 cx_attr_nonnull |
| |
700 cx_attr_nodiscard |
| 683 static inline void *cxMapRemoveAndGet( |
701 static inline void *cxMapRemoveAndGet( |
| 684 CxMap *map, |
702 CxMap *map, |
| 685 cxstring key |
703 cxstring key |
| 686 ) { |
704 ) { |
| 687 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
705 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
| 704 * @return the stored pointer or \c NULL if either the key is not present |
722 * @return the stored pointer or \c NULL if either the key is not present |
| 705 * in the map or the map is not storing pointers |
723 * in the map or the map is not storing pointers |
| 706 * @see cxMapStorePointers() |
724 * @see cxMapStorePointers() |
| 707 * @see cxMapDetach() |
725 * @see cxMapDetach() |
| 708 */ |
726 */ |
| 709 __attribute__((__nonnull__, __warn_unused_result__)) |
727 cx_attr_nonnull |
| |
728 cx_attr_nodiscard |
| 710 static inline void *cxMapRemoveAndGet( |
729 static inline void *cxMapRemoveAndGet( |
| 711 CxMap *map, |
730 CxMap *map, |
| 712 cxmutstr key |
731 cxmutstr key |
| 713 ) { |
732 ) { |
| 714 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
733 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
| 731 * @return the stored pointer or \c NULL if either the key is not present |
750 * @return the stored pointer or \c NULL if either the key is not present |
| 732 * in the map or the map is not storing pointers |
751 * in the map or the map is not storing pointers |
| 733 * @see cxMapStorePointers() |
752 * @see cxMapStorePointers() |
| 734 * @see cxMapDetach() |
753 * @see cxMapDetach() |
| 735 */ |
754 */ |
| 736 __attribute__((__nonnull__, __warn_unused_result__)) |
755 cx_attr_nonnull |
| |
756 cx_attr_nodiscard |
| |
757 cx_attr_cstr_arg(2) |
| 737 static inline void *cxMapRemoveAndGet( |
758 static inline void *cxMapRemoveAndGet( |
| 738 CxMap *map, |
759 CxMap *map, |
| 739 const char *key |
760 const char *key |
| 740 ) { |
761 ) { |
| 741 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); |
762 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); |
| 985 * without invoking the destructor. |
1013 * without invoking the destructor. |
| 986 * |
1014 * |
| 987 * @param map the map |
1015 * @param map the map |
| 988 * @param key the key |
1016 * @param key the key |
| 989 */ |
1017 */ |
| 990 __attribute__((__nonnull__)) |
1018 cx_attr_nonnull |
| 991 static inline void cx_map_detach( |
1019 static inline void cx_map_detach( |
| 992 CxMap *map, |
1020 CxMap *map, |
| 993 CxHashKey key |
1021 CxHashKey key |
| 994 ) { |
1022 ) { |
| 995 (void) map->cl->remove(map, key, false); |
1023 (void) map->cl->remove(map, key, false); |
| 1000 * without invoking the destructor. |
1028 * without invoking the destructor. |
| 1001 * |
1029 * |
| 1002 * @param map the map |
1030 * @param map the map |
| 1003 * @param key the key |
1031 * @param key the key |
| 1004 */ |
1032 */ |
| 1005 __attribute__((__nonnull__)) |
1033 cx_attr_nonnull |
| 1006 static inline void cx_map_detach_cxstr( |
1034 static inline void cx_map_detach_cxstr( |
| 1007 CxMap *map, |
1035 CxMap *map, |
| 1008 cxstring key |
1036 cxstring key |
| 1009 ) { |
1037 ) { |
| 1010 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); |
1038 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); |
| 1015 * without invoking the destructor. |
1043 * without invoking the destructor. |
| 1016 * |
1044 * |
| 1017 * @param map the map |
1045 * @param map the map |
| 1018 * @param key the key |
1046 * @param key the key |
| 1019 */ |
1047 */ |
| 1020 __attribute__((__nonnull__)) |
1048 cx_attr_nonnull |
| 1021 static inline void cx_map_detach_mustr( |
1049 static inline void cx_map_detach_mustr( |
| 1022 CxMap *map, |
1050 CxMap *map, |
| 1023 cxmutstr key |
1051 cxmutstr key |
| 1024 ) { |
1052 ) { |
| 1025 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); |
1053 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); |
| 1030 * without invoking the destructor. |
1058 * without invoking the destructor. |
| 1031 * |
1059 * |
| 1032 * @param map the map |
1060 * @param map the map |
| 1033 * @param key the key |
1061 * @param key the key |
| 1034 */ |
1062 */ |
| 1035 __attribute__((__nonnull__)) |
1063 cx_attr_nonnull |
| |
1064 cx_attr_cstr_arg(2) |
| 1036 static inline void cx_map_detach_str( |
1065 static inline void cx_map_detach_str( |
| 1037 CxMap *map, |
1066 CxMap *map, |
| 1038 const char *key |
1067 const char *key |
| 1039 ) { |
1068 ) { |
| 1040 (void) map->cl->remove(map, cx_hash_key_str(key), false); |
1069 (void) map->cl->remove(map, cx_hash_key_str(key), false); |
| 1068 * @param map the map |
1097 * @param map the map |
| 1069 * @param key the key |
1098 * @param key the key |
| 1070 * @return the stored pointer or \c NULL if either the key is not present |
1099 * @return the stored pointer or \c NULL if either the key is not present |
| 1071 * in the map or the map is not storing pointers |
1100 * in the map or the map is not storing pointers |
| 1072 */ |
1101 */ |
| 1073 __attribute__((__nonnull__, __warn_unused_result__)) |
1102 cx_attr_nonnull |
| |
1103 cx_attr_nodiscard |
| 1074 static inline void *cx_map_remove_and_get( |
1104 static inline void *cx_map_remove_and_get( |
| 1075 CxMap *map, |
1105 CxMap *map, |
| 1076 CxHashKey key |
1106 CxHashKey key |
| 1077 ) { |
1107 ) { |
| 1078 return map->cl->remove(map, key, !map->collection.store_pointer); |
1108 return map->cl->remove(map, key, !map->collection.store_pointer); |
| 1084 * @param map the map |
1114 * @param map the map |
| 1085 * @param key the key |
1115 * @param key the key |
| 1086 * @return the stored pointer or \c NULL if either the key is not present |
1116 * @return the stored pointer or \c NULL if either the key is not present |
| 1087 * in the map or the map is not storing pointers |
1117 * in the map or the map is not storing pointers |
| 1088 */ |
1118 */ |
| 1089 __attribute__((__nonnull__, __warn_unused_result__)) |
1119 cx_attr_nonnull |
| |
1120 cx_attr_nodiscard |
| 1090 static inline void *cx_map_remove_and_get_cxstr( |
1121 static inline void *cx_map_remove_and_get_cxstr( |
| 1091 CxMap *map, |
1122 CxMap *map, |
| 1092 cxstring key |
1123 cxstring key |
| 1093 ) { |
1124 ) { |
| 1094 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
1125 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
| 1100 * @param map the map |
1131 * @param map the map |
| 1101 * @param key the key |
1132 * @param key the key |
| 1102 * @return the stored pointer or \c NULL if either the key is not present |
1133 * @return the stored pointer or \c NULL if either the key is not present |
| 1103 * in the map or the map is not storing pointers |
1134 * in the map or the map is not storing pointers |
| 1104 */ |
1135 */ |
| 1105 __attribute__((__nonnull__, __warn_unused_result__)) |
1136 cx_attr_nonnull |
| |
1137 cx_attr_nodiscard |
| 1106 static inline void *cx_map_remove_and_get_mustr( |
1138 static inline void *cx_map_remove_and_get_mustr( |
| 1107 CxMap *map, |
1139 CxMap *map, |
| 1108 cxmutstr key |
1140 cxmutstr key |
| 1109 ) { |
1141 ) { |
| 1110 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
1142 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); |
| 1116 * @param map the map |
1148 * @param map the map |
| 1117 * @param key the key |
1149 * @param key the key |
| 1118 * @return the stored pointer or \c NULL if either the key is not present |
1150 * @return the stored pointer or \c NULL if either the key is not present |
| 1119 * in the map or the map is not storing pointers |
1151 * in the map or the map is not storing pointers |
| 1120 */ |
1152 */ |
| 1121 __attribute__((__nonnull__, __warn_unused_result__)) |
1153 cx_attr_nonnull |
| |
1154 cx_attr_nodiscard |
| |
1155 cx_attr_cstr_arg(2) |
| 1122 static inline void *cx_map_remove_and_get_str( |
1156 static inline void *cx_map_remove_and_get_str( |
| 1123 CxMap *map, |
1157 CxMap *map, |
| 1124 const char *key |
1158 const char *key |
| 1125 ) { |
1159 ) { |
| 1126 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); |
1160 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); |