8 * Crit-bit tree based map which supports unique prefix queries and
12 /** Opaque map type. */
13 typedef struct Map Map
;
15 /** Allocate a new map. */
17 /** Lookup a value, returns ``NULL`` if not found. */
18 void *map_get(const Map
*, const char *key
);
20 * Get first element of the map, or ``NULL`` if empty.
21 * @param key Updated with the key of the first element.
23 void *map_first(const Map
*, const char **key
);
25 * Lookup element by unique prefix match.
26 * @param prefix The prefix to search for.
27 * @return The corresponding value, if the given prefix is unique.
28 * Otherwise ``NULL``. If no such prefix exists, then ``errno``
29 * is set to ``ENOENT``.
31 void *map_closest(const Map
*, const char *prefix
);
33 * Check whether the map contains the given prefix.
34 * whether it can be extended to match a key of a map element.
36 bool map_contains(const Map
*, const char *prefix
);
38 * Check whether the given prefix can be extended to exactly one map element.
39 * True iff the prefix map contains exactly one element.
41 bool map_leaf(const Map
*, const char *prefix
);
43 * Store a key value pair in the map.
44 * @return False if we run out of memory (``errno = ENOMEM``), or if the key
45 * already appears in the map (``errno = EEXIST``).
47 bool map_put(Map
*, const char *key
, const void *value
);
49 * Remove a map element.
50 * @return The removed entry or ``NULL`` if no such element exists.
52 void *map_delete(Map
*, const char *key
);
53 /** Copy all entries from ``src`` into ``dest``, overwrites existing entries in ``dest``. */
54 bool map_copy(Map
*dest
, Map
*src
);
56 * Ordered iteration over a map.
57 * Invokes the passed callback for every map entry.
58 * If ``handle`` returns false, the iteration will stop.
59 * @param handle A function invoked for ever map element.
60 * @param data A context pointer, passed as last argument to ``handle``.
62 void map_iterate(const Map
*, bool (*handle
)(const char *key
, void *value
, void *data
), const void *data
);
64 * Get a sub map matching a prefix.
66 * .. warning:: This returns a pointer into the original map.
67 * Do not alter the map while using the return value.
70 const Map
*map_prefix(const Map
*, const char *prefix
);
71 /** Test whether the map is empty (contains no elements). */
72 bool map_empty(const Map
*);
75 /** Release all memory associated with this map. */
78 * Call `free(3)` for every map element, then free the map itself.
80 * .. warning:: Assumes map elements to be pointers.
83 void map_free_full(Map
*);