| 109 Note, that a wrapped tree by default has no create or search functions assigned. |
109 Note, that a wrapped tree by default has no create or search functions assigned. |
| 110 Therefore, if you wish to use one of the functions below that needs those function pointers set, |
110 Therefore, if you wish to use one of the functions below that needs those function pointers set, |
| 111 you will have to set them manually by assigning to the respective fields in the `CxTree` structure. |
111 you will have to set them manually by assigning to the respective fields in the `CxTree` structure. |
| 112 |
112 |
| 113 ### Example for wrapping a libxml2 tree |
113 ### Example for wrapping a libxml2 tree |
| |
114 |
| |
115 In this example we wrap the XML tree of the commonly used libxml2 library. |
| 114 |
116 |
| 115 ```C |
117 ```C |
| 116 #include <libxml/tree.h> |
118 #include <libxml/tree.h> |
| 117 #include <cx/tree.h> |
119 #include <cx/tree.h> |
| 118 |
120 |
| 297 ``` |
299 ``` |
| 298 |
300 |
| 299 There are two different kind of iterators for trees. |
301 There are two different kind of iterators for trees. |
| 300 The `CxTreeIterator` is performing a depth-first iteration with the capability of visiting a node twice: |
302 The `CxTreeIterator` is performing a depth-first iteration with the capability of visiting a node twice: |
| 301 first when the iterator enters the node coming from its parent, and secondly when the iterator tracks back from its last child. |
303 first when the iterator enters the node coming from its parent, and secondly when the iterator tracks back from its last child. |
| |
304 This behavior is controlled via the `visit_on_exit` argument. |
| |
305 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. |
| |
306 The above [example](#example-for-wrapping-a-libxml2-tree) for iterating through an XML tree illustrates this. |
| |
307 |
| 302 On the other hand, the `CxTreeVisitor` performs a breadth-first iteration and visits every node only once. |
308 On the other hand, the `CxTreeVisitor` performs a breadth-first iteration and visits every node only once. |
| 303 |
309 |
| 304 Since tree iteration needs to keep track of a stack (depth-first) or queue (breadth-frist), internal memory is allocated. |
310 Since tree iteration needs to keep track of a stack (depth-first) or queue (breadth-frist), internal memory is allocated. |
| 305 This memory is _automatically_ disposed when the iteration _completes_. |
311 This memory is _automatically_ disposed when the iteration _completes_. |
| 306 If you break from the iteration early, you must call `cxTreeIteratorDispose()` or `cxTreeVisitorDispose()`, respectively, to deallocate the memory. |
312 If you break from the iteration early, you must call `cxTreeIteratorDispose()` or `cxTreeVisitorDispose()`, respectively, to deallocate the memory. |
| 307 |
313 |
| 308 > It is strongly recommended to always invoke the dispose functions, even when it seems not necessary. |
314 > It is recommended to always invoke the dispose functions, even when it seems not necessary. |
| 309 > In the best case it just does nothing, but calling them guarantees that no memory can be leaking. |
315 > In the best case it just does nothing, but calling them guarantees that no memory can be leaking, even when your code changes in the future. |
| 310 >{style="note"} |
316 >{style="note"} |
| 311 |
317 |
| |
318 The macros `cxTreeIteratorContinue()` and `cxTreeVisitorContinue()` equivalently instruct the iterator/visitor to skip the subtree below the currently inspected node. |
| 312 |
319 |
| 313 ## Remove |
320 ## Remove |
| 314 |
321 |
| 315 ```C |
322 ```C |
| 316 #include <cx/tree.h> |
323 #include <cx/tree.h> |