From ae06b8af5cd9828240b474363bfabbbab8202e23 Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Mon, 4 Apr 2011 15:35:14 +0100 Subject: [PATCH] Make evaluators for Matrix and Array inherit from common base class. This gets rid of some code duplication. --- Eigen/src/Core/CoreEvaluators.h | 200 +++++++++++++------------------- 1 file changed, 82 insertions(+), 118 deletions(-) diff --git a/Eigen/src/Core/CoreEvaluators.h b/Eigen/src/Core/CoreEvaluators.h index 39ef6078c..6fe7177c6 100644 --- a/Eigen/src/Core/CoreEvaluators.h +++ b/Eigen/src/Core/CoreEvaluators.h @@ -106,128 +106,92 @@ protected: typename evaluator::type m_argImpl; }; -// -------------------- Matrix -------------------- +// -------------------- Matrix and Array-------------------- +// +// evaluator_impl is a common base class for the +// Matrix and Array evaluators. + +template +struct evaluator_impl > +{ + typedef PlainObjectBase PlainObjectType; + + evaluator_impl(const PlainObjectType& m) : m_plainObject(m) {} + + typedef typename PlainObjectType::Index Index; + typedef typename PlainObjectType::Scalar Scalar; + typedef typename PlainObjectType::CoeffReturnType CoeffReturnType; + typedef typename PlainObjectType::PacketScalar PacketScalar; + typedef typename PlainObjectType::PacketReturnType PacketReturnType; + + CoeffReturnType coeff(Index i, Index j) const + { + return m_plainObject.coeff(i, j); + } + + CoeffReturnType coeff(Index index) const + { + return m_plainObject.coeff(index); + } + + Scalar& coeffRef(Index i, Index j) + { + return m_plainObject.const_cast_derived().coeffRef(i, j); + } + + Scalar& coeffRef(Index index) + { + return m_plainObject.const_cast_derived().coeffRef(index); + } + + template + PacketReturnType packet(Index row, Index col) const + { + return m_plainObject.template packet(row, col); + } + + template + PacketReturnType packet(Index index) const + { + return m_plainObject.template packet(index); + } + + template + void writePacket(Index row, Index col, const PacketScalar& x) + { + m_plainObject.const_cast_derived().template writePacket(row, col, x); + } + + template + void writePacket(Index index, const PacketScalar& x) + { + m_plainObject.const_cast_derived().template writePacket(index, x); + } + +protected: + const PlainObjectType &m_plainObject; +}; template struct evaluator_impl > + : evaluator_impl > > { typedef Matrix MatrixType; - evaluator_impl(const MatrixType& m) : m_matrix(m) {} - - typedef typename MatrixType::Index Index; - - typename MatrixType::CoeffReturnType coeff(Index i, Index j) const - { - return m_matrix.coeff(i, j); - } - - typename MatrixType::CoeffReturnType coeff(Index index) const - { - return m_matrix.coeff(index); - } - - typename MatrixType::Scalar& coeffRef(Index i, Index j) - { - return m_matrix.const_cast_derived().coeffRef(i, j); - } - - typename MatrixType::Scalar& coeffRef(Index index) - { - return m_matrix.const_cast_derived().coeffRef(index); - } - - template - typename MatrixType::PacketReturnType packet(Index row, Index col) const - { - return m_matrix.template packet(row, col); - } - - template - typename MatrixType::PacketReturnType packet(Index index) const - { - // eigen_internal_assert(index >= 0 && index < size()); - return m_matrix.template packet(index); - } - - template - void writePacket(Index row, Index col, const typename MatrixType::PacketScalar& x) - { - m_matrix.const_cast_derived().template writePacket(row, col, x); - } - - template - void writePacket(Index index, const typename MatrixType::PacketScalar& x) - { - // eigen_internal_assert(index >= 0 && index < size()); - m_matrix.const_cast_derived().template writePacket(index, x); - } - -protected: - const MatrixType &m_matrix; + evaluator_impl(const MatrixType& m) + : evaluator_impl >(m) + { } }; -// -------------------- Array -------------------- - -// TODO: should be sharing code with Matrix case - template struct evaluator_impl > + : evaluator_impl > > { typedef Array ArrayType; - evaluator_impl(const ArrayType& a) : m_array(a) {} - - typedef typename ArrayType::Index Index; - - typename ArrayType::CoeffReturnType coeff(Index i, Index j) const - { - return m_array.coeff(i, j); - } - - typename ArrayType::CoeffReturnType coeff(Index index) const - { - return m_array.coeff(index); - } - - typename ArrayType::Scalar& coeffRef(Index i, Index j) - { - return m_array.const_cast_derived().coeffRef(i, j); - } - - typename ArrayType::Scalar& coeffRef(Index index) - { - return m_array.const_cast_derived().coeffRef(index); - } - - template - typename ArrayType::PacketReturnType packet(Index row, Index col) const - { - return m_array.template packet(row, col); - } - - template - typename ArrayType::PacketReturnType packet(Index index) const - { - // eigen_internal_assert(index >= 0 && index < size()); - return m_array.template packet(index); - } - - template - void writePacket(Index row, Index col, const typename ArrayType::PacketScalar& x) - { - m_array.const_cast_derived().template writePacket(row, col, x); - } - - template - void writePacket(Index index, const typename ArrayType::PacketScalar& x) - { - // eigen_internal_assert(index >= 0 && index < size()); - m_array.const_cast_derived().template writePacket(index, x); - } - -protected: - const ArrayType &m_array; + evaluator_impl(const ArrayType& m) + : evaluator_impl >(m) + { } }; // -------------------- CwiseNullaryOp -------------------- @@ -400,8 +364,8 @@ struct evaluator_impl @@ -424,8 +388,8 @@ struct evaluator_impl PacketReturnType packet(Index index) const { - return m_argImpl.template packet(m_startRow + (RowsAtCompileTime == 1 ? 0 : index), - m_startCol + (RowsAtCompileTime == 1 ? index : 0)); + return packet(RowsAtCompileTime == 1 ? 0 : index, + RowsAtCompileTime == 1 ? index : 0); } template @@ -437,9 +401,9 @@ struct evaluator_impl void writePacket(Index index, const PacketScalar& x) { - return m_argImpl.template writePacket(m_startRow + (RowsAtCompileTime == 1 ? 0 : index), - m_startCol + (RowsAtCompileTime == 1 ? index : 0), - x); + return writePacket(RowsAtCompileTime == 1 ? 0 : index, + RowsAtCompileTime == 1 ? index : 0, + x); } protected: