universe@816: /* universe@816: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@816: * universe@816: * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. universe@816: * universe@816: * Redistribution and use in source and binary forms, with or without universe@816: * modification, are permitted provided that the following conditions are met: universe@816: * universe@816: * 1. Redistributions of source code must retain the above copyright universe@816: * notice, this list of conditions and the following disclaimer. universe@816: * universe@816: * 2. Redistributions in binary form must reproduce the above copyright universe@816: * notice, this list of conditions and the following disclaimer in the universe@816: * documentation and/or other materials provided with the distribution. universe@816: * universe@816: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@816: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@816: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@816: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@816: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@816: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@816: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@816: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@816: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@816: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@816: * POSSIBILITY OF SUCH DAMAGE. universe@816: */ universe@816: universe@816: #include "cx/tree.h" universe@816: universe@826: #include "cx/array_list.h" universe@826: universe@816: #include universe@816: universe@816: #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) universe@816: #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) universe@816: #define tree_parent(node) CX_TREE_PTR(node, loc_parent) universe@816: #define tree_children(node) CX_TREE_PTR(node, loc_children) universe@816: #define tree_prev(node) CX_TREE_PTR(node, loc_prev) universe@816: #define tree_next(node) CX_TREE_PTR(node, loc_next) universe@816: universe@816: void cx_tree_link( universe@816: void *restrict parent, universe@816: void *restrict node, universe@816: ptrdiff_t loc_parent, universe@816: ptrdiff_t loc_children, universe@816: ptrdiff_t loc_prev, universe@816: ptrdiff_t loc_next universe@816: ) { universe@816: void *current_parent = tree_parent(node); universe@816: if (current_parent == parent) return; universe@816: if (current_parent != NULL) { universe@816: cx_tree_unlink(node, loc_parent, loc_children, universe@816: loc_prev, loc_next); universe@816: } universe@816: universe@816: if (tree_children(parent) == NULL) { universe@816: tree_children(parent) = node; universe@816: } else { universe@816: void *children = tree_children(parent); universe@816: tree_prev(children) = node; universe@816: tree_next(node) = children; universe@816: tree_children(parent) = node; universe@816: } universe@816: tree_parent(node) = parent; universe@816: } universe@816: universe@816: void cx_tree_unlink( universe@816: void *node, universe@816: ptrdiff_t loc_parent, universe@816: ptrdiff_t loc_children, universe@816: ptrdiff_t loc_prev, universe@816: ptrdiff_t loc_next universe@816: ) { universe@816: if (tree_parent(node) == NULL) return; universe@816: universe@816: void *left = tree_prev(node); universe@816: void *right = tree_next(node); universe@816: assert(left == NULL || tree_children(tree_parent(node)) != node); universe@816: if (left == NULL) { universe@816: tree_children(tree_parent(node)) = right; universe@816: } else { universe@816: tree_next(left) = right; universe@816: } universe@816: if (right != NULL) tree_prev(right) = left; universe@816: tree_parent(node) = NULL; universe@816: tree_prev(node) = NULL; universe@816: tree_next(node) = NULL; universe@816: } universe@826: universe@826: int cx_tree_search( universe@826: void const *root, universe@826: void const *data, universe@826: cx_tree_search_func sfunc, universe@826: void **result, universe@826: ptrdiff_t loc_children, universe@826: ptrdiff_t loc_next universe@826: ) { universe@826: int ret; universe@826: *result = NULL; universe@826: universe@826: // shortcut: compare root before doing anything else universe@826: ret = sfunc(root, data); universe@826: if (ret < 0) { universe@826: return ret; universe@826: } else if (ret == 0 || tree_children(root) == NULL) { universe@826: *result = (void*)root; universe@826: return ret; universe@826: } universe@826: universe@826: // create a working stack universe@826: size_t work_cap = 32; universe@826: size_t work_size = 0; universe@826: void const **work = malloc(sizeof(void*) * work_cap); universe@826: #define work_add(node) cx_array_add(&work, &work_size, &work_cap, \ universe@826: sizeof(void*), &(node), cx_array_default_reallocator) universe@826: universe@826: // add the children of root to the working stack universe@826: { universe@826: void *c = tree_children(root); universe@826: while (c != NULL) { universe@826: work_add(c); universe@826: c = tree_next(c); universe@826: } universe@826: } universe@826: universe@826: // remember a candidate for adding the data universe@826: // also remember the exact return code from sfunc universe@826: void *candidate = NULL; universe@826: int ret_candidate = -1; universe@826: universe@826: // process the working stack universe@826: while (work_size > 0) { universe@826: // pop element universe@826: void const *node = work[--work_size]; universe@826: universe@826: // apply the search function universe@826: ret = sfunc(node, data); universe@826: universe@826: if (ret == 0) { universe@826: // if found, exit the search universe@826: *result = (void*) node; universe@826: work_size = 0; universe@826: break; universe@826: } else if (ret > 0) { universe@826: // if children might contain the data, add them to the stack universe@826: void *c = tree_children(node); universe@826: while (c != NULL) { universe@826: work_add(c); universe@826: c = tree_next(c); universe@826: } universe@826: universe@826: // remember this node in case no child is suitable universe@826: if (ret_candidate < 0 || ret < ret_candidate) { universe@826: candidate = (void *) node; universe@826: ret_candidate = ret; universe@826: } universe@826: } universe@826: } universe@826: universe@826: // not found, but was there a candidate? universe@826: if (ret != 0 && candidate != NULL) { universe@826: ret = ret_candidate; universe@826: *result = candidate; universe@826: } universe@826: universe@826: // free the working queue and return universe@826: #undef workq_add universe@826: free(work); universe@826: return ret; universe@826: } universe@830: universe@830: static bool cx_tree_iter_valid(void const *it) { universe@830: struct cx_tree_iterator_s const *iter = it; universe@830: return iter->node != NULL; universe@830: } universe@830: universe@830: static void *cx_tree_iter_current(void const *it) { universe@830: struct cx_tree_iterator_s const *iter = it; universe@830: return iter->node; universe@830: } universe@830: universe@830: static void cx_tree_iter_stack_add( universe@830: struct cx_tree_iterator_s *iter, universe@830: void *node universe@830: ) { universe@830: cx_array_add(&iter->stack, &iter->depth, &iter->stack_capacity, universe@830: sizeof(void*), &node, cx_array_default_reallocator); universe@830: } universe@830: universe@830: static void cx_tree_iter_next(void *it) { universe@830: struct cx_tree_iterator_s *iter = it; universe@830: // TODO: support mutating iterator universe@830: universe@830: // TODO: implement universe@830: } universe@830: universe@830: universe@830: CxTreeIterator cx_tree_iterator( universe@830: void *root, universe@830: int passes, universe@830: ptrdiff_t loc_children, universe@830: ptrdiff_t loc_next universe@830: ) { universe@830: CxTreeIterator iter; universe@830: iter.loc_children = loc_children; universe@830: iter.loc_next = loc_next; universe@830: iter.requested_passes = passes; universe@830: universe@830: // invalidate iterator immediately when passes is invalid universe@830: if ((passes & (CX_TREE_ITERATOR_ENTER | universe@830: CX_TREE_ITERATOR_NEXT_CHILD | universe@830: CX_TREE_ITERATOR_EXIT)) == 0) { universe@830: iter.stack = NULL; universe@830: iter.node = NULL; universe@830: return iter; universe@830: } universe@830: universe@830: // allocate stack universe@830: iter.stack_capacity = 16; universe@830: iter.stack = malloc(sizeof(void *) * 16); universe@830: iter.depth = 0; universe@830: universe@830: // determine start universe@830: if ((passes & CX_TREE_ITERATOR_ENTER) == 0) { universe@830: // we have to skip the first "entering" passes universe@830: void *s = NULL; universe@830: void *n = root; universe@830: iter.counter = 0; universe@830: do { universe@830: iter.counter++; universe@830: iter.source = s; universe@830: iter.node = n; universe@830: cx_tree_iter_stack_add(&iter, n); universe@830: s = n; universe@830: n = tree_children(n); universe@830: } while (n != NULL); universe@830: // found a leaf node s (might be root itself if it has no children) universe@830: universe@830: // check if there is a sibling universe@830: n = tree_next(s); universe@830: universe@830: if (n == NULL) { universe@830: // no sibling found, exit back to parent node universe@830: // TODO: implement universe@830: } else { universe@830: // there is a sibling universe@830: if ((passes & CX_TREE_ITERATOR_EXIT) == 0) { universe@830: // no exit requested, conclude that only next_child is requested universe@830: iter.source = s; universe@830: iter.node = n; universe@830: iter.counter++; universe@830: iter.current_pass = CX_TREE_ITERATOR_NEXT_CHILD; universe@830: } else { universe@830: // exit requested, so we have found our first pass universe@830: // iter.node and iter.source are still correct universe@830: iter.current_pass = CX_TREE_ITERATOR_EXIT; universe@830: } universe@830: } universe@830: } else { universe@830: // enter passes are requested, we can start by entering the root node universe@830: iter.source = NULL; universe@830: iter.node = root; universe@830: iter.current_pass = CX_TREE_ITERATOR_ENTER; universe@830: iter.counter = 1; universe@830: iter.depth = 1; universe@830: iter.stack[0] = root; universe@830: } universe@830: universe@830: // assign base iterator functions universe@830: iter.base.mutating = false; universe@830: iter.base.remove = false; universe@830: iter.base.current_impl = NULL; universe@830: iter.base.valid = cx_tree_iter_valid; universe@830: iter.base.next = cx_tree_iter_next; universe@830: iter.base.current = cx_tree_iter_current; universe@830: universe@830: return iter; universe@830: }