diff -r 38cb8e3992e8 -r ce13a778654a ucx/cx/map.h --- a/ucx/cx/map.h Thu Oct 03 18:54:19 2024 +0200 +++ b/ucx/cx/map.h Sun Oct 06 12:00:31 2024 +0200 @@ -56,7 +56,10 @@ /** Structure for the UCX map. */ struct cx_map_s { - CX_COLLECTION_MEMBERS + /** + * Base attributes. + */ + CX_COLLECTION_BASE; /** The map class definition. */ cx_map_class *cl; }; @@ -110,7 +113,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) void *(*get)( - CxMap const *map, + const CxMap *map, CxHashKey key ); @@ -128,7 +131,7 @@ * Creates an iterator for this map. */ __attribute__((__nonnull__, __warn_unused_result__)) - CxIterator (*iterator)(CxMap const *map, enum cx_map_iterator_type type); + CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); }; /** @@ -138,7 +141,7 @@ /** * A pointer to the key. */ - CxHashKey const *key; + const CxHashKey *key; /** * A pointer to the value. */ @@ -163,7 +166,7 @@ */ __attribute__((__nonnull__)) static inline void cxMapStoreObjects(CxMap *map) { - map->store_pointer = false; + map->collection.store_pointer = false; } /** @@ -180,10 +183,21 @@ */ __attribute__((__nonnull__)) static inline void cxMapStorePointers(CxMap *map) { - map->store_pointer = true; - map->item_size = sizeof(void *); + map->collection.store_pointer = true; + map->collection.elem_size = sizeof(void *); } +/** + * Returns true, if this map is storing pointers instead of the actual data. + * + * @param map + * @return true, if this map is storing pointers + * @see cxMapStorePointers() + */ +__attribute__((__nonnull__)) +static inline bool cxMapIsStoringPointers(const CxMap *map) { + return map->collection.store_pointer; +} /** * Deallocates the memory of the specified map. @@ -206,6 +220,17 @@ map->cl->clear(map); } +/** + * Returns the number of elements in this map. + * + * @param map the map + * @return the number of stored elements + */ +__attribute__((__nonnull__)) +static inline size_t cxMapSize(const CxMap *map) { + return map->collection.size; +} + // TODO: set-like map operations (union, intersect, difference) @@ -219,7 +244,7 @@ * @return an iterator for the currently stored values */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxIterator cxMapIteratorValues(CxMap const *map) { +static inline CxIterator cxMapIteratorValues(const CxMap *map) { return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); } @@ -235,7 +260,7 @@ * @return an iterator for the currently stored keys */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxIterator cxMapIteratorKeys(CxMap const *map) { +static inline CxIterator cxMapIteratorKeys(const CxMap *map) { return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); } @@ -253,7 +278,7 @@ * @see cxMapIteratorValues() */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxIterator cxMapIterator(CxMap const *map) { +static inline CxIterator cxMapIterator(const CxMap *map) { return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); } @@ -268,7 +293,7 @@ * @return an iterator for the currently stored values */ __attribute__((__nonnull__, __warn_unused_result__)) -CxMutIterator cxMapMutIteratorValues(CxMap *map); +CxIterator cxMapMutIteratorValues(CxMap *map); /** * Creates a mutating iterator over the keys of a map. @@ -282,7 +307,7 @@ * @return an iterator for the currently stored keys */ __attribute__((__nonnull__, __warn_unused_result__)) -CxMutIterator cxMapMutIteratorKeys(CxMap *map); +CxIterator cxMapMutIteratorKeys(CxMap *map); /** * Creates a mutating iterator for a map. @@ -298,7 +323,7 @@ * @see cxMapMutIteratorValues() */ __attribute__((__nonnull__, __warn_unused_result__)) -CxMutIterator cxMapMutIterator(CxMap *map); +CxIterator cxMapMutIterator(CxMap *map); #ifdef __cplusplus } // end the extern "C" block here, because we want to start overloading @@ -366,7 +391,7 @@ __attribute__((__nonnull__)) static inline int cxMapPut( CxMap *map, - char const *key, + const char *key, void *value ) { return map->cl->put(map, cx_hash_key_str(key), value); @@ -381,7 +406,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapGet( - CxMap const *map, + const CxMap *map, CxHashKey const &key ) { return map->cl->get(map, key); @@ -396,7 +421,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapGet( - CxMap const *map, + const CxMap *map, cxstring const &key ) { return map->cl->get(map, cx_hash_key_cxstr(key)); @@ -411,7 +436,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapGet( - CxMap const *map, + const CxMap *map, cxmutstr const &key ) { return map->cl->get(map, cx_hash_key_cxstr(key)); @@ -426,8 +451,8 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapGet( - CxMap const *map, - char const *key + const CxMap *map, + const char *key ) { return map->cl->get(map, cx_hash_key_str(key)); } @@ -515,7 +540,7 @@ __attribute__((__nonnull__)) static inline void cxMapRemove( CxMap *map, - char const *key + const char *key ) { (void) map->cl->remove(map, cx_hash_key_str(key), true); } @@ -603,7 +628,7 @@ __attribute__((__nonnull__)) static inline void cxMapDetach( CxMap *map, - char const *key + const char *key ) { (void) map->cl->remove(map, cx_hash_key_str(key), false); } @@ -711,7 +736,7 @@ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cxMapRemoveAndGet( CxMap *map, - char const *key + const char *key ) { return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); } @@ -780,7 +805,7 @@ __attribute__((__nonnull__)) static inline int cx_map_put_str( CxMap *map, - char const *key, + const char *key, void *value ) { return map->cl->put(map, cx_hash_key_str(key), value); @@ -799,7 +824,7 @@ cxstring: cx_map_put_cxstr, \ cxmutstr: cx_map_put_mustr, \ char*: cx_map_put_str, \ - char const*: cx_map_put_str) \ + const char*: cx_map_put_str) \ (map, key, value) /** @@ -811,7 +836,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_get( - CxMap const *map, + const CxMap *map, CxHashKey key ) { return map->cl->get(map, key); @@ -826,7 +851,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_get_cxstr( - CxMap const *map, + const CxMap *map, cxstring key ) { return map->cl->get(map, cx_hash_key_cxstr(key)); @@ -841,7 +866,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_get_mustr( - CxMap const *map, + const CxMap *map, cxmutstr key ) { return map->cl->get(map, cx_hash_key_cxstr(key)); @@ -856,8 +881,8 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_get_str( - CxMap const *map, - char const *key + const CxMap *map, + const char *key ) { return map->cl->get(map, cx_hash_key_str(key)); } @@ -874,7 +899,7 @@ cxstring: cx_map_get_cxstr, \ cxmutstr: cx_map_get_mustr, \ char*: cx_map_get_str, \ - char const*: cx_map_get_str) \ + const char*: cx_map_get_str) \ (map, key) /** @@ -928,7 +953,7 @@ __attribute__((__nonnull__)) static inline void cx_map_remove_str( CxMap *map, - char const *key + const char *key ) { (void) map->cl->remove(map, cx_hash_key_str(key), true); } @@ -952,7 +977,7 @@ cxstring: cx_map_remove_cxstr, \ cxmutstr: cx_map_remove_mustr, \ char*: cx_map_remove_str, \ - char const*: cx_map_remove_str) \ + const char*: cx_map_remove_str) \ (map, key) /** @@ -1010,7 +1035,7 @@ __attribute__((__nonnull__)) static inline void cx_map_detach_str( CxMap *map, - char const *key + const char *key ) { (void) map->cl->remove(map, cx_hash_key_str(key), false); } @@ -1034,7 +1059,7 @@ cxstring: cx_map_detach_cxstr, \ cxmutstr: cx_map_detach_mustr, \ char*: cx_map_detach_str, \ - char const*: cx_map_detach_str) \ + const char*: cx_map_detach_str) \ (map, key) /** @@ -1050,7 +1075,7 @@ CxMap *map, CxHashKey key ) { - return map->cl->remove(map, key, !map->store_pointer); + return map->cl->remove(map, key, !map->collection.store_pointer); } /** @@ -1066,7 +1091,7 @@ CxMap *map, cxstring key ) { - return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); + return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); } /** @@ -1082,7 +1107,7 @@ CxMap *map, cxmutstr key ) { - return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); + return map->cl->remove(map, cx_hash_key_cxstr(key), !map->collection.store_pointer); } /** @@ -1096,9 +1121,9 @@ __attribute__((__nonnull__, __warn_unused_result__)) static inline void *cx_map_remove_and_get_str( CxMap *map, - char const *key + const char *key ) { - return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); + return map->cl->remove(map, cx_hash_key_str(key), !map->collection.store_pointer); } /** @@ -1125,7 +1150,7 @@ cxstring: cx_map_remove_and_get_cxstr, \ cxmutstr: cx_map_remove_and_get_mustr, \ char*: cx_map_remove_and_get_str, \ - char const*: cx_map_remove_and_get_str) \ + const char*: cx_map_remove_and_get_str) \ (map, key) #endif // __cplusplus