docs/Writerside/topics/tree.h.md

Fri, 21 Mar 2025 19:47:38 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Mar 2025 19:47:38 +0100
changeset 1255
a9d730c8b94a
parent 1254
6a342294499b
child 1265
07d67421092a
permissions
-rw-r--r--

add some tree.h documentation

relates to #451

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Tree
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
2
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
3 UCX provides several low-level function for working with arbitrary tree structures,
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
4 as well as some high-level functions for trees that induce a certain order on the data they store.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
5
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
6 The following convenience macros allow you to declare and use simple tree structures.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
7
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
8 ```C
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
9 #define CX_TREE_NODE_BASE(Type)
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
10
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
11 #define cx_tree_node_base_layout
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
12 ```
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
13
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
14 The `CX_TREE_NODE_BASE` macro declares four pointers of type `Type*`:
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
15 `parent`, `children`, `last_child`, `prev`, and `next`,
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
16 which must be placed as first member into your struct.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
17
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
18 You can use it for example like this:
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
19 ```C
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
20 typedef struct my_tree_node_s {
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
21 CX_TREE_NODE_BASE(struct my_tree_node_s);
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
22 int value;
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
23 char *desc;
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
24 } MyTreeNode;
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
25 ```
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
26
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
27 The macro `cx_tree_node_base_layout` expands to the offsets of the above-mentioned pointers.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
28 It will become handy when calling the low-level tree functions which expect all offsets to be passed as arguments.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
29
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
30 Before diving into the function definitions, there are four function pointer declarations you should know.
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
31
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
32 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
33 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
34
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
35 typedef void *(*cx_tree_node_create_func)(
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
36 const void *data, void *context);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
37
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
38 typedef int (*cx_tree_search_data_func)(
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
39 const void *node, const void *data);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
40
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
41 typedef int (*cx_tree_search_func)(
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
42 const void *node, const void *new_node);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
43
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
44 typedef void (*cx_tree_relink_func)(
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
45 void *node, const void *old_parent, const void *new_parent);
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
46 ```
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
47
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
48 A `cx_tree_node_create_func` takes a pointer to the `data` the new node shall contain, and a `context` (which might be an allocator, for example).
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
49 It shall allocate and return the created node, or `NULL` when node creation is not possible.
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
50
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
51 A `cx_tree_search_data_func` shall check if the `node` contains the `data`, or if a child node might contain the data.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
52 It shall return zero, when `node` contains the `data`.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
53 When a child node _might_ contain the data, the returned positive number shall indicate how close the match is.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
54 It is _not_ relevant if the data can actually be found in the subtree - the positive number also indicates that the data could be inserted in that subtree.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
55 Only when the entire subtree starting with `node` cannot contain the `data`, a negative number is supposed to be returned.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
56
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
57 A `cx_tree_search_func` behaves exactly the same, except that it does expect a pointer to the `data` but a pointer to a node structure.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
58 In combination with the `cx_tree_node_create_func`, when inserting nodes with the high-level functions, a `new_node` is created first,
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
59 and then an appropriate insertion point is searched with the `cx_tree_search_func`.
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
60
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
61 A `cx_tree_relink_func` is used when an intermediate node is removed from the tree and it's children need to be detached from
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
62 the removed `old_parent` and attached to a `new_parent`.
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
63
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
64 ## Create
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
65
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
66 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
67 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
68
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
69 CxTree *cxTreeCreate(const CxAllocator *allocator,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
70 cx_tree_node_create_func create_func,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
71 cx_tree_search_func search_func,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
72 cx_tree_search_data_func search_data_func,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
73 ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
74 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
75 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
76
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
77 CxTree *cxTreeCreateSimple(const CxAllocator *allocator,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
78 cx_tree_node_create_func create_func,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
79 cx_tree_search_func search_func,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
80 cx_tree_search_data_func search_data_func);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
81
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
82 CxTree *cxTreeCreateWrapped(const CxAllocator *allocator,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
83 void *root,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
84 ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
85 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
86 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
87 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
88
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
89 The function `cxTreeCreate()` creates a `CxTree` structure
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
90 where each node created by the `create_func` has the layout specified by the offset arguments.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
91 The function `cxTreeCreateSimple()` is exactly the same, except that it assumes the `cx_tree_node_base_layout`.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
92
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
93 In both cases the tree will be created empty, that is with no nodes, not even a root node.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
94 On the other hand, `cxTreeCreateWrapped()` creates a `CxTree` structure with the specified layout and `root` node
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
95 where `root` may already be the root of a larger tree.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
96
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
97 Note, that a wrapped tree by default has no create or search functions assigned.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
98 Therefore, if you wish to use one of the functions below that needs those function pointers set,
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
99 you will have to set them manually by assigning to the respective fields in the `CxTree` structure.
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
100
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
101 ## Add Nodes
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
102
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
103 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
104 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
105
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
106 void cx_tree_link(void *parent, void *node, ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
107 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
108 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
109
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
110 extern unsigned int cx_tree_add_look_around_depth;
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
111
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
112 size_t cx_tree_add_iter(struct cx_iterator_base_s *iter, size_t n,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
113 cx_tree_search_func sfunc, cx_tree_node_create_func cfunc,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
114 void *cdata, void **failed, void *root,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
115 ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
116 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
117 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
118
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
119 size_t cx_tree_add_array(const void *src, size_t n, size_t elem_size,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
120 cx_tree_search_func sfunc, cx_tree_node_create_func cfunc,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
121 void *cdata, void **failed, void *root,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
122 ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
123 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
124 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
125
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
126 int cx_tree_add(const void *src,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
127 cx_tree_search_func sfunc, cx_tree_node_create_func cfunc,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
128 void *cdata, void **cnode, void *root,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
129 ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
130 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
131 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
132
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
133
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
134 int cxTreeAddChild(CxTree *tree, void *parent, const void *data);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
135
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
136 void cxTreeAddChildNode(CxTree *tree, void *parent, void *child);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
137
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
138 void cxTreeSetParent(CxTree *tree, void *parent, void *child);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
139
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
140 int cxTreeInsert(CxTree *tree, const void *data);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
141
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
142 size_t cxTreeInsertIter(CxTree *tree, CxIteratorBase *iter, size_t n);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
143
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
144 size_t cxTreeInsertArray(CxTree *tree, const void *data,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
145 size_t elem_size, size_t n);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
146 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
147
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
148 <warning>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
149 TODO: document
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
150 </warning>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
151
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
152 ## Size and Depth
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
153
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
154 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
155 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
156
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
157 size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
158
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
159 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
160
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
161 size_t cxTreeDepth(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
162 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
163
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
164 The function `cxTreeSubtreeSize()` counts all nodes belonging to the subtree starting with `subtree_root`.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
165
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
166 The function `cxTreeSubtreeDepth()` reports the maximum depth of the subtree starting with `subtree_root`.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
167 If the `subtree_root` does not have any children, this results in a depth of one.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
168
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
169 The function `cxTreeDepth()` is equivalent to `cxTreeSubtreeDepth()` where `subtree_root` is the root node of the entire tree.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
170
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
171 > Passing a `NULL` pointer as `subtree_root` makes those functions return zero.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
172
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
173 > In the current UCX version there is no separate function `cxTreeSize()`, because
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
174 > the size attribute can be directly accessed in the `CxTree` structure.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
175 > The next UCX version is planned to have also a function for accessing that attribute.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
176 >{style="note"}
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
177
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
178 ## Search
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
179
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
180 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
181 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
182
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
183 #define CX_TREE_SEARCH_INFINITE_DEPTH
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
184
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
185 int cx_tree_search_data(const void *root, size_t depth,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
186 const void *data, cx_tree_search_data_func sfunc,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
187 void **result, ptrdiff_t loc_children, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
188
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
189 int cx_tree_search(const void *root, size_t depth,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
190 const void *node, cx_tree_search_func sfunc,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
191 void **result, ptrdiff_t loc_children, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
192
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
193 void *cxTreeFind(CxTree *tree, const void *data);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
194
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
195 void *cxTreeFindInSubtree(CxTree *tree, const void *data,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
196 void *subtree_root, size_t max_depth);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
197 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
198
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
199 <warning>
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
200 TODO: document low level functions
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
201 </warning>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
202
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
203 The function `cxTreeFind()` uses the `search_data_func` of the `CxTree`
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
204 to find the `data` in the tree, and returns a pointer to the node when the data was found (or `NULL`, otherwise).
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
205
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
206 The function `cxTreeFindInSubtree()` is equivalent, except that it restricts the search to nodes
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
207 in the subtree starting with (and including) `subtree_root`, and skipping all nodes below the `max_depth`.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
208 Note, that the `max_depth` is specified in relation to the `subtree_root` and not in relation to the entire tree.
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
209
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
210 ## Iterator and Visitor
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
211
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
212 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
213 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
214
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
215 CxTreeIterator cx_tree_iterator(void *root, bool visit_on_exit,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
216 ptrdiff_t loc_children, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
217
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218 CxTreeIterator cxTreeIterate(CxTree *tree, bool visit_on_exit);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
219
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220 CxTreeIterator cxTreeIterateSubtree(CxTree *tree,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
221 void *node, bool visit_on_exit);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
222
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
223 #define cxTreeIteratorContinue(iter)
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
224
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
225 void cxTreeIteratorDispose(CxTreeIterator *iter);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228 CxTreeVisitor cx_tree_visitor(void *root,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229 ptrdiff_t loc_children, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
230
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
231 CxTreeVisitor cxTreeVisit(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233 CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node)
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
234
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
235 #define cxTreeVisitorContinue(visitor)
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
236
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
237 void cxTreeVisitorDispose(CxTreeVisitor *visitor);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
238 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
239
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
240 <warning>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
241 TODO: document
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
242 </warning>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
243
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
244 ## Remove
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
245
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
246 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
247 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
248
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
249 void cx_tree_unlink(void *node, ptrdiff_t loc_parent,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
250 ptrdiff_t loc_children, ptrdiff_t loc_last_child,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
251 ptrdiff_t loc_prev, ptrdiff_t loc_next);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
253 int cxTreeRemoveNode(CxTree *tree, void *node,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
254 cx_tree_relink_func relink_func);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
255
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
256 void cxTreeRemoveSubtree(CxTree *tree, void *node);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
257 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
258
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
259 <warning>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
260 TODO: document
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
261 </warning>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
262
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
263 ## Dispose
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
264
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
265 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
266 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
267
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
268 int cxTreeDestroyNode(CxTree *tree, void *node,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
269 cx_tree_relink_func relink_func);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
270
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
271 void cxTreeDestroySubtree(CxTree *tree, void *node);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
272
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
273 void cxTreeClear(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
274
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
275 void cxTreeFree(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
276 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
277
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
278 <warning>
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
279 TODO: document and mention the destructor support
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
280 </warning>
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
281
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
282 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
283 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
284 <a href="https://ucx.sourceforge.io/api/tree_8h.html">tree.h</a>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
285 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
286 </seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
287

mercurial