diff -r 13068743197f -r 2672a2f79484 src/tree.c --- a/src/tree.c Mon Feb 19 22:08:09 2024 +0100 +++ b/src/tree.c Mon Feb 19 22:09:16 2024 +0100 @@ -178,9 +178,50 @@ static void cx_tree_iter_next(void *it) { struct cx_tree_iterator_s *iter = it; + off_t const loc_next = iter->loc_next; + off_t const loc_children = iter->loc_children; + // TODO: support mutating iterator + // TODO: support visit_on_exit - // TODO: implement + void *children = tree_children(iter->node); + if (children == NULL) { + // search for the next node + void *current = iter->node; + do { + // check if there is a sibling + void *next = tree_next(current); + if (next == NULL) { + // no sibling, check again for parent node + --iter->depth; + if (iter->depth == 0) { + // there is no parent - we have iterated the entire tree + // invalidate the iterator and free the node stack + iter->node = NULL; + current = NULL; + iter->stack_capacity = 0; + free(iter->stack); + iter->stack = NULL; + } else { + // the parent node can be obtained from the top of stack + // this way we can avoid the loc_parent in the iterator + current = iter->stack[iter->depth - 1]; + } + } else { + // move to the sibling and break the loop + current = NULL; + iter->counter++; + iter->node = next; + // new top of stack is the sibling + iter->stack[iter->depth - 1] = next; + } + } while (current != NULL); + } else { + // node has children, push the first child onto the stack and enter it + cx_array_simple_add(iter->stack, children); + iter->node = children; + iter->counter++; + } } CxTreeIterator cx_tree_iterator(