From d0654a201bc2717f9208098264a7a31dc6350817 Mon Sep 17 00:00:00 2001 From: Blake Date: Sun, 15 Feb 2026 10:31:33 -0500 Subject: [PATCH] Specialized enable_borrowed_ranges for VectorwiseOp class range iteration libeigen/eigen!2127 Closes #2882 --- Eigen/src/Core/VectorwiseOp.h | 10 ++++++++++ test/CMakeLists.txt | 8 ++++++++ test/vectorwiseop.cpp | 12 ++++++------ test/vectorwiseop_ranges.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 test/vectorwiseop_ranges.cpp diff --git a/Eigen/src/Core/VectorwiseOp.h b/Eigen/src/Core/VectorwiseOp.h index 9e34d8c99..38b996409 100644 --- a/Eigen/src/Core/VectorwiseOp.h +++ b/Eigen/src/Core/VectorwiseOp.h @@ -732,4 +732,14 @@ EIGEN_DEVICE_FUNC inline typename DenseBase::RowwiseReturnType DenseBas } // end namespace Eigen +/* Enable use of VectorwiseOp types inside + * of std::ranges algorithms. */ +#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L +namespace std { +template +inline constexpr bool ranges::enable_borrowed_range> = + true; +} +#endif + #endif // EIGEN_PARTIAL_REDUX_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dc5f958f1..69e2302dd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -304,6 +304,13 @@ if(NOT EIGEN_TEST_NO_EXCEPTIONS) endif() ei_add_test(prec_inverse_4x4) ei_add_test(vectorwiseop) +if("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES) + if(MSVC) + ei_add_test(vectorwiseop_ranges "/std:c++20") + else() + ei_add_test(vectorwiseop_ranges "-std=c++20") + endif() +endif() ei_add_test(special_numbers) ei_add_test(rvalue_types) ei_add_test(dense_storage) @@ -497,6 +504,7 @@ if(EIGEN_TEST_SYCL) set(EIGEN_SYCL OFF) endif() + cmake_dependent_option(EIGEN_TEST_BUILD_DOCUMENTATION "Test building the doxygen documentation" OFF "EIGEN_BUILD_DOC" OFF) if(EIGEN_TEST_BUILD_DOCUMENTATION) add_dependencies(buildtests doc) diff --git a/test/vectorwiseop.cpp b/test/vectorwiseop.cpp index d037bb49b..35f5f665e 100644 --- a/test/vectorwiseop.cpp +++ b/test/vectorwiseop.cpp @@ -241,11 +241,11 @@ EIGEN_DECLARE_TEST(vectorwiseop) { CALL_SUBTEST_3(vectorwiseop_array(ArrayXXf(3, 4))); CALL_SUBTEST_4(vectorwiseop_matrix(Matrix4cf())); CALL_SUBTEST_5(vectorwiseop_matrix(Matrix4f())); - CALL_SUBTEST_5(vectorwiseop_matrix(Vector4f())); - CALL_SUBTEST_5(vectorwiseop_matrix(Matrix())); - CALL_SUBTEST_6(vectorwiseop_matrix( + CALL_SUBTEST_6(vectorwiseop_matrix(Vector4f())); + CALL_SUBTEST_7(vectorwiseop_matrix(Matrix())); + CALL_SUBTEST_8(vectorwiseop_matrix( MatrixXd(internal::random(1, EIGEN_TEST_MAX_SIZE), internal::random(1, EIGEN_TEST_MAX_SIZE)))); - CALL_SUBTEST_7(vectorwiseop_matrix(VectorXd(internal::random(1, EIGEN_TEST_MAX_SIZE)))); - CALL_SUBTEST_7(vectorwiseop_matrix(RowVectorXd(internal::random(1, EIGEN_TEST_MAX_SIZE)))); - CALL_SUBTEST_8(vectorwiseop_mixedscalar()); + CALL_SUBTEST_9(vectorwiseop_matrix(VectorXd(internal::random(1, EIGEN_TEST_MAX_SIZE)))); + CALL_SUBTEST_10(vectorwiseop_matrix(RowVectorXd(internal::random(1, EIGEN_TEST_MAX_SIZE)))); + CALL_SUBTEST_11(vectorwiseop_mixedscalar()); } diff --git a/test/vectorwiseop_ranges.cpp b/test/vectorwiseop_ranges.cpp new file mode 100644 index 000000000..903e19233 --- /dev/null +++ b/test/vectorwiseop_ranges.cpp @@ -0,0 +1,31 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2011 Benoit Jacob +// Copyright (C) 2015 Gael Guennebaud +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#define TEST_ENABLE_TEMPORARY_TRACKING + +#include "main.h" + +#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L +#include +#endif + +void vectorwiseop_use_in_std_ranges() { + // verify basic std::ranges functionality; noop if ranges not present +#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L + Matrix3f a = Matrix3f::Random(); + int count = 0; + std::ranges::for_each(a.colwise(), [&count](auto&& col) { count += col.count(); }); + VERIFY_IS_EQUAL(count, 9); + std::ranges::for_each(a.rowwise(), [&count](auto&& row) { count += row.count(); }); + VERIFY_IS_EQUAL(count, 18); +#endif +} + +EIGEN_DECLARE_TEST(vectorwiseop_ranges) { CALL_SUBTEST_1(vectorwiseop_use_in_std_ranges()); }