docs/Writerside/topics/tree.h.md

Wed, 31 Dec 2025 14:58:52 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Dec 2025 14:58:52 +0100
changeset 1691
5e608d0e5bd1
parent 1690
7d41291b3095
child 1694
a2757c6427cc
permissions
-rw-r--r--

merge logo change

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
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
3 UCX provides several low-level functions for working with arbitrary tree structures,
1254
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
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
6 The following convenience macro allows you to declare and use simple tree structures.
1254
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
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
9 #define CX_TREE_NODE(Type)
1254
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
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
12 It declares four pointers of type `Type*`:
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
13 `parent`, `children`, `last_child`, `prev`, and `next`.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
14 It can be placed anywhere in your node structure, but it is recommended
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
15 to use them as first members to avoid padding issues.
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
16
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
17 You can use it, for example, like this:
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
18 ```C
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
19 typedef struct my_tree_node_s {
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
20 CX_TREE_NODE(struct my_tree_node_s);
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
21 int value;
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
22 char *desc;
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
23 } MyTreeNode;
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
24 ```
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
25
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
26 The macro `cx_tree_node_layout(name)` expands to the offsets of the above-mentioned pointers.
1254
6a342294499b add intro text for tree.h doc
Mike Becker <universe@uap-core.de>
parents: 1252
diff changeset
27 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
28
1266
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
29 > In all functions, the `last_child` and `prev` pointers are completely optional.
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
30 > If your tree structure does not contain the `last_child` pointer, the last child will be determined by
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
31 > traversing all children from the first child.
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
32 > The same happens when it does not have a `prev` pointer, and the left sibling of a child is needed.
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
33 > The `children` pointer (which points to the first child), and the `next` pointer are mandatory,
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
34 > and so is the `parent` pointer.
1265
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
35
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
36 ## Create
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 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
39 #include <cx/tree.h>
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 CxTree *cxTreeCreate(const CxAllocator *allocator,
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
42 size_t node_size, size_t elem_size,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
43 void *root, ptrdiff_t loc_data,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
44 ptrdiff_t loc_parent, ptrdiff_t loc_children,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
45 ptrdiff_t loc_last_child,
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
46 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
47 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
48
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
49 The function `cxTreeCreate()` creates a `CxTree` structure with the specified layout.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
50 You can use the `cx_tree_node_layout()` macro to fill in the locations starting with `loc_parent`.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
51
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
52 > The `node_size` is used by `cxTreeCreateRootData()` and `cxTreeAddData()` to allocate memory for new tree nodes.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
53 > The `elem_size` is used to copy the data into the new node.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
54 > See [below](#add-nodes) for details.
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
55
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
56 The `loc_data` location is optional.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
57 If specified, the functions `cxTreeCreateRootData()`, `cxTreeAddData()`, `cxTreeFind()`, and `cxTreeFindInSubtree()`
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
58 will resolve the location to copy or compare the data.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
59 A location of zero means that a pointer to the entire node is used
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
60 (in that case the `elem_size` MUST be a positive number and SHOULD equal the `node_size`).
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
61
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
62 If your tree structure does not have a `last_child` or a `prev` pointer,
1266
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
63 you may specify a negative location to indicate the missing pointer.
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
64 All other pointers are mandatory and a non-negative location is expected.
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
65
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
66 The `root` node is optional.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
67 If not specified, the tree is initialized empty and `cxFree` is registered as advanced destructor.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
68 If a `root` is specified, the initial tree size is calculated and stored in the collection's metadata.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
69 Also, no destructors are specified and the nodes will not be deallocated when destroying the tree to avoid
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
70 use-after-free bugs when the `root` is still in use elsewhere.
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
71
1266
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
72 ### Example for wrapping a libxml2 tree
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
73
1272
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
74 In this example we wrap the XML tree of the commonly used libxml2 library.
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
75
1266
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
76 ```C
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
77 #include <libxml/tree.h>
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
78 #include <cx/tree.h>
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
79
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
80 CxTree *wrap_xml_tree(xmlNode *root) {
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
81 return cxTreeCreate(
1266
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
82 cxDefaultAllocator, // or you can just write NULL
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
83 sizeof(xmlNode), sizeof(xmlNode), root, 0,
1266
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
84 offsetof(xmlNode, parent),
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
85 offsetof(xmlNode, children),
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
86 offsetof(xmlNode, last),
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
87 offsetof(xmlNode, prev),
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
88 offsetof(xmlNode, next)
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
89 );
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
90 }
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
91 ```
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
92
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
93 You can, for example, print the XML structure with the following code:
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
94
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
95 ```C
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
96 // continued from above
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
97
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
98 void print_xml_structure(CxTree *tree) {
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
99 // specify visit_on_exit argument = true
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
100 // so that we are visiting each node twice: on enter and on exit
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
101 CxTreeIterator iter = cxTreeIterate(tree, true);
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
102
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
103 cx_foreach(xmlNode*, node, iter) {
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
104 if (node->type == XML_ELEMENT_NODE) {
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
105 // the exiting field from the iterator indicates
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
106 // whether we are entring or leaving the subtree
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
107 // of the current element
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
108 printf("%s - %s\n",
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
109 node->name,
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
110 iter.exiting ? "start" : "end"
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
111 );
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
112 }
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
113 }
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
114 cxTreeIteratorDispose(&iter);
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
115 }
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
116 ```
a34373b17e58 wrapping xml tree example
Mike Becker <universe@uap-core.de>
parents: 1265
diff changeset
117
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
118 ## Add Nodes
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
119
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
120 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
121 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
122
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
123 void cx_tree_add(void *parent, void *node, ptrdiff_t loc_parent,
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
124 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
125 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
126
1690
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
127 void *cxTreeCreateRootData(CxTree *tree, const void *data);
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
128
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
129 void *cxTreeCreateRoot(CxTree *tree, void *child);
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
130
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
131 void *cxTreeCreateNode(CxTree *tree, void *parent);
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
132
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
133 void *cxTreeAddData(CxTree *tree, void *parent, const void *data);
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
134
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
135 void cxTreeAddNode(CxTree *tree, void *parent, void *child);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
136
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
137 void cxTreeSetParent(CxTree *tree, void *parent, void *child);
1690
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
138
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
139 void *cxTreeSetRoot(CxTree *tree, void *root);
1275
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
140 ```
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
141
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
142 The low-level function `cx_tree_add()` adds a `node` to a new `parent`, unlinking it from its previous parent, if required.
1275
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
143
1690
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
144 When you have created a high-level tree that is still empty, you can create a root node with `cxTreeCreateRoot()` or `cxTreeCreateRootData()`.
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
145 Both functions return an existing root node, if present, and `NULL` when the allocation of a new node failed.
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
146
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
147 The functions `cxTreeAddData()`, `cxTreeAddNode()`, and `cxTreeSetParent()` are similar to `cx_tree_add()`.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
148 The difference between `cxTreeAddData()` and `cxTreeAddNode()` is,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
149 that `cxTreeAddData()` uses the allocator of the `CxTree` to create the node first, before adding it.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
150 The function returns `NULL` in case the creation of the node fails, or the created node when creation was successful.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
151 The difference between `cxTreeSetParent()` and `cxTreeAddNode()` is,
1275
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
152 that `cxTreeSetParent()` does not increase the tree size, when `child` already had a parent.
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
153
1690
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
154 When you want to use `cxTreeCreateRootData()` or `cxTreeAddData()`,
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
155 the `loc_data` parameter of `cxTreeCreate()` must have been set to a non-negative value.
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
156 Otherwise, the functions will return `NULL`.
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
157
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
158 > Use `cxTreeAddNode()` to add _new_ nodes to the tree and `cxTreeSetParent()`
1275
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
159 > to relink a subtree of the `tree` with a new parent.
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
160 >
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
161 > Using `cxTreeSetParent()` on a `child` which already has a parent in a _different_ tree is a mistake.
0f21abc52241 complete tree documentation
Mike Becker <universe@uap-core.de>
parents: 1273
diff changeset
162
1690
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
163 The function `cxTreeSetRoot()` returns the current root node of the tree,
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
164 or `NULL` when the tree is empty, and sets a new root node.
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
165 It is up to the caller to take care of a possibly necessary deallocation of the old tree nodes.
7d41291b3095 complete tree.c testing
Mike Becker <universe@uap-core.de>
parents: 1681
diff changeset
166 Also, keep in mind that the tree might have destructors registered which will be applied to the nodes belonging to the new root.
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
167
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168 ## Size and Depth
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
169
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
170 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
171 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
172
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
173 size_t cx_tree_size(void *root,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
174 ptrdiff_t loc_children, ptrdiff_t loc_next);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
175
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
176 size_t cx_tree_depth(void *root,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
177 ptrdiff_t loc_children, ptrdiff_t loc_next);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
178
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
179 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
180
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
181 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
182
1295
b00c6ae1441a add cxTreeSize() - resolves #624
Mike Becker <universe@uap-core.de>
parents: 1275
diff changeset
183 size_t cxTreeSize(CxTree *tree);
b00c6ae1441a add cxTreeSize() - resolves #624
Mike Becker <universe@uap-core.de>
parents: 1275
diff changeset
184
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
185 size_t cxTreeDepth(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
186 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
187
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
188 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
189
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
190 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
191 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
192
1295
b00c6ae1441a add cxTreeSize() - resolves #624
Mike Becker <universe@uap-core.de>
parents: 1275
diff changeset
193 The functions `cxTreeSize()` and `cxTreeDepth()` are equivalent to `cxTreeSubtreeSize()` and `cxTreeSubtreeDepth()`, respectively, where `subtree_root` is the root node of the entire tree.
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
194
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
195 The functions `cx_tree_size()` and `cx_tree_depth()` are the low-level counterparts.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
196
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
197 > Passing a `NULL` pointer as `root` or `subtree_root` makes those functions return zero.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
198
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
199 > Since the size of a `CxTree` is stored in the [collection](collection.h.md) metadata, `cxTreeSize()` is actually a constant time operation.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
200 > This is not the case for `cxTreeSubtreeSize()` or `cx_tree_size()` where the size has to be calculated by traversing the tree.
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
201
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
202 ## Search
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
203
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
204 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
205 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
206
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
207 #define CX_TREE_SEARCH_INFINITE_DEPTH 0
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
208
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
209 typedef int (*cx_tree_search_func)(
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
210 const void *node, const void *data);
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
211
1273
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
212 int cx_tree_search(const void *root, size_t max_depth,
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
213 const void *data, cx_tree_search_func sfunc, void **result,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
214 bool use_dfs, ptrdiff_t loc_children, ptrdiff_t loc_next);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
215
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
216 void *cxTreeFindFast(CxTree *tree, const void *data,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
217 cx_tree_search_func sfunc);
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
219 void *cxTreeFind(CxTree *tree, const void *data, bool use_dfs);
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
221 void *cxTreeFindInSubtree(CxTree *tree, const void *data,
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
222 void *subtree_root, size_t max_depth, bool use_dfs);
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
223 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
225 The function `cx_tree_search()` efficiently finds a node in the tree.
1273
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
226
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
227 The search proceeds as follows, starting with the `root` node:
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
228
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
229 1. The current node is compared with `data` using the `sfunc`.
1273
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
230 2. If `sfunc` returns zero, a matching node has been found and the pointer to that node is stored at the location given by `result`.
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
231 3. If `sfunc` returns a negative number, the entire subtree is skipped.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
232 4. If `sfunc` returns a positive number, the subtree is searched, unless the number is larger than the number of an already searched subtree. The best match will be stored at the location given by `result`.
1273
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
233 5. The return value will be the return value of the `sfunc` for node that was stored in the `result`, or a negative number when no match was found
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
234
c35be6dc1667 document tree search functions
Mike Becker <universe@uap-core.de>
parents: 1272
diff changeset
235 > If the `sfunc` implements some kind of metric, the search functions will return the best match in the tree with respect to that metric.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
236 > When a positive number is returned, it means that a new node with the searched data could be added as a child of the found node.
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
237
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
238 The function `cxTreeFindFast()` uses the `sfunc` to find the `data` in the tree, and returns a pointer to the node when the data was found (or `NULL`, otherwise).
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
239
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
240 The function `cxTreeFind()` instead traverses the entire tree (either with breadth-first search or depth-first search)
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
241 and compares the `data` with the collections [comparator function](collection.h.md#comparator-functions).
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
242 For this it is necessary that the `loc_data` was specified when creating the tree, or the function will immediately return `NULL`.
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
243
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
244 The functions `cxTreeFindFastInSubtree()` and `cxTreeFindInSubtree()` are similar, except that they restrict the search to nodes
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
245 in the subtree starting with (and including) `subtree_root`, and skipping all nodes below the `max_depth`.
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
246 Note, that the `max_depth` is specified in relation to the `subtree_root` and not in relation to the entire tree.
1255
a9d730c8b94a add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1254
diff changeset
247
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
248 ## Iterator and Visitor
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
249
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
250 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
251 #include <cx/tree.h>
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 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
254 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
255
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
256 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
257
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
258 CxTreeIterator cxTreeIterateSubtree(CxTree *tree,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
259 void *node, bool visit_on_exit);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
260
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
261 CxTreeIterator cx_tree_visitor(void *root,
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
262 ptrdiff_t loc_children, ptrdiff_t loc_next);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
263
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
264 CxTreeIterator cxTreeVisit(CxTree *tree);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
265
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
266 CxTreeIterator cxTreeVisitSubtree(CxTree *tree, void *node)
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
267
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
268 #define cxTreeIteratorContinue(iter)
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
269
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
270 void cxTreeIteratorDispose(CxTreeIterator *iter);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
271 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
272
1429
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
273 There are two different kinds of iterators for trees: one for depth-first iteration and one for breadth-first iteration.
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
274
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
275 An iterator created with the _iterate_ functions is performing a depth-first iteration with the capability of visiting a node twice:
1271
e9c0ad327684 add iterator / visitor docu
Mike Becker <universe@uap-core.de>
parents: 1266
diff changeset
276 first when the iterator enters the node coming from its parent, and secondly when the iterator tracks back from its last child.
1272
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
277 This behavior is controlled via the `visit_on_exit` argument.
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
278 When set to `true`, the iterators `exiting` flag can be checked during iteration to see whether the iterator is currently entering or leaving the node.
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
279 The above [example](#example-for-wrapping-a-libxml2-tree) for iterating through an XML tree illustrates this.
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
280
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
281 On the other hand, an iterator created with the _visit_ functions performs a breadth-first iteration and visits every node only once.
1271
e9c0ad327684 add iterator / visitor docu
Mike Becker <universe@uap-core.de>
parents: 1266
diff changeset
282
1429
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
283 Both iterators do not support the removal of nodes.
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
284
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
285 Since tree iteration needs to keep track of a stack (depth-first) or queue (breadth-first), internal memory is allocated.
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
286 This memory is _automatically_ disposed of when the iteration _completes_.
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
287 If you break from the iteration early, you must call `cxTreeIteratorDispose()` to deallocate the memory.
1271
e9c0ad327684 add iterator / visitor docu
Mike Becker <universe@uap-core.de>
parents: 1266
diff changeset
288
1272
d016fe8a647c refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents: 1271
diff changeset
289 > It is recommended to always invoke the dispose functions, even when it seems not necessary.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
290 > In the best case it just does nothing, but calling them guarantees that no memory can be leaking, even when your code will change in the future.
1271
e9c0ad327684 add iterator / visitor docu
Mike Becker <universe@uap-core.de>
parents: 1266
diff changeset
291 >{style="note"}
e9c0ad327684 add iterator / visitor docu
Mike Becker <universe@uap-core.de>
parents: 1266
diff changeset
292
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
293 The macro `cxTreeIteratorContinue()` instruct the iterator to skip the subtree below the currently inspected node.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
294
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
295 > In contrast to iterators over [lists](list.h.md#iterators) or [maps](map.h.md#iterators),
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
296 > a tree iterator is _always_ iterating over the nodes.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
297 > That means, the pointer in a `cx_foreach` loop is always expected to be a pointer to a node and not a pointer to
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
298 > the element / payload within the node.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
299 >
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
300 >{style="warning"}
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
301
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
302 ## Remove
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
303
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
304 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
305 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
306
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
307 typedef void (*cx_tree_relink_func)(
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
308 void *node, const void *old_parent, const void *new_parent);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
309
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
310 void cx_tree_remove(void *node, ptrdiff_t loc_parent,
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
311 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
312 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
313
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
314 int cxTreeRemoveNode(CxTree *tree, void *node,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
315 cx_tree_relink_func relink_func);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
316
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
317 void cxTreeRemoveSubtree(CxTree *tree, void *node);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
318 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
319
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
320 The low-level function `cx_tree_remove()` removes the specified `node`,
1265
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
321 as well as the entire subtree beneath that node, from its parent.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
322 The high-level counterpart is `cxTreeRemoveSubtree()`.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
323
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
324 The function `cxTreeRemoveNode()`, on the other hand, _only_ removes the `node` from the tree,
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
325 links all children of `node` to the former parent of `node`,
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
326 and calls the optional `relink_func` for every former child of `node` which will be relinked to a new parent.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
327 Therefore, calling `cxTreeRemoveNode()` on the `root` node is an error and the function returns non-zero.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
328 In all other cases, the function returns zero.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
329
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
330 > When your tree is storing a scene graph, for example, a possible use-case for the `relink_func` might be updating
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
331 > the world transform matrices in the subtree of the removed node.
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
332
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
333 ## Dispose
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
334
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
335 ```C
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
336 #include <cx/tree.h>
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
337
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
338 typedef void (*cx_tree_relink_func)(
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
339 void *node, const void *old_parent, const void *new_parent);
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
340
1252
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
341 int cxTreeDestroyNode(CxTree *tree, void *node,
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
342 cx_tree_relink_func relink_func);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
343
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
344 void cxTreeDestroySubtree(CxTree *tree, void *node);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
345
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
346 void cxTreeClear(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
347
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
348 void cxTreeFree(CxTree *tree);
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
349 ```
14c227b28a96 basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
350
1265
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
351 The function `cxTreeDestroyNode()` first [removes](#remove) the `node` from the tree, like `cxTreeRemoveNode()`,
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
352 and then invokes the [destructor functions](collection.h.md#destructor-functions).
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
353 It is guaranteed that the simple destructor is called before the advanced destructor.
1265
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
354 If no destructor function is registered at all, the behavior is identical to `cxTreeRemoveNode()`.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
355 That means this function does not deallocate the memory for the node on its own and leaves that entirely to the destructor functions.
1265
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
356
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
357 The function `cxTreeDestroySubtree()` performs the above actions for the entire subtree starting with `node`.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1295
diff changeset
358 The order in which the destructor functions for the nodes of the subtree are called is determined by a [tree iterator](#iterator-and-visitor).
1265
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
359
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
360 The function `cxTreeClear()` is a shorthand for invoking `cxTreeDestroySubtree()` on the root node of the tree.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
361
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
362 The function `cxTreeFree()` behaves like `cxTreeClear()` and then deallocates the memory for the `CxTree` structure.
07d67421092a document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents: 1255
diff changeset
363
1681
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
364 > In contrast to other collections, the destructor functions of a `CxTree` are _always_ invoked with pointers to the _nodes_
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
365 > and not to pointers to the elements / the payload of the nodes.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
366 >
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
367 > Also, the nodes are _not_ deallocated by the tree and _MUST_ be deallocated by the destructor functions.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
368 > When the tree was created by `cxTreeCreate()` without specifying an existing `root` node, `cxFree` is automatically used as the destructor function.
56e76fbac167 rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
369 > {style="note"}
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
370
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
371 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
372 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
373 <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
374 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
375 </seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
376

mercurial