src/cx/map.h

changeset 1341
dc88d2ece7e4
parent 1180
4c3a69b9723a
equal deleted inserted replaced
1340:31c61b6dcaa5 1341:dc88d2ece7e4
183 */ 183 */
184 void (*clear)(struct cx_map_s *map); 184 void (*clear)(struct cx_map_s *map);
185 185
186 /** 186 /**
187 * Add or overwrite an element. 187 * Add or overwrite an element.
188 */ 188 * If the @p value is @c NULL, the implementation
189 int (*put)( 189 * shall only allocate memory instead of adding an existing value to the map.
190 * Returns a pointer to the allocated memory or @c NULL if allocation fails.
191 */
192 void *(*put)(
190 CxMap *map, 193 CxMap *map,
191 CxHashKey key, 194 CxHashKey key,
192 void *value 195 void *value
193 ); 196 );
194 197
385 static inline int cxMapPut( 388 static inline int cxMapPut(
386 CxMap *map, 389 CxMap *map,
387 CxHashKey const &key, 390 CxHashKey const &key,
388 void *value 391 void *value
389 ) { 392 ) {
390 return map->cl->put(map, key, value); 393 return map->cl->put(map, key, value) == NULL;
391 } 394 }
392 395
393 cx_attr_nonnull 396 cx_attr_nonnull
394 static inline int cxMapPut( 397 static inline int cxMapPut(
395 CxMap *map, 398 CxMap *map,
396 cxstring const &key, 399 cxstring const &key,
397 void *value 400 void *value
398 ) { 401 ) {
399 return map->cl->put(map, cx_hash_key_cxstr(key), value); 402 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
400 } 403 }
401 404
402 cx_attr_nonnull 405 cx_attr_nonnull
403 static inline int cxMapPut( 406 static inline int cxMapPut(
404 CxMap *map, 407 CxMap *map,
405 cxmutstr const &key, 408 cxmutstr const &key,
406 void *value 409 void *value
407 ) { 410 ) {
408 return map->cl->put(map, cx_hash_key_cxstr(key), value); 411 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
409 } 412 }
410 413
411 cx_attr_nonnull 414 cx_attr_nonnull
412 cx_attr_cstr_arg(2) 415 cx_attr_cstr_arg(2)
413 static inline int cxMapPut( 416 static inline int cxMapPut(
414 CxMap *map, 417 CxMap *map,
415 const char *key, 418 const char *key,
416 void *value 419 void *value
417 ) { 420 ) {
418 return map->cl->put(map, cx_hash_key_str(key), value); 421 return map->cl->put(map, cx_hash_key_str(key), value) == NULL;
422 }
423
424 cx_attr_nonnull
425 static inline void *cxMapEmplace(
426 CxMap *map,
427 CxHashKey const &key
428 ) {
429 return map->cl->put(map, key, NULL);
430 }
431
432 cx_attr_nonnull
433 static inline void *cxMapEmplace(
434 CxMap *map,
435 cxstring const &key
436 ) {
437 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
438 }
439
440 cx_attr_nonnull
441 static inline void *cxMapEmplace(
442 CxMap *map,
443 cxmutstr const &key
444 ) {
445 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
446 }
447
448 cx_attr_nonnull
449 cx_attr_cstr_arg(2)
450 static inline void *cxMapEmplace(
451 CxMap *map,
452 const char *key
453 ) {
454 return map->cl->put(map, cx_hash_key_str(key), NULL);
419 } 455 }
420 456
421 cx_attr_nonnull 457 cx_attr_nonnull
422 cx_attr_nodiscard 458 cx_attr_nodiscard
423 static inline void *cxMapGet( 459 static inline void *cxMapGet(
538 static inline int cx_map_put( 574 static inline int cx_map_put(
539 CxMap *map, 575 CxMap *map,
540 CxHashKey key, 576 CxHashKey key,
541 void *value 577 void *value
542 ) { 578 ) {
543 return map->cl->put(map, key, value); 579 return map->cl->put(map, key, value) == NULL;
544 } 580 }
545 581
546 /** 582 /**
547 * @copydoc cxMapPut() 583 * @copydoc cxMapPut()
548 */ 584 */
550 static inline int cx_map_put_cxstr( 586 static inline int cx_map_put_cxstr(
551 CxMap *map, 587 CxMap *map,
552 cxstring key, 588 cxstring key,
553 void *value 589 void *value
554 ) { 590 ) {
555 return map->cl->put(map, cx_hash_key_cxstr(key), value); 591 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
556 } 592 }
557 593
558 /** 594 /**
559 * @copydoc cxMapPut() 595 * @copydoc cxMapPut()
560 */ 596 */
562 static inline int cx_map_put_mustr( 598 static inline int cx_map_put_mustr(
563 CxMap *map, 599 CxMap *map,
564 cxmutstr key, 600 cxmutstr key,
565 void *value 601 void *value
566 ) { 602 ) {
567 return map->cl->put(map, cx_hash_key_cxstr(key), value); 603 return map->cl->put(map, cx_hash_key_cxstr(key), value) == NULL;
568 } 604 }
569 605
570 /** 606 /**
571 * @copydoc cxMapPut() 607 * @copydoc cxMapPut()
572 */ 608 */
575 static inline int cx_map_put_str( 611 static inline int cx_map_put_str(
576 CxMap *map, 612 CxMap *map,
577 const char *key, 613 const char *key,
578 void *value 614 void *value
579 ) { 615 ) {
580 return map->cl->put(map, cx_hash_key_str(key), value); 616 return map->cl->put(map, cx_hash_key_str(key), value) == NULL;
581 } 617 }
582 618
583 /** 619 /**
584 * Puts a key/value-pair into the map. 620 * Puts a key/value-pair into the map.
585 * 621 *
606 char*: cx_map_put_str, \ 642 char*: cx_map_put_str, \
607 const char*: cx_map_put_str) \ 643 const char*: cx_map_put_str) \
608 (map, key, value) 644 (map, key, value)
609 645
610 /** 646 /**
647 * @copydoc cxMapEmplace()
648 */
649 cx_attr_nonnull
650 static inline void *cx_map_emplace(
651 CxMap *map,
652 CxHashKey key
653 ) {
654 return map->cl->put(map, key, NULL);
655 }
656
657 /**
658 * @copydoc cxMapEmplace()
659 */
660 cx_attr_nonnull
661 static inline void *cx_map_emplace_cxstr(
662 CxMap *map,
663 cxstring key
664 ) {
665 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
666 }
667
668 /**
669 * @copydoc cxMapEmplace()
670 */
671 cx_attr_nonnull
672 static inline void *cx_map_emplace_mustr(
673 CxMap *map,
674 cxmutstr key
675 ) {
676 return map->cl->put(map, cx_hash_key_cxstr(key), NULL);
677 }
678
679 /**
680 * @copydoc cxMapEmplace()
681 */
682 cx_attr_nonnull
683 cx_attr_cstr_arg(2)
684 static inline void *cx_map_emplace_str(
685 CxMap *map,
686 const char *key
687 ) {
688 return map->cl->put(map, cx_hash_key_str(key), NULL);
689 }
690
691 /**
692 * Allocates memory for a value in the map associated with the specified key.
693 *
694 * A possible existing value will be overwritten.
695 * If destructor functions are specified, they are called for
696 * the overwritten element.
697 *
698 * If the map is storing pointers, this function returns a @c void** pointer,
699 * meaning a pointer to that pointer.
700 *
701 * The @p key is always copied.
702 *
703 * @param map (@c CxMap*) the map
704 * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
705 * @return the pointer to the allocated memory or @c NULL if allocation fails
706 * @retval zero success
707 * @retval non-zero value on memory allocation failure
708 */
709 #define cxMapEmplace(map, key) _Generic((key), \
710 CxHashKey: cx_map_emplace, \
711 cxstring: cx_map_emplace_cxstr, \
712 cxmutstr: cx_map_emplace_mustr, \
713 char*: cx_map_emplace_str, \
714 const char*: cx_map_emplace_str) \
715 (map, key)
716
717 /**
611 * @copydoc cxMapGet() 718 * @copydoc cxMapGet()
612 */ 719 */
613 cx_attr_nonnull 720 cx_attr_nonnull
614 cx_attr_nodiscard 721 cx_attr_nodiscard
615 static inline void *cx_map_get( 722 static inline void *cx_map_get(

mercurial