[libcxx-commits] [libcxx] [libc++] Optimize ranges::for_each for iterating over __trees (PR #164405)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 1 08:23:05 PST 2025


================
@@ -717,6 +718,63 @@ private:
   friend class __tree_const_iterator;
 };
 
+template <class _Reference, class _Break, class _NodePtr, class _Func, class _Proj>
+_LIBCPP_HIDE_FROM_ABI bool __tree_iterate_from_root(_Break __break, _NodePtr __root, _Func& __func, _Proj& __proj) {
+  if (__root->__left_) {
+    if (std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__left_), __func, __proj))
+      return true;
+  }
+  if (__break(__root))
+    return true;
+  __func(static_cast<_Reference>(__root->__get_value()));
+  if (__root->__right_)
+    return std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__right_), __func, __proj);
+  return false;
+}
+
+template <class _Reference, class _NodePtr, class _EndNodePtr, class _Func, class _Proj>
----------------
ldionne wrote:

I would suggest renaming this to `__tree_iterate_subrange` since that's basically what it does. `__from_begin` implies that it needs to be from the beginning of the tree, which isn't true IIUC.

Let's also add documentation for the function.

https://github.com/llvm/llvm-project/pull/164405


More information about the libcxx-commits mailing list