Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SparseBlock.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 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_SPARSE_BLOCK_H
12#define EIGEN_SPARSE_BLOCK_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19// Subset of columns or rows
20template <typename XprType, int BlockRows, int BlockCols>
21class BlockImpl<XprType, BlockRows, BlockCols, true, Sparse>
22 : public SparseCompressedBase<Block<XprType, BlockRows, BlockCols, true> > {
23 using BlockType = Block<XprType, BlockRows, BlockCols, true>;
24
25 public:
26 enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
27
28 protected:
29 enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
30 using Base = SparseCompressedBase<BlockType>;
31 using Base::convert_index;
32
33 public:
34 EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
35
36 inline BlockImpl(XprType& xpr, Index i) : m_matrix(xpr), m_outerStart(convert_index(i)), m_outerSize(OuterSize) {}
37
38 inline BlockImpl(XprType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
39 : m_matrix(xpr),
40 m_outerStart(convert_index(IsRowMajor ? startRow : startCol)),
41 m_outerSize(convert_index(IsRowMajor ? blockRows : blockCols)) {}
42
43 EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
44 EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
45
46 Index nonZeros() const {
47 return nonZerosImpl(std::integral_constant<bool, (internal::traits<XprType>::Flags & CompressedAccessBit) != 0>());
48 }
49
50 protected:
51 Index nonZerosImpl(std::true_type) const {
52 if (m_outerSize.value() == 0) return 0;
53 const StorageIndex* outer = m_matrix.outerIndexPtr();
54 if (!outer) return m_matrix.nonZeros();
55 const StorageIndex* innerNonZeros = m_matrix.innerNonZeroPtr();
56 if (!innerNonZeros) return outer[m_outerStart + m_outerSize.value()] - outer[m_outerStart];
57 Index nnz = 0;
58 for (Index j = m_outerStart; j < m_outerStart + m_outerSize.value(); ++j) nnz += innerNonZeros[j];
59 return nnz;
60 }
61
62 Index nonZerosImpl(std::false_type) const {
63 using EvaluatorType = internal::evaluator<XprType>;
64 EvaluatorType matEval(m_matrix);
65 Index nnz = 0;
66 Index end = m_outerStart + m_outerSize.value();
67 for (Index j = m_outerStart; j < end; ++j)
68 for (typename EvaluatorType::InnerIterator it(matEval, j); it; ++it) ++nnz;
69 return nnz;
70 }
71
72 public:
73 inline const Scalar coeff(Index row, Index col) const {
74 return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
75 }
76
77 inline const Scalar coeff(Index index) const {
78 return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
79 }
80
81 inline const XprType& nestedExpression() const { return m_matrix; }
82 inline XprType& nestedExpression() { return m_matrix; }
83 Index startRow() const { return IsRowMajor ? m_outerStart : 0; }
84 Index startCol() const { return IsRowMajor ? 0 : m_outerStart; }
85 Index blockRows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
86 Index blockCols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
87
88 inline const Scalar* valuePtr() const { return m_matrix.valuePtr(); }
89 inline Scalar* valuePtr() { return m_matrix.valuePtr(); }
90
91 inline const StorageIndex* innerIndexPtr() const { return m_matrix.innerIndexPtr(); }
92 inline StorageIndex* innerIndexPtr() { return m_matrix.innerIndexPtr(); }
93
94 inline const StorageIndex* outerIndexPtr() const {
95 const StorageIndex* p = m_matrix.outerIndexPtr();
96 return p ? p + m_outerStart : 0;
97 }
98 inline StorageIndex* outerIndexPtr() {
99 StorageIndex* p = m_matrix.outerIndexPtr();
100 return p ? p + m_outerStart : 0;
101 }
102
103 inline const StorageIndex* innerNonZeroPtr() const {
104 const StorageIndex* p = m_matrix.innerNonZeroPtr();
105 return p ? p + m_outerStart : 0;
106 }
107 inline StorageIndex* innerNonZeroPtr() {
108 StorageIndex* p = m_matrix.innerNonZeroPtr();
109 return p ? p + m_outerStart : 0;
110 }
111
112 bool isCompressed() const { return m_matrix.innerNonZeroPtr() == 0; }
113
114 protected:
115 typename internal::ref_selector<XprType>::non_const_type m_matrix;
116 Index m_outerStart;
117 const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
118
119 protected:
120 // Disable assignment with clear error message.
121 // Note that simply removing operator= yields compilation errors with ICC+MSVC
122 template <typename T>
123 BlockImpl& operator=(const T&) {
124 EIGEN_STATIC_ASSERT(sizeof(T) == 0, THIS_SPARSE_BLOCK_SUBEXPRESSION_IS_READ_ONLY);
125 return *this;
126 }
127};
128
129/***************************************************************************
130 * specialization for SparseMatrix
131 ***************************************************************************/
132
133namespace internal {
134
135template <typename SparseMatrixType, int BlockRows, int BlockCols>
136class sparse_matrix_block_impl : public SparseCompressedBase<Block<SparseMatrixType, BlockRows, BlockCols, true> > {
137 using BlockType = Block<SparseMatrixType, BlockRows, BlockCols, true>;
139 using Base::convert_index;
140
141 public:
142 enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
143 EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
144 protected:
145 enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
146
147 public:
148 inline sparse_matrix_block_impl(SparseMatrixType& xpr, Index i)
149 : m_matrix(xpr), m_outerStart(convert_index(i)), m_outerSize(OuterSize) {}
150
151 inline sparse_matrix_block_impl(SparseMatrixType& xpr, Index startRow, Index startCol, Index blockRows,
152 Index blockCols)
153 : m_matrix(xpr),
154 m_outerStart(convert_index(IsRowMajor ? startRow : startCol)),
155 m_outerSize(convert_index(IsRowMajor ? blockRows : blockCols)) {}
156
157 template <typename OtherDerived>
158 inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other) {
159 using NestedMatrixType_ = internal::remove_all_t<typename SparseMatrixType::Nested>;
160 NestedMatrixType_& matrix = m_matrix;
161 // This assignment is slow if this vector set is not empty
162 // and/or it is not at the end of the nonzeros of the underlying matrix.
163
164 // 1 - eval to a temporary to avoid transposition and/or aliasing issues. The entries are copied as one contiguous
165 // range below, so an uncompressed source is compressed into a copy rather than bound in place.
166 Ref<const SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, StorageIndex>, StandardCompressedFormat> tmp(
167 other.derived());
168 eigen_internal_assert(tmp.outerSize() == m_outerSize.value());
169 // the Ref binds to a slice of this very matrix without copying, e.g., A.col(0) = A.col(1)
170 const Scalar* first = matrix.valuePtr();
171 if (tmp.nonZeros() > 0 && std::less_equal<const Scalar*>()(first, tmp.valuePtr()) &&
172 std::less<const Scalar*>()(tmp.valuePtr(), first + matrix.data().allocatedSize()))
173 return operator=(SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, StorageIndex>(tmp));
174
175 // 2 - let's check whether there is enough allocated memory
176 Index nnz = tmp.nonZeros();
177 Index start =
178 m_outerStart == 0 ? 0 : m_matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
179 Index end = m_matrix.outerIndexPtr()[m_outerStart + m_outerSize.value()]; // ending position of the current block
180 Index block_size = end - start; // available room in the current block
181 Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
182
183 Index free_size = m_matrix.isCompressed() ? Index(matrix.data().allocatedSize()) + block_size : block_size;
184
185 Index tmp_start = tmp.outerIndexPtr()[0];
186
187 bool update_trailing_pointers = false;
188 if (nnz > free_size) {
189 // realloc manually to reduce copies; the old buffer is released below, so its entries are moved
190 typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
191
192 internal::move_or_copy(matrix.valuePtr(), matrix.valuePtr() + start, newdata.valuePtr());
193 internal::smart_copy(m_matrix.innerIndexPtr(), m_matrix.innerIndexPtr() + start, newdata.indexPtr());
194
195 internal::smart_copy(tmp.valuePtr() + tmp_start, tmp.valuePtr() + tmp_start + nnz, newdata.valuePtr() + start);
196 internal::smart_copy(tmp.innerIndexPtr() + tmp_start, tmp.innerIndexPtr() + tmp_start + nnz,
197 newdata.indexPtr() + start);
198
199 internal::move_or_copy(matrix.valuePtr() + end, matrix.valuePtr() + end + tail_size,
200 newdata.valuePtr() + start + nnz);
201 internal::smart_copy(matrix.innerIndexPtr() + end, matrix.innerIndexPtr() + end + tail_size,
202 newdata.indexPtr() + start + nnz);
203
204 newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
205
206 matrix.data().swap(newdata);
207
208 update_trailing_pointers = true;
209 } else {
210 if (m_matrix.isCompressed() && nnz != block_size) {
211 // no need to realloc, simply copy the tail at its respective position and insert tmp
212 matrix.data().resize(start + nnz + tail_size);
213
214 Scalar* values = matrix.valuePtr();
215 StorageIndex* indices = matrix.innerIndexPtr();
216 const Index tail_end = end + tail_size;
217 if (nnz < block_size) {
218 internal::move_or_copy(values + end, values + tail_end, values + start + nnz);
219 std::move(indices + end, indices + tail_end, indices + start + nnz);
220 } else {
221 internal::move_or_copy_backward(values + end, values + tail_end, values + start + nnz + tail_size);
222 std::move_backward(indices + end, indices + tail_end, indices + start + nnz + tail_size);
223 }
224
225 update_trailing_pointers = true;
226 }
227
228 internal::smart_copy(tmp.valuePtr() + tmp_start, tmp.valuePtr() + tmp_start + nnz, matrix.valuePtr() + start);
229 internal::smart_copy(tmp.innerIndexPtr() + tmp_start, tmp.innerIndexPtr() + tmp_start + nnz,
230 matrix.innerIndexPtr() + start);
231 }
232
233 // update outer index pointers and innerNonZeros
234 EIGEN_IF_CONSTEXPR (IsVectorAtCompileTime) {
235 if (!m_matrix.isCompressed()) matrix.innerNonZeroPtr()[m_outerStart] = StorageIndex(nnz);
236 matrix.outerIndexPtr()[m_outerStart] = StorageIndex(start);
237 } else {
238 StorageIndex p = StorageIndex(start);
239 for (Index k = 0; k < m_outerSize.value(); ++k) {
240 StorageIndex nnz_k = internal::convert_index<StorageIndex>(tmp.innerVector(k).nonZeros());
241 if (!m_matrix.isCompressed()) matrix.innerNonZeroPtr()[m_outerStart + k] = nnz_k;
242 matrix.outerIndexPtr()[m_outerStart + k] = p;
243 p += nnz_k;
244 }
245 }
246
247 if (update_trailing_pointers) {
248 StorageIndex offset = internal::convert_index<StorageIndex>(nnz - block_size);
249 for (Index k = m_outerStart + m_outerSize.value(); k <= matrix.outerSize(); ++k) {
250 matrix.outerIndexPtr()[k] += offset;
251 }
252 }
253
254 return derived();
255 }
256
257 inline BlockType& operator=(const BlockType& other) { return operator= <BlockType>(other); }
258
259 inline const Scalar* valuePtr() const { return m_matrix.valuePtr(); }
260 inline Scalar* valuePtr() { return m_matrix.valuePtr(); }
261
262 inline const StorageIndex* innerIndexPtr() const { return m_matrix.innerIndexPtr(); }
263 inline StorageIndex* innerIndexPtr() { return m_matrix.innerIndexPtr(); }
264
265 inline const StorageIndex* outerIndexPtr() const { return m_matrix.outerIndexPtr() + m_outerStart; }
266 inline StorageIndex* outerIndexPtr() { return m_matrix.outerIndexPtr() + m_outerStart; }
267
268 inline const StorageIndex* innerNonZeroPtr() const {
269 return isCompressed() ? 0 : (m_matrix.innerNonZeroPtr() + m_outerStart);
270 }
271 inline StorageIndex* innerNonZeroPtr() { return isCompressed() ? 0 : (m_matrix.innerNonZeroPtr() + m_outerStart); }
272
273 bool isCompressed() const { return m_matrix.innerNonZeroPtr() == 0; }
274
275 inline Scalar& coeffRef(Index row, Index col) {
276 return m_matrix.coeffRef(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
277 }
278
279 inline const Scalar coeff(Index row, Index col) const {
280 return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
281 }
282
283 inline const Scalar coeff(Index index) const {
284 return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
285 }
286
287 const Scalar& lastCoeff() const {
288 EIGEN_STATIC_ASSERT_VECTOR_ONLY(sparse_matrix_block_impl);
289 eigen_assert(Base::nonZeros() > 0);
290 if (m_matrix.isCompressed())
291 return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart + 1] - 1];
292 else
293 return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart] + m_matrix.innerNonZeroPtr()[m_outerStart] - 1];
294 }
295
296 EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
297 EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
298
299 inline const SparseMatrixType& nestedExpression() const { return m_matrix; }
300 inline SparseMatrixType& nestedExpression() { return m_matrix; }
301 Index startRow() const { return IsRowMajor ? m_outerStart : 0; }
302 Index startCol() const { return IsRowMajor ? 0 : m_outerStart; }
303 Index blockRows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
304 Index blockCols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
305
306 protected:
307 typename internal::ref_selector<SparseMatrixType>::non_const_type m_matrix;
308 Index m_outerStart;
309 const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
310};
311
312} // namespace internal
313
314template <typename Scalar_, int Options_, typename StorageIndex_, int BlockRows, int BlockCols>
315class BlockImpl<SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true, Sparse>
316 : public internal::sparse_matrix_block_impl<SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols> {
317 public:
318 using StorageIndex = StorageIndex_;
319 using SparseMatrixType = SparseMatrix<Scalar_, Options_, StorageIndex_>;
320 using Base = internal::sparse_matrix_block_impl<SparseMatrixType, BlockRows, BlockCols>;
321 inline BlockImpl(SparseMatrixType& xpr, Index i) : Base(xpr, i) {}
322
323 inline BlockImpl(SparseMatrixType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
324 : Base(xpr, startRow, startCol, blockRows, blockCols) {}
325
326 using Base::operator=;
327};
328
329template <typename Scalar_, int Options_, typename StorageIndex_, int BlockRows, int BlockCols>
330class BlockImpl<const SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true, Sparse>
331 : public internal::sparse_matrix_block_impl<const SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows,
332 BlockCols> {
333 public:
334 using StorageIndex = StorageIndex_;
335 using SparseMatrixType = const SparseMatrix<Scalar_, Options_, StorageIndex_>;
336 using Base = internal::sparse_matrix_block_impl<SparseMatrixType, BlockRows, BlockCols>;
337 inline BlockImpl(SparseMatrixType& xpr, Index i) : Base(xpr, i) {}
338
339 inline BlockImpl(SparseMatrixType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
340 : Base(xpr, startRow, startCol, blockRows, blockCols) {}
341
342 using Base::operator=;
343
344 private:
345 template <typename Derived>
346 BlockImpl(const SparseMatrixBase<Derived>& xpr, Index i);
347 template <typename Derived>
348 BlockImpl(const SparseMatrixBase<Derived>& xpr);
349};
350
351//----------
352
356template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
357class BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, Sparse>
358 : public SparseMatrixBase<Block<XprType, BlockRows, BlockCols, InnerPanel> >, internal::no_assignment_operator {
360 using Base = SparseMatrixBase<BlockType>;
361 using Base::convert_index;
362
363 public:
364 enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
365 EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
366
367 using MatrixTypeNested_ = internal::remove_all_t<typename XprType::Nested>;
368
371 inline BlockImpl(XprType& xpr, Index i)
372 : m_matrix(xpr),
373 m_startRow((BlockRows == 1) && (BlockCols == XprType::ColsAtCompileTime) ? convert_index(i) : 0),
374 m_startCol((BlockRows == XprType::RowsAtCompileTime) && (BlockCols == 1) ? convert_index(i) : 0),
375 m_blockRows(BlockRows == 1 ? 1 : xpr.rows()),
376 m_blockCols(BlockCols == 1 ? 1 : xpr.cols()) {}
377
380 inline BlockImpl(XprType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
381 : m_matrix(xpr),
382 m_startRow(convert_index(startRow)),
383 m_startCol(convert_index(startCol)),
384 m_blockRows(convert_index(blockRows)),
385 m_blockCols(convert_index(blockCols)) {}
386
387 inline Index rows() const { return m_blockRows.value(); }
388 inline Index cols() const { return m_blockCols.value(); }
389
390 inline Scalar& coeffRef(Index row, Index col) {
391 return m_matrix.coeffRef(row + m_startRow.value(), col + m_startCol.value());
392 }
393
394 inline const Scalar coeff(Index row, Index col) const {
395 return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
396 }
397
398 inline Scalar& coeffRef(Index index) {
399 return m_matrix.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
400 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
401 }
402
403 inline const Scalar coeff(Index index) const {
404 return m_matrix.coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
405 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
406 }
407
408 inline const XprType& nestedExpression() const { return m_matrix; }
409 inline XprType& nestedExpression() { return m_matrix; }
410 Index startRow() const { return m_startRow.value(); }
411 Index startCol() const { return m_startCol.value(); }
412 Index blockRows() const { return m_blockRows.value(); }
413 Index blockCols() const { return m_blockCols.value(); }
414
415 protected:
416 friend struct internal::unary_evaluator<Block<XprType, BlockRows, BlockCols, InnerPanel>, internal::IteratorBased,
417 Scalar>;
418
419 Index nonZeros() const { return Dynamic; }
420
421 typename internal::ref_selector<XprType>::non_const_type m_matrix;
422 const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
423 const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
424 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
425 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
426
427 protected:
428 // Disable assignment with clear error message.
429 // Note that simply removing operator= yields compilation errors with ICC+MSVC
430 template <typename T>
431 BlockImpl& operator=(const T&) {
432 EIGEN_STATIC_ASSERT(sizeof(T) == 0, THIS_SPARSE_BLOCK_SUBEXPRESSION_IS_READ_ONLY);
433 return *this;
434 }
435};
436
437namespace internal {
438
439template <typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
440struct unary_evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel>, IteratorBased>
441 : public evaluator_base<Block<ArgType, BlockRows, BlockCols, InnerPanel> > {
442 class InnerVectorInnerIterator;
443 class OuterVectorInnerIterator;
444
445 public:
446 using XprType = Block<ArgType, BlockRows, BlockCols, InnerPanel>;
447 using StorageIndex = typename XprType::StorageIndex;
448 using Scalar = typename XprType::Scalar;
449
450 enum {
451 IsRowMajor = XprType::IsRowMajor,
452 OuterVector = (BlockCols == 1 && ArgType::IsRowMajor) || (BlockRows == 1 && !ArgType::IsRowMajor),
453 CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
454 Flags = XprType::Flags
455 };
456
457 using InnerIterator = std::conditional_t<OuterVector, OuterVectorInnerIterator, InnerVectorInnerIterator>;
458
459 explicit unary_evaluator(const XprType& op) : m_argImpl(op.nestedExpression()), m_block(op) {}
460
461 inline Index nonZerosEstimate() const {
462 const Index nnz = m_block.nonZeros();
463 if (nnz < 0) {
464 // Scale the non-zero estimate for the underlying expression linearly with block size.
465 // Return zero if the underlying block is empty.
466 const Index nested_sz = m_block.nestedExpression().size();
467 return nested_sz == 0 ? 0 : m_argImpl.nonZerosEstimate() * m_block.size() / nested_sz;
468 }
469 return nnz;
470 }
471
472 protected:
473 using EvalIterator = typename evaluator<ArgType>::InnerIterator;
474
475 evaluator<ArgType> m_argImpl;
476 const XprType& m_block;
477};
478
479template <typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
480class unary_evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel>, IteratorBased>::InnerVectorInnerIterator
481 : public EvalIterator {
482 // NOTE MSVC fails to compile if we don't explicitly "import" IsRowMajor from unary_evaluator
483 // because the base class EvalIterator has a private IsRowMajor enum too. (bug #1786)
484 // NOTE We cannot call it IsRowMajor because it would shadow unary_evaluator::IsRowMajor
485 enum { XprIsRowMajor = unary_evaluator::IsRowMajor };
486 const XprType& m_block;
487 Index m_end;
488
489 public:
490 EIGEN_STRONG_INLINE InnerVectorInnerIterator(const unary_evaluator& aEval, Index outer)
491 : EvalIterator(aEval.m_argImpl, outer + (XprIsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol())),
492 m_block(aEval.m_block),
493 m_end(XprIsRowMajor ? aEval.m_block.startCol() + aEval.m_block.blockCols()
494 : aEval.m_block.startRow() + aEval.m_block.blockRows()) {
495 while ((EvalIterator::operator bool()) &&
496 (EvalIterator::index() < (XprIsRowMajor ? m_block.startCol() : m_block.startRow())))
497 EvalIterator::operator++();
498 }
499
500 inline StorageIndex index() const {
501 return EvalIterator::index() - convert_index<StorageIndex>(XprIsRowMajor ? m_block.startCol() : m_block.startRow());
502 }
503 inline Index outer() const {
504 return EvalIterator::outer() - (XprIsRowMajor ? m_block.startRow() : m_block.startCol());
505 }
506 inline Index row() const { return EvalIterator::row() - m_block.startRow(); }
507 inline Index col() const { return EvalIterator::col() - m_block.startCol(); }
508
509 inline operator bool() const { return EvalIterator::operator bool() && EvalIterator::index() < m_end; }
510};
511
512template <typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
513class unary_evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel>, IteratorBased>::OuterVectorInnerIterator {
514 // NOTE see above. The block crosses the outer vectors of ArgType; a 1x1 block's own flag is ArgType's.
515 enum { XprIsRowMajor = int(ArgType::IsRowMajor) == 0 };
516 // The iterator on an outer vector of ArgType exists only while that vector lies in the block. An empty block can
517 // start at or past ArgType's last outer vector, or ArgType can have none at all (a 3x0 column-major matrix), so
518 // there may be nothing to open an iterator on.
519 union ItStorage {
520 char unused;
521 EvalIterator it;
522 ItStorage() : unused() {}
523 ~ItStorage() {}
524 };
525 const unary_evaluator& m_eval;
526 Index m_outerPos;
527 const Index m_innerIndex;
528 Index m_end;
529 bool m_hasIt;
530 ItStorage m_storage;
531
532 void openIterator() {
533 if (m_hasIt) internal::destroy_at(&m_storage.it);
534 m_hasIt = false;
535 internal::construct_at(&m_storage.it, m_eval.m_argImpl, m_outerPos);
536 m_hasIt = true;
537 }
538
539 public:
540 EIGEN_STRONG_INLINE OuterVectorInnerIterator(const unary_evaluator& aEval, Index outer)
541 : m_eval(aEval),
542 m_outerPos((XprIsRowMajor ? aEval.m_block.startCol() : aEval.m_block.startRow())),
543 m_innerIndex(XprIsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol()),
544 m_end(XprIsRowMajor ? aEval.m_block.startCol() + aEval.m_block.blockCols()
545 : aEval.m_block.startRow() + aEval.m_block.blockRows()),
546 m_hasIt(false) {
547 EIGEN_UNUSED_VARIABLE(outer);
548 eigen_assert(outer == 0);
549
550 if (m_outerPos >= m_end) return;
551 openIterator();
552 while (m_storage.it && m_storage.it.index() < m_innerIndex) ++m_storage.it;
553 if ((!m_storage.it) || (m_storage.it.index() != m_innerIndex)) ++(*this);
554 }
555
556 OuterVectorInnerIterator(const OuterVectorInnerIterator& other)
557 : m_eval(other.m_eval),
558 m_outerPos(other.m_outerPos),
559 m_innerIndex(other.m_innerIndex),
560 m_end(other.m_end),
561 m_hasIt(other.m_hasIt) {
562 if (m_hasIt) internal::construct_at(&m_storage.it, other.m_storage.it);
563 }
564
565 ~OuterVectorInnerIterator() {
566 if (m_hasIt) internal::destroy_at(&m_storage.it);
567 }
568
569 inline StorageIndex index() const {
570 return convert_index<StorageIndex>(m_outerPos -
571 (XprIsRowMajor ? m_eval.m_block.startCol() : m_eval.m_block.startRow()));
572 }
573 inline Index outer() const { return 0; }
574 inline Index row() const { return XprIsRowMajor ? 0 : index(); }
575 inline Index col() const { return XprIsRowMajor ? index() : 0; }
576
577 inline Scalar value() const { return m_storage.it.value(); }
578 inline Scalar& valueRef() { return m_storage.it.valueRef(); }
579
580 inline OuterVectorInnerIterator& operator++() {
581 // search next non-zero entry
582 while (++m_outerPos < m_end) {
583 // Restart iterator at the next inner-vector:
584 openIterator();
585 // search for the key m_innerIndex in the current outer-vector
586 while (m_storage.it && m_storage.it.index() < m_innerIndex) ++m_storage.it;
587 if (m_storage.it && m_storage.it.index() == m_innerIndex) break;
588 }
589 return *this;
590 }
591
592 inline operator bool() const { return m_outerPos < m_end; }
593};
594
595template <typename Scalar_, int Options_, typename StorageIndex_, int BlockRows, int BlockCols>
596struct unary_evaluator<Block<SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true>, IteratorBased>
597 : evaluator<
598 SparseCompressedBase<Block<SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true> > > {
599 using XprType = Block<SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true>;
600 using Base = evaluator<SparseCompressedBase<XprType>>;
601 explicit unary_evaluator(const XprType& xpr) : Base(xpr) {}
602};
603
604template <typename Scalar_, int Options_, typename StorageIndex_, int BlockRows, int BlockCols>
605struct unary_evaluator<Block<const SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true>,
606 IteratorBased>
607 : evaluator<SparseCompressedBase<
608 Block<const SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true> > > {
609 using XprType = Block<const SparseMatrix<Scalar_, Options_, StorageIndex_>, BlockRows, BlockCols, true>;
610 using Base = evaluator<SparseCompressedBase<XprType>>;
611 explicit unary_evaluator(const XprType& xpr) : Base(xpr) {}
612};
613
614} // end namespace internal
615
616} // end namespace Eigen
617
618#endif // EIGEN_SPARSE_BLOCK_H
BlockImpl(XprType &xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition SparseBlock.h:380
BlockImpl(XprType &xpr, Index i)
Definition SparseBlock.h:371
Expression of a fixed-size or dynamic-size block.
Definition Block.h:111
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:44
constexpr RowXpr row(Index i)
Definition SparseMatrixBase.h:1095
typename internal::traits< Derived >::StorageIndex StorageIndex
Definition SparseMatrixBase.h:45
constexpr ColXpr col(Index i)
Definition SparseMatrixBase.h:1082
@ IsVectorAtCompileTime
Definition SparseMatrixBase.h:82
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
constexpr unsigned int CompressedAccessBit
Definition Constants.h:196
constexpr Derived & derived()
Definition EigenBase.h:50
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44
Definition Constants.h:545