![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_COREITERATORS_H 00011 #define EIGEN_COREITERATORS_H 00012 00013 namespace Eigen { 00014 00015 /* This file contains the respective InnerIterator definition of the expressions defined in Eigen/Core 00016 */ 00017 00018 namespace internal { 00019 00020 template<typename XprType, typename EvaluatorKind> 00021 class inner_iterator_selector; 00022 00023 } 00024 00032 template<typename XprType> 00033 class InnerIterator 00034 { 00035 protected: 00036 typedef internal::inner_iterator_selector<XprType, typename internal::evaluator_traits<XprType>::Kind> IteratorType; 00037 typedef internal::evaluator<XprType> EvaluatorType; 00038 typedef typename internal::traits<XprType>::Scalar Scalar; 00039 public: 00041 InnerIterator(const XprType &xpr, const Index &outerId) 00042 : m_eval(xpr), m_iter(m_eval, outerId, xpr.innerSize()) 00043 {} 00044 00046 EIGEN_STRONG_INLINE Scalar value() const { return m_iter.value(); } 00050 EIGEN_STRONG_INLINE InnerIterator& operator++() { m_iter.operator++(); return *this; } 00052 EIGEN_STRONG_INLINE Index index() const { return m_iter.index(); } 00054 EIGEN_STRONG_INLINE Index row() const { return m_iter.row(); } 00056 EIGEN_STRONG_INLINE Index col() const { return m_iter.col(); } 00058 EIGEN_STRONG_INLINE operator bool() const { return m_iter; } 00059 00060 protected: 00061 EvaluatorType m_eval; 00062 IteratorType m_iter; 00063 private: 00064 // If you get here, then you're not using the right InnerIterator type, e.g.: 00065 // SparseMatrix<double,RowMajor> A; 00066 // SparseMatrix<double>::InnerIterator it(A,0); 00067 template<typename T> InnerIterator(const EigenBase<T>&,Index outer); 00068 }; 00069 00070 namespace internal { 00071 00072 // Generic inner iterator implementation for dense objects 00073 template<typename XprType> 00074 class inner_iterator_selector<XprType, IndexBased> 00075 { 00076 protected: 00077 typedef evaluator<XprType> EvaluatorType; 00078 typedef typename traits<XprType>::Scalar Scalar; 00079 enum { IsRowMajor = (XprType::Flags&RowMajorBit)==RowMajorBit }; 00080 00081 public: 00082 EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &innerSize) 00083 : m_eval(eval), m_inner(0), m_outer(outerId), m_end(innerSize) 00084 {} 00085 00086 EIGEN_STRONG_INLINE Scalar value() const 00087 { 00088 return (IsRowMajor) ? m_eval.coeff(m_outer, m_inner) 00089 : m_eval.coeff(m_inner, m_outer); 00090 } 00091 00092 EIGEN_STRONG_INLINE inner_iterator_selector& operator++() { m_inner++; return *this; } 00093 00094 EIGEN_STRONG_INLINE Index index() const { return m_inner; } 00095 inline Index row() const { return IsRowMajor ? m_outer : index(); } 00096 inline Index col() const { return IsRowMajor ? index() : m_outer; } 00097 00098 EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; } 00099 00100 protected: 00101 const EvaluatorType& m_eval; 00102 Index m_inner; 00103 const Index m_outer; 00104 const Index m_end; 00105 }; 00106 00107 // For iterator-based evaluator, inner-iterator is already implemented as 00108 // evaluator<>::InnerIterator 00109 template<typename XprType> 00110 class inner_iterator_selector<XprType, IteratorBased> 00111 : public evaluator<XprType>::InnerIterator 00112 { 00113 protected: 00114 typedef typename evaluator<XprType>::InnerIterator Base; 00115 typedef evaluator<XprType> EvaluatorType; 00116 00117 public: 00118 EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &/*innerSize*/) 00119 : Base(eval, outerId) 00120 {} 00121 }; 00122 00123 } // end namespace internal 00124 00125 } // end namespace Eigen 00126 00127 #endif // EIGEN_COREITERATORS_H