From cb9f3685d351dc39200638c0b48891d730976656 Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Thu, 5 Jul 2012 11:09:42 +0100 Subject: [PATCH] Move implementation of coeff() &c to Matrix/Array evaluator. --- Eigen/src/Core/CoreEvaluators.h | 81 +++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/Eigen/src/Core/CoreEvaluators.h b/Eigen/src/Core/CoreEvaluators.h index 906df2af7..2a6099dd1 100644 --- a/Eigen/src/Core/CoreEvaluators.h +++ b/Eigen/src/Core/CoreEvaluators.h @@ -169,7 +169,19 @@ struct evaluator_impl > { typedef PlainObjectBase PlainObjectType; - evaluator_impl(const PlainObjectType& m) : m_plainObject(m) {} + evaluator_impl(void) + { } + + evaluator_impl(const PlainObjectType& m) + { + init(m); + } + + void init(const PlainObjectType& m) + { + m_data = m.data(); + m_outerStride = m.outerStride(); + } typedef typename PlainObjectType::Index Index; typedef typename PlainObjectType::Scalar Scalar; @@ -177,52 +189,71 @@ struct evaluator_impl > typedef typename PlainObjectType::PacketScalar PacketScalar; typedef typename PlainObjectType::PacketReturnType PacketReturnType; - CoeffReturnType coeff(Index i, Index j) const + enum { + IsRowMajor = PlainObjectType::IsRowMajor, + }; + + CoeffReturnType coeff(Index row, Index col) const { - return m_plainObject.coeff(i, j); + if (IsRowMajor) + return m_data[row * m_outerStride + col]; + else + return m_data[row + col * m_outerStride]; } CoeffReturnType coeff(Index index) const { - return m_plainObject.coeff(index); + return m_data[index]; } - Scalar& coeffRef(Index i, Index j) + Scalar& coeffRef(Index row, Index col) { - return m_plainObject.const_cast_derived().coeffRef(i, j); + if (IsRowMajor) + return const_cast(m_data)[row * m_outerStride + col]; + else + return const_cast(m_data)[row + col * m_outerStride]; } Scalar& coeffRef(Index index) { - return m_plainObject.const_cast_derived().coeffRef(index); + return const_cast(m_data)[index]; } template PacketReturnType packet(Index row, Index col) const { - return m_plainObject.template packet(row, col); + if (IsRowMajor) + return ploadt(m_data + row * m_outerStride + col); + else + return ploadt(m_data + row + col * m_outerStride); } template PacketReturnType packet(Index index) const { - return m_plainObject.template packet(index); + return ploadt(m_data + index); } template void writePacket(Index row, Index col, const PacketScalar& x) { - m_plainObject.const_cast_derived().template writePacket(row, col, x); + if (IsRowMajor) + return pstoret + (const_cast(m_data) + row * m_outerStride + col, x); + else + return pstoret + (const_cast(m_data) + row + col * m_outerStride, x); } template void writePacket(Index index, const PacketScalar& x) { - m_plainObject.const_cast_derived().template writePacket(index, x); + return pstoret(const_cast(m_data) + index, x); } protected: - const PlainObjectType &m_plainObject; + const Scalar *m_data; + Index m_outerStride; }; template @@ -231,6 +262,9 @@ struct evaluator_impl > { typedef Matrix XprType; + evaluator_impl(void) + { } + evaluator_impl(const XprType& m) : evaluator_impl >(m) { } @@ -242,6 +276,9 @@ struct evaluator_impl > { typedef Array XprType; + evaluator_impl(void) + { } + evaluator_impl(const XprType& m) : evaluator_impl >(m) { } @@ -295,17 +332,25 @@ struct evaluator_impl > typedef evaluator_impl BaseType; evaluator_impl(const XprType& xpr) - : BaseType(m_result) { - noalias_copy_using_evaluator(m_result, xpr.arg()); - }; + init(xpr.arg()); + } - // this constructor is used when nesting an EvalTo evaluator in another evaluator + // This constructor is used when nesting an EvalTo evaluator in another evaluator evaluator_impl(const ArgType& arg) - : BaseType(m_result) { + init(arg); + } + +protected: + void init(const ArgType& arg) + { + // We can only initialize the base class evaluator after m_result is initialized. + // TODO: Redesign to get rid of inheritance, so that we can remove default c'tors in + // PlainObject, Matrix and Array evaluators. noalias_copy_using_evaluator(m_result, arg); - }; + BaseType::init(m_result); + } protected: PlainObject m_result;