add first basic low level tree functions

Mon, 22 Jan 2024 19:34:38 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 22 Jan 2024 19:34:38 +0100
changeset 816
425234b05dff
parent 815
b0c4750cecd8
child 817
949908c97474

add first basic low level tree functions

relates to #165 tested by issue #167

src/Makefile file | annotate | diff | comparison | revisions
src/cx/tree.h file | annotate | diff | comparison | revisions
src/tree.c file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
tests/test_tree.c file | annotate | diff | comparison | revisions
tests/ucxtest.c file | annotate | diff | comparison | revisions
--- a/src/Makefile	Sat Jan 20 16:02:04 2024 +0100
+++ b/src/Makefile	Mon Jan 22 19:34:38 2024 +0100
@@ -24,7 +24,7 @@
 include ../config.mk
 
 SRC = allocator.c array_list.c buffer.c compare.c hash_key.c hash_map.c \
-  linked_list.c list.c map.c mempool.c printf.c string.c utils.c
+  linked_list.c list.c map.c tree.c mempool.c printf.c string.c utils.c
 
 OBJ_EXT=.o
 OBJ=$(SRC:%.c=$(build_dir)/%$(OBJ_EXT))
@@ -124,6 +124,10 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
+$(build_dir)/tree$(OBJ_EXT): tree.c cx/tree.h cx/common.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(build_dir)/utils$(OBJ_EXT): utils.c cx/utils.h cx/common.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cx/tree.h	Mon Jan 22 19:34:38 2024 +0100
@@ -0,0 +1,69 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/**
+ * \file tree.h
+ * \brief Interface for tree implementations.
+ * \author Mike Becker
+ * \author Olaf Wintermann
+ * \copyright 2-Clause BSD License
+ */
+
+#ifndef UCX_TREE_H
+#define UCX_TREE_H
+
+#include "common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+__attribute__((__nonnull__))
+void cx_tree_link(
+        void * restrict parent,
+        void * restrict node,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+);
+
+__attribute__((__nonnull__))
+void cx_tree_unlink(
+        void *node,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif //UCX_TREE_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tree.c	Mon Jan 22 19:34:38 2024 +0100
@@ -0,0 +1,87 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/tree.h"
+
+#include <assert.h>
+
+#define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
+#define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
+#define tree_parent(node) CX_TREE_PTR(node, loc_parent)
+#define tree_children(node) CX_TREE_PTR(node, loc_children)
+#define tree_prev(node) CX_TREE_PTR(node, loc_prev)
+#define tree_next(node) CX_TREE_PTR(node, loc_next)
+
+void cx_tree_link(
+        void *restrict parent,
+        void *restrict node,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+) {
+    void *current_parent = tree_parent(node);
+    if (current_parent == parent) return;
+    if (current_parent != NULL) {
+        cx_tree_unlink(node, loc_parent, loc_children,
+                       loc_prev, loc_next);
+    }
+
+    if (tree_children(parent) == NULL) {
+        tree_children(parent) = node;
+    } else {
+        void *children = tree_children(parent);
+        tree_prev(children) = node;
+        tree_next(node) = children;
+        tree_children(parent) = node;
+    }
+    tree_parent(node) = parent;
+}
+
+void cx_tree_unlink(
+        void *node,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+) {
+    if (tree_parent(node) == NULL) return;
+
+    void *left = tree_prev(node);
+    void *right = tree_next(node);
+    assert(left == NULL || tree_children(tree_parent(node)) != node);
+    if (left == NULL) {
+        tree_children(tree_parent(node)) = right;
+    } else {
+        tree_next(left) = right;
+    }
+    if (right != NULL) tree_prev(right) = left;
+    tree_parent(node) = NULL;
+    tree_prev(node) = NULL;
+    tree_next(node) = NULL;
+}
--- a/tests/Makefile	Sat Jan 20 16:02:04 2024 +0100
+++ b/tests/Makefile	Mon Jan 22 19:34:38 2024 +0100
@@ -28,7 +28,7 @@
 TEST_DIR=$(build_dir)/tests
 
 SRC = util_allocator.c test_utils.c test_hash_key.c test_allocator.c \
-	test_compare.c test_string.c test_buffer.c test_list.c \
+	test_compare.c test_string.c test_buffer.c test_list.c test_tree.c \
 	test_printf.c test_mempool.c test_hash_map.c ucxtest.c
 
 OBJ_EXT=.o
@@ -79,8 +79,9 @@
 
 $(TEST_DIR)/test_list$(OBJ_EXT): test_list.c ../src/cx/test.h \
  util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \
- ../src/cx/array_list.h ../src/cx/list.h ../src/cx/collection.h \
- ../src/cx/allocator.h ../src/cx/iterator.h ../src/cx/linked_list.h
+ ../src/cx/compare.h ../src/cx/utils.h ../src/cx/array_list.h \
+ ../src/cx/list.h ../src/cx/collection.h ../src/cx/allocator.h \
+ ../src/cx/iterator.h ../src/cx/linked_list.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -103,6 +104,11 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
+$(TEST_DIR)/test_tree$(OBJ_EXT): test_tree.c ../src/cx/tree.h \
+ ../src/cx/common.h ../src/cx/test.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(TEST_DIR)/test_utils$(OBJ_EXT): test_utils.c ../src/cx/test.h \
  ../src/cx/utils.h ../src/cx/common.h ../src/cx/buffer.h \
  ../src/cx/allocator.h ../src/szmul.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_tree.c	Mon Jan 22 19:34:38 2024 +0100
@@ -0,0 +1,172 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/tree.h"
+
+#include "cx/test.h"
+
+typedef struct tree_node {
+    struct tree_node *parent;
+    struct tree_node *next;
+    struct tree_node *prev;
+    struct tree_node *children;
+    int data;
+} tree_node;
+
+#define tree_node_layout \
+    offsetof(tree_node, parent), offsetof(tree_node, children), \
+    offsetof(tree_node, prev), offsetof(tree_node, next)
+
+CX_TEST(test_tree_link_new_child) {
+    tree_node parent = {0};
+    tree_node child = {0};
+
+    CX_TEST_DO {
+        cx_tree_link(&parent, &child, tree_node_layout);
+        CX_TEST_ASSERT(parent.next == NULL);
+        CX_TEST_ASSERT(parent.prev == NULL);
+        CX_TEST_ASSERT(parent.parent == NULL);
+        CX_TEST_ASSERT(parent.children == &child);
+        CX_TEST_ASSERT(child.parent == &parent);
+        CX_TEST_ASSERT(child.next == NULL);
+        CX_TEST_ASSERT(child.prev == NULL);
+        CX_TEST_ASSERT(child.children == NULL);
+    }
+}
+
+CX_TEST(test_tree_link_add_child) {
+    tree_node parent = {0};
+    tree_node child1 = {0};
+    tree_node child2 = {0};
+    tree_node child3 = {0};
+
+    CX_TEST_DO {
+        cx_tree_link(&parent, &child1, tree_node_layout);
+        cx_tree_link(&parent, &child2, tree_node_layout);
+        cx_tree_link(&parent, &child3, tree_node_layout);
+        CX_TEST_ASSERT(parent.next == NULL);
+        CX_TEST_ASSERT(parent.prev == NULL);
+        CX_TEST_ASSERT(parent.parent == NULL);
+        CX_TEST_ASSERT(parent.children == &child3);
+
+        CX_TEST_ASSERT(child1.parent == &parent);
+        CX_TEST_ASSERT(child2.parent == &parent);
+        CX_TEST_ASSERT(child3.parent == &parent);
+        CX_TEST_ASSERT(child1.children == NULL);
+        CX_TEST_ASSERT(child2.children == NULL);
+        CX_TEST_ASSERT(child3.children == NULL);
+
+        CX_TEST_ASSERT(child3.prev == NULL);
+        CX_TEST_ASSERT(child3.next == &child2);
+        CX_TEST_ASSERT(child2.prev == &child3);
+        CX_TEST_ASSERT(child2.next == &child1);
+        CX_TEST_ASSERT(child1.prev == &child2);
+        CX_TEST_ASSERT(child1.next == NULL);
+    }
+}
+
+CX_TEST(test_tree_link_move_to_other_parent) {
+    tree_node parent = {0};
+    tree_node child1 = {0};
+    tree_node child2 = {0};
+    tree_node child3 = {0};
+    cx_tree_link(&parent, &child1, tree_node_layout);
+    cx_tree_link(&parent, &child2, tree_node_layout);
+    cx_tree_link(&parent, &child3, tree_node_layout);
+
+    CX_TEST_DO {
+        cx_tree_link(&child3, &child2, tree_node_layout);
+
+        CX_TEST_ASSERT(parent.next == NULL);
+        CX_TEST_ASSERT(parent.prev == NULL);
+        CX_TEST_ASSERT(parent.parent == NULL);
+        CX_TEST_ASSERT(parent.children == &child3);
+
+        CX_TEST_ASSERT(child1.parent == &parent);
+        CX_TEST_ASSERT(child2.parent == &child3);
+        CX_TEST_ASSERT(child3.parent == &parent);
+        CX_TEST_ASSERT(child1.children == NULL);
+        CX_TEST_ASSERT(child2.children == NULL);
+        CX_TEST_ASSERT(child3.children == &child2);
+
+        CX_TEST_ASSERT(child3.prev == NULL);
+        CX_TEST_ASSERT(child3.next == &child1);
+        CX_TEST_ASSERT(child1.prev == &child3);
+        CX_TEST_ASSERT(child1.next == NULL);
+
+        CX_TEST_ASSERT(child2.prev == NULL);
+        CX_TEST_ASSERT(child2.next == NULL);
+    }
+}
+
+CX_TEST(test_tree_unlink) {
+    tree_node parent = {0};
+    tree_node child1 = {0};
+    tree_node child2 = {0};
+    tree_node child3 = {0};
+    cx_tree_link(&parent, &child1, tree_node_layout);
+    cx_tree_link(&parent, &child3, tree_node_layout);
+    cx_tree_link(&child3, &child2, tree_node_layout);
+
+    CX_TEST_DO {
+        cx_tree_unlink(&child3, tree_node_layout);
+
+        CX_TEST_ASSERT(parent.next == NULL);
+        CX_TEST_ASSERT(parent.prev == NULL);
+        CX_TEST_ASSERT(parent.parent == NULL);
+        CX_TEST_ASSERT(parent.children == &child1);
+
+        CX_TEST_ASSERT(child1.parent == &parent);
+        CX_TEST_ASSERT(child1.children == NULL);
+        CX_TEST_ASSERT(child1.prev == NULL);
+        CX_TEST_ASSERT(child1.next == NULL);
+
+        // child 3 is unlinked
+        CX_TEST_ASSERT(child3.parent == NULL);
+        CX_TEST_ASSERT(child3.prev == NULL);
+        CX_TEST_ASSERT(child3.next == NULL);
+
+        // child 2 is still child of the unlinked child 3
+        CX_TEST_ASSERT(child3.children == &child2);
+        CX_TEST_ASSERT(child2.parent == &child3);
+        CX_TEST_ASSERT(child2.children == NULL);
+        CX_TEST_ASSERT(child2.prev == NULL);
+        CX_TEST_ASSERT(child2.next == NULL);
+    }
+}
+
+CxTestSuite *cx_test_suite_tree_low_level(void) {
+    CxTestSuite *suite = cx_test_suite_new("tree (low level)");
+
+    cx_test_register(suite, test_tree_link_new_child);
+    cx_test_register(suite, test_tree_link_add_child);
+    cx_test_register(suite, test_tree_link_move_to_other_parent);
+    cx_test_register(suite, test_tree_unlink);
+
+    return suite;
+}
--- a/tests/ucxtest.c	Sat Jan 20 16:02:04 2024 +0100
+++ b/tests/ucxtest.c	Mon Jan 22 19:34:38 2024 +0100
@@ -39,6 +39,7 @@
 CxTestSuite *cx_test_suite_empty_list(void);
 CxTestSuite *cx_test_suite_array_list(void);
 CxTestSuite *cx_test_suite_linked_list(void);
+CxTestSuite *cx_test_suite_tree_low_level(void);
 CxTestSuite *cx_test_suite_mempool(void);
 CxTestSuite *cx_test_suite_hash_map(void);
 
@@ -62,6 +63,7 @@
             cx_test_suite_empty_list(),
             cx_test_suite_array_list(),
             cx_test_suite_linked_list(),
+            cx_test_suite_tree_low_level(),
             cx_test_suite_mempool(),
             cx_test_suite_hash_map()
     );

mercurial