Map

Crit-bit tree based map which supports unique prefix queries and ordered iteration.

Functions

Map *map_new(void)

Allocate a new map.

void *map_get(const Map*, const char *key)

Lookup a value, returns NULL if not found.

void *map_first(const Map*, const char **key)

Get first element of the map, or NULL if empty.

Parameters
  • key: Updated with the key of the first element.

void *map_closest(const Map*, const char *prefix)

Lookup element by unique prefix match.

Return

The corresponding value, if the given prefix is unique. Otherwise NULL. If no such prefix exists, then errno is set to ENOENT.

Parameters
  • prefix: The prefix to search for.

bool map_contains(const Map*, const char *prefix)

Check whether the map contains the given prefix.

whether it can be extended to match a key of a map element.

bool map_leaf(const Map*, const char *prefix)

Check whether the given prefix can be extended to exactly one map element.

True iff the prefix map contains exactly one element.

bool map_put(Map*, const char *key, const void *value)

Store a key value pair in the map.

Return

False if we run out of memory (errno = ENOMEM), or if the key already appears in the map (errno = EEXIST).

void *map_delete(Map*, const char *key)

Remove a map element.

Return

The removed entry or NULL if no such element exists.

bool map_copy(Map *dest, Map *src)

Copy all entries from src into dest, overwrites existing entries in dest.

void map_iterate(const Map*, bool (*handle)(const char *key, void *value, void *data), const void *data, )

Ordered iteration over a map.

Invokes the passed callback for every map entry. If handle returns false, the iteration will stop.

Parameters
  • handle: A function invoked for ever map element.

  • data: A context pointer, passed as last argument to handle.

const Map *map_prefix(const Map*, const char *prefix)

Get a sub map matching a prefix.

Warning

This returns a pointer into the original map. Do not alter the map while using the return value.

bool map_empty(const Map*)

Test whether the map is empty (contains no elements).

void map_clear(Map*)

Empty the map.

void map_free(Map*)

Release all memory associated with this map.

void map_free_full(Map*)

Call free(3) for every map element, then free the map itself.

Warning

Assumes map elements to be pointers.