Tue, 30 Dec 2025 21:48:07 +0100
remove the old UAP logo
|
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 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
127 | 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
|
128 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
129 | 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
|
130 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
131 | void cxTreeSetParent(CxTree *tree, void *parent, void *child); |
|
1275
0f21abc52241
complete tree documentation
Mike Becker <universe@uap-core.de>
parents:
1273
diff
changeset
|
132 | ``` |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
133 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
134 | 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
|
135 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
136 | 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
|
137 | 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
|
138 | 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
|
139 | 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
|
140 | The difference between `cxTreeSetParent()` and `cxTreeAddNode()` is, |
|
1275
0f21abc52241
complete tree documentation
Mike Becker <universe@uap-core.de>
parents:
1273
diff
changeset
|
141 | 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
|
142 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
143 | > 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
|
144 | > 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
|
145 | > |
|
0f21abc52241
complete tree documentation
Mike Becker <universe@uap-core.de>
parents:
1273
diff
changeset
|
146 | > 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
|
147 | |
|
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
148 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
149 | ## Size and Depth |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
150 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
151 | ```C |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
152 | #include <cx/tree.h> |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
153 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
154 | 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
|
155 | 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
|
156 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
157 | 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
|
158 | 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
|
159 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
160 | 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
|
161 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
162 | 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
|
163 | |
|
1295
b00c6ae1441a
add cxTreeSize() - resolves #624
Mike Becker <universe@uap-core.de>
parents:
1275
diff
changeset
|
164 | size_t cxTreeSize(CxTree *tree); |
|
b00c6ae1441a
add cxTreeSize() - resolves #624
Mike Becker <universe@uap-core.de>
parents:
1275
diff
changeset
|
165 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
166 | size_t cxTreeDepth(CxTree *tree); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
167 | ``` |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
168 | |
|
1255
a9d730c8b94a
add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1254
diff
changeset
|
169 | 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
|
170 | |
|
a9d730c8b94a
add some tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1254
diff
changeset
|
171 | 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
|
172 | 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
|
173 | |
|
1295
b00c6ae1441a
add cxTreeSize() - resolves #624
Mike Becker <universe@uap-core.de>
parents:
1275
diff
changeset
|
174 | 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
|
175 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
176 | 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
|
177 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
178 | > 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
|
179 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
180 | > 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
|
181 | > 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
|
182 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
183 | ## Search |
|
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 | ```C |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
186 | #include <cx/tree.h> |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
187 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
188 | #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
|
189 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
190 | 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
|
191 | const void *node, const void *data); |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
192 | |
|
1273
c35be6dc1667
document tree search functions
Mike Becker <universe@uap-core.de>
parents:
1272
diff
changeset
|
193 | 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
|
194 | 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
|
195 | 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
|
196 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
197 | 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
|
198 | cx_tree_search_func sfunc); |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
199 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
200 | 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
|
201 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
202 | 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
|
203 | 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
|
204 | ``` |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
205 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
206 | 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
|
207 | |
|
c35be6dc1667
document tree search functions
Mike Becker <universe@uap-core.de>
parents:
1272
diff
changeset
|
208 | 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
|
209 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
210 | 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
|
211 | 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
|
212 | 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
|
213 | 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
|
214 | 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
|
215 | |
|
c35be6dc1667
document tree search functions
Mike Becker <universe@uap-core.de>
parents:
1272
diff
changeset
|
216 | > 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
|
217 | > 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
|
218 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
219 | 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
|
220 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
221 | 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
|
222 | 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
|
223 | 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
|
224 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
225 | 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
|
226 | 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
|
227 | 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
|
228 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
229 | ## Iterator and Visitor |
|
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 | ```C |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
232 | #include <cx/tree.h> |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
233 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
234 | 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
|
235 | 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
|
236 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
237 | 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
|
238 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
239 | CxTreeIterator cxTreeIterateSubtree(CxTree *tree, |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
240 | void *node, bool visit_on_exit); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
241 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
242 | 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
|
243 | 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
|
244 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
245 | CxTreeIterator cxTreeVisit(CxTree *tree); |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
246 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
247 | 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
|
248 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
249 | #define cxTreeIteratorContinue(iter) |
|
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
250 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
251 | void cxTreeIteratorDispose(CxTreeIterator *iter); |
|
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 | |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
254 | 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
|
255 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
256 | 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
|
257 | 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
|
258 | 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
|
259 | 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
|
260 | 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
|
261 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
262 | 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
|
263 | |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
264 | 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
|
265 | |
|
1424
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1295
diff
changeset
|
266 | 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
|
267 | 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
|
268 | 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
|
269 | |
|
1272
d016fe8a647c
refine parts of the tree documentation
Mike Becker <universe@uap-core.de>
parents:
1271
diff
changeset
|
270 | > 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
|
271 | > 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
|
272 | >{style="note"} |
|
e9c0ad327684
add iterator / visitor docu
Mike Becker <universe@uap-core.de>
parents:
1266
diff
changeset
|
273 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
274 | 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
|
275 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
276 | > 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
|
277 | > 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
|
278 | > 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
|
279 | > 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
|
280 | > |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
281 | >{style="warning"} |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
282 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
283 | ## Remove |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
284 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
285 | ```C |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
286 | #include <cx/tree.h> |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
287 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
288 | 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
|
289 | 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
|
290 | |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
291 | 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
|
292 | 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
|
293 | 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
|
294 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
295 | int cxTreeRemoveNode(CxTree *tree, void *node, |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
296 | cx_tree_relink_func relink_func); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
297 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
298 | void cxTreeRemoveSubtree(CxTree *tree, void *node); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
299 | ``` |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
300 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
301 | 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
|
302 | 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
|
303 | The high-level counterpart is `cxTreeRemoveSubtree()`. |
|
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
304 | |
|
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
305 | 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
|
306 | 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
|
307 | 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
|
308 | 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
|
309 | 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
|
310 | |
|
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
311 | > 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
|
312 | > 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
|
313 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
314 | ## Dispose |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
315 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
316 | ```C |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
317 | #include <cx/tree.h> |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
318 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
319 | 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
|
320 | 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
|
321 | |
|
1252
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
322 | int cxTreeDestroyNode(CxTree *tree, void *node, |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
323 | cx_tree_relink_func relink_func); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
324 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
325 | void cxTreeDestroySubtree(CxTree *tree, void *node); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
326 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
327 | void cxTreeClear(CxTree *tree); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
328 | |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
329 | void cxTreeFree(CxTree *tree); |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
330 | ``` |
|
14c227b28a96
basic structure for tree.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
331 | |
|
1265
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
332 | 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
|
333 | 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
|
334 | 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
|
335 | 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
|
336 | 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
|
337 | |
|
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
338 | 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
|
339 | 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
|
340 | |
|
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
341 | 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
|
342 | |
|
07d67421092a
document remove and dispose for tree.h
Mike Becker <universe@uap-core.de>
parents:
1255
diff
changeset
|
343 | 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
|
344 | |
|
1681
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
345 | > 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
|
346 | > 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
|
347 | > |
|
56e76fbac167
rework of the entire tree API - resolves #772
Mike Becker <universe@uap-core.de>
parents:
1605
diff
changeset
|
348 | > 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
|
349 | > 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
|
350 | > {style="note"} |
|
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
351 | |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
352 | <seealso> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
353 | <category ref="apidoc"> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
354 | <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
|
355 | </category> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
356 | </seealso> |
|
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
357 |