--- a/ucx/ucx/map.h Mon Feb 04 14:11:57 2019 +0100 +++ b/ucx/ucx/map.h Mon Feb 04 17:17:48 2019 +0100 @@ -89,26 +89,36 @@ size_t count; }; -/** Structure for a key of a UcxMap. */ +/** Structure to publicly denote a key of a UcxMap. */ struct UcxKey { /** The key data. */ - void *data; + const void *data; /** The length of the key data. */ - size_t len; + size_t len; + /** A cache for the hash value of the key data. */ + int hash; +}; + +/** Internal structure for a key of a UcxMap. */ +struct UcxMapKey { + /** The key data. */ + void *data; + /** The length of the key data. */ + size_t len; /** The hash value of the key data. */ - int hash; + int hash; }; /** Structure for an element of a UcxMap. */ struct UcxMapElement { /** The value data. */ - void *data; + void *data; /** A pointer to the next element in the current list. */ - UcxMapElement *next; + UcxMapElement *next; /** The corresponding key. */ - UcxKey key; + struct UcxMapKey key; }; /** Structure for an iterator over a UcxMap. */ @@ -158,7 +168,10 @@ * Frees the contents of a hash map. * * This is a convenience function that iterates over the map and passes all - * values to the specified destructor function (e.g. stdlib free()). + * values to the specified destructor function. + * + * If no destructor is specified (<code>NULL</code>), the free() function of + * the map's own allocator is used. * * You must ensure, that it is valid to pass each value in the map to the same * destructor function. @@ -166,7 +179,7 @@ * You should free or clear the map afterwards, as the contents will be invalid. * * @param map for which the contents shall be freed - * @param destr pointer to the destructor function + * @param destr optional pointer to a destructor function * @see ucx_map_free() * @see ucx_map_clear() */ @@ -282,7 +295,7 @@ * @see ucx_map_put() */ #define ucx_map_cstr_put(map, key, value) \ - ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value) + ucx_map_put(map, ucx_key(key, strlen(key)), (void*)value) /** * Shorthand for putting data with an integer key into the map. @@ -293,7 +306,7 @@ * @see ucx_map_put() */ #define ucx_map_int_put(map, key, value) \ - ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value) + ucx_map_put(map, ucx_key(&key, sizeof(key)), (void*)value) /** * Shorthand for getting data from the map with a sstr_t key. @@ -313,7 +326,7 @@ * @see ucx_map_get() */ #define ucx_map_cstr_get(map, key) \ - ucx_map_get(map, ucx_key((void*)key, strlen(key))) + ucx_map_get(map, ucx_key(key, strlen(key))) /** * Shorthand for getting data from the map with an integer key. @@ -323,7 +336,7 @@ * @see ucx_map_get() */ #define ucx_map_int_get(map, key) \ - ucx_map_get(map, ucx_key((void*)&key, sizeof(int))) + ucx_map_get(map, ucx_key(&key, sizeof(int))) /** * Shorthand for removing data from the map with a sstr_t key. @@ -343,7 +356,7 @@ * @see ucx_map_remove() */ #define ucx_map_cstr_remove(map, key) \ - ucx_map_remove(map, ucx_key((void*)key, strlen(key))) + ucx_map_remove(map, ucx_key(key, strlen(key))) /** * Shorthand for removing data from the map with an integer key. @@ -353,7 +366,7 @@ * @see ucx_map_remove() */ #define ucx_map_int_remove(map, key) \ - ucx_map_remove(map, ucx_key((void*)&key, sizeof(key))) + ucx_map_remove(map, ucx_key(&key, sizeof(key))) /** * Creates a UcxKey based on the given data. @@ -365,7 +378,7 @@ * @return a UcxKey with implicitly computed hash * @see ucx_hash() */ -UcxKey ucx_key(void *data, size_t len); +UcxKey ucx_key(const void *data, size_t len); /** * Computes a murmur hash-2.