| 1 |
// This file is part of Eigen, a lightweight C++ template library |
| 2 |
// for linear algebra. |
| 3 |
// |
| 4 |
// Copyright (C) 2017 Gael Guennebaud <[email protected]> |
| 5 |
// |
| 6 |
// This Source Code Form is subject to the terms of the Mozilla |
| 7 |
// Public License v. 2.0. If a copy of the MPL was not distributed |
| 8 |
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 |
|
| 10 |
#if !defined(EIGEN_PARSED_BY_DOXYGEN) |
| 11 |
|
| 12 |
// This file is automatically included twice to generate const and non-const versions |
| 13 |
|
| 14 |
#ifndef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS |
| 15 |
#define EIGEN_INDEXED_VIEW_METHOD_CONST const |
| 16 |
#define EIGEN_INDEXED_VIEW_METHOD_TYPE ConstIndexedViewType |
| 17 |
#else |
| 18 |
#define EIGEN_INDEXED_VIEW_METHOD_CONST |
| 19 |
#define EIGEN_INDEXED_VIEW_METHOD_TYPE IndexedViewType |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifndef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS |
| 23 |
protected: |
| 24 |
|
| 25 |
// define some aliases to ease readability |
| 26 |
|
| 27 |
template<typename Indices> |
| 28 |
struct IvcRowType : public internal::IndexedViewCompatibleType<Indices,RowsAtCompileTime> {}; |
| 29 |
|
| 30 |
template<typename Indices> |
| 31 |
struct IvcColType : public internal::IndexedViewCompatibleType<Indices,ColsAtCompileTime> {}; |
| 32 |
|
| 33 |
template<typename Indices> |
| 34 |
struct IvcType : public internal::IndexedViewCompatibleType<Indices,SizeAtCompileTime> {}; |
| 35 |
|
| 36 |
typedef typename internal::IndexedViewCompatibleType<Index,1>::type IvcIndex; |
| 37 |
|
| 38 |
template<typename Indices> |
| 39 |
typename IvcRowType<Indices>::type |
| 40 |
ivcRow(const Indices& indices) const { |
| 41 |
return internal::makeIndexedViewCompatible(indices, internal::variable_if_dynamic<Index,RowsAtCompileTime>(derived().rows()),Specialized); |
| 42 |
} |
| 43 |
|
| 44 |
template<typename Indices> |
| 45 |
typename IvcColType<Indices>::type |
| 46 |
ivcCol(const Indices& indices) const { |
| 47 |
return internal::makeIndexedViewCompatible(indices, internal::variable_if_dynamic<Index,ColsAtCompileTime>(derived().cols()),Specialized); |
| 48 |
} |
| 49 |
|
| 50 |
template<typename Indices> |
| 51 |
typename IvcColType<Indices>::type |
| 52 |
ivcSize(const Indices& indices) const { |
| 53 |
return internal::makeIndexedViewCompatible(indices, internal::variable_if_dynamic<Index,SizeAtCompileTime>(derived().size()),Specialized); |
| 54 |
} |
| 55 |
|
| 56 |
public: |
| 57 |
|
| 58 |
#endif |
| 59 |
|
| 60 |
template<typename RowIndices, typename ColIndices> |
| 61 |
struct EIGEN_INDEXED_VIEW_METHOD_TYPE { |
| 62 |
typedef IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived, |
| 63 |
typename IvcRowType<RowIndices>::type, |
| 64 |
typename IvcColType<ColIndices>::type> type; |
| 65 |
}; |
| 66 |
|
| 67 |
// This is the generic version |
| 68 |
|
| 69 |
template<typename RowIndices, typename ColIndices> |
| 70 |
std::enable_if_t<internal::valid_indexed_view_overload<RowIndices,ColIndices>::value |
| 71 |
&& internal::traits<typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type>::ReturnAsIndexedView, |
| 72 |
typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type> |
| 73 |
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 74 |
{ |
| 75 |
return typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type |
| 76 |
(derived(), ivcRow(rowIndices), ivcCol(colIndices)); |
| 77 |
} |
| 78 |
|
| 79 |
// The following overload returns a Block<> object |
| 80 |
|
| 81 |
template<typename RowIndices, typename ColIndices> |
| 82 |
std::enable_if_t<internal::valid_indexed_view_overload<RowIndices,ColIndices>::value |
| 83 |
&& internal::traits<typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type>::ReturnAsBlock, |
| 84 |
typename internal::traits<typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type>::BlockType> |
| 85 |
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 86 |
{ |
| 87 |
typedef typename internal::traits<typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type>::BlockType BlockType; |
| 88 |
typename IvcRowType<RowIndices>::type actualRowIndices = ivcRow(rowIndices); |
| 89 |
typename IvcColType<ColIndices>::type actualColIndices = ivcCol(colIndices); |
| 90 |
return BlockType(derived(), |
| 91 |
internal::first(actualRowIndices), |
| 92 |
internal::first(actualColIndices), |
| 93 |
internal::index_list_size(actualRowIndices), |
| 94 |
internal::index_list_size(actualColIndices)); |
| 95 |
} |
| 96 |
|
| 97 |
// The following overload returns a Scalar |
| 98 |
|
| 99 |
template<typename RowIndices, typename ColIndices> |
| 100 |
std::enable_if_t<internal::valid_indexed_view_overload<RowIndices,ColIndices>::value |
| 101 |
&& internal::traits<typename EIGEN_INDEXED_VIEW_METHOD_TYPE<RowIndices,ColIndices>::type>::ReturnAsScalar, |
| 102 |
CoeffReturnType > |
| 103 |
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 104 |
{ |
| 105 |
return Base::operator()(internal::eval_expr_given_size(rowIndices,rows()),internal::eval_expr_given_size(colIndices,cols())); |
| 106 |
} |
| 107 |
|
| 108 |
// The following three overloads are needed to handle raw Index[N] arrays. |
| 109 |
|
| 110 |
template<typename RowIndicesT, std::size_t RowIndicesN, typename ColIndices> |
| 111 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,const RowIndicesT (&)[RowIndicesN],typename IvcColType<ColIndices>::type> |
| 112 |
operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 113 |
{ |
| 114 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,const RowIndicesT (&)[RowIndicesN],typename IvcColType<ColIndices>::type> |
| 115 |
(derived(), rowIndices, ivcCol(colIndices)); |
| 116 |
} |
| 117 |
|
| 118 |
template<typename RowIndices, typename ColIndicesT, std::size_t ColIndicesN> |
| 119 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,typename IvcRowType<RowIndices>::type, const ColIndicesT (&)[ColIndicesN]> |
| 120 |
operator()(const RowIndices& rowIndices, const ColIndicesT (&colIndices)[ColIndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 121 |
{ |
| 122 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,typename IvcRowType<RowIndices>::type,const ColIndicesT (&)[ColIndicesN]> |
| 123 |
(derived(), ivcRow(rowIndices), colIndices); |
| 124 |
} |
| 125 |
|
| 126 |
template<typename RowIndicesT, std::size_t RowIndicesN, typename ColIndicesT, std::size_t ColIndicesN> |
| 127 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,const RowIndicesT (&)[RowIndicesN], const ColIndicesT (&)[ColIndicesN]> |
| 128 |
operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndicesT (&colIndices)[ColIndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 129 |
{ |
| 130 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,const RowIndicesT (&)[RowIndicesN],const ColIndicesT (&)[ColIndicesN]> |
| 131 |
(derived(), rowIndices, colIndices); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// Overloads for 1D vectors/arrays |
| 136 |
|
| 137 |
template<typename Indices> |
| 138 |
std::enable_if_t< |
| 139 |
IsRowMajor && (!(internal::get_compile_time_incr<typename IvcType<Indices>::type>::value==1 || internal::is_valid_index_type<Indices>::value)), |
| 140 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,IvcIndex,typename IvcType<Indices>::type> > |
| 141 |
operator()(const Indices& indices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 142 |
{ |
| 143 |
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
| 144 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,IvcIndex,typename IvcType<Indices>::type> |
| 145 |
(derived(), IvcIndex(0), ivcCol(indices)); |
| 146 |
} |
| 147 |
|
| 148 |
template<typename Indices> |
| 149 |
std::enable_if_t< |
| 150 |
(!IsRowMajor) && (!(internal::get_compile_time_incr<typename IvcType<Indices>::type>::value==1 || internal::is_valid_index_type<Indices>::value)), |
| 151 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,typename IvcType<Indices>::type,IvcIndex> > |
| 152 |
operator()(const Indices& indices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 153 |
{ |
| 154 |
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
| 155 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,typename IvcType<Indices>::type,IvcIndex> |
| 156 |
(derived(), ivcRow(indices), IvcIndex(0)); |
| 157 |
} |
| 158 |
|
| 159 |
template<typename Indices> |
| 160 |
std::enable_if_t< |
| 161 |
(internal::get_compile_time_incr<typename IvcType<Indices>::type>::value==1) && (!internal::is_valid_index_type<Indices>::value) && (!symbolic::is_symbolic<Indices>::value), |
| 162 |
VectorBlock<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,internal::array_size<Indices>::value> > |
| 163 |
operator()(const Indices& indices) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 164 |
{ |
| 165 |
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
| 166 |
typename IvcType<Indices>::type actualIndices = ivcSize(indices); |
| 167 |
return VectorBlock<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,internal::array_size<Indices>::value> |
| 168 |
(derived(), internal::first(actualIndices), internal::index_list_size(actualIndices)); |
| 169 |
} |
| 170 |
|
| 171 |
template<typename IndexType> |
| 172 |
std::enable_if_t<symbolic::is_symbolic<IndexType>::value, CoeffReturnType > |
| 173 |
operator()(const IndexType& id) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 174 |
{ |
| 175 |
return Base::operator()(internal::eval_expr_given_size(id,size())); |
| 176 |
} |
| 177 |
|
| 178 |
template<typename IndicesT, std::size_t IndicesN> |
| 179 |
std::enable_if_t<IsRowMajor, |
| 180 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,IvcIndex,const IndicesT (&)[IndicesN]> > |
| 181 |
operator()(const IndicesT (&indices)[IndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 182 |
{ |
| 183 |
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
| 184 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,IvcIndex,const IndicesT (&)[IndicesN]> |
| 185 |
(derived(), IvcIndex(0), indices); |
| 186 |
} |
| 187 |
|
| 188 |
template<typename IndicesT, std::size_t IndicesN> |
| 189 |
std::enable_if_t<!IsRowMajor, |
| 190 |
IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,const IndicesT (&)[IndicesN],IvcIndex> > |
| 191 |
operator()(const IndicesT (&indices)[IndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST |
| 192 |
{ |
| 193 |
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
| 194 |
return IndexedView<EIGEN_INDEXED_VIEW_METHOD_CONST Derived,const IndicesT (&)[IndicesN],IvcIndex> |
| 195 |
(derived(), indices, IvcIndex(0)); |
| 196 |
} |
| 197 |
|
| 198 |
#undef EIGEN_INDEXED_VIEW_METHOD_CONST |
| 199 |
#undef EIGEN_INDEXED_VIEW_METHOD_TYPE |
| 200 |
|
| 201 |
#ifndef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS |
| 202 |
#define EIGEN_INDEXED_VIEW_METHOD_2ND_PASS |
| 203 |
#include "IndexedViewMethods.h" |
| 204 |
#undef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS |
| 205 |
#endif |
| 206 |
|
| 207 |
#else // EIGEN_PARSED_BY_DOXYGEN |
| 208 |
|
| 209 |
/** |
| 210 |
* \returns a generic submatrix view defined by the rows and columns indexed \a rowIndices and \a colIndices respectively. |
| 211 |
* |
| 212 |
* Each parameter must either be: |
| 213 |
* - An integer indexing a single row or column |
| 214 |
* - Eigen::placeholders::all indexing the full set of respective rows or columns in increasing order |
| 215 |
* - An ArithmeticSequence as returned by the Eigen::seq and Eigen::seqN functions |
| 216 |
* - Any %Eigen's vector/array of integers or expressions |
| 217 |
* - Plain C arrays: \c int[N] |
| 218 |
* - And more generally any type exposing the following two member functions: |
| 219 |
* \code |
| 220 |
* <integral type> operator[](<integral type>) const; |
| 221 |
* <integral type> size() const; |
| 222 |
* \endcode |
| 223 |
* where \c <integral \c type> stands for any integer type compatible with Eigen::Index (i.e. \c std::ptrdiff_t). |
| 224 |
* |
| 225 |
* The last statement implies compatibility with \c std::vector, \c std::valarray, \c std::array, many of the Range-v3's ranges, etc. |
| 226 |
* |
| 227 |
* If the submatrix can be represented using a starting position \c (i,j) and positive sizes \c (rows,columns), then this |
| 228 |
* method will returns a Block object after extraction of the relevant information from the passed arguments. This is the case |
| 229 |
* when all arguments are either: |
| 230 |
* - An integer |
| 231 |
* - Eigen::placeholders::all |
| 232 |
* - An ArithmeticSequence with compile-time increment strictly equal to 1, as returned by Eigen::seq(a,b), and Eigen::seqN(a,N). |
| 233 |
* |
| 234 |
* Otherwise a more general IndexedView<Derived,RowIndices',ColIndices'> object will be returned, after conversion of the inputs |
| 235 |
* to more suitable types \c RowIndices' and \c ColIndices'. |
| 236 |
* |
| 237 |
* For 1D vectors and arrays, you better use the operator()(const Indices&) overload, which behave the same way but taking a single parameter. |
| 238 |
* |
| 239 |
* See also this <a href="https://stackoverflow.com/questions/46110917/eigen-replicate-items-along-one-dimension-without-useless-allocations">question</a> and its answer for an example of how to duplicate coefficients. |
| 240 |
* |
| 241 |
* \sa operator()(const Indices&), class Block, class IndexedView, DenseBase::block(Index,Index,Index,Index) |
| 242 |
*/ |
| 243 |
template<typename RowIndices, typename ColIndices> |
| 244 |
IndexedView_or_Block |
| 245 |
operator()(const RowIndices& rowIndices, const ColIndices& colIndices); |
| 246 |
|
| 247 |
/** This is an overload of operator()(const RowIndices&, const ColIndices&) for 1D vectors or arrays |
| 248 |
* |
| 249 |
* \only_for_vectors |
| 250 |
*/ |
| 251 |
template<typename Indices> |
| 252 |
IndexedView_or_VectorBlock |
| 253 |
operator()(const Indices& indices); |
| 254 |
|
| 255 |
#endif // EIGEN_PARSED_BY_DOXYGEN |