C hashmap implementation.
More...
#include <stdlib.h>
#include <stdbool.h>
Go to the source code of this file.
|
enum | hashmap_ret_t {
MAP_SUCCESS = 0,
MAP_FULL = -1,
MAP_OMEM = -2,
MAP_KEY_EXISTS = -3,
MAP_KEY_NOT_EXISTS = -4
} |
|
C hashmap implementation.
◆ HASHMAP_LOOP
#define HASHMAP_LOOP |
( |
|
MAP, |
|
|
|
VALUE_TYPE, |
|
|
|
CODE |
|
) |
| |
Value: for(int i = 0; i < MAP->max_size; i++) { \
if(MAP->elems[i].in_use) { \
VALUE_TYPE* value = MAP->elems[i].value; \
const char* key = MAP->elems[i].key; \
CODE \
} \
}
Helper macro for looping over all of the elements in the hashmap.
For the given code block (i.e. the CODE parameter), there is a value and key variable made available with the const char* for the key and the value casted to the given VALUE_TYPE.
- Note
- This is not necessarily super efficient, because it must loop over all of the allocated/in-use elements. So it will be O(N) where N is the current max_size of the hashmap.
- Parameters
-
MAP | - The hashmap to loop over |
VALUE_TYPE | - Type to cast the void* value to to make available |
CODE | - Code block to be executed on each (key, value) pair |
◆ hashmap_ret_t
◆ hashmap_destroy()
Destroy the given hashmap.
- Parameters
-
◆ hashmap_get()
void* hashmap_get |
( |
hashmap_t * |
map, |
|
|
const char * |
key |
|
) |
| |
Get the value associated with the given key if it exists.
- Parameters
-
map | - Hashmap to retrieve a value from. |
key | - Key for the value |
- Returns
- void*, or NULL if the key does not exist
◆ hashmap_new()
Create a new hashmap.
- Note
- init_size will be the initial size of the hashmap. When it grows past that it will allocated init_size amount of additional memory.
- Parameters
-
init_size | - Initial size of the hashmap |
- Returns
hashmap_t*
◆ hashmap_put()
Put a (key, value) pair into the hashmap.
- Parameters
-
map | - Hashmap to put the pair into |
key | - Key for the value |
val | - Value associated with the key |
free_fn | - Free function for the value |
- Returns
hashmap_ret_t
◆ hashmap_remove()
Remove item from the hashmap.
- Parameters
-
map | - Hashmap to remove value from |
key | - Key for the value |
- Returns
hashmap_ret_t