EII Message Bus C Reference
Classes | Macros | Enumerations | Functions
hashmap.h File Reference

C hashmap implementation. More...

#include <stdlib.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  hashmap_elem_t
 
struct  hashmap_t
 

Macros

#define HASHMAP_LOOP(MAP, VALUE_TYPE, CODE)
 

Enumerations

enum  hashmap_ret_t {
  MAP_SUCCESS = 0, MAP_FULL = -1, MAP_OMEM = -2, MAP_KEY_EXISTS = -3,
  MAP_KEY_NOT_EXISTS = -4
}
 

Functions

hashmap_thashmap_new (size_t init_size)
 
hashmap_ret_t hashmap_put (hashmap_t *map, const char *key, void *val, void(*free_fn)(void *))
 
void * hashmap_get (hashmap_t *map, const char *key)
 
hashmap_ret_t hashmap_remove (hashmap_t *map, const char *key)
 
void hashmap_destroy (hashmap_t *map)
 

Detailed Description

C hashmap implementation.

Macro Definition Documentation

◆ 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

Enumeration Type Documentation

◆ hashmap_ret_t

Hashmap return values

Function Documentation

◆ hashmap_destroy()

void hashmap_destroy ( hashmap_t map)

Destroy the given hashmap.

Parameters
map- Hashmap to destroy

◆ 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()

hashmap_t* hashmap_new ( size_t  init_size)

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()

hashmap_ret_t hashmap_put ( hashmap_t map,
const char *  key,
void *  val,
void(*)(void *)  free_fn 
)

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()

hashmap_ret_t hashmap_remove ( hashmap_t map,
const char *  key 
)

Remove item from the hashmap.

Parameters
map- Hashmap to remove value from
key- Key for the value
Returns
hashmap_ret_t