osurs
0.0.1
|
Hashmap data structure. More...
#include <stddef.h>
Go to the source code of this file.
Data Structures | |
struct | HashMapEntry |
Entry of the hashmap. More... | |
struct | HashMap |
A hashmap. More... | |
Typedefs | |
typedef struct HashMapEntry | HashMapEntry |
Entry of the hashmap. More... | |
typedef struct HashMap | HashMap |
A hashmap. More... | |
Functions | |
void | hash_map_init (HashMap *map) |
Initialize the hashmap. More... | |
HashMap * | hash_map_create () |
Create a hashmap on the heap. More... | |
void | hash_map_put (HashMap *map, const char *key, void *value) |
Put a new entry into the hashmap. More... | |
void * | hash_map_get (HashMap *map, const char *key) |
Get a value from a key. More... | |
void * | hash_map_get_random (HashMap *map) |
Get a random value. More... | |
void | hash_map_remove (HashMap *map, const char *key) |
Remove an entry from the hashmap. More... | |
void | hash_map_print (HashMap *map) |
Print the hashmap content. More... | |
void | hash_map_clear (HashMap *map) |
Clear all values in the hashmap. More... | |
void | hash_map_free (HashMap *map) |
Free hashmap and entries. More... | |
Hashmap data structure.
Map abstract data type (ADT) implementation as a hashmap. The implementation uses closed-addressing (separate chaining) to handle duplicate index values. A bucket of the hashmap contains an entry, which can hold a reference to the next entry in the same bucket.
Note: This is not a multimap, entries on existing keys are replaced. This implementation is not thread-safe.
typedef struct HashMapEntry HashMapEntry |
Entry of the hashmap.
An entry in the hashmap, which knows the next entry (if there are any) in the same bucket (same index).
void hash_map_clear | ( | HashMap * | map | ) |
Clear all values in the hashmap.
Frees all entries in the hashmap. It is important to note, that the values itself have to be freed manually if needed, by iterating over all entries before clearing.
map | A hashmap struct. |
HashMap* hash_map_create | ( | ) |
Create a hashmap on the heap.
void hash_map_free | ( | HashMap * | map | ) |
Free hashmap and entries.
It is important to note, that the values itself have to be freed manually if needed, by iterating over all entries before clearing and freeing the hashmap.
map | A hashmap struct. |
void* hash_map_get | ( | HashMap * | map, |
const char * | key | ||
) |
Get a value from a key.
Returns a void pointer to the value or NULL if the key is not existing in the hashmap.
map | A hashmap. |
key | The key. |
void* hash_map_get_random | ( | HashMap * | map | ) |
Get a random value.
Returns a random void pointer to a value or NULL if the hashmap is empty.
map | A hashmap. |
void hash_map_init | ( | HashMap * | map | ) |
Initialize the hashmap.
map | A hashmap. |
void hash_map_print | ( | HashMap * | map | ) |
Print the hashmap content.
Prints the key, its index (hash value) and the address (void pointer) of the value.
map | A hashmap struct. |
void hash_map_put | ( | HashMap * | map, |
const char * | key, | ||
void * | value | ||
) |
Put a new entry into the hashmap.
Puts the entry into the bucket at the index (hash value) of the key. If the bucket is not empty, new entry is created and placed to the beginning of the chain. Then the old entry in the array is replaced with the new entry. If a key already exists, the value is replaced.
map | A hashmap. |
key | The key. |
value | A void pointer to the value. |
void hash_map_remove | ( | HashMap * | map, |
const char * | key | ||
) |
Remove an entry from the hashmap.
map | A hashmap structure. |
key | The key. |