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