A simple hash_map implementation.
More...
#include <stddef.h>
#include "utils/sstr.h"
Go to the source code of this file.
|
#define | HASH_MAP_OK 0 |
| return code of hash_map functions.
|
|
#define | HASH_MAP_DUPLICATE_KEY 1 |
|
#define | HASH_MAP_ERROR -1 |
|
|
struct hash_map_entry * | hash_map_entry_new (void *key, void *value) |
| create a new hash_map_entry with key and value. More...
|
|
void | hash_map_entry_free (struct hash_map *map, struct hash_map_entry *entry) |
| free a hash_map_entry. the key and value will be freed by key_free_func and value_free_func of map. More...
|
|
struct hash_map * | hash_map_new (int bucket_count, unsigned int(*hash_func)(void *), int(*key_cmp_func)(void *, void *), void(*key_free_func)(void *), void(*value_free_func)(void *)) |
| create a new hash_map. More...
|
|
void | hash_map_free (struct hash_map *map) |
| free a hash_map. More...
|
|
int | hash_map_insert (struct hash_map *map, void *key, void *value) |
| insert a new entry into hash_map. More...
|
|
int | hash_map_find (struct hash_map *map, void *key, void **value) |
| find a entry in hash_map. More...
|
|
int | hash_map_delete (struct hash_map *map, void *key) |
| remove a entry from hash_map. More...
|
|
void | hash_map_for_each (struct hash_map *map, void(*fn)(void *key, void *value, void *ptr), void *ptr) |
| for each key-value pair in hash_map, call fn(key, value). More...
|
|
unsigned int | hash (const char *data, size_t n) |
| calculate the hash value of a key. More...
|
|
unsigned int | sstr_key_hash (void *key) |
|
int | sstr_key_cmp (void *a, void *b) |
|
void | sstr_key_free (void *key) |
|
A simple hash_map implementation.
◆ hash()
unsigned int hash |
( |
const char * |
data, |
|
|
size_t |
n |
|
) |
| |
calculate the hash value of a key.
- Parameters
-
data | pointer to key buffer. |
n | the length of key buffer. |
- Returns
- unsigned int the hash value.
◆ hash_map_delete()
int hash_map_delete |
( |
struct hash_map * |
map, |
|
|
void * |
key |
|
) |
| |
remove a entry from hash_map.
- Parameters
-
map | the hash_map. |
key | the key of the entry. |
- Returns
- int HASH_MAP_OK if found, HASH_MAP_ERROR if not found.
- Note
- the entry will be free.
◆ hash_map_entry_free()
free a hash_map_entry. the key and value will be freed by key_free_func and value_free_func of map.
- Parameters
-
map | the hash_map, container map->key_free_func and map->value_free_func. |
entry | the entry to be freed. |
◆ hash_map_entry_new()
struct hash_map_entry* hash_map_entry_new |
( |
void * |
key, |
|
|
void * |
value |
|
) |
| |
create a new hash_map_entry with key and value.
- Parameters
-
key | the key of the entry. |
value | the value of the entry. |
- Returns
- struct hash_map_entry* the new entry.
- Return values
-
NULL | malloc failed. |
!NULL | the new entry. |
◆ hash_map_find()
int hash_map_find |
( |
struct hash_map * |
map, |
|
|
void * |
key, |
|
|
void ** |
value |
|
) |
| |
find a entry in hash_map.
- Parameters
-
map | the hash_map. |
key | the key of the entry. |
value | the value of the entry if found. |
- Returns
- int HASH_MAP_OK if found, HASH_MAP_ERROR if not found.
◆ hash_map_for_each()
void hash_map_for_each |
( |
struct hash_map * |
map, |
|
|
void(*)(void *key, void *value, void *ptr) |
fn, |
|
|
void * |
ptr |
|
) |
| |
for each key-value pair in hash_map, call fn(key, value).
- Parameters
-
map | the hash_map. |
fn | the function to be called. |
ptr | user data. |
◆ hash_map_free()
void hash_map_free |
( |
struct hash_map * |
map | ) |
|
◆ hash_map_insert()
int hash_map_insert |
( |
struct hash_map * |
map, |
|
|
void * |
key, |
|
|
void * |
value |
|
) |
| |
insert a new entry into hash_map.
- Parameters
-
map | the hash_map. |
key | the key of the entry. |
value | the value of the entry. |
- Returns
- int HASH_MAP_OK if success, HASH_MAP_DUPLICATE_KEY will ignore the entry, HASH_MAP_ERROR if malloc failed.
◆ hash_map_new()
struct hash_map* hash_map_new |
( |
int |
bucket_count, |
|
|
unsigned int(*)(void *) |
hash_func, |
|
|
int(*)(void *, void *) |
key_cmp_func, |
|
|
void(*)(void *) |
key_free_func, |
|
|
void(*)(void *) |
value_free_func |
|
) |
| |
create a new hash_map.
- Parameters
-
bucket_count | the number of buckets. |
hash_func | the hash function. |
key_cmp_func | the key compare function, return 0 if equal, else not 0. |
key_free_func | the key free function. |
value_free_func | the value free function. |
- Returns
- struct hash_map* the new hash_map.