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@834: CX_ARRAY_DECLARE(void const*, work); universe@833: cx_array_initialize(work, 32); 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@833: cx_array_simple_add(work, 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@833: cx_array_simple_add(work, 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: 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_next(void *it) { universe@830: struct cx_tree_iterator_s *iter = it; universe@845: ptrdiff_t const loc_next = iter->loc_next; universe@845: ptrdiff_t const loc_children = iter->loc_children; universe@830: universe@838: void *children; universe@838: universe@838: // check if we are currently exiting or entering nodes universe@838: if (iter->exiting) { universe@838: children = NULL; universe@848: // skipping on exit is pointless, just clear the flag universe@848: iter->skip = false; universe@838: } else { universe@848: if (iter->skip) { universe@848: // skip flag is set, pretend that there are no children universe@848: iter->skip = false; universe@848: children = NULL; universe@848: } else { universe@848: // try to enter the children (if any) universe@848: children = tree_children(iter->node); universe@848: } universe@838: } universe@838: universe@836: if (children == NULL) { universe@836: // search for the next node universe@838: void *next; universe@838: cx_tree_iter_search_next: universe@838: // check if there is a sibling universe@840: if (iter->exiting) { universe@840: next = iter->next; universe@840: } else { universe@840: next = tree_next(iter->node); universe@840: iter->next = next; universe@840: } universe@838: if (next == NULL) { universe@838: // no sibling, we are done with this node and exit universe@838: if (iter->visit_on_exit && !iter->exiting) { universe@838: // iter is supposed to visit the node again universe@838: iter->exiting = true; universe@838: } else { universe@838: iter->exiting = false; universe@838: if (iter->depth == 1) { universe@836: // there is no parent - we have iterated the entire tree universe@836: // invalidate the iterator and free the node stack universe@840: iter->node = iter->next = NULL; universe@838: iter->stack_capacity = iter->depth = 0; universe@836: free(iter->stack); universe@836: iter->stack = NULL; universe@836: } else { universe@836: // the parent node can be obtained from the top of stack universe@836: // this way we can avoid the loc_parent in the iterator universe@838: iter->depth--; universe@838: iter->node = iter->stack[iter->depth - 1]; universe@838: // retry with the parent node to find a sibling universe@838: goto cx_tree_iter_search_next; universe@836: } universe@838: } universe@838: } else { universe@838: if (iter->visit_on_exit && !iter->exiting) { universe@838: // iter is supposed to visit the node again universe@838: iter->exiting = true; universe@836: } else { universe@838: iter->exiting = false; universe@838: // move to the sibling universe@836: iter->counter++; universe@836: iter->node = next; universe@836: // new top of stack is the sibling universe@836: iter->stack[iter->depth - 1] = next; universe@836: } universe@838: } universe@836: } else { universe@836: // node has children, push the first child onto the stack and enter it universe@836: cx_array_simple_add(iter->stack, children); universe@836: iter->node = children; universe@836: iter->counter++; universe@836: } universe@830: } universe@830: universe@830: CxTreeIterator cx_tree_iterator( universe@830: void *root, universe@833: bool visit_on_exit, 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@833: iter.visit_on_exit = visit_on_exit; 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@833: // visit the root node universe@833: iter.node = root; universe@848: iter.next = NULL; universe@833: iter.counter = 1; universe@833: iter.depth = 1; universe@833: iter.stack[0] = root; universe@833: iter.exiting = false; universe@848: iter.skip = false; 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: } universe@845: universe@845: static bool cx_tree_visitor_valid(void const *it) { universe@845: struct cx_tree_visitor_s const *iter = it; universe@845: return iter->node != NULL; universe@845: } universe@845: universe@845: static void *cx_tree_visitor_current(void const *it) { universe@845: struct cx_tree_visitor_s const *iter = it; universe@845: return iter->node; universe@845: } universe@845: universe@845: __attribute__((__nonnull__)) universe@845: static void cx_tree_visitor_enqueue_siblings( universe@845: struct cx_tree_visitor_s *iter, void *node, ptrdiff_t loc_next) { universe@845: node = tree_next(node); universe@845: while (node != NULL) { universe@845: struct cx_tree_visitor_queue_s *q; universe@845: q = malloc(sizeof(struct cx_tree_visitor_queue_s)); universe@845: q->depth = iter->queue_last->depth; universe@845: q->node = node; universe@845: iter->queue_last->next = q; universe@845: iter->queue_last = q; universe@845: node = tree_next(node); universe@845: } universe@845: iter->queue_last->next = NULL; universe@845: } universe@845: universe@845: static void cx_tree_visitor_next(void *it) { universe@845: struct cx_tree_visitor_s *iter = it; universe@845: ptrdiff_t const loc_next = iter->loc_next; universe@845: ptrdiff_t const loc_children = iter->loc_children; universe@845: universe@848: // add the children of the current node to the queue universe@848: // unless the skip flag is set universe@848: void *children; universe@848: if (iter->skip) { universe@848: iter->skip = false; universe@848: children = NULL; universe@848: } else { universe@848: children = tree_children(iter->node); universe@848: } universe@848: if (children != NULL) { universe@848: struct cx_tree_visitor_queue_s *q; universe@848: q = malloc(sizeof(struct cx_tree_visitor_queue_s)); universe@848: q->depth = iter->depth + 1; universe@848: q->node = children; universe@848: if (iter->queue_last == NULL) { universe@848: assert(iter->queue_next == NULL); universe@848: iter->queue_next = q; universe@848: } else { universe@848: iter->queue_last->next = q; universe@848: } universe@848: iter->queue_last = q; universe@848: cx_tree_visitor_enqueue_siblings(iter, children, loc_next); universe@848: } universe@848: universe@845: // check if there is a next node universe@845: if (iter->queue_next == NULL) { universe@845: iter->node = NULL; universe@845: return; universe@845: } universe@845: universe@845: // dequeue the next node universe@845: iter->node = iter->queue_next->node; universe@845: iter->depth = iter->queue_next->depth; universe@845: { universe@845: struct cx_tree_visitor_queue_s *q = iter->queue_next; universe@845: iter->queue_next = q->next; universe@845: if (iter->queue_next == NULL) { universe@845: assert(iter->queue_last == q); universe@845: iter->queue_last = NULL; universe@845: } universe@845: free(q); universe@845: } universe@845: universe@845: // increment the node counter universe@845: iter->counter++; universe@845: } universe@845: universe@845: CxTreeVisitor cx_tree_visitor( universe@845: void *root, universe@845: ptrdiff_t loc_children, universe@845: ptrdiff_t loc_next universe@845: ) { universe@845: CxTreeVisitor iter; universe@845: iter.loc_children = loc_children; universe@845: iter.loc_next = loc_next; universe@845: universe@845: // allocate stack universe@845: iter.depth = 0; universe@845: universe@845: // visit the root node universe@845: iter.node = root; universe@845: iter.counter = 1; universe@845: iter.depth = 1; universe@848: iter.skip = false; universe@848: iter.queue_next = NULL; universe@848: iter.queue_last = NULL; universe@845: universe@845: // assign base iterator functions universe@845: iter.base.mutating = false; universe@845: iter.base.remove = false; universe@845: iter.base.current_impl = NULL; universe@845: iter.base.valid = cx_tree_visitor_valid; universe@845: iter.base.next = cx_tree_visitor_next; universe@845: iter.base.current = cx_tree_visitor_current; universe@845: universe@845: return iter; universe@845: } universe@845: