aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-01-12 22:38:45 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-01-13 21:38:20 +0100
commit2c0d472e87625000c71626fc965e7d1060f4ac7d (patch)
tree01409e1c5b469f23eb5263899a5b5a4cb3233cf3 /map.c
parent0b886f820677aa949c9ce6f16378a73863e537dd (diff)
downloadvis-2c0d472e87625000c71626fc965e7d1060f4ac7d.tar.gz
vis-2c0d472e87625000c71626fc965e7d1060f4ac7d.tar.xz
map: implement map_copy
Copies all entries from one map to another, overwriting existing entries.
Diffstat (limited to 'map.c')
-rw-r--r--map.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/map.c b/map.c
index 5fde157..57dc761 100644
--- a/map.c
+++ b/map.c
@@ -284,6 +284,28 @@ void map_clear(Map *map)
map->v = NULL;
}
+static bool copy(Map *dest, Map n)
+{
+ if (!n.v) {
+ return copy(dest, n.u.n->child[0]) &&
+ copy(dest, n.u.n->child[1]);
+ } else {
+ if (!map_put(dest, n.u.s, n.v) && map_get(dest, n.u.s) != n.v) {
+ map_delete(dest, n.u.s);
+ return map_put(dest, n.u.s, n.v);
+ }
+ return true;
+ }
+}
+
+bool map_copy(Map *dest, Map *src)
+{
+ if (!src || !src->u.n)
+ return true;
+
+ return copy(dest, *src);
+}
+
bool map_empty(const Map *map)
{
return map->u.n == NULL;