ucx/cx/map.h

changeset 11
0aa8cbd7912e
parent 0
1a157da63d7c
child 16
04c9f8d8f03b
equal deleted inserted replaced
10:80f9d007cb52 11:0aa8cbd7912e
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 (*deallocate)(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 CxMap const *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 * Implementations SHALL check if \p targetbuf is set and copy the elements
124 void *(*remove)( 125 * to the buffer without invoking any destructor.
126 * When \p targetbuf is not set, the destructors SHALL be invoked.
127 *
128 * The function SHALL return zero when the \p key was found and
129 * non-zero, otherwise.
130 */
131 cx_attr_nonnull_arg(1)
132 cx_attr_access_w(3)
133 int (*remove)(
125 CxMap *map, 134 CxMap *map,
126 CxHashKey key, 135 CxHashKey key,
127 bool destroy 136 void *targetbuf
128 ); 137 );
129 138
130 /** 139 /**
131 * Creates an iterator for this map. 140 * Creates an iterator for this map.
132 */ 141 */
133 __attribute__((__nonnull__, __warn_unused_result__)) 142 cx_attr_nonnull
134 CxIterator (*iterator)(CxMap const *map, enum cx_map_iterator_type type); 143 cx_attr_nodiscard
144 CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type);
135 }; 145 };
136 146
137 /** 147 /**
138 * A map entry. 148 * A map entry.
139 */ 149 */
140 struct cx_map_entry_s { 150 struct cx_map_entry_s {
141 /** 151 /**
142 * A pointer to the key. 152 * A pointer to the key.
143 */ 153 */
144 CxHashKey const *key; 154 const CxHashKey *key;
145 /** 155 /**
146 * A pointer to the value. 156 * A pointer to the value.
147 */ 157 */
148 void *value; 158 void *value;
149 }; 159 };
150 160
151 /** 161 /**
152 * A shared instance of an empty map. 162 * A shared instance of an empty map.
153 * 163 *
154 * Writing to that map is undefined. 164 * Writing to that map is not allowed.
155 */ 165 */
156 extern CxMap *const cxEmptyMap; 166 extern CxMap *const cxEmptyMap;
157 167
158 /** 168 /**
159 * Advises the map to store copies of the objects (default mode of operation). 169 * Advises the map to store copies of the objects (default mode of operation).
162 * within this list. 172 * within this list.
163 * 173 *
164 * @param map the map 174 * @param map the map
165 * @see cxMapStorePointers() 175 * @see cxMapStorePointers()
166 */ 176 */
167 __attribute__((__nonnull__)) 177 cx_attr_nonnull
168 static inline void cxMapStoreObjects(CxMap *map) { 178 static inline void cxMapStoreObjects(CxMap *map) {
169 map->collection.store_pointer = false; 179 map->collection.store_pointer = false;
170 } 180 }
171 181
172 /** 182 /**
179 * objects is undefined. 189 * objects is undefined.
180 * 190 *
181 * @param map the map 191 * @param map the map
182 * @see cxMapStoreObjects() 192 * @see cxMapStoreObjects()
183 */ 193 */
184 __attribute__((__nonnull__)) 194 cx_attr_nonnull
185 static inline void cxMapStorePointers(CxMap *map) { 195 static inline void cxMapStorePointers(CxMap *map) {
186 map->collection.store_pointer = true; 196 map->collection.store_pointer = true;
187 map->collection.elem_size = sizeof(void *); 197 map->collection.elem_size = sizeof(void *);
188 } 198 }
189 199
192 * 202 *
193 * @param map 203 * @param map
194 * @return true, if this map is storing pointers 204 * @return true, if this map is storing pointers
195 * @see cxMapStorePointers() 205 * @see cxMapStorePointers()
196 */ 206 */
197 __attribute__((__nonnull__)) 207 cx_attr_nonnull
198 static inline bool cxMapIsStoringPointers(CxMap const *map) { 208 static inline bool cxMapIsStoringPointers(const CxMap *map) {
199 return map->collection.store_pointer; 209 return map->collection.store_pointer;
200 } 210 }
201 211
202 /** 212 /**
203 * Deallocates the memory of the specified map. 213 * Deallocates the memory of the specified map.
204 * 214 *
205 * @param map the map to be destroyed 215 * @param map the map to be freed
206 */ 216 */
207 __attribute__((__nonnull__)) 217 static inline void cxMapFree(CxMap *map) {
208 static inline void cxMapDestroy(CxMap *map) { 218 if (map == NULL) return;
209 map->cl->destructor(map); 219 map->cl->deallocate(map);
210 } 220 }
211 221
212 222
213 /** 223 /**
214 * Clears a map by removing all elements. 224 * Clears a map by removing all elements.
215 * 225 *
216 * @param map the map to be cleared 226 * @param map the map to be cleared
217 */ 227 */
218 __attribute__((__nonnull__)) 228 cx_attr_nonnull
219 static inline void cxMapClear(CxMap *map) { 229 static inline void cxMapClear(CxMap *map) {
220 map->cl->clear(map); 230 map->cl->clear(map);
221 } 231 }
222 232
223 /** 233 /**
224 * Returns the number of elements in this map. 234 * Returns the number of elements in this map.
225 * 235 *
226 * @param map the map 236 * @param map the map
227 * @return the number of stored elements 237 * @return the number of stored elements
228 */ 238 */
229 __attribute__((__nonnull__)) 239 cx_attr_nonnull
230 static inline size_t cxMapSize(CxMap const *map) { 240 static inline size_t cxMapSize(const CxMap *map) {
231 return map->collection.size; 241 return map->collection.size;
232 } 242 }
233 243
234 244
235 // TODO: set-like map operations (union, intersect, difference) 245 // TODO: set-like map operations (union, intersect, difference)
236 246
237 /** 247 /**
238 * Creates a value iterator for a map. 248 * Creates a value iterator for a map.
239 * 249 *
240 * \note An iterator iterates over all elements successively. Therefore the order 250 * \note An iterator iterates over all elements successively. Therefore, the order
241 * highly depends on the map implementation and may change arbitrarily when the contents change. 251 * highly depends on the map implementation and may change arbitrarily when the contents change.
242 * 252 *
243 * @param map the map to create the iterator for 253 * @param map the map to create the iterator for
244 * @return an iterator for the currently stored values 254 * @return an iterator for the currently stored values
245 */ 255 */
246 __attribute__((__nonnull__, __warn_unused_result__)) 256 cx_attr_nonnull
247 static inline CxIterator cxMapIteratorValues(CxMap const *map) { 257 cx_attr_nodiscard
258 static inline CxIterator cxMapIteratorValues(const CxMap *map) {
248 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); 259 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
249 } 260 }
250 261
251 /** 262 /**
252 * Creates a key iterator for a map. 263 * Creates a key iterator for a map.
253 * 264 *
254 * The elements of the iterator are keys of type CxHashKey. 265 * The elements of the iterator are keys of type CxHashKey.
255 * 266 *
256 * \note An iterator iterates over all elements successively. Therefore the order 267 * \note An iterator iterates over all elements successively. Therefore, the order
257 * highly depends on the map implementation and may change arbitrarily when the contents change. 268 * highly depends on the map implementation and may change arbitrarily when the contents change.
258 * 269 *
259 * @param map the map to create the iterator for 270 * @param map the map to create the iterator for
260 * @return an iterator for the currently stored keys 271 * @return an iterator for the currently stored keys
261 */ 272 */
262 __attribute__((__nonnull__, __warn_unused_result__)) 273 cx_attr_nonnull
263 static inline CxIterator cxMapIteratorKeys(CxMap const *map) { 274 cx_attr_nodiscard
275 static inline CxIterator cxMapIteratorKeys(const CxMap *map) {
264 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); 276 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
265 } 277 }
266 278
267 /** 279 /**
268 * Creates an iterator for a map. 280 * Creates an iterator for a map.
269 * 281 *
270 * The elements of the iterator are key/value pairs of type CxMapEntry. 282 * The elements of the iterator are key/value pairs of type CxMapEntry.
271 * 283 *
272 * \note An iterator iterates over all elements successively. Therefore the order 284 * \note An iterator iterates over all elements successively. Therefore, the order
273 * highly depends on the map implementation and may change arbitrarily when the contents change. 285 * highly depends on the map implementation and may change arbitrarily when the contents change.
274 * 286 *
275 * @param map the map to create the iterator for 287 * @param map the map to create the iterator for
276 * @return an iterator for the currently stored entries 288 * @return an iterator for the currently stored entries
277 * @see cxMapIteratorKeys() 289 * @see cxMapIteratorKeys()
278 * @see cxMapIteratorValues() 290 * @see cxMapIteratorValues()
279 */ 291 */
280 __attribute__((__nonnull__, __warn_unused_result__)) 292 cx_attr_nonnull
281 static inline CxIterator cxMapIterator(CxMap const *map) { 293 cx_attr_nodiscard
294 static inline CxIterator cxMapIterator(const CxMap *map) {
282 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); 295 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
283 } 296 }
284 297
285 298
286 /** 299 /**
287 * Creates a mutating iterator over the values of a map. 300 * Creates a mutating iterator over the values of a map.
288 * 301 *
289 * \note An iterator iterates over all elements successively. Therefore the order 302 * \note An iterator iterates over all elements successively. Therefore, the order
290 * highly depends on the map implementation and may change arbitrarily when the contents change. 303 * highly depends on the map implementation and may change arbitrarily when the contents change.
291 * 304 *
292 * @param map the map to create the iterator for 305 * @param map the map to create the iterator for
293 * @return an iterator for the currently stored values 306 * @return an iterator for the currently stored values
294 */ 307 */
295 __attribute__((__nonnull__, __warn_unused_result__)) 308 cx_attr_nonnull
309 cx_attr_nodiscard
296 CxIterator cxMapMutIteratorValues(CxMap *map); 310 CxIterator cxMapMutIteratorValues(CxMap *map);
297 311
298 /** 312 /**
299 * Creates a mutating iterator over the keys of a map. 313 * Creates a mutating iterator over the keys of a map.
300 * 314 *
301 * The elements of the iterator are keys of type CxHashKey. 315 * The elements of the iterator are keys of type CxHashKey.
302 * 316 *
303 * \note An iterator iterates over all elements successively. Therefore the order 317 * \note An iterator iterates over all elements successively. Therefore, the order
304 * highly depends on the map implementation and may change arbitrarily when the contents change. 318 * highly depends on the map implementation and may change arbitrarily when the contents change.
305 * 319 *
306 * @param map the map to create the iterator for 320 * @param map the map to create the iterator for
307 * @return an iterator for the currently stored keys 321 * @return an iterator for the currently stored keys
308 */ 322 */
309 __attribute__((__nonnull__, __warn_unused_result__)) 323 cx_attr_nonnull
324 cx_attr_nodiscard
310 CxIterator cxMapMutIteratorKeys(CxMap *map); 325 CxIterator cxMapMutIteratorKeys(CxMap *map);
311 326
312 /** 327 /**
313 * Creates a mutating iterator for a map. 328 * Creates a mutating iterator for a map.
314 * 329 *
315 * The elements of the iterator are key/value pairs of type CxMapEntry. 330 * The elements of the iterator are key/value pairs of type CxMapEntry.
316 * 331 *
317 * \note An iterator iterates over all elements successively. Therefore the order 332 * \note An iterator iterates over all elements successively. Therefore, the order
318 * highly depends on the map implementation and may change arbitrarily when the contents change. 333 * highly depends on the map implementation and may change arbitrarily when the contents change.
319 * 334 *
320 * @param map the map to create the iterator for 335 * @param map the map to create the iterator for
321 * @return an iterator for the currently stored entries 336 * @return an iterator for the currently stored entries
322 * @see cxMapMutIteratorKeys() 337 * @see cxMapMutIteratorKeys()
323 * @see cxMapMutIteratorValues() 338 * @see cxMapMutIteratorValues()
324 */ 339 */
325 __attribute__((__nonnull__, __warn_unused_result__)) 340 cx_attr_nonnull
341 cx_attr_nodiscard
326 CxIterator cxMapMutIterator(CxMap *map); 342 CxIterator cxMapMutIterator(CxMap *map);
327 343
328 #ifdef __cplusplus 344 #ifdef __cplusplus
329 } // end the extern "C" block here, because we want to start overloading 345 } // end the extern "C" block here, because we want to start overloading
330 346
334 * @param map the map 350 * @param map the map
335 * @param key the key 351 * @param key the key
336 * @param value the value 352 * @param value the value
337 * @return 0 on success, non-zero value on failure 353 * @return 0 on success, non-zero value on failure
338 */ 354 */
339 __attribute__((__nonnull__)) 355 cx_attr_nonnull
340 static inline int cxMapPut( 356 static inline int cxMapPut(
341 CxMap *map, 357 CxMap *map,
342 CxHashKey const &key, 358 CxHashKey const &key,
343 void *value 359 void *value
344 ) { 360 ) {
352 * @param map the map 368 * @param map the map
353 * @param key the key 369 * @param key the key
354 * @param value the value 370 * @param value the value
355 * @return 0 on success, non-zero value on failure 371 * @return 0 on success, non-zero value on failure
356 */ 372 */
357 __attribute__((__nonnull__)) 373 cx_attr_nonnull
358 static inline int cxMapPut( 374 static inline int cxMapPut(
359 CxMap *map, 375 CxMap *map,
360 cxstring const &key, 376 cxstring const &key,
361 void *value 377 void *value
362 ) { 378 ) {
369 * @param map the map 385 * @param map the map
370 * @param key the key 386 * @param key the key
371 * @param value the value 387 * @param value the value
372 * @return 0 on success, non-zero value on failure 388 * @return 0 on success, non-zero value on failure
373 */ 389 */
374 __attribute__((__nonnull__)) 390 cx_attr_nonnull
375 static inline int cxMapPut( 391 static inline int cxMapPut(
376 CxMap *map, 392 CxMap *map,
377 cxmutstr const &key, 393 cxmutstr const &key,
378 void *value 394 void *value
379 ) { 395 ) {
386 * @param map the map 402 * @param map the map
387 * @param key the key 403 * @param key the key
388 * @param value the value 404 * @param value the value
389 * @return 0 on success, non-zero value on failure 405 * @return 0 on success, non-zero value on failure
390 */ 406 */
391 __attribute__((__nonnull__)) 407 cx_attr_nonnull
408 cx_attr_cstr_arg(2)
392 static inline int cxMapPut( 409 static inline int cxMapPut(
393 CxMap *map, 410 CxMap *map,
394 char const *key, 411 const char *key,
395 void *value 412 void *value
396 ) { 413 ) {
397 return map->cl->put(map, cx_hash_key_str(key), value); 414 return map->cl->put(map, cx_hash_key_str(key), value);
398 } 415 }
399 416
402 * 419 *
403 * @param map the map 420 * @param map the map
404 * @param key the key 421 * @param key the key
405 * @return the value 422 * @return the value
406 */ 423 */
407 __attribute__((__nonnull__, __warn_unused_result__)) 424 cx_attr_nonnull
425 cx_attr_nodiscard
408 static inline void *cxMapGet( 426 static inline void *cxMapGet(
409 CxMap const *map, 427 const CxMap *map,
410 CxHashKey const &key 428 CxHashKey const &key
411 ) { 429 ) {
412 return map->cl->get(map, key); 430 return map->cl->get(map, key);
413 } 431 }
414 432
417 * 435 *
418 * @param map the map 436 * @param map the map
419 * @param key the key 437 * @param key the key
420 * @return the value 438 * @return the value
421 */ 439 */
422 __attribute__((__nonnull__, __warn_unused_result__)) 440 cx_attr_nonnull
441 cx_attr_nodiscard
423 static inline void *cxMapGet( 442 static inline void *cxMapGet(
424 CxMap const *map, 443 const CxMap *map,
425 cxstring const &key 444 cxstring const &key
426 ) { 445 ) {
427 return map->cl->get(map, cx_hash_key_cxstr(key)); 446 return map->cl->get(map, cx_hash_key_cxstr(key));
428 } 447 }
429 448
432 * 451 *
433 * @param map the map 452 * @param map the map
434 * @param key the key 453 * @param key the key
435 * @return the value 454 * @return the value
436 */ 455 */
437 __attribute__((__nonnull__, __warn_unused_result__)) 456 cx_attr_nonnull
457 cx_attr_nodiscard
438 static inline void *cxMapGet( 458 static inline void *cxMapGet(
439 CxMap const *map, 459 const CxMap *map,
440 cxmutstr const &key 460 cxmutstr const &key
441 ) { 461 ) {
442 return map->cl->get(map, cx_hash_key_cxstr(key)); 462 return map->cl->get(map, cx_hash_key_cxstr(key));
443 } 463 }
444 464
447 * 467 *
448 * @param map the map 468 * @param map the map
449 * @param key the key 469 * @param key the key
450 * @return the value 470 * @return the value
451 */ 471 */
452 __attribute__((__nonnull__, __warn_unused_result__)) 472 cx_attr_nonnull
473 cx_attr_nodiscard
474 cx_attr_cstr_arg(2)
453 static inline void *cxMapGet( 475 static inline void *cxMapGet(
454 CxMap const *map, 476 const CxMap *map,
455 char const *key 477 const char *key
456 ) { 478 ) {
457 return map->cl->get(map, cx_hash_key_str(key)); 479 return map->cl->get(map, cx_hash_key_str(key));
458 } 480 }
459 481
460 /** 482 /**
461 * Removes a key/value-pair from the map by using the key. 483 * Removes a key/value-pair from the map by using the key.
462 * 484 *
463 * Always invokes the destructor function, if any, on the removed element. 485 * Always invokes the destructors functions, if any, on the removed element.
464 * If this map is storing pointers and you just want to retrieve the pointer 486 *
465 * without invoking the destructor, use cxMapRemoveAndGet(). 487 * @param map the map
466 * If you just want to detach the element from the map without invoking the 488 * @param key the key
467 * destructor or returning the element, use cxMapDetach(). 489 * @return zero on success, non-zero if the key was not found
468 * 490 *
469 * @param map the map
470 * @param key the key
471 * @see cxMapRemoveAndGet() 491 * @see cxMapRemoveAndGet()
472 * @see cxMapDetach() 492 */
473 */ 493 cx_attr_nonnull
474 __attribute__((__nonnull__)) 494 static inline int cxMapRemove(
475 static inline void cxMapRemove(
476 CxMap *map, 495 CxMap *map,
477 CxHashKey const &key 496 CxHashKey const &key
478 ) { 497 ) {
479 (void) map->cl->remove(map, key, true); 498 return map->cl->remove(map, key, nullptr);
480 } 499 }
481 500
482 /** 501 /**
483 * Removes a key/value-pair from the map by using the key. 502 * Removes a key/value-pair from the map by using the key.
484 * 503 *
485 * Always invokes the destructor function, if any, on the removed element. 504 * Always invokes the destructors functions, if any, on the removed element.
486 * If this map is storing pointers and you just want to retrieve the pointer 505 *
487 * without invoking the destructor, use cxMapRemoveAndGet(). 506 * @param map the map
488 * If you just want to detach the element from the map without invoking the 507 * @param key the key
489 * destructor or returning the element, use cxMapDetach(). 508 * @return zero on success, non-zero if the key was not found
490 * 509 *
491 * @param map the map
492 * @param key the key
493 * @see cxMapRemoveAndGet() 510 * @see cxMapRemoveAndGet()
494 * @see cxMapDetach() 511 */
495 */ 512 cx_attr_nonnull
496 __attribute__((__nonnull__)) 513 static inline int cxMapRemove(
497 static inline void cxMapRemove(
498 CxMap *map, 514 CxMap *map,
499 cxstring const &key 515 cxstring const &key
500 ) { 516 ) {
501 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 517 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
502 } 518 }
503 519
504 /** 520 /**
505 * Removes a key/value-pair from the map by using the key. 521 * Removes a key/value-pair from the map by using the key.
506 * 522 *
507 * Always invokes the destructor function, if any, on the removed element. 523 * Always invokes the destructors functions, if any, on the removed element.
508 * If this map is storing pointers and you just want to retrieve the pointer 524 *
509 * without invoking the destructor, use cxMapRemoveAndGet(). 525 * @param map the map
510 * If you just want to detach the element from the map without invoking the 526 * @param key the key
511 * destructor or returning the element, use cxMapDetach(). 527 * @return zero on success, non-zero if the key was not found
512 * 528 *
513 * @param map the map
514 * @param key the key
515 * @see cxMapRemoveAndGet() 529 * @see cxMapRemoveAndGet()
516 * @see cxMapDetach() 530 */
517 */ 531 cx_attr_nonnull
518 __attribute__((__nonnull__)) 532 static inline int cxMapRemove(
519 static inline void cxMapRemove(
520 CxMap *map, 533 CxMap *map,
521 cxmutstr const &key 534 cxmutstr const &key
522 ) { 535 ) {
523 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 536 return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
524 } 537 }
525 538
526 /** 539 /**
527 * Removes a key/value-pair from the map by using the key. 540 * Removes a key/value-pair from the map by using the key.
528 * 541 *
529 * Always invokes the destructor function, if any, on the removed element. 542 * Always invokes the destructors functions, if any, on the removed element.
530 * If this map is storing pointers and you just want to retrieve the pointer 543 *
531 * without invoking the destructor, use cxMapRemoveAndGet(). 544 * @param map the map
532 * If you just want to detach the element from the map without invoking the 545 * @param key the key
533 * destructor or returning the element, use cxMapDetach(). 546 * @return zero on success, non-zero if the key was not found
534 * 547 *
535 * @param map the map
536 * @param key the key
537 * @see cxMapRemoveAndGet() 548 * @see cxMapRemoveAndGet()
538 * @see cxMapDetach() 549 */
539 */ 550 cx_attr_nonnull
540 __attribute__((__nonnull__)) 551 cx_attr_cstr_arg(2)
541 static inline void cxMapRemove( 552 static inline int cxMapRemove(
542 CxMap *map, 553 CxMap *map,
543 char const *key 554 const char *key
544 ) { 555 ) {
545 (void) map->cl->remove(map, cx_hash_key_str(key), true); 556 return map->cl->remove(map, cx_hash_key_str(key), nullptr);
546 } 557 }
547 558
548 /** 559 /**
549 * Detaches a key/value-pair from the map by using the key 560 * Removes a key/value-pair from the map by using the key.
550 * without invoking the destructor. 561 *
551 * 562 * This function will copy the contents to the target buffer
552 * In general, you should only use this function if the map does not own 563 * which must be guaranteed to be large enough to hold the element.
553 * the data and there is a valid reference to the data somewhere else 564 * The destructor functions, if any, will \em not be called.
554 * in the program. In all other cases it is preferable to use 565 *
555 * cxMapRemove() or cxMapRemoveAndGet(). 566 * If this map is storing pointers, the element is the pointer itself
556 * 567 * and not the object it points to.
557 * @param map the map 568 *
558 * @param key the key 569 * @param map the map
570 * @param key the key
571 * @param targetbuf the buffer where the element shall be copied to
572 * @return zero on success, non-zero if the key was not found
573 *
574 * @see cxMapStorePointers()
559 * @see cxMapRemove() 575 * @see cxMapRemove()
560 * @see cxMapRemoveAndGet() 576 */
561 */ 577 cx_attr_nonnull
562 __attribute__((__nonnull__)) 578 cx_attr_access_w(3)
563 static inline void cxMapDetach( 579 static inline int cxMapRemoveAndGet(
564 CxMap *map, 580 CxMap *map,
565 CxHashKey const &key 581 CxHashKey key,
566 ) { 582 void *targetbuf
567 (void) map->cl->remove(map, key, false); 583 ) {
568 } 584 return map->cl->remove(map, key, targetbuf);
569 585 }
570 /** 586
571 * Detaches a key/value-pair from the map by using the key 587 /**
572 * without invoking the destructor. 588 * Removes a key/value-pair from the map by using the key.
573 * 589 *
574 * In general, you should only use this function if the map does not own 590 * This function will copy the contents to the target buffer
575 * the data and there is a valid reference to the data somewhere else 591 * which must be guaranteed to be large enough to hold the element.
576 * in the program. In all other cases it is preferable to use 592 * The destructor functions, if any, will \em not be called.
577 * cxMapRemove() or cxMapRemoveAndGet(). 593 *
578 * 594 * If this map is storing pointers, the element is the pointer itself
579 * @param map the map 595 * and not the object it points to.
580 * @param key the key 596 *
597 * @param map the map
598 * @param key the key
599 * @param targetbuf the buffer where the element shall be copied to
600 * @return zero on success, non-zero if the key was not found
601 *
602 * @see cxMapStorePointers()
581 * @see cxMapRemove() 603 * @see cxMapRemove()
582 * @see cxMapRemoveAndGet() 604 */
583 */ 605 cx_attr_nonnull
584 __attribute__((__nonnull__)) 606 cx_attr_access_w(3)
585 static inline void cxMapDetach( 607 static inline int cxMapRemoveAndGet(
586 CxMap *map, 608 CxMap *map,
587 cxstring const &key 609 cxstring key,
588 ) { 610 void *targetbuf
589 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 611 ) {
590 } 612 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
591 613 }
592 /** 614
593 * Detaches a key/value-pair from the map by using the key 615 /**
594 * without invoking the destructor. 616 * Removes a key/value-pair from the map by using the key.
595 * 617 *
596 * In general, you should only use this function if the map does not own 618 * This function will copy the contents to the target buffer
597 * the data and there is a valid reference to the data somewhere else 619 * which must be guaranteed to be large enough to hold the element.
598 * in the program. In all other cases it is preferable to use 620 * The destructor functions, if any, will \em not be called.
599 * cxMapRemove() or cxMapRemoveAndGet(). 621 *
600 * 622 * If this map is storing pointers, the element is the pointer itself
601 * @param map the map 623 * and not the object it points to.
602 * @param key the key 624 *
625 * @param map the map
626 * @param key the key
627 * @param targetbuf the buffer where the element shall be copied to
628 * @return zero on success, non-zero if the key was not found
629 *
630 * @see cxMapStorePointers()
603 * @see cxMapRemove() 631 * @see cxMapRemove()
604 * @see cxMapRemoveAndGet() 632 */
605 */ 633 cx_attr_nonnull
606 __attribute__((__nonnull__)) 634 cx_attr_access_w(3)
607 static inline void cxMapDetach( 635 static inline int cxMapRemoveAndGet(
608 CxMap *map, 636 CxMap *map,
609 cxmutstr const &key 637 cxmutstr key,
610 ) { 638 void *targetbuf
611 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 639 ) {
612 } 640 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
613 641 }
614 /** 642
615 * Detaches a key/value-pair from the map by using the key 643 /**
616 * without invoking the destructor. 644 * Removes a key/value-pair from the map by using the key.
617 * 645 *
618 * In general, you should only use this function if the map does not own 646 * This function will copy the contents to the target buffer
619 * the data and there is a valid reference to the data somewhere else 647 * which must be guaranteed to be large enough to hold the element.
620 * in the program. In all other cases it is preferable to use 648 * The destructor functions, if any, will \em not be called.
621 * cxMapRemove() or cxMapRemoveAndGet(). 649 *
622 * 650 * If this map is storing pointers, the element is the pointer itself
623 * @param map the map 651 * and not the object it points to.
624 * @param key the key 652 *
653 * @param map the map
654 * @param key the key
655 * @param targetbuf the buffer where the element shall be copied to
656 * @return zero on success, non-zero if the key was not found
657 *
658 * @see cxMapStorePointers()
625 * @see cxMapRemove() 659 * @see cxMapRemove()
626 * @see cxMapRemoveAndGet() 660 */
627 */ 661 cx_attr_nonnull
628 __attribute__((__nonnull__)) 662 cx_attr_access_w(3)
629 static inline void cxMapDetach( 663 cx_attr_cstr_arg(2)
630 CxMap *map, 664 static inline int cxMapRemoveAndGet(
631 char const *key 665 CxMap *map,
632 ) { 666 const char *key,
633 (void) map->cl->remove(map, cx_hash_key_str(key), false); 667 void *targetbuf
634 } 668 ) {
635 669 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
636 /**
637 * Removes a key/value-pair from the map by using the key.
638 *
639 * This function can be used when the map is storing pointers,
640 * in order to retrieve the pointer from the map without invoking
641 * any destructor function. Sometimes you do not want the pointer
642 * to be returned - in that case (instead of suppressing the "unused
643 * result" warning) you can use cxMapDetach().
644 *
645 * If this map is not storing pointers, this function behaves like
646 * cxMapRemove() and returns \c NULL.
647 *
648 * @param map the map
649 * @param key the key
650 * @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
652 * @see cxMapStorePointers()
653 * @see cxMapDetach()
654 */
655 __attribute__((__nonnull__, __warn_unused_result__))
656 static inline void *cxMapRemoveAndGet(
657 CxMap *map,
658 CxHashKey key
659 ) {
660 return map->cl->remove(map, key, !map->store_pointer);
661 }
662
663 /**
664 * Removes a key/value-pair from the map by using the key.
665 *
666 * This function can be used when the map is storing pointers,
667 * in order to retrieve the pointer from the map without invoking
668 * any destructor function. Sometimes you do not want the pointer
669 * to be returned - in that case (instead of suppressing the "unused
670 * result" warning) you can use cxMapDetach().
671 *
672 * If this map is not storing pointers, this function behaves like
673 * cxMapRemove() and returns \c NULL.
674 *
675 * @param map the map
676 * @param key the key
677 * @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
679 * @see cxMapStorePointers()
680 * @see cxMapDetach()
681 */
682 __attribute__((__nonnull__, __warn_unused_result__))
683 static inline void *cxMapRemoveAndGet(
684 CxMap *map,
685 cxstring key
686 ) {
687 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
688 }
689
690 /**
691 * Removes a key/value-pair from the map by using the key.
692 *
693 * This function can be used when the map is storing pointers,
694 * in order to retrieve the pointer from the map without invoking
695 * any destructor function. Sometimes you do not want the pointer
696 * to be returned - in that case (instead of suppressing the "unused
697 * result" warning) you can use cxMapDetach().
698 *
699 * If this map is not storing pointers, this function behaves like
700 * cxMapRemove() and returns \c NULL.
701 *
702 * @param map the map
703 * @param key the key
704 * @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
706 * @see cxMapStorePointers()
707 * @see cxMapDetach()
708 */
709 __attribute__((__nonnull__, __warn_unused_result__))
710 static inline void *cxMapRemoveAndGet(
711 CxMap *map,
712 cxmutstr key
713 ) {
714 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer);
715 }
716
717 /**
718 * Removes a key/value-pair from the map by using the key.
719 *
720 * This function can be used when the map is storing pointers,
721 * in order to retrieve the pointer from the map without invoking
722 * any destructor function. Sometimes you do not want the pointer
723 * to be returned - in that case (instead of suppressing the "unused
724 * result" warning) you can use cxMapDetach().
725 *
726 * If this map is not storing pointers, this function behaves like
727 * cxMapRemove() and returns \c NULL.
728 *
729 * @param map the map
730 * @param key the key
731 * @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
733 * @see cxMapStorePointers()
734 * @see cxMapDetach()
735 */
736 __attribute__((__nonnull__, __warn_unused_result__))
737 static inline void *cxMapRemoveAndGet(
738 CxMap *map,
739 char const *key
740 ) {
741 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer);
742 } 670 }
743 671
744 #else // __cplusplus 672 #else // __cplusplus
745 673
746 /** 674 /**
749 * @param map the map 677 * @param map the map
750 * @param key the key 678 * @param key the key
751 * @param value the value 679 * @param value the value
752 * @return 0 on success, non-zero value on failure 680 * @return 0 on success, non-zero value on failure
753 */ 681 */
754 __attribute__((__nonnull__)) 682 cx_attr_nonnull
755 static inline int cx_map_put( 683 static inline int cx_map_put(
756 CxMap *map, 684 CxMap *map,
757 CxHashKey key, 685 CxHashKey key,
758 void *value 686 void *value
759 ) { 687 ) {
766 * @param map the map 694 * @param map the map
767 * @param key the key 695 * @param key the key
768 * @param value the value 696 * @param value the value
769 * @return 0 on success, non-zero value on failure 697 * @return 0 on success, non-zero value on failure
770 */ 698 */
771 __attribute__((__nonnull__)) 699 cx_attr_nonnull
772 static inline int cx_map_put_cxstr( 700 static inline int cx_map_put_cxstr(
773 CxMap *map, 701 CxMap *map,
774 cxstring key, 702 cxstring key,
775 void *value 703 void *value
776 ) { 704 ) {
783 * @param map the map 711 * @param map the map
784 * @param key the key 712 * @param key the key
785 * @param value the value 713 * @param value the value
786 * @return 0 on success, non-zero value on failure 714 * @return 0 on success, non-zero value on failure
787 */ 715 */
788 __attribute__((__nonnull__)) 716 cx_attr_nonnull
789 static inline int cx_map_put_mustr( 717 static inline int cx_map_put_mustr(
790 CxMap *map, 718 CxMap *map,
791 cxmutstr key, 719 cxmutstr key,
792 void *value 720 void *value
793 ) { 721 ) {
800 * @param map the map 728 * @param map the map
801 * @param key the key 729 * @param key the key
802 * @param value the value 730 * @param value the value
803 * @return 0 on success, non-zero value on failure 731 * @return 0 on success, non-zero value on failure
804 */ 732 */
805 __attribute__((__nonnull__)) 733 cx_attr_nonnull
734 cx_attr_cstr_arg(2)
806 static inline int cx_map_put_str( 735 static inline int cx_map_put_str(
807 CxMap *map, 736 CxMap *map,
808 char const *key, 737 const char *key,
809 void *value 738 void *value
810 ) { 739 ) {
811 return map->cl->put(map, cx_hash_key_str(key), value); 740 return map->cl->put(map, cx_hash_key_str(key), value);
812 } 741 }
813 742
822 #define cxMapPut(map, key, value) _Generic((key), \ 751 #define cxMapPut(map, key, value) _Generic((key), \
823 CxHashKey: cx_map_put, \ 752 CxHashKey: cx_map_put, \
824 cxstring: cx_map_put_cxstr, \ 753 cxstring: cx_map_put_cxstr, \
825 cxmutstr: cx_map_put_mustr, \ 754 cxmutstr: cx_map_put_mustr, \
826 char*: cx_map_put_str, \ 755 char*: cx_map_put_str, \
827 char const*: cx_map_put_str) \ 756 const char*: cx_map_put_str) \
828 (map, key, value) 757 (map, key, value)
829 758
830 /** 759 /**
831 * Retrieves a value by using a key. 760 * Retrieves a value by using a key.
832 * 761 *
833 * @param map the map 762 * @param map the map
834 * @param key the key 763 * @param key the key
835 * @return the value 764 * @return the value
836 */ 765 */
837 __attribute__((__nonnull__, __warn_unused_result__)) 766 cx_attr_nonnull
767 cx_attr_nodiscard
838 static inline void *cx_map_get( 768 static inline void *cx_map_get(
839 CxMap const *map, 769 const CxMap *map,
840 CxHashKey key 770 CxHashKey key
841 ) { 771 ) {
842 return map->cl->get(map, key); 772 return map->cl->get(map, key);
843 } 773 }
844 774
847 * 777 *
848 * @param map the map 778 * @param map the map
849 * @param key the key 779 * @param key the key
850 * @return the value 780 * @return the value
851 */ 781 */
852 __attribute__((__nonnull__, __warn_unused_result__)) 782 cx_attr_nonnull
783 cx_attr_nodiscard
853 static inline void *cx_map_get_cxstr( 784 static inline void *cx_map_get_cxstr(
854 CxMap const *map, 785 const CxMap *map,
855 cxstring key 786 cxstring key
856 ) { 787 ) {
857 return map->cl->get(map, cx_hash_key_cxstr(key)); 788 return map->cl->get(map, cx_hash_key_cxstr(key));
858 } 789 }
859 790
862 * 793 *
863 * @param map the map 794 * @param map the map
864 * @param key the key 795 * @param key the key
865 * @return the value 796 * @return the value
866 */ 797 */
867 __attribute__((__nonnull__, __warn_unused_result__)) 798 cx_attr_nonnull
799 cx_attr_nodiscard
868 static inline void *cx_map_get_mustr( 800 static inline void *cx_map_get_mustr(
869 CxMap const *map, 801 const CxMap *map,
870 cxmutstr key 802 cxmutstr key
871 ) { 803 ) {
872 return map->cl->get(map, cx_hash_key_cxstr(key)); 804 return map->cl->get(map, cx_hash_key_cxstr(key));
873 } 805 }
874 806
877 * 809 *
878 * @param map the map 810 * @param map the map
879 * @param key the key 811 * @param key the key
880 * @return the value 812 * @return the value
881 */ 813 */
882 __attribute__((__nonnull__, __warn_unused_result__)) 814 cx_attr_nonnull
815 cx_attr_nodiscard
816 cx_attr_cstr_arg(2)
883 static inline void *cx_map_get_str( 817 static inline void *cx_map_get_str(
884 CxMap const *map, 818 const CxMap *map,
885 char const *key 819 const char *key
886 ) { 820 ) {
887 return map->cl->get(map, cx_hash_key_str(key)); 821 return map->cl->get(map, cx_hash_key_str(key));
888 } 822 }
889 823
890 /** 824 /**
897 #define cxMapGet(map, key) _Generic((key), \ 831 #define cxMapGet(map, key) _Generic((key), \
898 CxHashKey: cx_map_get, \ 832 CxHashKey: cx_map_get, \
899 cxstring: cx_map_get_cxstr, \ 833 cxstring: cx_map_get_cxstr, \
900 cxmutstr: cx_map_get_mustr, \ 834 cxmutstr: cx_map_get_mustr, \
901 char*: cx_map_get_str, \ 835 char*: cx_map_get_str, \
902 char const*: cx_map_get_str) \ 836 const char*: cx_map_get_str) \
903 (map, key) 837 (map, key)
904 838
905 /** 839 /**
906 * Removes a key/value-pair from the map by using the key. 840 * Removes a key/value-pair from the map by using the key.
907 * 841 *
908 * @param map the map 842 * Always invokes the destructors functions, if any, on the removed element.
909 * @param key the key 843 *
910 */ 844 * @param map the map
911 __attribute__((__nonnull__)) 845 * @param key the key
912 static inline void cx_map_remove( 846 * @return zero on success, non-zero if the key was not found
847 */
848 cx_attr_nonnull
849 static inline int cx_map_remove(
913 CxMap *map, 850 CxMap *map,
914 CxHashKey key 851 CxHashKey key
915 ) { 852 ) {
916 (void) map->cl->remove(map, key, true); 853 return map->cl->remove(map, key, NULL);
917 } 854 }
918 855
919 /** 856 /**
920 * Removes a key/value-pair from the map by using the key. 857 * Removes a key/value-pair from the map by using the key.
921 * 858 *
922 * @param map the map 859 * Always invokes the destructors functions, if any, on the removed element.
923 * @param key the key 860 *
924 */ 861 * @param map the map
925 __attribute__((__nonnull__)) 862 * @param key the key
926 static inline void cx_map_remove_cxstr( 863 * @return zero on success, non-zero if the key was not found
864 */
865 cx_attr_nonnull
866 static inline int cx_map_remove_cxstr(
927 CxMap *map, 867 CxMap *map,
928 cxstring key 868 cxstring key
929 ) { 869 ) {
930 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 870 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
931 } 871 }
932 872
933 /** 873 /**
934 * Removes a key/value-pair from the map by using the key. 874 * Removes a key/value-pair from the map by using the key.
935 * 875 *
936 * @param map the map 876 * Always invokes the destructors functions, if any, on the removed element.
937 * @param key the key 877 *
938 */ 878 * @param map the map
939 __attribute__((__nonnull__)) 879 * @param key the key
940 static inline void cx_map_remove_mustr( 880 * @return zero on success, non-zero if the key was not found
881 */
882 cx_attr_nonnull
883 static inline int cx_map_remove_mustr(
941 CxMap *map, 884 CxMap *map,
942 cxmutstr key 885 cxmutstr key
943 ) { 886 ) {
944 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); 887 return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
945 } 888 }
946 889
947 /** 890 /**
948 * 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.
949 * 892 *
950 * @param map the map 893 * Always invokes the destructors functions, if any, on the removed element.
951 * @param key the key 894 *
952 */ 895 * @param map the map
953 __attribute__((__nonnull__)) 896 * @param key the key
954 static inline void cx_map_remove_str( 897 * @return zero on success, non-zero if the key was not found
955 CxMap *map, 898 */
956 char const *key 899 cx_attr_nonnull
957 ) { 900 cx_attr_cstr_arg(2)
958 (void) map->cl->remove(map, cx_hash_key_str(key), true); 901 static inline int cx_map_remove_str(
959 } 902 CxMap *map,
960 903 const char *key
961 /** 904 ) {
962 * Removes a key/value-pair from the map by using the key. 905 return map->cl->remove(map, cx_hash_key_str(key), NULL);
963 * 906 }
964 * Always invokes the destructor function, if any, on the removed element. 907
965 * If this map is storing pointers and you just want to retrieve the pointer 908 /**
966 * without invoking the destructor, use cxMapRemoveAndGet(). 909 * Removes a key/value-pair from the map by using the key.
967 * If you just want to detach the element from the map without invoking the 910 *
968 * destructor or returning the element, use cxMapDetach(). 911 * Always invokes the destructors functions, if any, on the removed element.
969 * 912 *
970 * @param map the map 913 * @param map the map
971 * @param key the key 914 * @param key the key
915 * @return zero on success, non-zero if the key was not found
916 *
972 * @see cxMapRemoveAndGet() 917 * @see cxMapRemoveAndGet()
973 * @see cxMapDetach()
974 */ 918 */
975 #define cxMapRemove(map, key) _Generic((key), \ 919 #define cxMapRemove(map, key) _Generic((key), \
976 CxHashKey: cx_map_remove, \ 920 CxHashKey: cx_map_remove, \
977 cxstring: cx_map_remove_cxstr, \ 921 cxstring: cx_map_remove_cxstr, \
978 cxmutstr: cx_map_remove_mustr, \ 922 cxmutstr: cx_map_remove_mustr, \
979 char*: cx_map_remove_str, \ 923 char*: cx_map_remove_str, \
980 char const*: cx_map_remove_str) \ 924 const char*: cx_map_remove_str) \
981 (map, key) 925 (map, key)
982 926
983 /** 927 /**
984 * Detaches a key/value-pair from the map by using the key 928 * Removes a key/value-pair from the map by using the key.
985 * without invoking the destructor. 929 *
986 * 930 * This function will copy the contents to the target buffer
987 * @param map the map 931 * which must be guaranteed to be large enough to hold the element.
988 * @param key the key 932 * The destructor functions, if any, will \em not be called.
989 */ 933 *
990 __attribute__((__nonnull__)) 934 * If this map is storing pointers, the element is the pointer itself
991 static inline void cx_map_detach( 935 * and not the object it points to.
992 CxMap *map, 936 *
993 CxHashKey key 937 * @param map the map
994 ) { 938 * @param key the key
995 (void) map->cl->remove(map, key, false); 939 * @param targetbuf the buffer where the element shall be copied to
996 } 940 * @return zero on success, non-zero if the key was not found
997 941 */
998 /** 942 cx_attr_nonnull
999 * Detaches a key/value-pair from the map by using the key 943 cx_attr_access_w(3)
1000 * without invoking the destructor. 944 static inline int cx_map_remove_and_get(
1001 * 945 CxMap *map,
1002 * @param map the map 946 CxHashKey key,
1003 * @param key the key 947 void *targetbuf
1004 */ 948 ) {
1005 __attribute__((__nonnull__)) 949 return map->cl->remove(map, key, targetbuf);
1006 static inline void cx_map_detach_cxstr( 950 }
1007 CxMap *map, 951
1008 cxstring key 952 /**
1009 ) { 953 * Removes a key/value-pair from the map by using the key.
1010 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 954 *
1011 } 955 * This function will copy the contents to the target buffer
1012 956 * which must be guaranteed to be large enough to hold the element.
1013 /** 957 * The destructor functions, if any, will \em not be called.
1014 * Detaches a key/value-pair from the map by using the key 958 *
1015 * without invoking the destructor. 959 * If this map is storing pointers, the element is the pointer itself
1016 * 960 * and not the object it points to.
1017 * @param map the map 961 *
1018 * @param key the key 962 * @param map the map
1019 */ 963 * @param key the key
1020 __attribute__((__nonnull__)) 964 * @param targetbuf the buffer where the element shall be copied to
1021 static inline void cx_map_detach_mustr( 965 * @return zero on success, non-zero if the key was not found
1022 CxMap *map, 966 */
1023 cxmutstr key 967 cx_attr_nonnull
1024 ) { 968 cx_attr_access_w(3)
1025 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); 969 static inline int cx_map_remove_and_get_cxstr(
1026 } 970 CxMap *map,
1027 971 cxstring key,
1028 /** 972 void *targetbuf
1029 * Detaches a key/value-pair from the map by using the key 973 ) {
1030 * without invoking the destructor. 974 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
1031 * 975 }
1032 * @param map the map 976
1033 * @param key the key 977 /**
1034 */ 978 * Removes a key/value-pair from the map by using the key.
1035 __attribute__((__nonnull__)) 979 *
1036 static inline void cx_map_detach_str( 980 * This function will copy the contents to the target buffer
1037 CxMap *map, 981 * which must be guaranteed to be large enough to hold the element.
1038 char const *key 982 * The destructor functions, if any, will \em not be called.
1039 ) { 983 *
1040 (void) map->cl->remove(map, cx_hash_key_str(key), false); 984 * If this map is storing pointers, the element is the pointer itself
1041 } 985 * and not the object it points to.
1042 986 *
1043 /** 987 * @param map the map
1044 * Detaches a key/value-pair from the map by using the key 988 * @param key the key
1045 * without invoking the destructor. 989 * @param targetbuf the buffer where the element shall be copied to
1046 * 990 * @return zero on success, non-zero if the key was not found
1047 * In general, you should only use this function if the map does not own 991 */
1048 * the data and there is a valid reference to the data somewhere else 992 cx_attr_nonnull
1049 * in the program. In all other cases it is preferable to use 993 cx_attr_access_w(3)
1050 * cxMapRemove() or cxMapRemoveAndGet(). 994 static inline int cx_map_remove_and_get_mustr(
1051 * 995 CxMap *map,
1052 * @param map the map 996 cxmutstr key,
1053 * @param key the key 997 void *targetbuf
998 ) {
999 return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
1000 }
1001
1002 /**
1003 * Removes a key/value-pair from the map by using the key.
1004 *
1005 * This function will copy the contents to the target buffer
1006 * which must be guaranteed to be large enough to hold the element.
1007 * The destructor functions, if any, will \em not be called.
1008 *
1009 * If this map is storing pointers, the element is the pointer itself
1010 * and not the object it points to.
1011 *
1012 * @param map the map
1013 * @param key the key
1014 * @param targetbuf the buffer where the element shall be copied to
1015 * @return zero on success, non-zero if the key was not found
1016 */
1017 cx_attr_nonnull
1018 cx_attr_access_w(3)
1019 cx_attr_cstr_arg(2)
1020 static inline int cx_map_remove_and_get_str(
1021 CxMap *map,
1022 const char *key,
1023 void *targetbuf
1024 ) {
1025 return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
1026 }
1027
1028 /**
1029 * Removes a key/value-pair from the map by using the key.
1030 *
1031 * This function will copy the contents to the target buffer
1032 * which must be guaranteed to be large enough to hold the element.
1033 * The destructor functions, if any, will \em not be called.
1034 *
1035 * If this map is storing pointers, the element is the pointer itself
1036 * and not the object it points to.
1037 *
1038 * @param map the map
1039 * @param key the key
1040 * @param targetbuf the buffer where the element shall be copied to
1041 * @return zero on success, non-zero if the key was not found
1042 *
1043 * @see cxMapStorePointers()
1054 * @see cxMapRemove() 1044 * @see cxMapRemove()
1055 * @see cxMapRemoveAndGet() 1045 */
1056 */ 1046 #define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \
1057 #define cxMapDetach(map, key) _Generic((key), \
1058 CxHashKey: cx_map_detach, \
1059 cxstring: cx_map_detach_cxstr, \
1060 cxmutstr: cx_map_detach_mustr, \
1061 char*: cx_map_detach_str, \
1062 char const*: cx_map_detach_str) \
1063 (map, key)
1064
1065 /**
1066 * Removes a key/value-pair from the map by using the key.
1067 *
1068 * @param map the map
1069 * @param key the key
1070 * @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
1072 */
1073 __attribute__((__nonnull__, __warn_unused_result__))
1074 static inline void *cx_map_remove_and_get(
1075 CxMap *map,
1076 CxHashKey key
1077 ) {
1078 return map->cl->remove(map, key, !map->collection.store_pointer);
1079 }
1080
1081 /**
1082 * Removes a key/value-pair from the map by using the key.
1083 *
1084 * @param map the map
1085 * @param key the key
1086 * @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
1088 */
1089 __attribute__((__nonnull__, __warn_unused_result__))
1090 static inline void *cx_map_remove_and_get_cxstr(
1091 CxMap *map,
1092 cxstring key
1093 ) {
1094 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
1095 }
1096
1097 /**
1098 * Removes a key/value-pair from the map by using the key.
1099 *
1100 * @param map the map
1101 * @param key the key
1102 * @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
1104 */
1105 __attribute__((__nonnull__, __warn_unused_result__))
1106 static inline void *cx_map_remove_and_get_mustr(
1107 CxMap *map,
1108 cxmutstr key
1109 ) {
1110 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer);
1111 }
1112
1113 /**
1114 * Removes a key/value-pair from the map by using the key.
1115 *
1116 * @param map the map
1117 * @param key the key
1118 * @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
1120 */
1121 __attribute__((__nonnull__, __warn_unused_result__))
1122 static inline void *cx_map_remove_and_get_str(
1123 CxMap *map,
1124 char const *key
1125 ) {
1126 return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer);
1127 }
1128
1129 /**
1130 * Removes a key/value-pair from the map by using the key.
1131 *
1132 * This function can be used when the map is storing pointers,
1133 * in order to retrieve the pointer from the map without invoking
1134 * any destructor function. Sometimes you do not want the pointer
1135 * to be returned - in that case (instead of suppressing the "unused
1136 * result" warning) you can use cxMapDetach().
1137 *
1138 * If this map is not storing pointers, this function behaves like
1139 * cxMapRemove() and returns \c NULL.
1140 *
1141 * @param map the map
1142 * @param key the key
1143 * @return the stored pointer or \c NULL if either the key is not present
1144 * in the map or the map is not storing pointers
1145 * @see cxMapStorePointers()
1146 * @see cxMapDetach()
1147 */
1148 #define cxMapRemoveAndGet(map, key) _Generic((key), \
1149 CxHashKey: cx_map_remove_and_get, \ 1047 CxHashKey: cx_map_remove_and_get, \
1150 cxstring: cx_map_remove_and_get_cxstr, \ 1048 cxstring: cx_map_remove_and_get_cxstr, \
1151 cxmutstr: cx_map_remove_and_get_mustr, \ 1049 cxmutstr: cx_map_remove_and_get_mustr, \
1152 char*: cx_map_remove_and_get_str, \ 1050 char*: cx_map_remove_and_get_str, \
1153 char const*: cx_map_remove_and_get_str) \ 1051 const char*: cx_map_remove_and_get_str) \
1154 (map, key) 1052 (map, key, targetbuf)
1155 1053
1156 #endif // __cplusplus 1054 #endif // __cplusplus
1157 1055
1158 #endif // UCX_MAP_H 1056 #endif // UCX_MAP_H

mercurial