improve hashmap

This commit is contained in:
2025-08-02 15:53:36 +02:00
parent 8f874634b1
commit 65ed488d1e
2 changed files with 52 additions and 15 deletions

View File

@@ -48,19 +48,20 @@ static mcl_bucket *mcl_find_bucket(mcl_hashmap *hashmap, void *key, mcl_bucket *
return NULL;
}
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn) {
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn, size_t key_size,
size_t value_size) {
mcl_hashmap *hashmap = malloc(sizeof(mcl_hashmap));
if (hashmap == NULL) {
return NULL;
}
/* Initialize hash map with given parameters */
hashmap->hash_fn = hash_fn;
hashmap->equal_fn = equal_fn;
hashmap->free_key_fn = free_key_fn;
hashmap->free_value_fn = free_value_fn;
hashmap->key_size = key_size;
hashmap->value_size = value_size;
/* Clear all buckets in the map */
memset(hashmap->map, 0, sizeof(hashmap->map));
return hashmap;
@@ -95,11 +96,10 @@ void mcl_hm_free(mcl_hashmap *hashmap) {
}
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
if (hashmap == NULL || key == NULL) {
if (hashmap == NULL || key == NULL || value == NULL) {
return false;
}
/* Try to find existing bucket */
mcl_bucket *prev;
mcl_bucket *existing = mcl_find_bucket(hashmap, key, &prev);
@@ -108,7 +108,12 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
if (hashmap->free_value_fn != NULL && existing->value != NULL) {
hashmap->free_value_fn(existing->value);
}
existing->value = value;
existing->value = malloc(hashmap->value_size);
if (existing->value == NULL) {
return false;
}
memcpy(existing->value, value, hashmap->value_size);
return true;
}
@@ -116,10 +121,22 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
size_t index = mcl_get_bucket_index(hashmap, key);
mcl_bucket *bucket = &hashmap->map[index];
/* If first bucket is empty, use it */
if (bucket->key == NULL) {
bucket->key = key;
bucket->value = value;
/* First bucket is empty, use it */
bucket->key = malloc(hashmap->key_size);
if (bucket->key == NULL) {
return false;
}
bucket->value = malloc(hashmap->value_size);
if (bucket->value == NULL) {
free(bucket->key);
bucket->key = NULL;
return false;
}
memcpy(bucket->key, key, hashmap->key_size);
memcpy(bucket->value, value, hashmap->value_size);
bucket->next = NULL;
return true;
}
@@ -130,8 +147,21 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
return false;
}
new_bucket->key = key;
new_bucket->value = value;
new_bucket->key = malloc(hashmap->key_size);
if (new_bucket->key == NULL) {
free(new_bucket);
return false;
}
new_bucket->value = malloc(hashmap->value_size);
if (new_bucket->value == NULL) {
free(new_bucket->key);
free(new_bucket);
return false;
}
memcpy(new_bucket->key, key, hashmap->key_size);
memcpy(new_bucket->value, value, hashmap->value_size);
new_bucket->next = bucket->next;
bucket->next = new_bucket;

View File

@@ -60,6 +60,8 @@ typedef struct mcl_hashmap_t {
mcl_equal_fn *equal_fn; /**< Equality comparison function */
mcl_free_key_fn *free_key_fn; /**< Key deallocation function (optional) */
mcl_free_value_fn *free_value_fn; /**< Value deallocation function (optional) */
size_t key_size; /**< Size in bytes of the key */
size_t value_size; /**< Size in bytes of the value */
mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */
} mcl_hashmap;
@@ -68,14 +70,18 @@ typedef struct mcl_hashmap_t {
*
* Creates a new hash map and initializes it with the provided function pointers.
* The free functions can be NULL if no automatic memory management is needed.
* Keys and values will be copied into the hashmap using memcpy with the specified sizes.
*
* @param[in] hash_fn Function used to hash keys (required)
* @param[in] equal_fn Function used to compare keys (required)
* @param[in] free_key_fn Function used to free keys (optional, can be NULL)
* @param[in] free_value_fn Function used to free values (optional, can be NULL)
* @param[in] key_size Size in bytes of each key to be stored
* @param[in] value_size Size in bytes of each value to be stored
* @return A pointer to the newly initialized hash map, or NULL on failure
*/
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn);
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn, size_t key_size,
size_t value_size);
/**
* @brief Free all resources used by the hash map
@@ -92,11 +98,12 @@ void mcl_hm_free(mcl_hashmap *hashmap);
*
* If the key already exists, the old value is freed (if free_value_fn is provided)
* and replaced with the new value. If the key doesn't exist, a new entry is created.
* Both key and value are copied into the hashmap using memcpy.
*
* @param[in] hashmap Pointer to the hash map
* @param[in] key Pointer to the key to insert (must not be NULL)
* @param[in] value Pointer to the value to insert (can be NULL)
* @return true if the operation succeeded, false on failure (NULL hashmap/key or memory allocation failure)
* @param[in] key Pointer to the key to insert (will be copied, must not be NULL)
* @param[in] value Pointer to the value to insert (will be copied, must not be NULL)
* @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure)
*/
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value);