Thu, 30 Oct 2025 19:27:18 +0100
fix typo bug in cxListDifference() - resolves #745
| 1143 
0559812df10c
assign proper names to the documentation topics
 Mike Becker <universe@uap-core.de> parents: 
1142diff
changeset | 1 | # Tree | 
| 1142 
9437530176bc
add symbols that need documentation as TODOs
 Mike Becker <universe@uap-core.de> parents: 
1141diff
changeset | 2 | |
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1295diff
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: 
1252diff
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: 
1252diff
changeset | 5 | |
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 6 | The following convenience macros allow you to declare and use simple tree structures. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 7 | |
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 8 | ```C | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 9 | #define CX_TREE_NODE_BASE(Type) | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 10 | |
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 11 | #define cx_tree_node_base_layout | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 12 | ``` | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 13 | |
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 14 | The `CX_TREE_NODE_BASE` macro declares four pointers of type `Type*`: | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 15 | `parent`, `children`, `last_child`, `prev`, and `next`, | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 16 | which must be placed as first member into your struct. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 17 | |
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1295diff
changeset | 18 | You can use it, for example, like this: | 
| 1254 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 19 | ```C | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 20 | typedef struct my_tree_node_s { | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 21 | CX_TREE_NODE_BASE(struct my_tree_node_s); | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 22 | int value; | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 23 | char *desc; | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 24 | } MyTreeNode; | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 25 | ``` | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 26 | |
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 27 | The macro `cx_tree_node_base_layout` expands to the offsets of the above-mentioned pointers. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 28 | It will become handy when calling the low-level tree functions which expect all offsets to be passed as arguments. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 29 | |
| 1266 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 30 | > In all functions, the `last_child` and `prev` pointers are completely optional. | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 31 | > 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: 
1265diff
changeset | 32 | > traversing all children from the first child. | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 33 | > 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: 
1265diff
changeset | 34 | > 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: 
1265diff
changeset | 35 | > and so is the `parent` pointer. | 
| 1265 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 36 | |
| 1254 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 37 | Before diving into the function definitions, there are four function pointer declarations you should know. | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 38 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 39 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 40 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 41 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 42 | typedef void *(*cx_tree_node_create_func)( | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 43 | const void *data, void *context); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 44 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 45 | typedef int (*cx_tree_search_data_func)( | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 46 | const void *node, const void *data); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 47 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 48 | typedef int (*cx_tree_search_func)( | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 49 | const void *node, const void *new_node); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 50 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 51 | typedef void (*cx_tree_relink_func)( | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 52 | void *node, const void *old_parent, const void *new_parent); | 
| 1254 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 53 | ``` | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 54 | |
| 1254 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 55 | A `cx_tree_node_create_func` takes a pointer to the `data` the new node shall contain, and a `context` (which might be an allocator, for example). | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 56 | It shall allocate and return the created node, or `NULL` when node creation is not possible. | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 57 | |
| 1254 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 58 | A `cx_tree_search_data_func` shall check if the `node` contains the `data`, or if a child node might contain the data. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 59 | It shall return zero, when `node` contains the `data`. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 60 | When a child node _might_ contain the data, the returned positive number shall indicate how close the match is. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 61 | It is _not_ relevant if the data can actually be found in the subtree - the positive number also indicates that the data could be inserted in that subtree. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 62 | Only when the entire subtree starting with `node` cannot contain the `data`, a negative number is supposed to be returned. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 63 | |
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 64 | A `cx_tree_search_func` behaves exactly the same, except that it does expect a pointer to the `data` but a pointer to a node structure. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 65 | In combination with the `cx_tree_node_create_func`, when inserting nodes with the high-level functions, a `new_node` is created first, | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 66 | and then an appropriate insertion point is searched with the `cx_tree_search_func`. | 
| 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 67 | |
| 1265 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 68 | A `cx_tree_relink_func` is called when an intermediate node was removed from the tree and it's children need to be detached from | 
| 1254 
6a342294499b
add intro text for tree.h doc
 Mike Becker <universe@uap-core.de> parents: 
1252diff
changeset | 69 | the removed `old_parent` and attached to a `new_parent`. | 
| 1265 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 70 | The function is called for every child of the removed node and can be used, for example, to update the contents of the node when needed. | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 71 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 72 | ## Create | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 73 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 74 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 75 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 76 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 77 | CxTree *cxTreeCreate(const CxAllocator *allocator, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 78 | cx_tree_node_create_func create_func, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 79 | cx_tree_search_func search_func, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 80 | cx_tree_search_data_func search_data_func, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 81 | ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 82 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 83 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 84 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 85 | CxTree *cxTreeCreateSimple(const CxAllocator *allocator, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 86 | cx_tree_node_create_func create_func, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 87 | cx_tree_search_func search_func, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 88 | cx_tree_search_data_func search_data_func); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 89 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 90 | CxTree *cxTreeCreateWrapped(const CxAllocator *allocator, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 91 | void *root, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 92 | ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 93 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 94 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 95 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 96 | |
| 1255 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 97 | The function `cxTreeCreate()` creates a `CxTree` structure | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 98 | where each node created by the `create_func` has the layout specified by the offset arguments. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 99 | The function `cxTreeCreateSimple()` is exactly the same, except that it assumes the `cx_tree_node_base_layout`. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 100 | |
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 101 | In both cases the tree will be created empty, that is with no nodes, not even a root node. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 102 | On the other hand, `cxTreeCreateWrapped()` creates a `CxTree` structure with the specified layout and `root` node | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 103 | where `root` may already be the root of a larger tree. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 104 | |
| 1266 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 105 | If your wrapped tree structure does not have a `last_child` or a `prev` pointer, | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 106 | you may specify a negative location to indicate the missing pointer. | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 107 | All other pointers are mandatory and a non-negative location is expected. | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 108 | |
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1295diff
changeset | 109 | Note that a wrapped tree by default has no create or search functions assigned. | 
| 1255 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 110 | Therefore, if you wish to use one of the functions below that needs those function pointers set, | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 111 | you will have to set them manually by assigning to the respective fields in the `CxTree` structure. | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 112 | |
| 1266 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 113 | ### Example for wrapping a libxml2 tree | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 114 | |
| 1272 
d016fe8a647c
refine parts of the tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1271diff
changeset | 115 | 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: 
1271diff
changeset | 116 | |
| 1266 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 117 | ```C | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 118 | #include <libxml/tree.h> | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 119 | #include <cx/tree.h> | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 120 | |
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 121 | CxTree *wrap_xml_tree(xmlNode *root) { | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 122 | return cxTreeCreateWrapped( | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 123 | cxDefaultAllocator, // or you can just write NULL | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 124 | root, | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 125 | offsetof(xmlNode, parent), | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 126 | offsetof(xmlNode, children), | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 127 | offsetof(xmlNode, last), | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 128 | offsetof(xmlNode, prev), | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 129 | offsetof(xmlNode, next) | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 130 | ); | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 131 | } | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 132 | ``` | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 133 | |
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 134 | You do not need to specify any function pointers or destructors in the `CxTree` structure, | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 135 | if you just want to use the resulting `CxTree` for [iterating](#iterator-and-visitor) over the nodes. | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 136 | |
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 137 | You can, for example, print the XML structure with the following code: | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 138 | |
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 139 | ```C | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 140 | // continued from above | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 141 | |
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 142 | void print_xml_structure(CxTree *tree) { | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 143 | // specify visit_on_exit argument = true | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 144 | // 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: 
1265diff
changeset | 145 | CxTreeIterator iter = cxTreeIterate(tree, true); | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 146 | |
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 147 | cx_foreach(xmlNode*, node, iter) { | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 148 | if (node->type == XML_ELEMENT_NODE) { | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 149 | // the exiting field from the iterator indicates | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 150 | // whether we are entring or leaving the subtree | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 151 | // of the current element | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 152 | printf("%s - %s\n", | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 153 | node->name, | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 154 | iter.exiting ? "start" : "end" | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 155 | ); | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 156 | } | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 157 | } | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 158 | cxTreeIteratorDispose(&iter); | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 159 | } | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 160 | ``` | 
| 
a34373b17e58
wrapping xml tree example
 Mike Becker <universe@uap-core.de> parents: 
1265diff
changeset | 161 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 162 | ## Add Nodes | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 163 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 164 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 165 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 166 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 167 | void cx_tree_link(void *parent, void *node, ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 168 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 169 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 170 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 171 | extern unsigned int cx_tree_add_look_around_depth; | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 172 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 173 | size_t cx_tree_add_iter(struct cx_iterator_base_s *iter, size_t n, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 174 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 175 | void *cdata, void **failed, void *root, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 176 | ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 177 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 178 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 179 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 180 | size_t cx_tree_add_array(const void *src, size_t n, size_t elem_size, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 181 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 182 | void *cdata, void **failed, void *root, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 183 | ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 184 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 185 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 186 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 187 | int cx_tree_add(const void *src, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 188 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 189 | void *cdata, void **cnode, void *root, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 190 | ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 191 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 192 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 1275 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 193 | ``` | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 194 | |
| 1275 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 195 | The low-level function `cx_tree_link()` adds a `node` to a new `parent`, unlinking it from its previous parent, if required. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 196 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 197 | The functions `cx_tree_add_array()` and `cx_tree_add_iter()` are equivalent, | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 198 | except that the former adds `n` items of size `elem_size` from the `src` array, | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 199 | and the latter adds _up to_ `n` items from the iterator `iter`. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 200 | For each element, the `cfun` is called with the element as first argument and the `cdata` context as second argument. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 201 | The `cfun` function is supposed to return a node for which then the `sfunc` is used to find the location where to insert the node. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 202 | If a node could not be added, because `sfunc` always returns a negative number, it is stored at the location where `failed` points to. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 203 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 204 | > The functions `cx_tree_add_array()` and `cx_tree_add_iter()` are optimized for adding multiple nodes to | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 205 | > the same subtree without starting the search all over from the root node. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 206 | > This behavior can be controlled with `cx_tree_add_look_around_depth`, which specifies for how many parent nodes | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 207 | > the algorithm backtracks in the current subtree before restarting the search from the root node. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 208 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 209 | The function `cx_tree_add()` is similar to the other add-functions, | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 210 | except that it does only add one single item to the tree and always stores the created node at the location where `cnode` points to. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 211 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 212 | > By design, the add-functions can only be used to add children to the tree. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 213 | > If you want to add a new root node, this is much easier by simply calling `cx_tree_link(newroot, oldroot, layout...)` . | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 214 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 215 | The following high-level functions can be used to add nodes to a `CxTree`. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 216 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 217 | ```C | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 218 | #include <cx/tree.h> | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 219 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 220 | int cxTreeAddChild(CxTree *tree, void *parent, const void *data); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 221 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 222 | void cxTreeAddChildNode(CxTree *tree, void *parent, void *child); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 223 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 224 | void cxTreeSetParent(CxTree *tree, void *parent, void *child); | 
| 1275 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 225 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 226 | int cxTreeInsert(CxTree *tree, const void *data); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 227 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 228 | size_t cxTreeInsertIter(CxTree *tree, CxIteratorBase *iter, size_t n); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 229 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 230 | size_t cxTreeInsertArray(CxTree *tree, const void *data, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 231 | size_t elem_size, size_t n); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 232 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 233 | |
| 1275 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 234 | The functions `cxTreeAddChild()`, `cxTreeAddChildNode()`, and `cxTreeSetParent()` are similar to `cx_tree_link()`. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 235 | The difference between `cxTreeAddChild()` and `cxTreeAddChildNode()` is, | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 236 | that `cxTreeAddChild()` uses the `cx_tree_node_create_func` of the `CxTree` to create the node first, before adding it. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 237 | The function returns non-zero in case the creation of the node fails. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 238 | The difference between `cxTreeSetParent()` and `cxTreeAddChildNode()` is, | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 239 | 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: 
1273diff
changeset | 240 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 241 | > Use `cxTreeAddChildNode()` to add _new_ nodes to the tree and `cxTreeSetParent()` | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 242 | > to relink a subtree of the `tree` with a new parent. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 243 | > | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 244 | > 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: 
1273diff
changeset | 245 | |
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 246 | The functions `cxTreeInsert()`, `cxTreeInsertIter()`, and `cxTreeInsertArray()` behave | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 247 | like `cx_tree_add()`, `cx_tree_add_iter()`, and `cx_tree_add_array()`. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 248 | As creation context `cdata` for the `cx_tree_node_create_func` a pointer to the `CxTree` is passed. | 
| 
0f21abc52241
complete tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1273diff
changeset | 249 | Instead of returning a `failed` node, like the low-level functions, the high-level functions automatically free the memory for the failed node. | 
| 1190 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 250 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 251 | ## Size and Depth | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 252 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 253 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 254 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 255 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 256 | size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 257 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 258 | size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 259 | |
| 1295 
b00c6ae1441a
add cxTreeSize() - resolves #624
 Mike Becker <universe@uap-core.de> parents: 
1275diff
changeset | 260 | size_t cxTreeSize(CxTree *tree); | 
| 
b00c6ae1441a
add cxTreeSize() - resolves #624
 Mike Becker <universe@uap-core.de> parents: 
1275diff
changeset | 261 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 262 | size_t cxTreeDepth(CxTree *tree); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 263 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 264 | |
| 1255 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 265 | 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: 
1254diff
changeset | 266 | |
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 267 | 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: 
1254diff
changeset | 268 | 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: 
1254diff
changeset | 269 | |
| 1295 
b00c6ae1441a
add cxTreeSize() - resolves #624
 Mike Becker <universe@uap-core.de> parents: 
1275diff
changeset | 270 | 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: 
1254diff
changeset | 271 | |
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 272 | > Passing a `NULL` pointer as `subtree_root` makes those functions return zero. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 273 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 274 | ## Search | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 275 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 276 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 277 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 278 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 279 | #define CX_TREE_SEARCH_INFINITE_DEPTH | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 280 | |
| 1273 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 281 | int cx_tree_search_data(const void *root, size_t max_depth, | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 282 | const void *data, cx_tree_search_data_func sfunc, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 283 | void **result, ptrdiff_t loc_children, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 284 | |
| 1273 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 285 | int cx_tree_search(const void *root, size_t max_depth, | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 286 | const void *node, cx_tree_search_func sfunc, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 287 | void **result, ptrdiff_t loc_children, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 288 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 289 | void *cxTreeFind(CxTree *tree, const void *data); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 290 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 291 | void *cxTreeFindInSubtree(CxTree *tree, const void *data, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 292 | void *subtree_root, size_t max_depth); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 293 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 294 | |
| 1273 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 295 | The functions `cx_tree_search_data()` and `cx_tree_search()` are equivalent, | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 296 | except that `cx_tree_search_data()` compares a currently investigated node against the `data` with the specified `cx_tree_search_data_func`, | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 297 | and `cx_tree_search()` compares against a `node` with the specified `cx_tree_search_func`. | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 298 | |
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 299 | Usually you the latter is rarely needed. | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 300 | It is used, for example, by `cx_tree_add()` to find the correct position where to add a freshly created node. | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 301 | |
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 302 | The search proceeds as follows, starting with the `root` node: | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 303 | |
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 304 | 1. The current node is compared with `data` / `node` using the `sfunc`. | 
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 305 | 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: 
1272diff
changeset | 306 | 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: 
1295diff
changeset | 307 | 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: 
1272diff
changeset | 308 | 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: 
1272diff
changeset | 309 | |
| 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 310 | > 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: 
1295diff
changeset | 311 | > 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. | 
| 1273 
c35be6dc1667
document tree search functions
 Mike Becker <universe@uap-core.de> parents: 
1272diff
changeset | 312 | > This is exactly how `cx_tree_add()` finds the best position to add a node to the tree. | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 313 | |
| 1255 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 314 | The function `cxTreeFind()` uses the `search_data_func` of the `CxTree` | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 315 | to find the `data` in the tree, and returns a pointer to the node when the data was found (or `NULL`, otherwise). | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 316 | |
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 317 | The function `cxTreeFindInSubtree()` is equivalent, except that it restricts the search to nodes | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 318 | in the subtree starting with (and including) `subtree_root`, and skipping all nodes below the `max_depth`. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 319 | Note, that the `max_depth` is specified in relation to the `subtree_root` and not in relation to the entire tree. | 
| 
a9d730c8b94a
add some tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1254diff
changeset | 320 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 321 | ## Iterator and Visitor | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 322 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 323 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 324 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 325 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 326 | CxTreeIterator cx_tree_iterator(void *root, bool visit_on_exit, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 327 | ptrdiff_t loc_children, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 328 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 329 | CxTreeIterator cxTreeIterate(CxTree *tree, bool visit_on_exit); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 330 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 331 | CxTreeIterator cxTreeIterateSubtree(CxTree *tree, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 332 | void *node, bool visit_on_exit); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 333 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 334 | #define cxTreeIteratorContinue(iter) | 
| 1142 
9437530176bc
add symbols that need documentation as TODOs
 Mike Becker <universe@uap-core.de> parents: 
1141diff
changeset | 335 | |
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 336 | void cxTreeIteratorDispose(CxTreeIterator *iter); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 337 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 338 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 339 | CxTreeVisitor cx_tree_visitor(void *root, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 340 | ptrdiff_t loc_children, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 341 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 342 | CxTreeVisitor cxTreeVisit(CxTree *tree); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 343 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 344 | CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node) | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 345 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 346 | #define cxTreeVisitorContinue(visitor) | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 347 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 348 | void cxTreeVisitorDispose(CxTreeVisitor *visitor); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 349 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 350 | |
| 1429 
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
 Mike Becker <universe@uap-core.de> parents: 
1424diff
changeset | 351 | 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: 
1424diff
changeset | 352 | |
| 1271 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 353 | The `CxTreeIterator` is performing a depth-first iteration with the capability of visiting a node twice: | 
| 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 354 | 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: 
1271diff
changeset | 355 | This behavior is controlled via the `visit_on_exit` argument. | 
| 
d016fe8a647c
refine parts of the tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1271diff
changeset | 356 | 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: 
1271diff
changeset | 357 | 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: 
1271diff
changeset | 358 | |
| 1271 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 359 | On the other hand, the `CxTreeVisitor` performs a breadth-first iteration and visits every node only once. | 
| 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 360 | |
| 1429 
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
 Mike Becker <universe@uap-core.de> parents: 
1424diff
changeset | 361 | 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: 
1424diff
changeset | 362 | |
| 1424 
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
 Mike Becker <universe@uap-core.de> parents: 
1295diff
changeset | 363 | 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: 
1295diff
changeset | 364 | This memory is _automatically_ disposed of when the iteration _completes_. | 
| 1271 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 365 | If you break from the iteration early, you must call `cxTreeIteratorDispose()` or `cxTreeVisitorDispose()`, respectively, to deallocate the memory. | 
| 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 366 | |
| 1272 
d016fe8a647c
refine parts of the tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1271diff
changeset | 367 | > 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: 
1295diff
changeset | 368 | > 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: 
1266diff
changeset | 369 | >{style="note"} | 
| 
e9c0ad327684
add iterator / visitor docu
 Mike Becker <universe@uap-core.de> parents: 
1266diff
changeset | 370 | |
| 1272 
d016fe8a647c
refine parts of the tree documentation
 Mike Becker <universe@uap-core.de> parents: 
1271diff
changeset | 371 | The macros `cxTreeIteratorContinue()` and `cxTreeVisitorContinue()` equivalently instruct the iterator/visitor to skip the subtree below the currently inspected node. | 
| 1252 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 372 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 373 | ## Remove | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 374 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 375 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 376 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 377 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 378 | void cx_tree_unlink(void *node, ptrdiff_t loc_parent, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 379 | ptrdiff_t loc_children, ptrdiff_t loc_last_child, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 380 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 381 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 382 | int cxTreeRemoveNode(CxTree *tree, void *node, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 383 | cx_tree_relink_func relink_func); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 384 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 385 | void cxTreeRemoveSubtree(CxTree *tree, void *node); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 386 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 387 | |
| 1265 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 388 | The low-level function `cx_tree_unlink()` removes the specified `node`, | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 389 | 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: 
1255diff
changeset | 390 | The high-level counterpart is `cxTreeRemoveSubtree()`. | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 391 | |
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 392 | 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: 
1255diff
changeset | 393 | 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: 
1255diff
changeset | 394 | 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: 
1255diff
changeset | 395 | 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: 
1255diff
changeset | 396 | In all other cases, the function returns zero. | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 397 | |
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 398 | > 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: 
1255diff
changeset | 399 | > 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: 
1190diff
changeset | 400 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 401 | ## Dispose | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 402 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 403 | ```C | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 404 | #include <cx/tree.h> | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 405 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 406 | int cxTreeDestroyNode(CxTree *tree, void *node, | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 407 | cx_tree_relink_func relink_func); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 408 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 409 | void cxTreeDestroySubtree(CxTree *tree, void *node); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 410 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 411 | void cxTreeClear(CxTree *tree); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 412 | |
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 413 | void cxTreeFree(CxTree *tree); | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 414 | ``` | 
| 
14c227b28a96
basic structure for tree.h documentation
 Mike Becker <universe@uap-core.de> parents: 
1190diff
changeset | 415 | |
| 1265 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 416 | 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: 
1255diff
changeset | 417 | 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: 
1295diff
changeset | 418 | 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: 
1255diff
changeset | 419 | 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: 
1295diff
changeset | 420 | 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: 
1255diff
changeset | 421 | |
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 422 | 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: 
1295diff
changeset | 423 | 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: 
1255diff
changeset | 424 | |
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 425 | 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: 
1255diff
changeset | 426 | |
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 427 | 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: 
1255diff
changeset | 428 | |
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 429 | > Although `CxTree` supports the general concept of [destructor functions](collection.h.md#destructor-functions), | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 430 | > it is not based on `CX_COLLECTION_BASE`. | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 431 | > Therefore, the `cxDefineDestructor()` and `cxDefineAdvancedDestructor()` macros cannot be used on a `CxTree` and | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 432 | > the fields must be set manually. | 
| 
07d67421092a
document remove and dispose for tree.h
 Mike Becker <universe@uap-core.de> parents: 
1255diff
changeset | 433 | >{style="note"} | 
| 1190 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 434 | |
| 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 435 | <seealso> | 
| 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 436 | <category ref="apidoc"> | 
| 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 437 | <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: 
1143diff
changeset | 438 | </category> | 
| 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 439 | </seealso> | 
| 
a7b913d5d589
bring incomplete docs into a shape that can be released
 Mike Becker <universe@uap-core.de> parents: 
1143diff
changeset | 440 |