src/ucx/tree.c

changeset 621
956c03c25edd
parent 582
82b60a8dd55c
child 645
0c85c4cd0dd8
--- a/src/ucx/tree.c	Fri Nov 07 11:06:58 2025 +0100
+++ b/src/ucx/tree.c	Sun Nov 09 23:51:03 2025 +0100
@@ -375,7 +375,7 @@
     iter.skip = false;
 
     // assign base iterator functions
-    iter.base.mutating = false;
+    iter.base.allow_remove = false;
     iter.base.remove = false;
     iter.base.current_impl = NULL;
     iter.base.valid = cx_tree_iter_valid;
@@ -496,7 +496,7 @@
     iter.queue_last = NULL;
 
     // assign base iterator functions
-    iter.base.mutating = false;
+    iter.base.allow_remove = false;
     iter.base.remove = false;
     iter.base.current_impl = NULL;
     iter.base.valid = cx_tree_visitor_valid;
@@ -717,7 +717,7 @@
     }
 
     // otherwise, create iterator and hand over to other function
-    CxIterator iter = cxIterator(src, elem_size, num);
+    CxIterator iter = cxIterator(src, elem_size, num, false);
     return cx_tree_add_iter(cxIteratorRef(iter), num, sfunc,
                             cfunc, cdata, failed, root,
                             loc_parent, loc_children, loc_last_child,
@@ -804,16 +804,12 @@
         cx_tree_default_find
 };
 
-CxTree *cxTreeCreate(
-        const CxAllocator *allocator,
+CxTree *cxTreeCreate(const CxAllocator *allocator,
         cx_tree_node_create_func create_func,
         cx_tree_search_func search_func,
         cx_tree_search_data_func search_data_func,
-        ptrdiff_t loc_parent,
-        ptrdiff_t loc_children,
-        ptrdiff_t loc_last_child,
-        ptrdiff_t loc_prev,
-        ptrdiff_t loc_next
+        ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child,
+        ptrdiff_t loc_prev, ptrdiff_t loc_next
 ) {
     if (allocator == NULL) {
         allocator = cxDefaultAllocator;
@@ -852,15 +848,9 @@
     cxFree(tree->allocator, tree);
 }
 
-CxTree *cxTreeCreateWrapped(
-        const CxAllocator *allocator,
-        void *root,
-        ptrdiff_t loc_parent,
-        ptrdiff_t loc_children,
-        ptrdiff_t loc_last_child,
-        ptrdiff_t loc_prev,
-        ptrdiff_t loc_next
-) {
+CxTree *cxTreeCreateWrapped(const CxAllocator *allocator, void *root,
+        ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child,
+        ptrdiff_t loc_prev, ptrdiff_t loc_next) {
     if (allocator == NULL) {
         allocator = cxDefaultAllocator;
     }
@@ -888,11 +878,7 @@
     return tree;
 }
 
-void cxTreeSetParent(
-        CxTree *tree,
-        void *parent,
-        void *child
-) {
+void cxTreeSetParent(CxTree *tree, void *parent, void *child) {
     size_t loc_parent = tree->loc_parent;
     if (tree_parent(child) == NULL) {
         tree->size++;
@@ -900,19 +886,12 @@
     cx_tree_link(parent, child, cx_tree_node_layout(tree));
 }
 
-void cxTreeAddChildNode(
-        CxTree *tree,
-        void *parent,
-        void *child
-) {
+void cxTreeAddChildNode(CxTree *tree, void *parent, void *child) {
     cx_tree_link(parent, child, cx_tree_node_layout(tree));
     tree->size++;
 }
 
-int cxTreeAddChild(
-        CxTree *tree,
-        void *parent,
-        const void *data) {
+int cxTreeAddChild(CxTree *tree, void *parent, const void *data) {
     void *node = tree->node_create(data, tree);
     if (node == NULL) return 1;
     cx_tree_zero_pointers(node, cx_tree_node_layout(tree));
@@ -921,6 +900,29 @@
     return 0;
 }
 
+int cxTreeInsert(CxTree *tree, const void *data) {
+    return tree->cl->insert_element(tree, data);
+}
+
+size_t cxTreeInsertIter(CxTree *tree, CxIteratorBase *iter, size_t n) {
+    return tree->cl->insert_many(tree, iter, n);
+}
+
+size_t cxTreeInsertArray(CxTree *tree, const void *data, size_t elem_size, size_t n) {
+    if (n == 0) return 0;
+    if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0;
+    CxIterator iter = cxIterator(data, elem_size, n, false);
+    return cxTreeInsertIter(tree, cxIteratorRef(iter), n);
+}
+
+void *cxTreeFind( CxTree *tree, const void *data) {
+    return tree->cl->find(tree, tree->root, data, 0);
+}
+
+void *cxTreeFindInSubtree(CxTree *tree, const void *data, void *subtree_root, size_t max_depth) {
+    return tree->cl->find(tree, subtree_root, data, max_depth);
+}
+
 size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root) {
     CxTreeVisitor visitor = cx_tree_visitor(
             subtree_root,
@@ -945,6 +947,10 @@
     return visitor.depth;
 }
 
+size_t cxTreeSize(CxTree *tree) {
+    return tree->size;
+}
+
 size_t cxTreeDepth(CxTree *tree) {
     CxTreeVisitor visitor = cx_tree_visitor(
             tree->root, tree->loc_children, tree->loc_next
@@ -1052,3 +1058,38 @@
         tree->root = NULL;
     }
 }
+
+void cxTreeIteratorDispose(CxTreeIterator *iter) {
+    cxFreeDefault(iter->stack);
+    iter->stack = NULL;
+}
+
+void cxTreeVisitorDispose(CxTreeVisitor *visitor) {
+    struct cx_tree_visitor_queue_s *q = visitor->queue_next;
+    while (q != NULL) {
+        struct cx_tree_visitor_queue_s *next = q->next;
+        cxFreeDefault(q);
+        q = next;
+    }
+}
+
+CxTreeIterator cxTreeIterateSubtree(CxTree *tree, void *node, bool visit_on_exit) {
+    return cx_tree_iterator(
+            node, visit_on_exit,
+            tree->loc_children, tree->loc_next
+    );
+}
+
+CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node) {
+    return cx_tree_visitor(
+            node, tree->loc_children, tree->loc_next
+    );
+}
+
+CxTreeIterator cxTreeIterate(CxTree *tree, bool visit_on_exit) {
+    return cxTreeIterateSubtree(tree, tree->root, visit_on_exit);
+}
+
+CxTreeVisitor cxTreeVisit(CxTree *tree) {
+    return cxTreeVisitSubtree(tree, tree->root);
+}

mercurial