From 97101f1f82ba55015484c124d2f2990c0773b75c Mon Sep 17 00:00:00 2001 From: Jakub Sitnicki Date: Mon, 7 Jul 2014 06:23:22 +0200 Subject: [PATCH] mu: Prune empty container when an only child gets promoted to the root set --- lib/mu-container.c | 17 ++++++++++++++++- lib/mu-container.h | 17 +++++++++++++++-- lib/mu-threader.c | 13 +++---------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/mu-container.c b/lib/mu-container.c index 7011fec5..a201c30c 100644 --- a/lib/mu-container.c +++ b/lib/mu-container.c @@ -263,9 +263,24 @@ mu_container_foreach (MuContainer *c, MuContainerForeachFunc func, return func (c, user_data); } +MuContainer* +mu_container_splice_children (MuContainer *c, MuContainer *sibling) +{ + MuContainer *children; + + g_return_val_if_fail (c, NULL); + g_return_val_if_fail (sibling, NULL); + + children = sibling->child; + sibling->child = NULL; + + c = mu_container_remove_sibling (c, sibling); + + return mu_container_append_siblings (c, children); +} MuContainer* -mu_container_splice_children (MuContainer *parent, MuContainer *child) +mu_container_splice_grandchildren (MuContainer *parent, MuContainer *child) { MuContainer *newchild; diff --git a/lib/mu-container.h b/lib/mu-container.h index ec9d7f72..c80f0273 100644 --- a/lib/mu-container.h +++ b/lib/mu-container.h @@ -122,6 +122,19 @@ MuContainer* mu_container_remove_child (MuContainer *c, MuContainer *child); */ MuContainer* mu_container_remove_sibling (MuContainer *c, MuContainer *sibling); +/** + * promote sibling's children to be this container's siblings and + * remove the sibling + * + * @param c a container instance + * @param sibling a sibling of this container + * + * @return the container with the sibling's children promoted and the + * sibling itself removed + */ + +MuContainer* mu_container_splice_children (MuContainer *c, + MuContainer *sibling); /** * promote child's children to be parent's children and remove child @@ -131,8 +144,8 @@ MuContainer* mu_container_remove_sibling (MuContainer *c, MuContainer *sibling); * * @return the new container with it's children's children promoted */ -MuContainer* mu_container_splice_children (MuContainer *parent, - MuContainer *child); +MuContainer* mu_container_splice_grandchildren (MuContainer *parent, + MuContainer *child); typedef gboolean (*MuContainerForeachFunc) (MuContainer*, gpointer); diff --git a/lib/mu-threader.c b/lib/mu-threader.c index 719508d2..c42e166c 100644 --- a/lib/mu-threader.c +++ b/lib/mu-threader.c @@ -388,7 +388,7 @@ prune_maybe (MuContainer *c) if (cur->flags & MU_CONTAINER_FLAG_DELETE) c = mu_container_remove_child (c, cur); else if (cur->flags & MU_CONTAINER_FLAG_SPLICE) - c = mu_container_splice_children (c, cur); + c = mu_container_splice_grandchildren (c, cur); } g_return_val_if_fail (c, FALSE); @@ -433,17 +433,10 @@ prune_empty_containers (MuContainer *root_set) /* and prune the root_set itself... */ for (cur = root_set; cur; cur = cur->next) { - if (cur->flags & MU_CONTAINER_FLAG_DELETE) root_set = mu_container_remove_sibling (root_set, cur); - - else if (cur->flags & MU_CONTAINER_FLAG_SPLICE) { - MuContainer *newchild; - newchild = cur->child; - cur->child = NULL; - root_set = mu_container_append_siblings (root_set, - newchild); - } + else if (cur->flags & MU_CONTAINER_FLAG_SPLICE) + root_set = mu_container_splice_children (root_set, cur); } return root_set;