Hashmap data structure.
More...
#include "osurs/ds/hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Hashmap data structure.
- Date
- : 2022-12-19
- Author
- : Merlin Unterfinger
◆ INIT_CAPACITY
The initial hashmap capacity.
◆ LOAD_FACTOR
The load factor to trigger resizing of the hashmap capacity.
◆ hash_map_clear()
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.
- Parameters
-
◆ hash_map_create()
Create a hashmap on the heap.
- Returns
- HashMap*
◆ hash_map_free()
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.
- Parameters
-
◆ hash_map_get()
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.
- Parameters
-
map | A hashmap. |
key | The key. |
- Returns
- void* The pointer pointing to the value.
◆ hash_map_get_random()
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.
- Parameters
-
- Returns
- void*
◆ hash_map_init()
void hash_map_init |
( |
HashMap * |
map | ) |
|
Initialize the hashmap.
- Parameters
-
◆ hash_map_print()
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.
- Parameters
-
◆ hash_map_put()
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.
- Parameters
-
map | A hashmap. |
key | The key. |
value | A void pointer to the value. |
◆ hash_map_remove()
void hash_map_remove |
( |
HashMap * |
map, |
|
|
const char * |
key |
|
) |
| |
Remove an entry from the hashmap.
- Parameters
-
map | A hashmap structure. |
key | The key. |