add prototypes for cx_tree_add() family of functions feature/tree_add

Sun, 07 Jul 2024 12:21:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Jul 2024 12:21:58 +0200
branch
feature/tree_add
changeset 860
558ed4c6abd0
parent 859
6367456bf2d9
child 863
4a220afebad0

add prototypes for cx_tree_add() family of functions

relates to #390

src/cx/tree.h file | annotate | diff | comparison | revisions
src/tree.c file | annotate | diff | comparison | revisions
--- a/src/cx/tree.h	Wed Jul 03 22:07:42 2024 +0200
+++ b/src/cx/tree.h	Sun Jul 07 12:21:58 2024 +0200
@@ -404,6 +404,49 @@
         ptrdiff_t loc_next
 );
 
+/**
+ * Describes a function that creates a tree node from the specified data.
+ */
+typedef void (*cx_tree_node_create_fun)(void const*);
+
+__attribute__((__nonnull__))
+size_t cx_tree_add_iter(
+        struct cx_iterator_base_s *iter,
+        cx_tree_search_func sfunc,
+        cx_tree_node_create_fun cfunc,
+        void **root,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+);
+
+__attribute__((__nonnull__))
+size_t cx_tree_add_array(
+        void const *src,
+        size_t num,
+        size_t elem_size,
+        cx_tree_search_func sfunc,
+        cx_tree_node_create_fun cfunc,
+        void **root,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+);
+
+__attribute__((__nonnull__))
+void *cx_tree_add(
+        void const *src,
+        cx_tree_search_func sfunc,
+        cx_tree_node_create_fun cfunc,
+        void **root,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+);
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
--- a/src/tree.c	Wed Jul 03 22:07:42 2024 +0200
+++ b/src/tree.c	Sun Jul 07 12:21:58 2024 +0200
@@ -399,3 +399,65 @@
     return iter;
 }
 
+void *cx_tree_add(
+        void const *src,
+        cx_tree_search_func sfunc,
+        cx_tree_node_create_fun cfunc,
+        void **root,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+) {
+    // TODO: implement
+    return NULL;
+}
+
+size_t cx_tree_add_iter(
+        struct cx_iterator_base_s *iter,
+        cx_tree_search_func sfunc,
+        cx_tree_node_create_fun cfunc,
+        void **root,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+) {
+    // TODO: implement
+    return 0;
+}
+
+size_t cx_tree_add_array(
+        void const *src,
+        size_t num,
+        size_t elem_size,
+        cx_tree_search_func sfunc,
+        cx_tree_node_create_fun cfunc,
+        void **root,
+        ptrdiff_t loc_parent,
+        ptrdiff_t loc_children,
+        ptrdiff_t loc_prev,
+        ptrdiff_t loc_next
+) {
+    // super special case: zero elements
+    if (num == 0) {
+        return 0;
+    }
+
+    // special case: one element does not need an iterator
+    if (num == 1) {
+        if (NULL != cx_tree_add(
+                src, sfunc, cfunc, root,
+                loc_parent, loc_children, loc_prev, loc_next)) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+
+    // otherwise, create iterator and hand over to other function
+    CxIterator iter = cxIterator(src, elem_size, num);
+    return cx_tree_add_iter(cxIteratorRef(iter), sfunc, cfunc, root,
+                            loc_parent, loc_children, loc_prev, loc_next);
+}
+

mercurial