From 7476ccc8211577e0e91b9cde2ca7bcbb5e346f3a Mon Sep 17 00:00:00 2001 From: Nemo D'ACREMONT Date: Wed, 7 May 2025 12:30:27 +0200 Subject: [PATCH] fix: use child list properly --- src/utils/ufd.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/utils/ufd.c b/src/utils/ufd.c index e5908f8..e649515 100644 --- a/src/utils/ufd.c +++ b/src/utils/ufd.c @@ -1,6 +1,7 @@ #include "ufd.h" #include "debug.h" #include "stdlib.h" +#include #include @@ -21,10 +22,11 @@ inline void ufd__join(struct ufd_t* th1, struct ufd_t* th2) if (a != b) { - a->parent = th2; - th2->child = a; - a->repr = b; - TAILQ_INSERT_TAIL(&b->repr->children, a, link); + th1->parent = th2; + th1->repr = b; + th2->child = th1; + if (th1 != b) + TAILQ_INSERT_TAIL(&b->children, th1, link); } } @@ -44,13 +46,17 @@ inline struct ufd_t* ufd__find(struct ufd_t* th) inline void ufd__delete(struct ufd_t* th) { - struct ufd_t* child = NULL; + struct ufd_t* child; - TAILQ_FOREACH(child, &th->children, link) + while (!TAILQ_EMPTY(&th->children)) { - if (child->parent == th) - child->parent = th->child; - if (child->repr == th) - child->repr = th->child; + child = TAILQ_FIRST(&th->children); + TAILQ_REMOVE(&th->children, child, link); + + child->parent = th->child; + child->repr = th->child; + if (child != th->child) + TAILQ_INSERT_TAIL(&th->child->children, child, link); } + DBG("prefix %d", a); }