47 lines
1.3 KiB
C
47 lines
1.3 KiB
C
#ifndef HASH_MAP_H
|
|
#define HASH_MAP_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
|
|
struct pair_list
|
|
{
|
|
const char *key;
|
|
void *value;
|
|
struct pair_list *next;
|
|
};
|
|
|
|
struct hash_map
|
|
{
|
|
struct pair_list **data;
|
|
size_t size;
|
|
};
|
|
|
|
struct hash_map *hash_map_init(size_t size);
|
|
|
|
/**
|
|
* @brief Inserts a key-value pair into the hash map. Key and value are expected
|
|
* to be on the heap and will be managed by the hash map. It means they are
|
|
* consumed by this function. If the key already exists, its value is updated
|
|
* and the key is not consumed so it must be freed by the caller.
|
|
*
|
|
* @param hash_map The hash map.
|
|
* @param key The key to insert.
|
|
* @param value The value to insert.
|
|
* @param updated If not NULL, set to true if the key was already present and
|
|
* updated, false if the key was newly inserted.
|
|
* @return true on success, false on failure.
|
|
*/
|
|
bool hash_map_insert(struct hash_map *hash_map, const char *key, void *value,
|
|
bool *updated);
|
|
|
|
void hash_map_free(struct hash_map **hash_map);
|
|
|
|
void hash_map_foreach(struct hash_map *hash_map,
|
|
void (*fn)(const char *, const void *));
|
|
|
|
const void *hash_map_get(const struct hash_map *hash_map, const char *key);
|
|
|
|
bool hash_map_remove(struct hash_map *hash_map, const char *key);
|
|
|
|
#endif /* ! HASH_MAP_H */
|