CoreIterators.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 /* This file contains the respective InnerIterator definition of the expressions defined in Eigen/Core
18  */
19 
20 namespace internal {
21 
22 template<typename XprType, typename EvaluatorKind>
23 class inner_iterator_selector;
24 
25 }
26 
34 template<typename XprType>
35 class InnerIterator
36 {
37 protected:
38  typedef internal::inner_iterator_selector<XprType, typename internal::evaluator_traits<XprType>::Kind> IteratorType;
39  typedef internal::evaluator<XprType> EvaluatorType;
40  typedef typename internal::traits<XprType>::Scalar Scalar;
41 public:
43  InnerIterator(const XprType &xpr, const Index &outerId)
44  : m_eval(xpr), m_iter(m_eval, outerId, xpr.innerSize())
45  {}
46 
48  EIGEN_STRONG_INLINE Scalar value() const { return m_iter.value(); }
52  EIGEN_STRONG_INLINE InnerIterator& operator++() { m_iter.operator++(); return *this; }
53  EIGEN_STRONG_INLINE InnerIterator& operator+=(Index i) { m_iter.operator+=(i); return *this; }
54  EIGEN_STRONG_INLINE InnerIterator operator+(Index i)
55  { InnerIterator result(*this); result+=i; return result; }
56 
57 
59  EIGEN_STRONG_INLINE Index index() const { return m_iter.index(); }
61  EIGEN_STRONG_INLINE Index row() const { return m_iter.row(); }
63  EIGEN_STRONG_INLINE Index col() const { return m_iter.col(); }
65  EIGEN_STRONG_INLINE operator bool() const { return m_iter; }
66 
67 protected:
68  EvaluatorType m_eval;
69  IteratorType m_iter;
70 private:
71  // If you get here, then you're not using the right InnerIterator type, e.g.:
72  // SparseMatrix<double,RowMajor> A;
73  // SparseMatrix<double>::InnerIterator it(A,0);
74  template<typename T> InnerIterator(const EigenBase<T>&,Index outer);
75 };
76 
77 namespace internal {
78 
79 // Generic inner iterator implementation for dense objects
80 template<typename XprType>
81 class inner_iterator_selector<XprType, IndexBased>
82 {
83 protected:
84  typedef evaluator<XprType> EvaluatorType;
85  typedef typename traits<XprType>::Scalar Scalar;
86  enum { IsRowMajor = (XprType::Flags&RowMajorBit)==RowMajorBit };
87 
88 public:
89  EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &innerSize)
90  : m_eval(eval), m_inner(0), m_outer(outerId), m_end(innerSize)
91  {}
92 
93  EIGEN_STRONG_INLINE Scalar value() const
94  {
95  return (IsRowMajor) ? m_eval.coeff(m_outer, m_inner)
96  : m_eval.coeff(m_inner, m_outer);
97  }
98 
99  EIGEN_STRONG_INLINE inner_iterator_selector& operator++() { m_inner++; return *this; }
100 
101  EIGEN_STRONG_INLINE Index index() const { return m_inner; }
102  inline Index row() const { return IsRowMajor ? m_outer : index(); }
103  inline Index col() const { return IsRowMajor ? index() : m_outer; }
104 
105  EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; }
106 
107 protected:
108  const EvaluatorType& m_eval;
109  Index m_inner;
110  const Index m_outer;
111  const Index m_end;
112 };
113 
114 // For iterator-based evaluator, inner-iterator is already implemented as
115 // evaluator<>::InnerIterator
116 template<typename XprType>
117 class inner_iterator_selector<XprType, IteratorBased>
118  : public evaluator<XprType>::InnerIterator
119 {
120 protected:
121  typedef typename evaluator<XprType>::InnerIterator Base;
122  typedef evaluator<XprType> EvaluatorType;
123 
124 public:
125  EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &/*innerSize*/)
126  : Base(eval, outerId)
127  {}
128 };
129 
130 } // end namespace internal
131 
132 } // end namespace Eigen
133 
134 #endif // EIGEN_COREITERATORS_H
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
const unsigned int RowMajorBit
Definition: Constants.h:68
bfloat16 operator++(bfloat16 &a)
Definition: BFloat16.h:298
bfloat16 & operator+=(bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:282
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const CwiseBinaryOp< internal::scalar_sum_op< typename DenseDerived::Scalar, typename SparseDerived::Scalar >, const DenseDerived, const SparseDerived > operator+(const MatrixBase< DenseDerived > &a, const SparseMatrixBase< SparseDerived > &b)