IndexedView.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
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 #ifndef EIGEN_INDEXED_VIEW_H
11 #define EIGEN_INDEXED_VIEW_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename XprType, typename RowIndices, typename ColIndices>
20 struct traits<IndexedView<XprType, RowIndices, ColIndices> >
21  : traits<XprType>
22 {
23  enum {
24  RowsAtCompileTime = int(array_size<RowIndices>::value),
25  ColsAtCompileTime = int(array_size<ColIndices>::value),
26  MaxRowsAtCompileTime = RowsAtCompileTime,
27  MaxColsAtCompileTime = ColsAtCompileTime,
28 
29  XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0,
30  IsRowMajor = (MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1
31  : (MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0
32  : XprTypeIsRowMajor,
33 
34  RowIncr = int(get_compile_time_incr<RowIndices>::value),
35  ColIncr = int(get_compile_time_incr<ColIndices>::value),
36  InnerIncr = IsRowMajor ? ColIncr : RowIncr,
37  OuterIncr = IsRowMajor ? RowIncr : ColIncr,
38 
39  HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
40  XprInnerStride = HasSameStorageOrderAsXprType ? int(inner_stride_at_compile_time<XprType>::ret) : int(outer_stride_at_compile_time<XprType>::ret),
41  XprOuterstride = HasSameStorageOrderAsXprType ? int(outer_stride_at_compile_time<XprType>::ret) : int(inner_stride_at_compile_time<XprType>::ret),
42 
43  InnerSize = XprTypeIsRowMajor ? ColsAtCompileTime : RowsAtCompileTime,
44  IsBlockAlike = InnerIncr==1 && OuterIncr==1,
45  IsInnerPannel = HasSameStorageOrderAsXprType && is_same<AllRange<InnerSize>,std::conditional_t<XprTypeIsRowMajor,ColIndices,RowIndices>>::value,
46 
47  InnerStrideAtCompileTime = InnerIncr<0 || InnerIncr==DynamicIndex || XprInnerStride==Dynamic || InnerIncr==UndefinedIncr ? Dynamic : XprInnerStride * InnerIncr,
48  OuterStrideAtCompileTime = OuterIncr<0 || OuterIncr==DynamicIndex || XprOuterstride==Dynamic || OuterIncr==UndefinedIncr ? Dynamic : XprOuterstride * OuterIncr,
49 
50  ReturnAsScalar = is_same<RowIndices,SingleRange>::value && is_same<ColIndices,SingleRange>::value,
51  ReturnAsBlock = (!ReturnAsScalar) && IsBlockAlike,
52  ReturnAsIndexedView = (!ReturnAsScalar) && (!ReturnAsBlock),
53 
54  // FIXME we deal with compile-time strides if and only if we have DirectAccessBit flag,
55  // but this is too strict regarding negative strides...
56  DirectAccessMask = (int(InnerIncr)!=UndefinedIncr && int(OuterIncr)!=UndefinedIncr && InnerIncr>=0 && OuterIncr>=0) ? DirectAccessBit : 0,
57  FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
58  FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
59  FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
60  Flags = (traits<XprType>::Flags & (HereditaryBits | DirectAccessMask )) | FlagsLvalueBit | FlagsRowMajorBit | FlagsLinearAccessBit
61  };
62 
63  typedef Block<XprType,RowsAtCompileTime,ColsAtCompileTime,IsInnerPannel> BlockType;
64 };
65 
66 }
67 
68 template<typename XprType, typename RowIndices, typename ColIndices, typename StorageKind>
69 class IndexedViewImpl;
70 
71 
109 template<typename XprType, typename RowIndices, typename ColIndices>
110 class IndexedView : public IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind>
111 {
112 public:
116 
117  typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
119 
120  template<typename T0, typename T1>
121  IndexedView(XprType& xpr, const T0& rowIndices, const T1& colIndices)
122  : m_xpr(xpr), m_rowIndices(rowIndices), m_colIndices(colIndices)
123  {}
124 
126  Index rows() const { return internal::index_list_size(m_rowIndices); }
127 
129  Index cols() const { return internal::index_list_size(m_colIndices); }
130 
133  nestedExpression() const { return m_xpr; }
134 
136  std::remove_reference_t<XprType>&
137  nestedExpression() { return m_xpr; }
138 
140  const RowIndices& rowIndices() const { return m_rowIndices; }
141 
143  const ColIndices& colIndices() const { return m_colIndices; }
144 
145 protected:
147  RowIndices m_rowIndices;
148  ColIndices m_colIndices;
149 };
150 
151 
152 // Generic API dispatcher
153 template<typename XprType, typename RowIndices, typename ColIndices, typename StorageKind>
155  : public internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices> >::type
156 {
157 public:
158  typedef typename internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices> >::type Base;
159 };
160 
161 namespace internal {
162 
163 
164 template<typename ArgType, typename RowIndices, typename ColIndices>
165 struct unary_evaluator<IndexedView<ArgType, RowIndices, ColIndices>, IndexBased>
166  : evaluator_base<IndexedView<ArgType, RowIndices, ColIndices> >
167 {
169 
170  enum {
171  CoeffReadCost = evaluator<ArgType>::CoeffReadCost /* TODO + cost of row/col index */,
172 
173  FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1) ? LinearAccessBit : 0,
174 
175  FlagsRowMajorBit = traits<XprType>::FlagsRowMajorBit,
176 
177  Flags = (evaluator<ArgType>::Flags & (HereditaryBits & ~RowMajorBit /*| LinearAccessBit | DirectAccessBit*/)) | FlagsLinearAccessBit | FlagsRowMajorBit,
178 
179  Alignment = 0
180  };
181 
182  EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr)
183  {
184  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
185  }
186 
187  typedef typename XprType::Scalar Scalar;
188  typedef typename XprType::CoeffReturnType CoeffReturnType;
189 
190  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
191  CoeffReturnType coeff(Index row, Index col) const
192  {
193  eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows()
194  && m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
195  return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
196  }
197 
198  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
199  Scalar& coeffRef(Index row, Index col)
200  {
201  eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows()
202  && m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
203  return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
204  }
205 
206  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
207  Scalar& coeffRef(Index index)
208  {
210  Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
211  Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
212  eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows()
213  && m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
214  return m_argImpl.coeffRef( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
215  }
216 
217  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
218  const Scalar& coeffRef(Index index) const
219  {
220  Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
221  Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
222  eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows()
223  && m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
224  return m_argImpl.coeffRef( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
225  }
226 
227  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
228  const CoeffReturnType coeff(Index index) const
229  {
230  Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
231  Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
232  eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows()
233  && m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
234  return m_argImpl.coeff( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
235  }
236 
237 protected:
238 
239  evaluator<ArgType> m_argImpl;
240  const XprType& m_xpr;
241 
242 };
243 
244 } // end namespace internal
245 
246 } // end namespace Eigen
247 
248 #endif // EIGEN_INDEXED_VIEW_H
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
Definition: StaticAssert.h:96
#define EIGEN_INTERNAL_CHECK_COST_VALUE(C)
Definition: StaticAssert.h:112
internal::generic_xpr_base< IndexedView< XprType, RowIndices, ColIndices > >::type Base
Definition: IndexedView.h:158
Expression of a non-sequential sub-matrix defined by arbitrary sequences of row and column indices.
Definition: IndexedView.h:111
RowIndices m_rowIndices
Definition: IndexedView.h:147
internal::ref_selector< XprType >::non_const_type MatrixTypeNested
Definition: IndexedView.h:117
internal::remove_all_t< XprType > NestedExpression
Definition: IndexedView.h:118
const internal::remove_all_t< XprType > & nestedExpression() const
Definition: IndexedView.h:133
Index cols() const
Definition: IndexedView.h:129
ColIndices m_colIndices
Definition: IndexedView.h:148
IndexedViewImpl< XprType, RowIndices, ColIndices, typename internal::traits< XprType >::StorageKind >::Base Base
Definition: IndexedView.h:113
std::remove_reference_t< XprType > & nestedExpression()
Definition: IndexedView.h:137
Index rows() const
Definition: IndexedView.h:126
MatrixTypeNested m_xpr
Definition: IndexedView.h:146
const ColIndices & colIndices() const
Definition: IndexedView.h:143
const RowIndices & rowIndices() const
Definition: IndexedView.h:140
const unsigned int LinearAccessBit
Definition: Constants.h:132
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
EIGEN_CONSTEXPR auto index_list_size(const T &x)
Definition: Meta.h:241
: InteropHeaders
Definition: Core:139
const int UndefinedIncr
Definition: Constants.h:33
const unsigned int HereditaryBits
Definition: Constants.h:197
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int DynamicIndex
Definition: Constants.h:29
const int Dynamic
Definition: Constants.h:24
Definition: BFloat16.h:222