diff options
author | Gael Guennebaud <g.gael@free.fr> | 2018-10-01 23:21:37 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2018-10-01 23:21:37 +0200 |
commit | b0c66adfb1c72d060ec98ebf1004a73b6e4cd559 (patch) | |
tree | 1fa3c3cbe2247a5d87255c1c36a41588d80a3dfd /Eigen/src | |
parent | 2088c0897f6ea7175d06de98fe04c71cd453a34d (diff) |
bug #231: initial implementation of STL iterators for dense expressions
Diffstat (limited to 'Eigen/src')
-rw-r--r-- | Eigen/src/Core/DenseBase.h | 11 | ||||
-rw-r--r-- | Eigen/src/Core/StlIterators.h | 235 | ||||
-rw-r--r-- | Eigen/src/Core/util/ForwardDeclarations.h | 3 |
3 files changed, 249 insertions, 0 deletions
diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h index 0c0ea95f4..93410670f 100644 --- a/Eigen/src/Core/DenseBase.h +++ b/Eigen/src/Core/DenseBase.h @@ -572,6 +572,17 @@ template<typename Derived> class DenseBase } EIGEN_DEVICE_FUNC void reverseInPlace(); + inline DenseStlIterator<Derived> begin(); + inline DenseStlIterator<const Derived> begin() const; + inline DenseStlIterator<const Derived> cbegin() const; + inline DenseStlIterator<Derived> end(); + inline DenseStlIterator<const Derived> end() const; + inline DenseStlIterator<const Derived> cend() const; + inline ColsProxy<Derived> allCols(); + inline ColsProxy<const Derived> allCols() const; + inline RowsProxy<Derived> allRows(); + inline RowsProxy<const Derived> allRows() const; + #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND) diff --git a/Eigen/src/Core/StlIterators.h b/Eigen/src/Core/StlIterators.h new file mode 100644 index 000000000..c2b162a7b --- /dev/null +++ b/Eigen/src/Core/StlIterators.h @@ -0,0 +1,235 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2018 Gael Guennebaud <gael.guennebaud@inria.fr> +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +namespace Eigen { + +template<typename XprType,typename Derived> +class DenseStlIteratorBase +{ +public: + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; + + DenseStlIteratorBase() : mp_xpr(0), m_index(0) {} + DenseStlIteratorBase(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {} + + void swap(DenseStlIteratorBase& other) { + std::swap(mp_xpr,other.mp_xpr); + std::swap(m_index,other.m_index); + } + + Derived& operator++() { ++m_index; return derived(); } + Derived& operator--() { --m_index; return derived(); } + + Derived operator++(int) { Derived prev(derived()); operator++(); return prev;} + Derived operator--(int) { Derived prev(derived()); operator--(); return prev;} + + friend Derived operator+(const DenseStlIteratorBase& a, int b) { Derived ret(a.derived()); ret += b; return ret; } + friend Derived operator-(const DenseStlIteratorBase& a, int b) { Derived ret(a.derived()); ret -= b; return ret; } + friend Derived operator+(int a, const DenseStlIteratorBase& b) { Derived ret(b.derived()); ret += a; return ret; } + friend Derived operator-(int a, const DenseStlIteratorBase& b) { Derived ret(b.derived()); ret -= a; return ret; } + + Derived& operator+=(int b) { m_index += b; return derived(); } + Derived& operator-=(int b) { m_index -= b; return derived(); } + + difference_type operator-(const DenseStlIteratorBase& other) const { eigen_assert(mp_xpr == other.mp_xpr);return m_index - other.m_index; } + + bool operator==(const DenseStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; } + bool operator!=(const DenseStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; } + bool operator< (const DenseStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; } + bool operator<=(const DenseStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; } + bool operator> (const DenseStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; } + bool operator>=(const DenseStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; } + +protected: + + Derived& derived() { return static_cast<Derived&>(*this); } + const Derived& derived() const { return static_cast<const Derived&>(*this); } + + XprType *mp_xpr; + Index m_index; +}; + +template<typename XprType> +class DenseStlIterator : public DenseStlIteratorBase<XprType, DenseStlIterator<XprType> > +{ +public: + typedef typename XprType::Scalar value_type; + +protected: + + enum { + has_direct_access = (internal::traits<XprType>::Flags & DirectAccessBit) ? 1 : 0, + has_write_access = internal::is_lvalue<XprType>::value + }; + + typedef DenseStlIteratorBase<XprType,DenseStlIterator> Base; + using Base::m_index; + using Base::mp_xpr; + + typedef typename internal::conditional<bool(has_direct_access), const value_type&, const value_type>::type read_only_ref_t; + +public: + + typedef typename internal::conditional<bool(has_write_access), value_type *, const value_type *>::type pointer; + typedef typename internal::conditional<bool(has_write_access), value_type&, read_only_ref_t>::type reference; + + + DenseStlIterator() : Base() {} + DenseStlIterator(XprType& xpr, Index index) : Base(xpr,index) {} + + reference operator*() const { return (*mp_xpr)(m_index); } + reference operator[](int i) const { return (*mp_xpr)(i); } + + pointer operator->() const { return &((*mp_xpr)(m_index)); } +}; + +template<typename XprType,typename Derived> +void swap(DenseStlIteratorBase<XprType,Derived>& a, DenseStlIteratorBase<XprType,Derived>& b) { + a.swap(b); +} + +template<typename Derived> +inline DenseStlIterator<Derived> DenseBase<Derived>::begin() +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); + return DenseStlIterator<Derived>(derived(), 0); +} + +template<typename Derived> +inline DenseStlIterator<const Derived> DenseBase<Derived>::begin() const +{ + return cbegin(); +} + +template<typename Derived> +inline DenseStlIterator<const Derived> DenseBase<Derived>::cbegin() const +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); + return DenseStlIterator<const Derived>(derived(), 0); +} + +template<typename Derived> +inline DenseStlIterator<Derived> DenseBase<Derived>::end() +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); + return DenseStlIterator<Derived>(derived(), size()); +} + +template<typename Derived> +inline DenseStlIterator<const Derived> DenseBase<Derived>::end() const +{ + return cend(); +} + +template<typename Derived> +inline DenseStlIterator<const Derived> DenseBase<Derived>::cend() const +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); + return DenseStlIterator<const Derived>(derived(), size()); +} + +template<typename XprType> +class DenseColStlIterator : public DenseStlIteratorBase<XprType, DenseColStlIterator<XprType> > +{ +protected: + + enum { is_lvalue = internal::is_lvalue<XprType>::value }; + + typedef DenseStlIteratorBase<XprType,DenseColStlIterator> Base; + using Base::m_index; + using Base::mp_xpr; + +public: + typedef typename internal::conditional<bool(is_lvalue), typename XprType::ColXpr, typename XprType::ConstColXpr>::type value_type; + typedef value_type* pointer; + typedef value_type reference; + + DenseColStlIterator() : Base() {} + DenseColStlIterator(XprType& xpr, Index index) : Base(xpr,index) {} + + reference operator*() const { return (*mp_xpr).col(m_index); } + reference operator[](int i) const { return (*mp_xpr).col(i); } + + pointer operator->() const { return &((*mp_xpr).col(m_index)); } +}; + +template<typename XprType> +class DenseRowStlIterator : public DenseStlIteratorBase<XprType, DenseRowStlIterator<XprType> > +{ +protected: + + enum { is_lvalue = internal::is_lvalue<XprType>::value }; + + typedef DenseStlIteratorBase<XprType,DenseRowStlIterator> Base; + using Base::m_index; + using Base::mp_xpr; + +public: + typedef typename internal::conditional<bool(is_lvalue), typename XprType::RowXpr, typename XprType::ConstRowXpr>::type value_type; + typedef value_type* pointer; + typedef value_type reference; + + DenseRowStlIterator() : Base() {} + DenseRowStlIterator(XprType& xpr, Index index) : Base(xpr,index) {} + + reference operator*() const { return (*mp_xpr).row(m_index); } + reference operator[](int i) const { return (*mp_xpr).row(i); } + + pointer operator->() const { return &((*mp_xpr).row(m_index)); } +}; + + +template<typename Xpr> +class ColsProxy +{ +public: + ColsProxy(Xpr& xpr) : m_xpr(xpr) {} + DenseColStlIterator<Xpr> begin() const { return DenseColStlIterator<Xpr>(m_xpr, 0); } + DenseColStlIterator<const Xpr> cbegin() const { return DenseColStlIterator<const Xpr>(m_xpr, 0); } + + DenseColStlIterator<Xpr> end() const { return DenseColStlIterator<Xpr>(m_xpr, m_xpr.cols()); } + DenseColStlIterator<const Xpr> cend() const { return DenseColStlIterator<const Xpr>(m_xpr, m_xpr.cols()); } + +protected: + Xpr& m_xpr; +}; + +template<typename Xpr> +class RowsProxy +{ +public: + RowsProxy(Xpr& xpr) : m_xpr(xpr) {} + DenseRowStlIterator<Xpr> begin() const { return DenseRowStlIterator<Xpr>(m_xpr, 0); } + DenseRowStlIterator<const Xpr> cbegin() const { return DenseRowStlIterator<const Xpr>(m_xpr, 0); } + + DenseRowStlIterator<Xpr> end() const { return DenseRowStlIterator<Xpr>(m_xpr, m_xpr.rows()); } + DenseRowStlIterator<const Xpr> cend() const { return DenseRowStlIterator<const Xpr>(m_xpr, m_xpr.rows()); } + +protected: + Xpr& m_xpr; +}; + +template<typename Derived> +ColsProxy<Derived> DenseBase<Derived>::allCols() +{ return ColsProxy<Derived>(derived()); } + +template<typename Derived> +ColsProxy<const Derived> DenseBase<Derived>::allCols() const +{ return ColsProxy<const Derived>(derived()); } + +template<typename Derived> +RowsProxy<Derived> DenseBase<Derived>::allRows() +{ return RowsProxy<Derived>(derived()); } + +template<typename Derived> +RowsProxy<const Derived> DenseBase<Derived>::allRows() const +{ return RowsProxy<const Derived>(derived()); } + +} // namespace Eigen diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index fca8a350e..d2532d854 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -133,6 +133,9 @@ template<typename ExpressionType> class ArrayWrapper; template<typename ExpressionType> class MatrixWrapper; template<typename Derived> class SolverBase; template<typename XprType> class InnerIterator; +template<typename XprType> class DenseStlIterator; +template<typename XprType> class ColsProxy; +template<typename XprType> class RowsProxy; namespace internal { template<typename DecompositionType> struct kernel_retval_base; |