This documentation is under development!

map

map#

A type-safe hash map implementation for C.

See rxi/map for the official documentation.

Prototypes for provided macros

typedef map_t(T) MAP_T;
Creates a map struct for containing values of type T.
void map_init(MAP_T *m);
Initialises the map, this must be called before the map can be used.
void map_deinit(MAP_T *m);
Deinitialises the map, freeing the memory the map allocated during use; this should be called when we’re finished with a map.
void *map_get(MAP_T *m, const char *key);
Returns a pointer to the value of the given key. If no mapping for the key exists then NULL will be returned.
int map_set(MAP_T *m, const char *key, T value);
Sets the given key to the given value. Returns 0 on success, otherwise -1 is returned and the map remains unchanged.
void map_remove(MAP_T *m, const char *key);
Removes the mapping of the given key from the map. If the key does not exist in the map then the function has no effect. Note: this function should not be called while iterating over the map.
map_iter_t map_iter(MAP_T *m);
Returns a map_iter_t which can be used with map_next() to iterate all the keys in the map.
const char *map_next(MAP_T *m, map_iter_t *iter);
Uses the map_iter_t returned by map_iter() to iterate all the keys in the map. map_next() returns a key with each call and returns NULL when there are no more keys.

Predefined map types

typedef map_t(void*) map_void_t;
typedef map_t(char*) map_str_t;
typedef map_t(int) map_int_t;
typedef map_t(char) map_char_t;
typedef map_t(float) map_float_t;
typedef map_t(double) map_double_t;
Example

typedef map_t(unsigned int) map_uint_t;

map_uint_t m;
unsigned int *p;
const char *key;
map_iter_t iter;

map_init(&m);
map_set(&m, "testkey", 123);
p = map_get(&m, "testkey");

iter = map_iter(&m);
while ((key = map_next(&m, &iter))) {
  printf("%s -> %u\n", key, *map_get(&m, key));
}

map_deinit(&m);

Internal functions

These functions are called via the macros. Don’t call them directly.

void map_deinit_(map_base_t *m)#
void *map_get_(const map_base_t *m, const char *key)#
int map_set_(map_base_t *m, const char *key, void *value, int vsize)#
void map_remove_(map_base_t *m, const char *key)#
map_iter_t map_iter_(void)#
const char *map_next_(map_base_t *m, map_iter_t *iter)#

Defines

MAP_VERSION#
map_t(T)#

Defines a new map type.

map_init(m)#

Initialises the map, this must be called before the map can be used.

map_deinit(m)#

Deinitialises the map, freeing the memory the map allocated during use; this should be called when we’re finished with a map.

map_get(m, key)#

Returns a pointer to the value of the given key. If no mapping for the key exists then NULL will be returned.

map_set(m, key, value)#

Sets the given key to the given value. Returns 0 on success, otherwise -1 is returned and the map remains unchanged.

map_remove(m, key)#

Removes the mapping of the given key from the map. If the key does not exist in the map then the function has no effect.

map_iter(m)#

Returns a map_iter_t which can be used with map_next() to iterate all the keys in the map.

map_next(m, iter)#

Uses the map_iter_t returned by map_iter() to iterate all the keys in the map. map_next() returns a key with each call and returns NULL when there are no more keys.

Typedefs

typedef struct map_node_t map_node_t#
struct map_base_t#

Public Members

map_node_t **buckets#
unsigned nbuckets#
unsigned nnodes#
struct map_iter_t#

Public Members

unsigned bucketidx#
map_node_t *node#