From 752bd92ba53de344eba66b8cec4480f9d3207025 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Wed, 11 Jan 2017 17:24:02 +0100 Subject: Large code refactoring: - generalize some utilities and move them to Meta (size(), array_size()) - move handling of all and single indices to IndexedViewHelper.h - several cleanup changes --- Eigen/src/Core/ArithmeticSequence.h | 135 +++----------------------------- Eigen/src/Core/IndexedView.h | 7 +- Eigen/src/Core/util/IndexedViewHelper.h | 111 ++++++++++++++++++++++++++ Eigen/src/Core/util/Meta.h | 47 +++++++++++ Eigen/src/plugins/IndexedViewMethods.h | 96 ++++++++++++++++------- 5 files changed, 242 insertions(+), 154 deletions(-) create mode 100644 Eigen/src/Core/util/IndexedViewHelper.h (limited to 'Eigen/src') diff --git a/Eigen/src/Core/ArithmeticSequence.h b/Eigen/src/Core/ArithmeticSequence.h index c221afcfd..afb014ac5 100644 --- a/Eigen/src/Core/ArithmeticSequence.h +++ b/Eigen/src/Core/ArithmeticSequence.h @@ -12,26 +12,10 @@ namespace Eigen { -//-------------------------------------------------------------------------------- -// Pseudo keywords: all, last, end -//-------------------------------------------------------------------------------- - -namespace internal { - -struct all_t { all_t() {} }; - -} - -/** \var all - * \ingroup Core_Module - * Can be used as a parameter to DenseBase::operator()(const RowIndices&, const ColIndices&) to index all rows or columns - */ -static const internal::all_t all; - /** \namespace Eigen::placeholders * \ingroup Core_Module * - * Namespace containing symbolic placeholders + * Namespace containing symbolic placeholder and identifiers */ namespace placeholders { @@ -268,7 +252,7 @@ typename internal::enable_if::value, Symbolic::QuotientExpr, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type::type> >::type + typename internal::cleanup_seq_type::type> >::type seq(FirstType f, const Symbolic::BaseExpr &l, IncrType incr) { typedef typename internal::cleanup_seq_type::type CleanedIncrType; @@ -281,7 +265,7 @@ ArithemeticSequence >, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type::type> + typename internal::cleanup_seq_type::type> seq(const Symbolic::BaseExpr &f, const Symbolic::BaseExpr &l, IncrType incr) { typedef typename internal::cleanup_seq_type::type CleanedIncrType; @@ -293,76 +277,6 @@ seq(const Symbolic::BaseExpr &f, const Symbolic::BaseExpr -Index size(const T& x) { return x.size(); } - -template -Index size(const T (&) [N]) { return N; } - -template -Index first(const T& x) { return x.first(); } - -template struct get_compile_time_size { - enum { value = Dynamic }; -}; - -template struct get_compile_time_size::type> { - enum { value = T::SizeAtCompileTime }; -}; - -template struct get_compile_time_size { - enum { value = N }; -}; - -#ifdef EIGEN_HAS_CXX11 -template struct get_compile_time_size,XprSize> { - enum { value = N }; -}; -#endif - -template struct get_compile_time_incr { - enum { value = UndefinedIncr }; -}; - -template -struct get_compile_time_incr > { - enum { value = get_compile_time::value }; -}; - - -// MakeIndexing/make_indexing turn an arbitrary object of type T into something usable by MatrixSlice -template -struct MakeIndexing { - typedef T type; -}; - -template -const T& make_indexing(const T& x, Index /*size*/) { return x; } - -struct IntAsArray { - enum { - SizeAtCompileTime = 1 - }; - IntAsArray(Index val) : m_value(val) {} - Index operator[](Index) const { return m_value; } - Index size() const { return 1; } - Index first() const { return m_value; } - Index m_value; -}; - -template<> struct get_compile_time_incr { - enum { value = 1 }; // 1 or 0 ?? -}; - -// Turn a single index into something that looks like an array (i.e., that exposes a .size(), and operatro[](int) methods) -template -struct MakeIndexing::value>::type> { - // Here we could simply use Array, but maybe it's less work for the compiler to use - // a simpler wrapper as IntAsArray - //typedef Eigen::Array type; - typedef IntAsArray type; -}; - // Replace symbolic last/end "keywords" by their true runtime value inline Index eval_expr_given_size(Index x, Index /* size */) { return x; } @@ -381,45 +295,21 @@ struct make_size_type { typedef typename internal::conditional::value, Index, T>::type type; }; -template -struct MakeIndexing > { +template +struct IndexedViewCompatibleType, XprSize> { typedef ArithemeticSequence::type,IncrType> type; }; template ArithemeticSequence::type,IncrType> -make_indexing(const ArithemeticSequence& ids, Index size) { +makeIndexedViewCompatible(const ArithemeticSequence& ids, Index size) { return ArithemeticSequence::type,IncrType>( eval_expr_given_size(ids.firstObject(),size),eval_expr_given_size(ids.sizeObject(),size),ids.incrObject()); } -// Convert a symbolic 'all' into a usable range -// Implementation-wise, it would be more efficient to not having to store m_size since -// this information is already in the nested expression. To this end, we would need a -// get_size(indices, underlying_size); function returning indices.size() by default. -struct AllRange { - AllRange(Index size) : m_size(size) {} - Index operator[](Index i) const { return i; } - Index size() const { return m_size; } - Index first() const { return 0; } - Index m_size; -}; - -template<> -struct MakeIndexing { - typedef AllRange type; -}; - -inline AllRange make_indexing(all_t , Index size) { - return AllRange(size); -} - -template struct get_compile_time_size { - enum { value = XprSize }; -}; - -template<> struct get_compile_time_incr { - enum { value = 1 }; +template +struct get_compile_time_incr > { + enum { value = get_compile_time::value }; }; } // end namespace internal @@ -428,6 +318,7 @@ template<> struct get_compile_time_incr { namespace legacy { // Here are some initial code that I keep here for now to compare the quality of the code generated by the compilers +// This part will be removed once we have checked everything is right. struct shifted_last { explicit shifted_last(int o) : offset(o) {} @@ -522,14 +413,14 @@ struct get_compile_time_incr -struct MakeIndexing > { +template +struct IndexedViewCompatibleType,XprSize> { typedef legacy::ArithemeticSequenceProxyWithBounds type; }; template legacy::ArithemeticSequenceProxyWithBounds -make_indexing(const legacy::ArithemeticSequenceProxyWithBounds& ids, Index size) { +makeIndexedViewCompatible(const legacy::ArithemeticSequenceProxyWithBounds& ids, Index size) { return legacy::ArithemeticSequenceProxyWithBounds( eval_expr_given_size(ids.firstObject(),size),eval_expr_given_size(ids.lastObject(),size),ids.incrObject()); } diff --git a/Eigen/src/Core/IndexedView.h b/Eigen/src/Core/IndexedView.h index d975c6e80..38ee69638 100644 --- a/Eigen/src/Core/IndexedView.h +++ b/Eigen/src/Core/IndexedView.h @@ -19,8 +19,8 @@ struct traits > : traits { enum { - RowsAtCompileTime = get_compile_time_size::RowsAtCompileTime>::value, - ColsAtCompileTime = get_compile_time_size::ColsAtCompileTime>::value, + RowsAtCompileTime = array_size::value, + ColsAtCompileTime = array_size::value, MaxRowsAtCompileTime = RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime) : int(traits::MaxRowsAtCompileTime), MaxColsAtCompileTime = ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime) : int(traits::MaxColsAtCompileTime), @@ -38,8 +38,9 @@ struct traits > XprInnerStride = HasSameStorageOrderAsXprType ? int(inner_stride_at_compile_time::ret) : int(outer_stride_at_compile_time::ret), XprOuterstride = HasSameStorageOrderAsXprType ? int(outer_stride_at_compile_time::ret) : int(inner_stride_at_compile_time::ret), + InnerSize = XprTypeIsRowMajor ? ColsAtCompileTime : RowsAtCompileTime, IsBlockAlike = InnerIncr==1 && OuterIncr==1, - IsInnerPannel = HasSameStorageOrderAsXprType && is_same::type>::value, + IsInnerPannel = HasSameStorageOrderAsXprType && is_same,typename conditional::type>::value, InnerStrideAtCompileTime = InnerIncr<0 || InnerIncr==DynamicIndex || XprInnerStride==Dynamic ? Dynamic : XprInnerStride * InnerIncr, OuterStrideAtCompileTime = OuterIncr<0 || OuterIncr==DynamicIndex || XprOuterstride==Dynamic ? Dynamic : XprOuterstride * OuterIncr, diff --git a/Eigen/src/Core/util/IndexedViewHelper.h b/Eigen/src/Core/util/IndexedViewHelper.h new file mode 100644 index 000000000..4f6dd065e --- /dev/null +++ b/Eigen/src/Core/util/IndexedViewHelper.h @@ -0,0 +1,111 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2017 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/. + + +#ifndef EIGEN_INDEXED_VIEW_HELPER_H +#define EIGEN_INDEXED_VIEW_HELPER_H + +namespace Eigen { + +namespace internal { + +// Extract increment/step at compile time +template struct get_compile_time_incr { + enum { value = UndefinedIncr }; +}; + +// Analogue of std::get<0>(x), but tailored for our needs. +template +Index first(const T& x) { return x.first(); } + +// IndexedViewCompatibleType/makeIndexedViewCompatible turn an arbitrary object of type T into something usable by MatrixSlice +// The generic implementation is a no-op +template +struct IndexedViewCompatibleType { + typedef T type; +}; + +template +const T& makeIndexedViewCompatible(const T& x, Index /*size*/) { return x; } + +//-------------------------------------------------------------------------------- +// Handling of a single Index +//-------------------------------------------------------------------------------- + +struct SingleRange { + enum { + SizeAtCompileTime = 1 + }; + SingleRange(Index val) : m_value(val) {} + Index operator[](Index) const { return m_value; } + Index size() const { return 1; } + Index first() const { return m_value; } + Index m_value; +}; + +template<> struct get_compile_time_incr { + enum { value = 1 }; // 1 or 0 ?? +}; + +// Turn a single index into something that looks like an array (i.e., that exposes a .size(), and operatro[](int) methods) +template +struct IndexedViewCompatibleType::value>::type> { + // Here we could simply use Array, but maybe it's less work for the compiler to use + // a simpler wrapper as SingleRange + //typedef Eigen::Array type; + typedef SingleRange type; +}; + +//-------------------------------------------------------------------------------- +// Handling of all +//-------------------------------------------------------------------------------- + +struct all_t { all_t() {} }; + +// Convert a symbolic 'all' into a usable range type +template +struct AllRange { + enum { SizeAtCompileTime = XprSize }; + AllRange(Index size = XprSize) : m_size(size) {} + Index operator[](Index i) const { return i; } + Index size() const { return m_size.value(); } + Index first() const { return 0; } + variable_if_dynamic m_size; +}; + +template +struct IndexedViewCompatibleType { + typedef AllRange type; +}; + +template +inline AllRange::value> makeIndexedViewCompatible(all_t , XprSizeType size) { + return AllRange::value>(size); +} + +template struct get_compile_time_incr > { + enum { value = 1 }; +}; + +} // end namespace internal + + +namespace placeholders { + +/** \var all + * \ingroup Core_Module + * Can be used as a parameter to DenseBase::operator()(const RowIndices&, const ColIndices&) to index all rows or columns + */ +static const Eigen::internal::all_t all; + +} + +} // end namespace Eigen + +#endif // EIGEN_INDEXED_VIEW_HELPER_H diff --git a/Eigen/src/Core/util/Meta.h b/Eigen/src/Core/util/Meta.h index 7f6370755..804657f7b 100755 --- a/Eigen/src/Core/util/Meta.h +++ b/Eigen/src/Core/util/Meta.h @@ -278,6 +278,53 @@ protected: EIGEN_DEVICE_FUNC ~noncopyable() {} }; +/** \internal + * Provides access to the number of elements in the object of as a compile-time constant expression. + * It "returns" Eigen::Dynamic if the size cannot be resolved at compile-time (default). + * + * Similar to std::tuple_size, but more general. + * + * It currently supports: + * - any types T defining T::SizeAtCompileTime + * - plain C arrays as T[N] + * - std::array (c++11) + * - some internal types such as SingleRange and AllRange + * + * The second template parameter ease SFINAE-based specializations. + */ +template struct array_size { + enum { value = Dynamic }; +}; + +template struct array_size::type> { + enum { value = T::SizeAtCompileTime }; +}; + +template struct array_size { + enum { value = N }; +}; + +#ifdef EIGEN_HAS_CXX11 +template struct array_size > { + enum { value = N }; +}; +#endif + +/** \internal + * Analogue of the std::size free function. + * It returns the size of the container or view \a x of type \c T + * + * It currently supports: + * - any types T defining a member T::size() const + * - plain C arrays as T[N] + * + */ +template +Index size(const T& x) { return x.size(); } + +template +Index size(const T (&) [N]) { return N; } + /** \internal * Convenient struct to get the result type of a unary or binary functor. * diff --git a/Eigen/src/plugins/IndexedViewMethods.h b/Eigen/src/plugins/IndexedViewMethods.h index ae817e90b..774a5cbe5 100644 --- a/Eigen/src/plugins/IndexedViewMethods.h +++ b/Eigen/src/plugins/IndexedViewMethods.h @@ -19,11 +19,49 @@ #define EIGEN_INDEXED_VIEW_METHOD_TYPE IndexedViewType #endif +#ifndef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS +protected: + +// define some aliases to ease readability + +template +struct IvcRowType : public internal::IndexedViewCompatibleType {}; + +template +struct IvcColType : public internal::IndexedViewCompatibleType {}; + +template +struct IvcType : public internal::IndexedViewCompatibleType {}; + +typedef typename internal::IndexedViewCompatibleType::type IvcIndex; + +template +typename IvcRowType::type +ivcRow(const Indices& indices) const { + return internal::makeIndexedViewCompatible(indices, internal::variable_if_dynamic(derived().rows())); +}; + +template +typename IvcColType::type +ivcCol(const Indices& indices) const { + return internal::makeIndexedViewCompatible(indices, internal::variable_if_dynamic(derived().cols())); +}; + +template +typename IvcColType::type +ivcSize(const Indices& indices) const { + return internal::makeIndexedViewCompatible(indices, internal::variable_if_dynamic(derived().size())); +}; + +public: + +#endif + template struct EIGEN_INDEXED_VIEW_METHOD_TYPE { typedef IndexedView::type, - typename internal::MakeIndexing::type> type; + typename IvcRowType::type, + typename IvcColType::type> type; }; // This is the generic version @@ -36,7 +74,7 @@ typename internal::enable_if< operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST { return typename EIGEN_INDEXED_VIEW_METHOD_TYPE::type - (derived(), internal::make_indexing(rowIndices,derived().rows()), internal::make_indexing(colIndices,derived().cols())); + (derived(), ivcRow(rowIndices), ivcCol(colIndices)); } // The folowing overload returns a Block<> object @@ -49,8 +87,8 @@ typename internal::enable_if< operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST { typedef typename internal::traits::type>::BlockType BlockType; - typename internal::MakeIndexing::type actualRowIndices = internal::make_indexing(rowIndices,derived().rows()); - typename internal::MakeIndexing::type actualColIndices = internal::make_indexing(colIndices,derived().cols()); + typename IvcRowType::type actualRowIndices = ivcRow(rowIndices); + typename IvcColType::type actualColIndices = ivcCol(colIndices); return BlockType(derived(), internal::first(actualRowIndices), internal::first(actualColIndices), @@ -61,19 +99,19 @@ operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_IND // The folowing three overloads are needed to handle raw Index[N] arrays. template -IndexedView::type> +IndexedView::type> operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST { - return IndexedView::type> - (derived(), rowIndices, internal::make_indexing(colIndices,derived().cols())); + return IndexedView::type> + (derived(), rowIndices, ivcCol(colIndices)); } template -IndexedView::type, const ColIndicesT (&)[ColIndicesN]> +IndexedView::type, const ColIndicesT (&)[ColIndicesN]> operator()(const RowIndices& rowIndices, const ColIndicesT (&colIndices)[ColIndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST { - return IndexedView::type,const ColIndicesT (&)[ColIndicesN]> - (derived(), internal::make_indexing(rowIndices,derived().rows()), colIndices); + return IndexedView::type,const ColIndicesT (&)[ColIndicesN]> + (derived(), ivcRow(rowIndices), colIndices); } template @@ -88,56 +126,56 @@ operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndicesT (&col template typename internal::enable_if< - IsRowMajor && (!(internal::get_compile_time_incr::type>::value==1 || internal::is_integral::value)), - IndexedView::type,typename internal::MakeIndexing::type> >::type + IsRowMajor && (!(internal::get_compile_time_incr::type>::value==1 || internal::is_integral::value)), + IndexedView::type> >::type operator()(const Indices& indices) EIGEN_INDEXED_VIEW_METHOD_CONST { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return IndexedView::type,typename internal::MakeIndexing::type> - (derived(), internal::make_indexing(0,derived().rows()), internal::make_indexing(indices,derived().cols())); + return IndexedView::type> + (derived(), IvcIndex(0), ivcCol(indices)); } template typename internal::enable_if< - (!IsRowMajor) && (!(internal::get_compile_time_incr::type>::value==1 || internal::is_integral::value)), - IndexedView::type,typename internal::MakeIndexing::type> >::type + (!IsRowMajor) && (!(internal::get_compile_time_incr::type>::value==1 || internal::is_integral::value)), + IndexedView::type,IvcIndex> >::type operator()(const Indices& indices) EIGEN_INDEXED_VIEW_METHOD_CONST { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return IndexedView::type,typename internal::MakeIndexing::type> - (derived(), internal::make_indexing(indices,derived().rows()), internal::make_indexing(Index(0),derived().cols())); + return IndexedView::type,IvcIndex> + (derived(), ivcRow(indices), IvcIndex(0)); } template typename internal::enable_if< - (internal::get_compile_time_incr::type>::value==1) && (!internal::is_integral::value), - VectorBlock::value> >::type + (internal::get_compile_time_incr::type>::value==1) && (!internal::is_integral::value), + VectorBlock::value> >::type operator()(const Indices& indices) EIGEN_INDEXED_VIEW_METHOD_CONST { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - typename internal::MakeIndexing::type actualIndices = internal::make_indexing(indices,derived().size()); - return VectorBlock::value> + typename IvcType::type actualIndices = ivcSize(indices); + return VectorBlock::value> (derived(), internal::first(actualIndices), internal::size(actualIndices)); } template typename internal::enable_if::type,const IndicesT (&)[IndicesN]> >::type + IndexedView >::type operator()(const IndicesT (&indices)[IndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return IndexedView::type,const IndicesT (&)[IndicesN]> - (derived(), internal::make_indexing(0,derived().rows()), indices); + return IndexedView + (derived(), IvcIndex(0), indices); } template typename internal::enable_if::type> >::type + IndexedView >::type operator()(const IndicesT (&indices)[IndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) - return IndexedView::type> - (derived(), indices, internal::make_indexing(0,derived().rows())); + return IndexedView + (derived(), indices, IvcIndex(0)); } #undef EIGEN_INDEXED_VIEW_METHOD_CONST -- cgit v1.2.3