aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-12-15 20:21:27 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-01-16 19:46:07 +0100
commit55f79e75f34ccf740d3f1c92ab35c604b067b01c (patch)
treebb4fca4a9a5741ac6d9ea69b61495491bf3131c6 /map.c
parent201c3ffa3724f635ba0c98f4acecbd4b8db293c1 (diff)
downloadvis-55f79e75f34ccf740d3f1c92ab35c604b067b01c.tar.gz
vis-55f79e75f34ccf740d3f1c92ab35c604b067b01c.tar.xz
map: add map_leaf utility function
Tests whether the given prefix can be extended to exactly one map element i.e. true iff the prefix map contains exactly one element.
Diffstat (limited to 'map.c')
-rw-r--r--map.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/map.c b/map.c
index 7994f34..94e941c 100644
--- a/map.c
+++ b/map.c
@@ -78,6 +78,19 @@ bool map_contains(const Map *map, const char *prefix)
return !map_empty(map_prefix(map, prefix));
}
+static bool leaf(const char *key, void *value, void *data)
+{
+ int *nodes = data;
+ return (*nodes)++ < 1;
+}
+
+bool map_leaf(const Map *map, const char *prefix)
+{
+ int nodes = 0;
+ map_iterate(map_prefix(map, prefix), leaf, &nodes);
+ return nodes == 1;
+}
+
bool map_put(Map *map, const char *k, const void *value)
{
size_t len = strlen(k);