diff --git a/repos/base/include/util/avl_tree.h b/repos/base/include/util/avl_tree.h index 39e6b37ca5..a2b2a191ac 100644 --- a/repos/base/include/util/avl_tree.h +++ b/repos/base/include/util/avl_tree.h @@ -161,6 +161,19 @@ struct Genode::Avl_node : Avl_node_base * \noapi */ void recompute() { } + + /** + * Apply a functor (read-only) to every node within this subtree + * + * \param functor function that takes a const NT reference + */ + template + void for_each(FUNC && functor) const + { + if (NT * l = child(Avl_node::LEFT)) l->for_each(functor); + functor(*static_cast(this)); + if (NT * r = child(Avl_node::RIGHT)) r->for_each(functor); + } }; @@ -209,6 +222,17 @@ class Genode::Avl_tree : Avl_node * \return first node, or nullptr if the tree is empty */ inline NT *first() const { return this->child(Avl_node::LEFT); } + + /** + * Apply a functor (read-only) to every node within the tree + * + * \param functor function that takes a const NT reference + * + * The iteration order corresponds to the order of the keys + */ + template + void for_each(FUNC && functor) const { + if (first()) first()->for_each(functor); } }; #endif /* _INCLUDE__UTIL__AVL_TREE_H_ */