[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:06 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>
+_LIBCPP_HIDE_FROM_ABI void
+__tree_iterate_from_begin(_EndNodePtr __first, _EndNodePtr __last, _Func& __func, _Proj& __proj) {
----------------
ldionne wrote:

I think this function would provide a much cleaner interface if it took `__tree_iterator`s instead. That means you'll need to unwrap the underlying pointer in this function but I think that's acceptable complexity to allow the rest of the code to use the nice iterator-based interface.

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


More information about the libcxx-commits mailing list