Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
IndexedView.h
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 
110 template<typename XprType, typename RowIndices, typename ColIndices>
111 class IndexedView : public IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind>
112 {
113 public:
114  typedef typename IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind>::Base Base;
115  EIGEN_GENERIC_PUBLIC_INTERFACE(IndexedView)
116  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedView)
117 
118  typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
119  typedef internal::remove_all_t<XprType> NestedExpression;
120 
121  template<typename T0, typename T1>
122  IndexedView(XprType& xpr, const T0& rowIndices, const T1& colIndices)
123  : m_xpr(xpr), m_rowIndices(rowIndices), m_colIndices(colIndices)
124  {}
125 
127  Index rows() const { return internal::index_list_size(m_rowIndices); }
128 
130  Index cols() const { return internal::index_list_size(m_colIndices); }
131 
133  const internal::remove_all_t<XprType>&
134  nestedExpression() const { return m_xpr; }
135 
137  std::remove_reference_t<XprType>&
138  nestedExpression() { return m_xpr; }
139 
141  const RowIndices& rowIndices() const { return m_rowIndices; }
142 
144  const ColIndices& colIndices() const { return m_colIndices; }
145 
146 protected:
147  MatrixTypeNested m_xpr;
148  RowIndices m_rowIndices;
149  ColIndices m_colIndices;
150 };
151 
152 
153 // Generic API dispatcher
154 template<typename XprType, typename RowIndices, typename ColIndices, typename StorageKind>
155 class IndexedViewImpl
156  : public internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices> >::type
157 {
158 public:
159  typedef typename internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices> >::type Base;
160 };
161 
162 namespace internal {
163 
164 
165 template<typename ArgType, typename RowIndices, typename ColIndices>
166 struct unary_evaluator<IndexedView<ArgType, RowIndices, ColIndices>, IndexBased>
167  : evaluator_base<IndexedView<ArgType, RowIndices, ColIndices> >
168 {
169  typedef IndexedView<ArgType, RowIndices, ColIndices> XprType;
170 
171  enum {
172  CoeffReadCost = evaluator<ArgType>::CoeffReadCost /* TODO + cost of row/col index */,
173 
174  FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1) ? LinearAccessBit : 0,
175 
176  FlagsRowMajorBit = traits<XprType>::FlagsRowMajorBit,
177 
178  Flags = (evaluator<ArgType>::Flags & (HereditaryBits & ~RowMajorBit /*| LinearAccessBit | DirectAccessBit*/)) | FlagsLinearAccessBit | FlagsRowMajorBit,
179 
180  Alignment = 0
181  };
182 
183  EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr)
184  {
185  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
186  }
187 
188  typedef typename XprType::Scalar Scalar;
189  typedef typename XprType::CoeffReturnType CoeffReturnType;
190 
191  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
192  CoeffReturnType coeff(Index row, Index col) const
193  {
194  return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
195  }
196 
197  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
198  Scalar& coeffRef(Index row, Index col)
199  {
200  return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
201  }
202 
203  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
204  Scalar& coeffRef(Index index)
205  {
206  EIGEN_STATIC_ASSERT_LVALUE(XprType)
207  Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
208  Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
209  return m_argImpl.coeffRef( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
210  }
211 
212  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
213  const Scalar& coeffRef(Index index) const
214  {
215  Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
216  Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
217  return m_argImpl.coeffRef( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
218  }
219 
220  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
221  const CoeffReturnType coeff(Index index) const
222  {
223  Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
224  Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
225  return m_argImpl.coeff( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
226  }
227 
228 protected:
229 
230  evaluator<ArgType> m_argImpl;
231  const XprType& m_xpr;
232 
233 };
234 
235 } // end namespace internal
236 
237 } // end namespace Eigen
238 
239 #endif // EIGEN_INDEXED_VIEW_H
Expression of a non-sequential sub-matrix defined by arbitrary sequences of row and column indices.
Definition: IndexedView.h:112
const internal::remove_all_t< XprType > & nestedExpression() const
Definition: IndexedView.h:134
Index cols() const
Definition: IndexedView.h:130
std::remove_reference_t< XprType > & nestedExpression()
Definition: IndexedView.h:138
Index rows() const
Definition: IndexedView.h:127
const ColIndices & colIndices() const
Definition: IndexedView.h:144
const RowIndices & rowIndices() const
Definition: IndexedView.h:141
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
Namespace containing all symbols from the Eigen library.
Definition: Core:139
const int UndefinedIncr
Definition: Constants.h:33
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int DynamicIndex
Definition: Constants.h:29
const int Dynamic
Definition: Constants.h:24