Sun, 06 Oct 2024 19:02:32 +0200
add functions to start iteration in a subtree
src/cx/tree.h | file | annotate | diff | comparison | revisions |
--- a/src/cx/tree.h Sun Oct 06 13:48:00 2024 +0200 +++ b/src/cx/tree.h Sun Oct 06 19:02:32 2024 +0200 @@ -1087,23 +1087,61 @@ size_t cxTreeDepth(CxTree *tree); /** + * Creates a depth-first iterator for the specified tree starting in \p node. + * + * If the node is not part of the tree, the behavior is undefined. + * + * @param tree the tree to iterate + * @param node the node where to start + * @param visit_on_exit true, if the iterator shall visit a node again when + * leaving the sub-tree + * @return a tree iterator (depth-first) + * @see cxTreeVisit() + */ +__attribute__((__nonnull__, __warn_unused_result__)) +static inline CxTreeIterator cxTreeIterateSubtree( + CxTree *tree, + void *node, + bool visit_on_exit +) { + return cx_tree_iterator( + node, visit_on_exit, + tree->loc_children, tree->loc_next + ); +} + +/** + * Creates a breadth-first iterator for the specified tree starting in \p node. + * + * If the node is not part of the tree, the behavior is undefined. + * + * @param tree the tree to iterate + * @param node the node where to start + * @return a tree visitor (a.k.a. breadth-first iterator) + * @see cxTreeIterate() + */ +__attribute__((__nonnull__, __warn_unused_result__)) +static inline CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node) { + return cx_tree_visitor( + node, tree->loc_children, tree->loc_next + ); +} + +/** * Creates a depth-first iterator for the specified tree. * * @param tree the tree to iterate * @param visit_on_exit true, if the iterator shall visit a node again when * leaving the sub-tree * @return a tree iterator (depth-first) - * @see cxTreeVisitor() + * @see cxTreeVisit() */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxTreeIterator cxTreeIterator( +static inline CxTreeIterator cxTreeIterate( CxTree *tree, bool visit_on_exit ) { - return cx_tree_iterator( - tree->root, visit_on_exit, - tree->loc_children, tree->loc_next - ); + return cxTreeIterateSubtree(tree, tree->root, visit_on_exit); } /** @@ -1111,13 +1149,11 @@ * * @param tree the tree to iterate * @return a tree visitor (a.k.a. breadth-first iterator) - * @see cxTreeIterator() + * @see cxTreeIterate() */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxTreeVisitor cxTreeVisitor(CxTree *tree) { - return cx_tree_visitor( - tree->root, tree->loc_children, tree->loc_next - ); +static inline CxTreeVisitor cxTreeVisit(CxTree *tree) { + return cxTreeVisitSubtree(tree, tree->root); } /**