Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_INDEXED_VIEW_H
12#define EIGEN_INDEXED_VIEW_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename XprType, typename RowIndices, typename ColIndices>
22struct traits<IndexedView<XprType, RowIndices, ColIndices>> : traits<XprType> {
23 enum {
24 RowsAtCompileTime = int(IndexedViewHelper<RowIndices>::SizeAtCompileTime),
25 ColsAtCompileTime = int(IndexedViewHelper<ColIndices>::SizeAtCompileTime),
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(IndexedViewHelper<RowIndices>::IncrAtCompileTime),
35 ColIncr = int(IndexedViewHelper<ColIndices>::IncrAtCompileTime),
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>::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),
44
45 InnerSize = XprTypeIsRowMajor ? ColsAtCompileTime : RowsAtCompileTime,
46 IsBlockAlike = InnerIncr == 1 && OuterIncr == 1,
47 IsInnerPannel =
48 HasSameStorageOrderAsXprType &&
49 std::is_same<AllRange<InnerSize>, std::conditional_t<XprTypeIsRowMajor, ColIndices, RowIndices>>::value,
50
51 InnerStrideAtCompileTime =
52 InnerIncr < 0 || InnerIncr == DynamicIndex || XprInnerStride == Dynamic || InnerIncr == Undefined
53 ? Dynamic
54 : XprInnerStride * InnerIncr,
55 OuterStrideAtCompileTime =
56 OuterIncr < 0 || OuterIncr == DynamicIndex || XprOuterstride == Dynamic || OuterIncr == Undefined
57 ? Dynamic
58 : XprOuterstride * OuterIncr,
59
60 ReturnAsScalar = is_single_range<RowIndices>::value && is_single_range<ColIndices>::value,
61 ReturnAsBlock = (!ReturnAsScalar) && IsBlockAlike,
62 ReturnAsIndexedView = (!ReturnAsScalar) && (!ReturnAsBlock),
63
64 // FIXME: we deal with compile-time strides if and only if we have DirectAccessBit flag,
65 // but this is too strict regarding negative strides...
66 DirectAccessMask = (int(InnerIncr) != Undefined && int(OuterIncr) != Undefined && InnerIncr >= 0 && OuterIncr >= 0)
68 : 0,
69 FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
70 FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
71 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
72 // IsRowMajor above pins a single-column view to column-major and a single-row view to
73 // row-major, so the nested expression's RowMajorBit must not be inherited on top of it.
74 Flags = (traits<XprType>::Flags & ((HereditaryBits & ~RowMajorBit) | DirectAccessMask)) | FlagsLvalueBit |
75 FlagsRowMajorBit | FlagsLinearAccessBit
76 };
77
78 using BlockType = Block<XprType, RowsAtCompileTime, ColsAtCompileTime, IsInnerPannel>;
79};
80
81template <typename XprType, typename RowIndices, typename ColIndices, typename StorageKind, bool DirectAccess>
82class IndexedViewImpl;
83
84} // namespace internal
85
124template <typename XprType, typename RowIndices, typename ColIndices>
125class IndexedView
126 : public internal::IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind,
127 (internal::traits<IndexedView<XprType, RowIndices, ColIndices>>::Flags &
128 DirectAccessBit) != 0> {
129 public:
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)
135
136 template <typename T0, typename T1>
137 IndexedView(XprType& xpr, const T0& rowIndices, const T1& colIndices) : Base(xpr, rowIndices, colIndices) {}
138};
139
140namespace internal {
141
142// Generic API dispatcher
143template <typename XprType, typename RowIndices, typename ColIndices, typename StorageKind, bool DirectAccess>
144class IndexedViewImpl : public internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices>>::type {
145 public:
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;
150
151 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedViewImpl)
152
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) {}
156
158 Index rows() const { return IndexedViewHelper<RowIndices>::size(m_rowIndices); }
159
161 Index cols() const { return IndexedViewHelper<ColIndices>::size(m_colIndices); }
162
164 const internal::remove_all_t<XprType>& nestedExpression() const { return m_xpr; }
165
167 std::remove_reference_t<XprType>& nestedExpression() { return m_xpr; }
168
170 const RowIndices& rowIndices() const { return m_rowIndices; }
171
173 const ColIndices& colIndices() const { return m_colIndices; }
174
175 constexpr Scalar& coeffRef(Index rowId, Index colId) {
176 return nestedExpression().coeffRef(m_rowIndices[rowId], m_colIndices[colId]);
177 }
178
179 constexpr const Scalar& coeffRef(Index rowId, Index colId) const {
180 return nestedExpression().coeffRef(m_rowIndices[rowId], m_colIndices[colId]);
181 }
182
183 // The two-index overloads above hide the base's linear coeffRef(), so vector views need their own.
184 constexpr Scalar& coeffRef(Index index) {
185 EIGEN_STATIC_ASSERT(Base::IsVectorAtCompileTime, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
186 return coeffRef(Base::RowsAtCompileTime == 1 ? 0 : index, Base::RowsAtCompileTime == 1 ? index : 0);
187 }
188
189 constexpr const Scalar& coeffRef(Index index) const {
190 EIGEN_STATIC_ASSERT(Base::IsVectorAtCompileTime, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
191 return coeffRef(Base::RowsAtCompileTime == 1 ? 0 : index, Base::RowsAtCompileTime == 1 ? index : 0);
192 }
193
194 protected:
195 MatrixTypeNested m_xpr;
196 RowIndices m_rowIndices;
197 ColIndices m_colIndices;
198};
199
200template <typename XprType, typename RowIndices, typename ColIndices, typename StorageKind>
201class IndexedViewImpl<XprType, RowIndices, ColIndices, StorageKind, true>
202 : public IndexedViewImpl<XprType, RowIndices, ColIndices, StorageKind, false> {
203 public:
204 using Base = internal::IndexedViewImpl<XprType, RowIndices, ColIndices,
205 typename internal::traits<XprType>::StorageKind, false>;
206 using Derived = IndexedView<XprType, RowIndices, ColIndices>;
207
208 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedViewImpl)
209
210 template <typename T0, typename T1>
211 IndexedViewImpl(XprType& xpr, const T0& rowIndices, const T1& colIndices) : Base(xpr, rowIndices, colIndices) {}
212
213 Index rowIncrement() const {
214 EIGEN_IF_CONSTEXPR (traits<Derived>::RowIncr != DynamicIndex && traits<Derived>::RowIncr != Undefined) {
215 return traits<Derived>::RowIncr;
216 }
217 return IndexedViewHelper<RowIndices>::incr(this->rowIndices());
218 }
219 Index colIncrement() const {
220 EIGEN_IF_CONSTEXPR (traits<Derived>::ColIncr != DynamicIndex && traits<Derived>::ColIncr != Undefined) {
221 return traits<Derived>::ColIncr;
222 }
223 return IndexedViewHelper<ColIndices>::incr(this->colIndices());
224 }
225
226 Index innerIncrement() const { return traits<Derived>::IsRowMajor ? colIncrement() : rowIncrement(); }
227
228 Index outerIncrement() const { return traits<Derived>::IsRowMajor ? rowIncrement() : colIncrement(); }
229
230 using ScalarWithConstIfNotLvalue =
231 std::conditional_t<is_lvalue<XprType>::value, std::decay_t<typename XprType::Scalar>,
232 const std::decay_t<typename XprType::Scalar>>;
233
234 ScalarWithConstIfNotLvalue* data() {
235 Index row_offset = this->rowIndices()[0] * this->nestedExpression().rowStride();
236 Index col_offset = this->colIndices()[0] * this->nestedExpression().colStride();
237 return this->nestedExpression().data() + row_offset + col_offset;
238 }
239
240 const std::decay_t<typename XprType::Scalar>* data() const {
241 Index row_offset = this->rowIndices()[0] * this->nestedExpression().rowStride();
242 Index col_offset = this->colIndices()[0] * this->nestedExpression().colStride();
243 return this->nestedExpression().data() + row_offset + col_offset;
244 }
245
246 EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept {
247 EIGEN_IF_CONSTEXPR (traits<Derived>::InnerStrideAtCompileTime != Dynamic) {
248 return traits<Derived>::InnerStrideAtCompileTime;
249 }
250 // A vector-shaped view need not share the nested storage order, so step along the view's own inner dimension.
251 return traits<Derived>::IsRowMajor ? colIncrement() * this->nestedExpression().colStride()
252 : rowIncrement() * this->nestedExpression().rowStride();
253 }
254
255 EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept {
256 EIGEN_IF_CONSTEXPR (traits<Derived>::OuterStrideAtCompileTime != Dynamic) {
257 return traits<Derived>::OuterStrideAtCompileTime;
258 }
259 return traits<Derived>::IsRowMajor ? rowIncrement() * this->nestedExpression().rowStride()
260 : colIncrement() * this->nestedExpression().colStride();
261 }
262};
263
264template <typename ArgType, typename RowIndices, typename ColIndices>
265struct unary_evaluator<IndexedView<ArgType, RowIndices, ColIndices>, IndexBased>
266 : evaluator_base<IndexedView<ArgType, RowIndices, ColIndices>> {
267 using XprType = IndexedView<ArgType, RowIndices, ColIndices>;
268
269 enum {
270 CoeffReadCost = evaluator<ArgType>::CoeffReadCost /* TODO + cost of row/col index */,
271
272 FlagsLinearAccessBit =
273 (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1) ? LinearAccessBit : 0,
274
275 FlagsRowMajorBit = traits<XprType>::FlagsRowMajorBit,
276
277 Flags = (evaluator<ArgType>::Flags & (HereditaryBits & ~RowMajorBit /*| LinearAccessBit | DirectAccessBit*/)) |
278 FlagsLinearAccessBit | FlagsRowMajorBit,
279
280 Alignment = 0
281 };
282
283 EIGEN_DEVICE_FUNC constexpr explicit unary_evaluator(const XprType& xpr)
284 : m_argImpl(xpr.nestedExpression()), m_xpr(xpr) {
285 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
286 }
287
288 using Scalar = typename XprType::Scalar;
289 using CoeffReturnType = typename XprType::CoeffReturnType;
290
291 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const {
292 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
293 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
294 return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
295 }
296
297 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
298 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
299 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
300 return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
301 }
302
303 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
304 EIGEN_STATIC_ASSERT_LVALUE(XprType)
305 Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
306 Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
307 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
308 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
309 return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
310 }
311
312 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar& coeffRef(Index index) const {
313 Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
314 Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
315 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
316 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
317 return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
318 }
319
320 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index index) const {
321 Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
322 Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
323 eigen_assert(m_xpr.rowIndices()[row] >= 0 && m_xpr.rowIndices()[row] < m_xpr.nestedExpression().rows() &&
324 m_xpr.colIndices()[col] >= 0 && m_xpr.colIndices()[col] < m_xpr.nestedExpression().cols());
325 return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
326 }
327
328 protected:
329 evaluator<ArgType> m_argImpl;
330 const XprType& m_xpr;
331};
332
333// Catch assignments to an IndexedView.
334template <typename ArgType, typename RowIndices, typename ColIndices>
335struct evaluator_assume_aliasing<IndexedView<ArgType, RowIndices, ColIndices>> : std::true_type {};
336
337} // end namespace internal
338
339} // end namespace Eigen
340
341#endif // EIGEN_INDEXED_VIEW_H
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