11#ifndef EIGEN_INDEXED_VIEW_H
12#define EIGEN_INDEXED_VIEW_H
15#include "./InternalHeaderCheck.h"
21template <
typename XprType,
typename RowIndices,
typename ColIndices>
22struct traits<IndexedView<XprType, RowIndices, ColIndices>> : traits<XprType> {
24 RowsAtCompileTime = int(IndexedViewHelper<RowIndices>::SizeAtCompileTime),
25 ColsAtCompileTime = int(IndexedViewHelper<ColIndices>::SizeAtCompileTime),
26 MaxRowsAtCompileTime = RowsAtCompileTime,
27 MaxColsAtCompileTime = ColsAtCompileTime,
29 XprTypeIsRowMajor = (int(traits<XprType>::Flags) &
RowMajorBit) != 0,
30 IsRowMajor = (MaxRowsAtCompileTime == 1 && MaxColsAtCompileTime != 1) ? 1
31 : (MaxColsAtCompileTime == 1 && MaxRowsAtCompileTime != 1) ? 0
34 RowIncr = int(IndexedViewHelper<RowIndices>::IncrAtCompileTime),
35 ColIncr = int(IndexedViewHelper<ColIndices>::IncrAtCompileTime),
36 InnerIncr = IsRowMajor ? ColIncr : RowIncr,
37 OuterIncr = IsRowMajor ? RowIncr : ColIncr,
39 HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
40 XprInnerStride = HasSameStorageOrderAsXprType ? int(inner_stride_at_compile_time<XprType>::value)
41 : int(outer_stride_at_compile_time<XprType>::value),
42 XprOuterstride = HasSameStorageOrderAsXprType ? int(outer_stride_at_compile_time<XprType>::value)
43 : int(inner_stride_at_compile_time<XprType>::value),
45 InnerSize = XprTypeIsRowMajor ? ColsAtCompileTime : RowsAtCompileTime,
46 IsBlockAlike = InnerIncr == 1 && OuterIncr == 1,
48 HasSameStorageOrderAsXprType &&
49 std::is_same<AllRange<InnerSize>, std::conditional_t<XprTypeIsRowMajor, ColIndices, RowIndices>>::value,
51 InnerStrideAtCompileTime =
52 InnerIncr < 0 || InnerIncr == DynamicIndex || XprInnerStride == Dynamic || InnerIncr == Undefined
54 : XprInnerStride * InnerIncr,
55 OuterStrideAtCompileTime =
56 OuterIncr < 0 || OuterIncr == DynamicIndex || XprOuterstride == Dynamic || OuterIncr == Undefined
58 : XprOuterstride * OuterIncr,
60 ReturnAsScalar = is_single_range<RowIndices>::value && is_single_range<ColIndices>::value,
61 ReturnAsBlock = (!ReturnAsScalar) && IsBlockAlike,
62 ReturnAsIndexedView = (!ReturnAsScalar) && (!ReturnAsBlock),
66 DirectAccessMask = (int(InnerIncr) != Undefined && int(OuterIncr) != Undefined && InnerIncr >= 0 && OuterIncr >= 0)
70 FlagsLvalueBit = is_lvalue<XprType>::value ?
LvalueBit : 0,
71 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ?
LinearAccessBit : 0,
74 Flags = (traits<XprType>::Flags & ((HereditaryBits & ~
RowMajorBit) | DirectAccessMask)) | FlagsLvalueBit |
75 FlagsRowMajorBit | FlagsLinearAccessBit
78 using BlockType = Block<XprType, RowsAtCompileTime, ColsAtCompileTime, IsInnerPannel>;
81template <
typename XprType,
typename RowIndices,
typename ColIndices,
typename StorageKind,
bool DirectAccess>
124template <
typename XprType,
typename RowIndices,
typename ColIndices>
126 :
public internal::IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind,
127 (internal::traits<IndexedView<XprType, RowIndices, ColIndices>>::Flags &
128 DirectAccessBit) != 0> {
130 using Base =
typename internal::IndexedViewImpl<
131 XprType, RowIndices, ColIndices,
typename internal::traits<XprType>::StorageKind,
132 (internal::traits<IndexedView<XprType, RowIndices, ColIndices>>::Flags &
DirectAccessBit) != 0>;
133 EIGEN_GENERIC_PUBLIC_INTERFACE(IndexedView)
134 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedView)
136 template <
typename T0,
typename T1>
137 IndexedView(XprType& xpr,
const T0& rowIndices,
const T1& colIndices) : Base(xpr, rowIndices, colIndices) {}
143template <
typename XprType,
typename RowIndices,
typename ColIndices,
typename StorageKind,
bool DirectAccess>
144class IndexedViewImpl :
public internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices>>::type {
146 using Base =
typename internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices>>::type;
147 using MatrixTypeNested =
typename internal::ref_selector<XprType>::non_const_type;
148 using NestedExpression = internal::remove_all_t<XprType>;
149 using Scalar =
typename XprType::Scalar;
151 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedViewImpl)
153 template <
typename T0,
typename T1>
154 IndexedViewImpl(XprType& xpr,
const T0& rowIndices,
const T1& colIndices)
155 : m_xpr(xpr), m_rowIndices(rowIndices), m_colIndices(colIndices) {}
158 Index rows()
const {
return IndexedViewHelper<RowIndices>::size(m_rowIndices); }
161 Index cols()
const {
return IndexedViewHelper<ColIndices>::size(m_colIndices); }
164 const internal::remove_all_t<XprType>& nestedExpression()
const {
return m_xpr; }
167 std::remove_reference_t<XprType>& nestedExpression() {
return m_xpr; }
170 const RowIndices& rowIndices()
const {
return m_rowIndices; }
173 const ColIndices& colIndices()
const {
return m_colIndices; }
175 constexpr Scalar& coeffRef(Index rowId, Index colId) {
176 return nestedExpression().coeffRef(m_rowIndices[rowId], m_colIndices[colId]);
179 constexpr const Scalar& coeffRef(Index rowId, Index colId)
const {
180 return nestedExpression().coeffRef(m_rowIndices[rowId], m_colIndices[colId]);
184 MatrixTypeNested m_xpr;
185 RowIndices m_rowIndices;
186 ColIndices m_colIndices;
189template <
typename XprType,
typename RowIndices,
typename ColIndices,
typename StorageKind>
190class IndexedViewImpl<XprType, RowIndices, ColIndices, StorageKind, true>
191 :
public IndexedViewImpl<XprType, RowIndices, ColIndices, StorageKind, false> {
193 using Base = internal::IndexedViewImpl<XprType, RowIndices, ColIndices,
194 typename internal::traits<XprType>::StorageKind,
false>;
195 using Derived = IndexedView<XprType, RowIndices, ColIndices>;
197 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedViewImpl)
199 template <
typename T0,
typename T1>
200 IndexedViewImpl(XprType& xpr,
const T0& rowIndices,
const T1& colIndices) : Base(xpr, rowIndices, colIndices) {}
202 Index rowIncrement()
const {
203 EIGEN_IF_CONSTEXPR (traits<Derived>::RowIncr != DynamicIndex && traits<Derived>::RowIncr != Undefined) {
204 return traits<Derived>::RowIncr;
206 return IndexedViewHelper<RowIndices>::incr(this->rowIndices());
208 Index colIncrement()
const {
209 EIGEN_IF_CONSTEXPR (traits<Derived>::ColIncr != DynamicIndex && traits<Derived>::ColIncr != Undefined) {
210 return traits<Derived>::ColIncr;
212 return IndexedViewHelper<ColIndices>::incr(this->colIndices());
215 Index innerIncrement()
const {
return traits<Derived>::IsRowMajor ? colIncrement() : rowIncrement(); }
217 Index outerIncrement()
const {
return traits<Derived>::IsRowMajor ? rowIncrement() : colIncrement(); }
219 using ScalarWithConstIfNotLvalue =
220 std::conditional_t<is_lvalue<XprType>::value, std::decay_t<typename XprType::Scalar>,
221 const std::decay_t<typename XprType::Scalar>>;
223 ScalarWithConstIfNotLvalue* data() {
224 Index row_offset = this->rowIndices()[0] * this->nestedExpression().rowStride();
225 Index col_offset = this->colIndices()[0] * this->nestedExpression().colStride();
226 return this->nestedExpression().data() + row_offset + col_offset;
229 const std::decay_t<typename XprType::Scalar>* data()
const {
230 Index row_offset = this->rowIndices()[0] * this->nestedExpression().rowStride();
231 Index col_offset = this->colIndices()[0] * this->nestedExpression().colStride();
232 return this->nestedExpression().data() + row_offset + col_offset;
235 EIGEN_DEVICE_FUNC
constexpr Index innerStride() const noexcept {
236 EIGEN_IF_CONSTEXPR (traits<Derived>::InnerStrideAtCompileTime != Dynamic) {
237 return traits<Derived>::InnerStrideAtCompileTime;
240 return traits<Derived>::IsRowMajor ? colIncrement() * this->nestedExpression().colStride()
241 : rowIncrement() * this->nestedExpression().rowStride();
244 EIGEN_DEVICE_FUNC
constexpr Index outerStride() const noexcept {
245 EIGEN_IF_CONSTEXPR (traits<Derived>::OuterStrideAtCompileTime != Dynamic) {
246 return traits<Derived>::OuterStrideAtCompileTime;
248 return traits<Derived>::IsRowMajor ? rowIncrement() * this->nestedExpression().rowStride()
249 : colIncrement() * this->nestedExpression().colStride();
253template <
typename ArgType,
typename RowIndices,
typename ColIndices>
254struct unary_evaluator<IndexedView<ArgType, RowIndices, ColIndices>, IndexBased>
255 : evaluator_base<IndexedView<ArgType, RowIndices, ColIndices>> {
256 using XprType = IndexedView<ArgType, RowIndices, ColIndices>;
259 CoeffReadCost = evaluator<ArgType>::CoeffReadCost ,
261 FlagsLinearAccessBit =
262 (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1) ? LinearAccessBit : 0,
264 FlagsRowMajorBit = traits<XprType>::FlagsRowMajorBit,
266 Flags = (evaluator<ArgType>::Flags & (HereditaryBits & ~
RowMajorBit )) |
267 FlagsLinearAccessBit | FlagsRowMajorBit,
272 EIGEN_DEVICE_FUNC
constexpr explicit unary_evaluator(
const XprType& xpr)
273 : m_argImpl(xpr.nestedExpression()), m_xpr(xpr) {
274 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
277 using Scalar =
typename XprType::Scalar;
278 using CoeffReturnType =
typename XprType::CoeffReturnType;
280 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col)
const {
281 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
282 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
283 return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
286 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
287 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
288 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
289 return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
292 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
293 EIGEN_STATIC_ASSERT_LVALUE(XprType)
294 Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
295 Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
296 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
297 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
298 return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
301 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef(Index index)
const {
302 Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
303 Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
304 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
305 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
306 return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
309 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType coeff(Index index)
const {
310 Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
311 Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
312 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
313 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
314 return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
318 evaluator<ArgType> m_argImpl;
319 const XprType& m_xpr;
323template <
typename ArgType,
typename RowIndices,
typename ColIndices>
324struct evaluator_assume_aliasing<IndexedView<ArgType, RowIndices, ColIndices>> : std::true_type {};
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71