From dcdb0233c1d4e15ec81f391a3339b0254a571e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20S=C3=A1nchez?= Date: Fri, 12 Apr 2024 17:05:20 +0000 Subject: [PATCH] Refactor indexed view to appease MSVC 14.16. --- Eigen/src/Core/util/IndexedViewHelper.h | 172 +++++++++++++++++ Eigen/src/plugins/IndexedViewMethods.inc | 224 +++-------------------- test/indexed_view.cpp | 4 +- 3 files changed, 200 insertions(+), 200 deletions(-) diff --git a/Eigen/src/Core/util/IndexedViewHelper.h b/Eigen/src/Core/util/IndexedViewHelper.h index c1870024a..59486ea50 100644 --- a/Eigen/src/Core/util/IndexedViewHelper.h +++ b/Eigen/src/Core/util/IndexedViewHelper.h @@ -308,6 +308,178 @@ struct IndexedViewHelper, void> { static Index incr(const Indices& indices) { return indices.incr(); } }; +// this helper class assumes internal::valid_indexed_view_overload::value == true +template +struct IndexedViewSelector; + +template +using IvcType = typename internal::IndexedViewHelperIndicesWrapper::type; + +template +inline IvcType CreateIndexSequence(size_t size, const Indices& indices) { + return internal::IndexedViewHelperIndicesWrapper::CreateIndexSequence(indices, size); +} + +// Generic +template +struct IndexedViewSelector, + IvcType>>::ReturnAsIndexedView>> { + using ReturnType = IndexedView, + IvcType>; + using ConstReturnType = IndexedView, + IvcType>; + + static inline ReturnType run(Derived& derived, const RowIndices& rowIndices, const ColIndices& colIndices) { + return ReturnType(derived, CreateIndexSequence(derived.rows(), rowIndices), + CreateIndexSequence(derived.cols(), colIndices)); + } + static inline ConstReturnType run(const Derived& derived, const RowIndices& rowIndices, + const ColIndices& colIndices) { + return ConstReturnType(derived, CreateIndexSequence(derived.rows(), rowIndices), + CreateIndexSequence(derived.cols(), colIndices)); + } +}; + +// Block +template +struct IndexedViewSelector< + Derived, RowIndices, ColIndices, + std::enable_if_t, + IvcType>>::ReturnAsBlock>> { + using ActualRowIndices = IvcType; + using ActualColIndices = IvcType; + using IndexedViewType = IndexedView; + using ConstIndexedViewType = IndexedView; + using ReturnType = typename internal::traits::BlockType; + using ConstReturnType = typename internal::traits::BlockType; + using RowHelper = internal::IndexedViewHelper; + using ColHelper = internal::IndexedViewHelper; + + static inline ReturnType run(Derived& derived, const RowIndices& rowIndices, const ColIndices& colIndices) { + auto actualRowIndices = CreateIndexSequence(derived.rows(), rowIndices); + auto actualColIndices = CreateIndexSequence(derived.cols(), colIndices); + return ReturnType(derived, RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices), + RowHelper::size(actualRowIndices), ColHelper::size(actualColIndices)); + } + static inline ConstReturnType run(const Derived& derived, const RowIndices& rowIndices, + const ColIndices& colIndices) { + auto actualRowIndices = CreateIndexSequence(derived.rows(), rowIndices); + auto actualColIndices = CreateIndexSequence(derived.cols(), colIndices); + return ConstReturnType(derived, RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices), + RowHelper::size(actualRowIndices), ColHelper::size(actualColIndices)); + } +}; + +// Scalar +template +struct IndexedViewSelector< + Derived, RowIndices, ColIndices, + std::enable_if_t, + IvcType>>::ReturnAsScalar>> { + using ReturnType = typename DenseBase::Scalar&; + using ConstReturnType = typename DenseBase::CoeffReturnType; + using ActualRowIndices = IvcType; + using ActualColIndices = IvcType; + using RowHelper = internal::IndexedViewHelper; + using ColHelper = internal::IndexedViewHelper; + static inline ReturnType run(Derived& derived, const RowIndices& rowIndices, const ColIndices& colIndices) { + auto actualRowIndices = CreateIndexSequence(derived.rows(), rowIndices); + auto actualColIndices = CreateIndexSequence(derived.cols(), colIndices); + return derived(RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices)); + } + static inline ConstReturnType run(const Derived& derived, const RowIndices& rowIndices, + const ColIndices& colIndices) { + auto actualRowIndices = CreateIndexSequence(derived.rows(), rowIndices); + auto actualColIndices = CreateIndexSequence(derived.cols(), colIndices); + return derived(RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices)); + } +}; + +// this helper class assumes internal::is_valid_index_type::value == false +template +struct VectorIndexedViewSelector; + +// Generic +template +struct VectorIndexedViewSelector< + Derived, Indices, + std::enable_if_t>::value && + internal::IndexedViewHelper>::IncrAtCompileTime != + 1>> { + static constexpr bool IsRowMajor = DenseBase::IsRowMajor; + using ZeroIndex = internal::SingleRange; + using RowMajorReturnType = IndexedView>; + using ConstRowMajorReturnType = IndexedView>; + + using ColMajorReturnType = IndexedView, ZeroIndex>; + using ConstColMajorReturnType = IndexedView, ZeroIndex>; + + using ReturnType = typename internal::conditional::type; + using ConstReturnType = + typename internal::conditional::type; + + template = true> + static inline RowMajorReturnType run(Derived& derived, const Indices& indices) { + return RowMajorReturnType(derived, ZeroIndex(0), + CreateIndexSequence(derived.cols(), indices)); + } + template = true> + static inline ConstRowMajorReturnType run(const Derived& derived, const Indices& indices) { + return ConstRowMajorReturnType(derived, ZeroIndex(0), + CreateIndexSequence(derived.cols(), indices)); + } + template = true> + static inline ColMajorReturnType run(Derived& derived, const Indices& indices) { + return ColMajorReturnType(derived, CreateIndexSequence(derived.rows(), indices), + ZeroIndex(0)); + } + template = true> + static inline ConstColMajorReturnType run(const Derived& derived, const Indices& indices) { + return ConstColMajorReturnType(derived, CreateIndexSequence(derived.rows(), indices), + ZeroIndex(0)); + } +}; + +// Block +template +struct VectorIndexedViewSelector< + Derived, Indices, + std::enable_if_t>::value && + internal::IndexedViewHelper>::IncrAtCompileTime == + 1>> { + using Helper = internal::IndexedViewHelper>; + using ReturnType = VectorBlock; + using ConstReturnType = VectorBlock; + static inline ReturnType run(Derived& derived, const Indices& indices) { + auto actualIndices = CreateIndexSequence(derived.size(), indices); + return ReturnType(derived, Helper::first(actualIndices), Helper::size(actualIndices)); + } + static inline ConstReturnType run(const Derived& derived, const Indices& indices) { + auto actualIndices = CreateIndexSequence(derived.size(), indices); + return ConstReturnType(derived, Helper::first(actualIndices), Helper::size(actualIndices)); + } +}; + +// Symbolic +template +struct VectorIndexedViewSelector< + Derived, Indices, + std::enable_if_t>::value>> { + using ReturnType = typename DenseBase::Scalar&; + using ConstReturnType = typename DenseBase::CoeffReturnType; + using Helper = internal::IndexedViewHelper>; + static inline ReturnType run(Derived& derived, const Indices& indices) { + auto actualIndices = CreateIndexSequence(derived.size(), indices); + return derived(Helper::first(actualIndices)); + } + static inline ConstReturnType run(const Derived& derived, const Indices& indices) { + auto actualIndices = CreateIndexSequence(derived.size(), indices); + return derived(Helper::first(actualIndices)); + } +}; + } // end namespace internal } // end namespace Eigen diff --git a/Eigen/src/plugins/IndexedViewMethods.inc b/Eigen/src/plugins/IndexedViewMethods.inc index c3df42971..a51e3492f 100644 --- a/Eigen/src/plugins/IndexedViewMethods.inc +++ b/Eigen/src/plugins/IndexedViewMethods.inc @@ -10,184 +10,6 @@ #if !defined(EIGEN_PARSED_BY_DOXYGEN) public: -// define some aliases to ease readability - -template -using IvcRowType = typename internal::IndexedViewHelperIndicesWrapper::type; - -template -using IvcColType = typename internal::IndexedViewHelperIndicesWrapper::type; - -template -using IvcSizeType = typename internal::IndexedViewHelperIndicesWrapper::type; - -template -inline IvcRowType ivcRow(const Indices& indices) const { - return internal::IndexedViewHelperIndicesWrapper::CreateIndexSequence(indices, - derived().rows()); -} - -template -inline IvcColType ivcCol(const Indices& indices) const { - return internal::IndexedViewHelperIndicesWrapper::CreateIndexSequence(indices, - derived().cols()); -} - -template -inline IvcSizeType ivcSize(const Indices& indices) const { - return internal::IndexedViewHelperIndicesWrapper::CreateIndexSequence(indices, - derived().size()); - ; -} - -// this helper class assumes internal::valid_indexed_view_overload::value == true -template -struct IndexedViewSelector; - -// Generic -template -struct IndexedViewSelector< - RowIndices, ColIndices, - std::enable_if_t< - internal::traits, IvcColType>>::ReturnAsIndexedView>> { - using ReturnType = IndexedView, IvcColType>; - using ConstReturnType = IndexedView, IvcColType>; - - static inline ReturnType run(Derived& derived, const RowIndices& rowIndices, const ColIndices& colIndices) { - return ReturnType(derived, derived.ivcRow(rowIndices), derived.ivcCol(colIndices)); - } - static inline ConstReturnType run(const Derived& derived, const RowIndices& rowIndices, - const ColIndices& colIndices) { - return ConstReturnType(derived, derived.ivcRow(rowIndices), derived.ivcCol(colIndices)); - } -}; - -// Block -template -struct IndexedViewSelector, IvcColType>>::ReturnAsBlock>> { - using ActualRowIndices = IvcRowType; - using ActualColIndices = IvcColType; - using IndexedViewType = IndexedView; - using ConstIndexedViewType = IndexedView; - using ReturnType = typename internal::traits::BlockType; - using ConstReturnType = typename internal::traits::BlockType; - using RowHelper = internal::IndexedViewHelper; - using ColHelper = internal::IndexedViewHelper; - - static inline ReturnType run(Derived& derived, const RowIndices& rowIndices, const ColIndices& colIndices) { - auto actualRowIndices = derived.ivcRow(rowIndices); - auto actualColIndices = derived.ivcCol(colIndices); - return ReturnType(derived, RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices), - RowHelper::size(actualRowIndices), ColHelper::size(actualColIndices)); - } - static inline ConstReturnType run(const Derived& derived, const RowIndices& rowIndices, - const ColIndices& colIndices) { - auto actualRowIndices = derived.ivcRow(rowIndices); - auto actualColIndices = derived.ivcCol(colIndices); - return ConstReturnType(derived, RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices), - RowHelper::size(actualRowIndices), ColHelper::size(actualColIndices)); - } -}; - -// Scalar -template -struct IndexedViewSelector, IvcColType>>::ReturnAsScalar>> { - using ReturnType = typename DenseBase::Scalar&; - using ConstReturnType = typename DenseBase::CoeffReturnType; - using ActualRowIndices = IvcRowType; - using ActualColIndices = IvcColType; - using RowHelper = internal::IndexedViewHelper; - using ColHelper = internal::IndexedViewHelper; - static inline ReturnType run(Derived& derived, const RowIndices& rowIndices, const ColIndices& colIndices) { - auto actualRowIndices = derived.ivcRow(rowIndices); - auto actualColIndices = derived.ivcCol(colIndices); - return derived(RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices)); - } - static inline ConstReturnType run(const Derived& derived, const RowIndices& rowIndices, - const ColIndices& colIndices) { - auto actualRowIndices = derived.ivcRow(rowIndices); - auto actualColIndices = derived.ivcCol(colIndices); - return derived(RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices)); - } -}; - -// this helper class assumes internal::is_valid_index_type::value == false -template -struct VectorIndexedViewSelector; - -// Generic -template -struct VectorIndexedViewSelector< - Indices, std::enable_if_t>::value && - internal::IndexedViewHelper>::IncrAtCompileTime != 1>> { - static constexpr bool IsRowMajor = DenseBase::IsRowMajor; - using ZeroIndex = internal::SingleRange; - using RowMajorReturnType = IndexedView>; - using ConstRowMajorReturnType = IndexedView>; - - using ColMajorReturnType = IndexedView, ZeroIndex>; - using ConstColMajorReturnType = IndexedView, ZeroIndex>; - - using ReturnType = typename internal::conditional::type; - using ConstReturnType = - typename internal::conditional::type; - - template = true> - static inline RowMajorReturnType run(Derived& derived, const Indices& indices) { - return RowMajorReturnType(derived, ZeroIndex(0), derived.ivcCol(indices)); - } - template = true> - static inline ConstRowMajorReturnType run(const Derived& derived, const Indices& indices) { - return ConstRowMajorReturnType(derived, ZeroIndex(0), derived.ivcCol(indices)); - } - template = true> - static inline ColMajorReturnType run(Derived& derived, const Indices& indices) { - return ColMajorReturnType(derived, derived.ivcRow(indices), ZeroIndex(0)); - } - template = true> - static inline ConstColMajorReturnType run(const Derived& derived, const Indices& indices) { - return ConstColMajorReturnType(derived, derived.ivcRow(indices), ZeroIndex(0)); - } -}; - -// Block -template -struct VectorIndexedViewSelector< - Indices, std::enable_if_t>::value && - internal::IndexedViewHelper>::IncrAtCompileTime == 1>> { - using Helper = internal::IndexedViewHelper>; - using ReturnType = VectorBlock; - using ConstReturnType = VectorBlock; - static inline ReturnType run(Derived& derived, const Indices& indices) { - auto actualIndices = derived.ivcSize(indices); - return ReturnType(derived, Helper::first(actualIndices), Helper::size(actualIndices)); - } - static inline ConstReturnType run(const Derived& derived, const Indices& indices) { - auto actualIndices = derived.ivcSize(indices); - return ConstReturnType(derived, Helper::first(actualIndices), Helper::size(actualIndices)); - } -}; - -// Symbolic -template -struct VectorIndexedViewSelector>::value>> { - using ReturnType = typename DenseBase::Scalar&; - using ConstReturnType = typename DenseBase::CoeffReturnType; - using Helper = internal::IndexedViewHelper>; - static inline ReturnType run(Derived& derived, const Indices& indices) { - auto actualIndices = derived.ivcSize(indices); - return derived(Helper::first(actualIndices)); - } - static inline ConstReturnType run(const Derived& derived, const Indices& indices) { - auto actualIndices = derived.ivcSize(indices); - return derived(Helper::first(actualIndices)); - } -}; - // SFINAE dummy types template @@ -210,24 +32,26 @@ public: // non-const versions -template -using IndexedViewType = typename IndexedViewSelector::ReturnType; + template + using IndexedViewType = typename internal::IndexedViewSelector::ReturnType; -template = true> -IndexedViewType operator()(const RowIndices& rowIndices, const ColIndices& colIndices) { - return IndexedViewSelector::run(derived(), rowIndices, colIndices); -} + template = true> + IndexedViewType operator()(const RowIndices& rowIndices, const ColIndices& colIndices) { + return internal::IndexedViewSelector::run(derived(), rowIndices, colIndices); + } template , EnableOverload = true> IndexedViewType operator()(const RowType (&rowIndices)[RowSize], const ColIndices& colIndices) { - return IndexedViewSelector::run(derived(), RowIndices{rowIndices}, colIndices); + return internal::IndexedViewSelector::run(derived(), RowIndices{rowIndices}, + colIndices); } template , EnableOverload = true> IndexedViewType operator()(const RowIndices& rowIndices, const ColType (&colIndices)[ColSize]) { - return IndexedViewSelector::run(derived(), rowIndices, ColIndices{colIndices}); + return internal::IndexedViewSelector::run(derived(), rowIndices, + ColIndices{colIndices}); } template = true> IndexedViewType operator()(const RowType (&rowIndices)[RowSize], const ColType (&colIndices)[ColSize]) { - return IndexedViewSelector::run(derived(), RowIndices{rowIndices}, ColIndices{colIndices}); + return internal::IndexedViewSelector::run(derived(), RowIndices{rowIndices}, + ColIndices{colIndices}); } // const versions template -using ConstIndexedViewType = typename IndexedViewSelector::ConstReturnType; +using ConstIndexedViewType = typename internal::IndexedViewSelector::ConstReturnType; template = true> ConstIndexedViewType operator()(const RowIndices& rowIndices, const ColIndices& colIndices) const { - return IndexedViewSelector::run(derived(), rowIndices, colIndices); + return internal::IndexedViewSelector::run(derived(), rowIndices, colIndices); } template , EnableConstOverload = true> ConstIndexedViewType operator()(const RowType (&rowIndices)[RowSize], const ColIndices& colIndices) const { - return IndexedViewSelector::run(derived(), RowIndices{rowIndices}, colIndices); + return internal::IndexedViewSelector::run(derived(), RowIndices{rowIndices}, + colIndices); } template , EnableConstOverload = true> ConstIndexedViewType operator()(const RowIndices& rowIndices, const ColType (&colIndices)[ColSize]) const { - return IndexedViewSelector::run(derived(), rowIndices, ColIndices{colIndices}); + return internal::IndexedViewSelector::run(derived(), rowIndices, + ColIndices{colIndices}); } template = true> ConstIndexedViewType operator()(const RowType (&rowIndices)[RowSize], const ColType (&colIndices)[ColSize]) const { - return IndexedViewSelector::run(derived(), RowIndices{rowIndices}, ColIndices{colIndices}); + return internal::IndexedViewSelector::run(derived(), RowIndices{rowIndices}, + ColIndices{colIndices}); } // Public API for 1D vectors/arrays @@ -276,37 +104,37 @@ ConstIndexedViewType operator()(const RowType (&rowIndic // non-const versions template -using VectorIndexedViewType = typename VectorIndexedViewSelector::ReturnType; +using VectorIndexedViewType = typename internal::VectorIndexedViewSelector::ReturnType; template = true> VectorIndexedViewType operator()(const Indices& indices) { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return VectorIndexedViewSelector::run(derived(), indices); + return internal::VectorIndexedViewSelector::run(derived(), indices); } template , EnableVectorOverload = true> VectorIndexedViewType operator()(const IndexType (&indices)[Size]) { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return VectorIndexedViewSelector::run(derived(), Indices{indices}); + return internal::VectorIndexedViewSelector::run(derived(), Indices{indices}); } // const versions template -using ConstVectorIndexedViewType = typename VectorIndexedViewSelector::ConstReturnType; +using ConstVectorIndexedViewType = typename internal::VectorIndexedViewSelector::ConstReturnType; template = true> ConstVectorIndexedViewType operator()(const Indices& indices) const { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return VectorIndexedViewSelector::run(derived(), indices); + return internal::VectorIndexedViewSelector::run(derived(), indices); } template , EnableConstVectorOverload = true> ConstVectorIndexedViewType operator()(const IndexType (&indices)[Size]) const { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return VectorIndexedViewSelector::run(derived(), Indices{indices}); + return internal::VectorIndexedViewSelector::run(derived(), Indices{indices}); } #else // EIGEN_PARSED_BY_DOXYGEN diff --git a/test/indexed_view.cpp b/test/indexed_view.cpp index f165e8b46..1f1e80861 100644 --- a/test/indexed_view.cpp +++ b/test/indexed_view.cpp @@ -447,7 +447,7 @@ void check_indexed_view() { // Check compilation of varying integer types as index types: Index i = n / 2; - short i_short(i); + short i_short = static_cast(i); std::size_t i_sizet(i); VERIFY_IS_EQUAL(a(i), a.coeff(i_short)); VERIFY_IS_EQUAL(a(i), a.coeff(i_sizet)); @@ -812,7 +812,7 @@ void check_tutorial_examples() { { std::vector ind{4, 2, 5, 5, 3}; auto slice1 = A(all, ind); - for (int i = 0; i < ind.size(); ++i) { + for (size_t i = 0; i < ind.size(); ++i) { VERIFY_IS_EQUAL(slice1.col(i), A.col(ind[i])); }