aboutsummaryrefslogtreecommitdiff
path: root/map.h
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-03-11 23:16:57 +0100
committerMarc André Tanner <mat@brain-dump.org>2015-03-18 20:52:00 +0100
commit286e1911b926d47e2028e132451f9c0f99f4affb (patch)
tree30b10aa3e1e26b787bd42b61044816a02ca70acb /map.h
parent1540d03c9745bbde26e46722782d618b4345c903 (diff)
downloadvis-286e1911b926d47e2028e132451f9c0f99f4affb.tar.gz
vis-286e1911b926d47e2028e132451f9c0f99f4affb.tar.xz
Crit-bit tree based map
It supports lookups based on unique prefixes as well as ordered iteration. More information about the data structure can be found at: http://cr.yp.to/critbit.htm http://github.com/agl/critbit http://ccodearchive.net/info/strmap.html It will be used to implement ":"-commands which means any unique prefix will be recognized as a command. It could also be used to implement (runtime) key bindings.
Diffstat (limited to 'map.h')
-rw-r--r--map.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/map.h b/map.h
new file mode 100644
index 0000000..ed42393
--- /dev/null
+++ b/map.h
@@ -0,0 +1,34 @@
+#ifndef MAP_H
+#define MAP_H
+
+#include <stdbool.h>
+
+typedef struct Map Map;
+
+/* Allocate a new map. */
+Map *map_new(void);
+/* Retrieves a value, or NULL if it isn't in the map */
+void *map_get(const Map*, const char *key);
+/* Returns the corresponding value if the given prefix is unique.
+ * Otherwise NULL, if no such prefix exists then errno is set to ENOENT. */
+void *map_closest(const Map*, const char *prefix);
+/* Place a member in the map. This returns false if we run out of memory
+ * (errno = ENOMEM), or if that key already appears in the map (errno = EEXIST). */
+bool map_put(Map*, const char *key, const void *value);
+/* Remove a member from the map. Returns the removed entry or NULL
+ * if there was no entry found using the given key*/
+void *map_delete(Map*, const char *key);
+/* Ordered iteration over a map, call handle for every entry. If handle
+ * returns false, the iteration will stop. */
+void map_iterate(const Map*, bool (*handle)(const char *key, void *value, void *data), const void *data);
+/* Return a submap matching a prefix. This returns a pointer into the
+ * original map, so don't alter the map while using the return value. */
+const Map *map_prefix(const Map*, const char *prefix);
+/* Test whether the map is empty i.e. contains no elements */
+bool map_empty(const Map*);
+/* Remove every member from the map. The map will be empty after this. */
+void map_clear(Map*);
+/* Release all memory associated with this map */
+void map_free(Map*);
+
+#endif