src/ucx/cx/tree.h

changeset 582
82b60a8dd55c
parent 579
e10457d74fe1
child 621
956c03c25edd
equal deleted inserted replaced
581:4a049e416021 582:82b60a8dd55c
118 * Internal stack size. 118 * Internal stack size.
119 */ 119 */
120 size_t stack_size; 120 size_t stack_size;
121 /** 121 /**
122 * The current depth in the tree. 122 * The current depth in the tree.
123 * The node with which the iteration starts has depth 1.
123 */ 124 */
124 size_t depth; 125 size_t depth;
125 }; 126 };
126 } CxTreeIterator; 127 } CxTreeIterator;
127 128
133 * The tree node to visit. 134 * The tree node to visit.
134 */ 135 */
135 void *node; 136 void *node;
136 /** 137 /**
137 * The depth of the node. 138 * The depth of the node.
139 * The first visited node has depth 1.
138 */ 140 */
139 size_t depth; 141 size_t depth;
140 /** 142 /**
141 * The next element in the queue or @c NULL. 143 * The next element in the queue or @c NULL.
142 */ 144 */
209 * Releases internal memory of the given tree iterator. 211 * Releases internal memory of the given tree iterator.
210 * @param iter the iterator 212 * @param iter the iterator
211 */ 213 */
212 cx_attr_nonnull 214 cx_attr_nonnull
213 static inline void cxTreeIteratorDispose(CxTreeIterator *iter) { 215 static inline void cxTreeIteratorDispose(CxTreeIterator *iter) {
214 free(iter->stack); 216 cxFreeDefault(iter->stack);
215 iter->stack = NULL; 217 iter->stack = NULL;
216 } 218 }
217 219
218 /** 220 /**
219 * Releases internal memory of the given tree visitor. 221 * Releases internal memory of the given tree visitor.
222 cx_attr_nonnull 224 cx_attr_nonnull
223 static inline void cxTreeVisitorDispose(CxTreeVisitor *visitor) { 225 static inline void cxTreeVisitorDispose(CxTreeVisitor *visitor) {
224 struct cx_tree_visitor_queue_s *q = visitor->queue_next; 226 struct cx_tree_visitor_queue_s *q = visitor->queue_next;
225 while (q != NULL) { 227 while (q != NULL) {
226 struct cx_tree_visitor_queue_s *next = q->next; 228 struct cx_tree_visitor_queue_s *next = q->next;
227 free(q); 229 cxFreeDefault(q);
228 q = next; 230 q = next;
229 } 231 }
230 } 232 }
231 233
232 /** 234 /**
439 441
440 /** 442 /**
441 * Creates a depth-first iterator for a tree with the specified root node. 443 * Creates a depth-first iterator for a tree with the specified root node.
442 * 444 *
443 * @note A tree iterator needs to maintain a stack of visited nodes, which is 445 * @note A tree iterator needs to maintain a stack of visited nodes, which is
444 * allocated using stdlib malloc(). 446 * allocated using the cxDefaultAllocator.
445 * When the iterator becomes invalid, this memory is automatically released. 447 * When the iterator becomes invalid, this memory is automatically released.
446 * However, if you wish to cancel the iteration before the iterator becomes 448 * However, if you wish to cancel the iteration before the iterator becomes
447 * invalid by itself, you MUST call cxTreeIteratorDispose() manually to release 449 * invalid by itself, you MUST call cxTreeIteratorDispose() manually to release
448 * the memory. 450 * the memory.
449 * 451 *
467 ); 469 );
468 470
469 /** 471 /**
470 * Creates a breadth-first iterator for a tree with the specified root node. 472 * Creates a breadth-first iterator for a tree with the specified root node.
471 * 473 *
472 * @note A tree visitor needs to maintain a queue of to be visited nodes, which 474 * @note A tree visitor needs to maintain a queue of to-be visited nodes, which
473 * is allocated using stdlib malloc(). 475 * is allocated using the cxDefaultAllocator.
474 * When the visitor becomes invalid, this memory is automatically released. 476 * When the visitor becomes invalid, this memory is automatically released.
475 * However, if you wish to cancel the iteration before the visitor becomes 477 * However, if you wish to cancel the iteration before the visitor becomes
476 * invalid by itself, you MUST call cxTreeVisitorDispose() manually to release 478 * invalid by itself, you MUST call cxTreeVisitorDispose() manually to release
477 * the memory. 479 * the memory.
478 * 480 *
954 * 956 *
955 * @note This function will also register an advanced destructor which 957 * @note This function will also register an advanced destructor which
956 * will free the nodes with the allocator's free() method. 958 * will free the nodes with the allocator's free() method.
957 * 959 *
958 * @param allocator the allocator that shall be used 960 * @param allocator the allocator that shall be used
959 * (if @c NULL, a default stdlib allocator will be used) 961 * (if @c NULL, the cxDefaultAllocator will be used)
960 * @param create_func a function that creates new nodes 962 * @param create_func a function that creates new nodes
961 * @param search_func a function that compares two nodes 963 * @param search_func a function that compares two nodes
962 * @param search_data_func a function that compares a node with data 964 * @param search_data_func a function that compares a node with data
963 * @param loc_parent offset in the node struct for the parent pointer 965 * @param loc_parent offset in the node struct for the parent pointer
964 * @param loc_children offset in the node struct for the children linked list 966 * @param loc_children offset in the node struct for the children linked list
1018 * where neither the create function, the search function, nor a destructor 1020 * where neither the create function, the search function, nor a destructor
1019 * will be set. If you wish to use any of this functionality for the wrapped 1021 * will be set. If you wish to use any of this functionality for the wrapped
1020 * tree, you need to specify those functions afterwards. 1022 * tree, you need to specify those functions afterwards.
1021 * 1023 *
1022 * @param allocator the allocator that was used for nodes of the wrapped tree 1024 * @param allocator the allocator that was used for nodes of the wrapped tree
1023 * (if @c NULL, a default stdlib allocator is assumed) 1025 * (if @c NULL, the cxDefaultAllocator is assumed)
1024 * @param root the root node of the tree that shall be wrapped 1026 * @param root the root node of the tree that shall be wrapped
1025 * @param loc_parent offset in the node struct for the parent pointer 1027 * @param loc_parent offset in the node struct for the parent pointer
1026 * @param loc_children offset in the node struct for the children linked list 1028 * @param loc_children offset in the node struct for the children linked list
1027 * @param loc_last_child optional offset in the node struct for the pointer to 1029 * @param loc_last_child optional offset in the node struct for the pointer to
1028 * the last child in the linked list (negative if there is no such pointer) 1030 * the last child in the linked list (negative if there is no such pointer)
1186 cx_attr_nodiscard 1188 cx_attr_nodiscard
1187 cx_attr_export 1189 cx_attr_export
1188 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root); 1190 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root);
1189 1191
1190 /** 1192 /**
1193 * Determines the size of the entire tree.
1194 *
1195 * @param tree the tree
1196 * @return the tree size, counting the root as one
1197 */
1198 cx_attr_nonnull
1199 cx_attr_nodiscard
1200 static inline size_t cxTreeSize(CxTree *tree) {
1201 return tree->size;
1202 }
1203
1204 /**
1191 * Determines the depth of the entire tree. 1205 * Determines the depth of the entire tree.
1192 * 1206 *
1193 * @param tree the tree 1207 * @param tree the tree
1194 * @return the tree depth, counting the root as one 1208 * @return the tree depth, counting the root as one
1195 */ 1209 */

mercurial