# HG changeset patch # User Mike Becker # Date 1728234152 -7200 # Node ID ff8ad2c9e1bd4a76065d7fa7d1c7ae8cb0401fc9 # Parent 526ed389c3d28bb30a2d42cb741fdc53480f5421 add functions to start iteration in a subtree diff -r 526ed389c3d2 -r ff8ad2c9e1bd src/cx/tree.h --- 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); } /**