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