src/cx/tree.h

changeset 905
39aa4f106a71
parent 904
cdc49211d87f
--- a/src/cx/tree.h	Thu Oct 03 16:31:09 2024 +0200
+++ b/src/cx/tree.h	Thu Oct 03 17:39:21 2024 +0200
@@ -436,7 +436,6 @@
  * @return the new tree iterator
  * @see cxTreeIteratorDispose()
  */
-__attribute__((__nonnull__))
 CxTreeIterator cx_tree_iterator(
         void *root,
         bool visit_on_exit,
@@ -462,7 +461,6 @@
  * @return the new tree visitor
  * @see cxTreeVisitorDispose()
  */
-__attribute__((__nonnull__))
 CxTreeVisitor cx_tree_visitor(
         void *root,
         ptrdiff_t loc_children,
@@ -741,6 +739,11 @@
     cx_tree_search_func search;
 
     /**
+     * A function to compare a node with data.
+     */
+    cx_tree_search_data_func search_data;
+
+    /**
      * The number of currently stored elements.
      */
     size_t size;
@@ -878,6 +881,7 @@
  * @param allocator the allocator that shall be used
  * @param create_func a function that creates new nodes
  * @param search_func a function that compares two nodes
+ * @param search_data_func a function that compares a node with data
  * @param loc_parent offset in the node struct for the parent pointer
  * @param loc_children offset in the node struct for the children linked list
  * @param loc_last_child optional offset in the node struct for the pointer to
@@ -893,6 +897,7 @@
         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,
@@ -913,6 +918,7 @@
  * @param allocator the allocator that shall be used
  * @param create_func a function that creates new nodes
  * @param search_func a function that compares two nodes
+ * @param search_data_func a function that compares a node with data
  * @return the new tree
  * @see cxTreeCreate()
  */
@@ -920,12 +926,14 @@
 static inline CxTree *cxTreeCreateSimple(
         const CxAllocator *allocator,
         cx_tree_node_create_func create_func,
-        cx_tree_search_func search_func
+        cx_tree_search_func search_func,
+        cx_tree_search_data_func search_data_func
 ) {
     return cxTreeCreate(
             allocator,
             create_func,
             search_func,
+            search_data_func,
             cx_tree_node_base_layout
     );
 }
@@ -933,8 +941,7 @@
 /**
  * Creates a new tree structure based on an existing tree.
  *
- * The specified \p allocator will be used for creating the tree struct
- * and SHALL be used by \p create_func to allocate memory for the nodes.
+ * The specified \p allocator will be used for creating the tree struct.
  *
  * \attention This function will create an incompletely defined tree structure
  * where neither the create function, the search function, nor a destructor
@@ -953,6 +960,7 @@
  */
 __attribute__((__nonnull__, __warn_unused_result__))
 CxTree *cxTreeCreateWrapped(
+        const CxAllocator *allocator,
         void *root,
         ptrdiff_t loc_parent,
         ptrdiff_t loc_children,
@@ -1045,7 +1053,7 @@
 /**
  * Searches the data in the specified tree.
  *
- * \remark For this function to work, the tree needs a specified search
+ * \remark For this function to work, the tree needs a specified \c search_data
  * function, which might not be available wrapped trees
  * (see #cxTreeCreateWrapped()).
  *
@@ -1067,7 +1075,7 @@
  * \note When \p subtree_root is not part of the \p tree, the behavior is
  * undefined.
  *
- * \remark For this function to work, the tree needs a specified search
+ * \remark For this function to work, the tree needs a specified \c search_data
  * function, which might not be the case for wrapped trees
  * (see #cxTreeCreateWrapped()).
  *
@@ -1106,6 +1114,15 @@
 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root);
 
 /**
+ * Determines the depth of the entire tree.
+ *
+ * @param tree the tree
+ * @return the tree depth, counting the root as one
+ */
+__attribute__((__nonnull__))
+size_t cxTreeDepth(CxTree *tree);
+
+/**
  * Creates a depth-first iterator for the specified tree.
  *
  * @param tree the tree to iterate
@@ -1179,22 +1196,18 @@
 );
 
 /**
- * Removes a node from the tree.
+ * Removes a node and it's subtree from the tree.
  *
  * If the node is not part of the tree, the behavior is undefined.
  *
- * \note The destructor function, if any, will \em not be invoked.
+ * \note The destructor function, if any, will \em not be invoked. That means
+ * you will need to free the removed subtree by yourself, eventually.
  *
  * @param tree the tree
  * @param node the node to remove
  */
 __attribute__((__nonnull__))
-static inline void cxTreeRemove(
-        CxTree *tree,
-        void *node) {
-    cx_tree_unlink(node, cx_tree_node_layout(tree));
-    tree->size--;
-}
+void cxTreeRemove(CxTree *tree, void *node);
 
 #ifdef __cplusplus
 } // extern "C"

mercurial