src/tree.c

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
child 826
21840975d541
permissions
-rw-r--r--

add first basic low level tree functions

relates to #165 tested by issue #167

universe@816 1 /*
universe@816 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@816 3 *
universe@816 4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
universe@816 5 *
universe@816 6 * Redistribution and use in source and binary forms, with or without
universe@816 7 * modification, are permitted provided that the following conditions are met:
universe@816 8 *
universe@816 9 * 1. Redistributions of source code must retain the above copyright
universe@816 10 * notice, this list of conditions and the following disclaimer.
universe@816 11 *
universe@816 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@816 13 * notice, this list of conditions and the following disclaimer in the
universe@816 14 * documentation and/or other materials provided with the distribution.
universe@816 15 *
universe@816 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@816 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@816 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@816 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@816 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@816 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@816 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@816 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@816 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@816 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@816 26 * POSSIBILITY OF SUCH DAMAGE.
universe@816 27 */
universe@816 28
universe@816 29 #include "cx/tree.h"
universe@816 30
universe@816 31 #include <assert.h>
universe@816 32
universe@816 33 #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
universe@816 34 #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
universe@816 35 #define tree_parent(node) CX_TREE_PTR(node, loc_parent)
universe@816 36 #define tree_children(node) CX_TREE_PTR(node, loc_children)
universe@816 37 #define tree_prev(node) CX_TREE_PTR(node, loc_prev)
universe@816 38 #define tree_next(node) CX_TREE_PTR(node, loc_next)
universe@816 39
universe@816 40 void cx_tree_link(
universe@816 41 void *restrict parent,
universe@816 42 void *restrict node,
universe@816 43 ptrdiff_t loc_parent,
universe@816 44 ptrdiff_t loc_children,
universe@816 45 ptrdiff_t loc_prev,
universe@816 46 ptrdiff_t loc_next
universe@816 47 ) {
universe@816 48 void *current_parent = tree_parent(node);
universe@816 49 if (current_parent == parent) return;
universe@816 50 if (current_parent != NULL) {
universe@816 51 cx_tree_unlink(node, loc_parent, loc_children,
universe@816 52 loc_prev, loc_next);
universe@816 53 }
universe@816 54
universe@816 55 if (tree_children(parent) == NULL) {
universe@816 56 tree_children(parent) = node;
universe@816 57 } else {
universe@816 58 void *children = tree_children(parent);
universe@816 59 tree_prev(children) = node;
universe@816 60 tree_next(node) = children;
universe@816 61 tree_children(parent) = node;
universe@816 62 }
universe@816 63 tree_parent(node) = parent;
universe@816 64 }
universe@816 65
universe@816 66 void cx_tree_unlink(
universe@816 67 void *node,
universe@816 68 ptrdiff_t loc_parent,
universe@816 69 ptrdiff_t loc_children,
universe@816 70 ptrdiff_t loc_prev,
universe@816 71 ptrdiff_t loc_next
universe@816 72 ) {
universe@816 73 if (tree_parent(node) == NULL) return;
universe@816 74
universe@816 75 void *left = tree_prev(node);
universe@816 76 void *right = tree_next(node);
universe@816 77 assert(left == NULL || tree_children(tree_parent(node)) != node);
universe@816 78 if (left == NULL) {
universe@816 79 tree_children(tree_parent(node)) = right;
universe@816 80 } else {
universe@816 81 tree_next(left) = right;
universe@816 82 }
universe@816 83 if (right != NULL) tree_prev(right) = left;
universe@816 84 tree_parent(node) = NULL;
universe@816 85 tree_prev(node) = NULL;
universe@816 86 tree_next(node) = NULL;
universe@816 87 }

mercurial