Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
CoreIterators.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_COREITERATORS_H
12#define EIGEN_COREITERATORS_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19/* This file contains the respective InnerIterator definition of the expressions defined in Eigen/Core
20 */
21
22namespace internal {
23
24template <typename XprType, typename EvaluatorKind>
25class inner_iterator_selector;
26
27}
28
37template <typename XprType>
39 protected:
40 using IteratorType = internal::inner_iterator_selector<XprType, typename internal::evaluator_traits<XprType>::Kind>;
41 using EvaluatorType = internal::evaluator<XprType>;
42 using Scalar = typename internal::traits<XprType>::Scalar;
43
44 public:
46 InnerIterator(const XprType &xpr, const Index &outerId) : m_eval(xpr), m_iter(m_eval, outerId, xpr.innerSize()) {}
47
49 EIGEN_STRONG_INLINE Scalar value() const { return m_iter.value(); }
53 EIGEN_STRONG_INLINE InnerIterator &operator++() {
54 m_iter.operator++();
55 return *this;
56 }
57 EIGEN_STRONG_INLINE InnerIterator &operator+=(Index i) {
58 m_iter.operator+=(i);
59 return *this;
60 }
61 EIGEN_STRONG_INLINE InnerIterator operator+(Index i) const {
62 InnerIterator result(*this);
63 result += i;
64 return result;
65 }
66
68 EIGEN_STRONG_INLINE Index index() const { return m_iter.index(); }
70 EIGEN_STRONG_INLINE Index row() const { return m_iter.row(); }
72 EIGEN_STRONG_INLINE Index col() const { return m_iter.col(); }
74 EIGEN_STRONG_INLINE operator bool() const { return m_iter; }
75
76 protected:
77 EvaluatorType m_eval;
78 IteratorType m_iter;
79
80 private:
81 // If you get here, then you're not using the right InnerIterator type, e.g.:
82 // SparseMatrix<double,RowMajor> A;
83 // SparseMatrix<double>::InnerIterator it(A,0);
84 template <typename T>
85 InnerIterator(const EigenBase<T> &, Index outer);
86};
87
88namespace internal {
89
90// Generic inner iterator implementation for dense objects
91template <typename XprType>
92class inner_iterator_selector<XprType, IndexBased> {
93 protected:
94 using EvaluatorType = evaluator<XprType>;
95 using Scalar = typename traits<XprType>::Scalar;
96 enum { IsRowMajor = (XprType::Flags & RowMajorBit) == RowMajorBit };
97
98 public:
99 EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &innerSize)
100 : m_eval(eval), m_inner(0), m_outer(outerId), m_end(innerSize) {}
101
102 EIGEN_STRONG_INLINE Scalar value() const {
103 return (IsRowMajor) ? m_eval.coeff(m_outer, m_inner) : m_eval.coeff(m_inner, m_outer);
104 }
105
106 EIGEN_STRONG_INLINE inner_iterator_selector &operator++() {
107 m_inner++;
108 return *this;
109 }
110 EIGEN_STRONG_INLINE inner_iterator_selector &operator+=(Index i) {
111 m_inner += i;
112 return *this;
113 }
114
115 EIGEN_STRONG_INLINE Index index() const { return m_inner; }
116 inline Index row() const { return IsRowMajor ? m_outer : index(); }
117 inline Index col() const { return IsRowMajor ? index() : m_outer; }
118
119 EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner >= 0; }
120
121 protected:
122 const EvaluatorType &m_eval;
123 Index m_inner;
124 const Index m_outer;
125 const Index m_end;
126};
127
128// For iterator-based evaluator, inner-iterator is already implemented as
129// evaluator<>::InnerIterator
130template <typename XprType>
131class inner_iterator_selector<XprType, IteratorBased> : public evaluator<XprType>::InnerIterator {
132 protected:
133 using Base = typename evaluator<XprType>::InnerIterator;
134 using EvaluatorType = evaluator<XprType>;
135
136 public:
137 EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId,
138 const Index & /*innerSize*/)
139 : Base(eval, outerId) {}
140};
141
142} // end namespace internal
143
144} // end namespace Eigen
145
146#endif // EIGEN_COREITERATORS_H
An InnerIterator allows to loop over the element of any matrix expression.
Definition CoreIterators.h:38
InnerIterator & operator++()
Definition CoreIterators.h:53
Index index() const
Definition CoreIterators.h:68
Scalar value() const
Definition CoreIterators.h:49
Index col() const
Definition CoreIterators.h:72
Index row() const
Definition CoreIterators.h:70
InnerIterator(const XprType &xpr, const Index &outerId)
Definition CoreIterators.h:46
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition EigenBase.h:34