From 7d41ad9d90a51f788fcea260f3703d932af67611 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sat, 29 Sep 2007 17:27:20 +0000 Subject: Get rid of a nasty const_cast by introducing a MatrixConstRef class. Remove the __restrict__'s for now. --- src/internal/Block.h | 4 +++- src/internal/Matrix.h | 16 ++++++++++------ src/internal/MatrixOps.h | 30 ++++++++++++++++++------------ src/internal/MatrixRef.h | 30 ++++++++++++++++++++++++++++-- src/internal/MatrixStorage.h | 8 ++++---- src/internal/Minor.h | 4 +++- src/internal/Object.h | 9 +++++---- src/internal/RowAndCol.h | 8 ++++++-- src/internal/ScalarOps.h | 14 ++++++++------ src/internal/Util.h | 5 ++++- 10 files changed, 89 insertions(+), 39 deletions(-) (limited to 'src/internal') diff --git a/src/internal/Block.h b/src/internal/Block.h index 0664abb58..2104a23c9 100644 --- a/src/internal/Block.h +++ b/src/internal/Block.h @@ -34,6 +34,7 @@ template class EiBlock typedef typename MatrixType::Ref MatRef; friend class EiObject >; typedef EiBlock Ref; + typedef EiBlock ConstRef; static const int RowsAtCompileTime = EiDynamic, ColsAtCompileTime = EiDynamic; @@ -55,7 +56,8 @@ template class EiBlock EI_INHERIT_ASSIGNMENT_OPERATORS(EiBlock) private: - const Ref& _ref() const { return *this; } + Ref& _ref() { return *this; } + const Ref& _constRef() const { return *this; } int _rows() const { return m_endRow - m_startRow + 1; } int _cols() const { return m_endCol - m_startCol + 1; } diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h index faed649c2..e5481df2f 100644 --- a/src/internal/Matrix.h +++ b/src/internal/Matrix.h @@ -41,25 +41,29 @@ class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >, typedef EiMatrixStorage<_Scalar, _Rows, _Cols> Storage; typedef _Scalar Scalar; typedef EiMatrixRef Ref; + typedef EiMatrixConstRef ConstRef; + friend class EiMatrixRef; + friend class EiMatrixConstRef; static const int RowsAtCompileTime = _Rows, ColsAtCompileTime = _Cols; - const Scalar* EI_RESTRICT array() const + const Scalar* array() const { return Storage::m_array; } - Scalar* EI_RESTRICT array() + Scalar* array() { return Storage::m_array; } private: - Ref _ref() const { return Ref(*const_cast(this)); } - - const Scalar& EI_RESTRICT _read(int row, int col = 0) const + Ref _ref() { return Ref(*this); } + ConstRef _constRef() const { return ConstRef(*this); } + + const Scalar& _read(int row, int col = 0) const { EI_CHECK_RANGES(*this, row, col); return array()[row + col * Storage::_rows()]; } - Scalar& EI_RESTRICT _write(int row, int col = 0) + Scalar& _write(int row, int col = 0) { EI_CHECK_RANGES(*this, row, col); return array()[row + col * Storage::_rows()]; diff --git a/src/internal/MatrixOps.h b/src/internal/MatrixOps.h index 2798dd9c1..cad341db8 100644 --- a/src/internal/MatrixOps.h +++ b/src/internal/MatrixOps.h @@ -31,15 +31,16 @@ template class EiSum { public: typedef typename Lhs::Scalar Scalar; - typedef typename Lhs::Ref LhsRef; - typedef typename Rhs::Ref RhsRef; + typedef typename Lhs::ConstRef LhsRef; + typedef typename Rhs::ConstRef RhsRef; friend class EiObject; typedef EiSum Ref; + typedef EiSum ConstRef; static const int RowsAtCompileTime = Lhs::RowsAtCompileTime, ColsAtCompileTime = Rhs::ColsAtCompileTime; - EiSum(const LhsRef& EI_RESTRICT lhs, const RhsRef& EI_RESTRICT rhs) + EiSum(const LhsRef& lhs, const RhsRef& rhs) : m_lhs(lhs), m_rhs(rhs) { assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols()); @@ -53,6 +54,7 @@ template class EiSum private: const Ref& _ref() const { return *this; } + const ConstRef& _constRef() const { return *this; } int _rows() const { return m_lhs.rows(); } int _cols() const { return m_lhs.cols(); } @@ -71,15 +73,16 @@ template class EiDifference { public: typedef typename Lhs::Scalar Scalar; - typedef typename Lhs::Ref LhsRef; - typedef typename Rhs::Ref RhsRef; + typedef typename Lhs::ConstRef LhsRef; + typedef typename Rhs::ConstRef RhsRef; friend class EiObject; typedef EiDifference Ref; + typedef EiDifference ConstRef; static const int RowsAtCompileTime = Lhs::RowsAtCompileTime, ColsAtCompileTime = Rhs::ColsAtCompileTime; - EiDifference(const LhsRef& EI_RESTRICT lhs, const RhsRef& EI_RESTRICT rhs) + EiDifference(const LhsRef& lhs, const RhsRef& rhs) : m_lhs(lhs), m_rhs(rhs) { assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols()); @@ -92,6 +95,7 @@ template class EiDifference private: const Ref& _ref() const { return *this; } + const Ref& _constRef() const { return *this; } int _rows() const { return m_lhs.rows(); } int _cols() const { return m_lhs.cols(); } @@ -110,15 +114,16 @@ template class EiMatrixProduct { public: typedef typename Lhs::Scalar Scalar; - typedef typename Lhs::Ref LhsRef; - typedef typename Rhs::Ref RhsRef; + typedef typename Lhs::ConstRef LhsRef; + typedef typename Rhs::ConstRef RhsRef; friend class EiObject; typedef EiMatrixProduct Ref; + typedef EiMatrixProduct ConstRef; static const int RowsAtCompileTime = Lhs::RowsAtCompileTime, ColsAtCompileTime = Rhs::ColsAtCompileTime; - EiMatrixProduct(const LhsRef& EI_RESTRICT lhs, const RhsRef& EI_RESTRICT rhs) + EiMatrixProduct(const LhsRef& lhs, const RhsRef& rhs) : m_lhs(lhs), m_rhs(rhs) { assert(lhs.cols() == rhs.rows()); @@ -131,6 +136,7 @@ template class EiMatrixProduct private: const Ref& _ref() const { return *this; } + const Ref& _constRef() const { return *this; } int _rows() const { return m_lhs.rows(); } int _cols() const { return m_rhs.cols(); } @@ -158,14 +164,14 @@ template EiSum operator+(const EiObject &mat1, const EiObject &mat2) { - return EiSum(mat1.ref(), mat2.ref()); + return EiSum(mat1.constRef(), mat2.constRef()); } template EiDifference operator-(const EiObject &mat1, const EiObject &mat2) { - return EiDifference(mat1.ref(), mat2.ref()); + return EiDifference(mat1.constRef(), mat2.constRef()); } template @@ -173,7 +179,7 @@ template EiMatrixProduct EiObject::lazyMul(const EiObject &other) const { - return EiMatrixProduct(ref(), other.ref()); + return EiMatrixProduct(constRef(), other.constRef()); } template diff --git a/src/internal/MatrixRef.h b/src/internal/MatrixRef.h index 57e44e69c..7f45b36d1 100644 --- a/src/internal/MatrixRef.h +++ b/src/internal/MatrixRef.h @@ -26,6 +26,32 @@ #ifndef EI_MATRIXREF_H #define EI_MATRIXREF_H +template class EiMatrixConstRef + : public EiObject > +{ + public: + typedef typename MatrixType::Scalar Scalar; + friend class EiObject; + + EiMatrixConstRef(const MatrixType& matrix) : m_matrix(matrix) {} + EiMatrixConstRef(const EiMatrixConstRef& other) : m_matrix(other.m_matrix) {} + ~EiMatrixConstRef() {} + + EI_INHERIT_ASSIGNMENT_OPERATORS(EiMatrixConstRef) + + private: + int _rows() const { return m_matrix.rows(); } + int _cols() const { return m_matrix.cols(); } + + const Scalar& _read(int row, int col) const + { + return m_matrix._read(row, col); + } + + protected: + const MatrixType& m_matrix; +}; + template class EiMatrixRef : public EiObject > { @@ -43,9 +69,9 @@ template class EiMatrixRef int _rows() const { return m_matrix.rows(); } int _cols() const { return m_matrix.cols(); } - Scalar _read(int row, int col) const + const Scalar& _read(int row, int col) const { - return m_matrix.read(row, col); + return m_matrix._read(row, col); } Scalar& _write(int row, int col) diff --git a/src/internal/MatrixStorage.h b/src/internal/MatrixStorage.h index 609bf7c29..8e44ae6de 100644 --- a/src/internal/MatrixStorage.h +++ b/src/internal/MatrixStorage.h @@ -32,7 +32,7 @@ template { protected: int m_rows; - Scalar* EI_RESTRICT m_array; + Scalar* m_array; void resize(int rows, int cols) { @@ -94,7 +94,7 @@ class EiMatrixStorage { protected: int m_cols; - Scalar* EI_RESTRICT m_array; + Scalar* m_array; void resize(int rows, int cols) { @@ -129,7 +129,7 @@ class EiMatrixStorage { protected: int m_rows, m_cols; - Scalar* EI_RESTRICT m_array; + Scalar* m_array; void resize(int rows, int cols) { diff --git a/src/internal/Minor.h b/src/internal/Minor.h index abd8fe03c..1a1a15242 100644 --- a/src/internal/Minor.h +++ b/src/internal/Minor.h @@ -34,6 +34,7 @@ template class EiMinor typedef typename MatrixType::Ref MatRef; friend class EiObject >; typedef EiMinor Ref; + typedef EiMinor ConstRef; static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime - 1, ColsAtCompileTime = MatrixType::ColsAtCompileTime - 1; @@ -51,7 +52,8 @@ template class EiMinor EI_INHERIT_ASSIGNMENT_OPERATORS(EiMinor) private: - const Ref& _ref() const { return *this; } + Ref& _ref() { return *this; } + const ConstRef& _constRef() const { return *this; } int _rows() const { return m_matrix.rows() - 1; } int _cols() const { return m_matrix.cols() - 1; } diff --git a/src/internal/Object.h b/src/internal/Object.h index 7ec59da8c..435759413 100644 --- a/src/internal/Object.h +++ b/src/internal/Object.h @@ -56,6 +56,7 @@ template class EiObject public: typedef typename EiForwardDecl::Ref Ref; + typedef typename EiForwardDecl::ConstRef ConstRef; int rows() const { return static_cast(this)->_rows(); } int cols() const { return static_cast(this)->_cols(); } @@ -64,10 +65,10 @@ template class EiObject Ref ref() { return static_cast(this)->_ref(); } - Ref ref() const - { return static_cast(this)->_ref(); } + ConstRef constRef() const + { return static_cast(this)->_constRef(); } - Scalar& EI_RESTRICT write(int row, int col) + Scalar& write(int row, int col) { return static_cast(this)->_write(row, col); } @@ -127,7 +128,7 @@ template class EiObject Scalar operator()(int row, int col = 0) const { return read(row, col); } - Scalar& EI_RESTRICT operator()(int row, int col = 0) + Scalar& operator()(int row, int col = 0) { return write(row, col); } EiEval eval() const EI_ALWAYS_INLINE; diff --git a/src/internal/RowAndCol.h b/src/internal/RowAndCol.h index 00079c81f..8235ce1fc 100644 --- a/src/internal/RowAndCol.h +++ b/src/internal/RowAndCol.h @@ -34,6 +34,7 @@ template class EiRow typedef typename MatrixType::Ref MatRef; friend class EiObject >; typedef EiRow Ref; + typedef EiRow ConstRef; static const int RowsAtCompileTime = MatrixType::ColsAtCompileTime, ColsAtCompileTime = 1; @@ -56,7 +57,8 @@ template class EiRow EI_INHERIT_ASSIGNMENT_OPERATORS(EiRow) private: - const Ref& _ref() const { return *this; } + Ref& _ref() { return *this; } + const ConstRef& _constRef() const { return *this; } int _rows() const { return m_matrix.cols(); } int _cols() const { return 1; } @@ -88,6 +90,7 @@ template class EiColumn typedef typename MatrixType::Ref MatRef; friend class EiObject >; typedef EiColumn Ref; + typedef EiColumn ConstRef; static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = 1; @@ -104,7 +107,8 @@ template class EiColumn EI_INHERIT_ASSIGNMENT_OPERATORS(EiColumn) private: - const Ref& _ref() const { return *this; } + Ref& _ref() { return *this; } + const ConstRef& _constRef() const { return *this; } int _rows() const { return m_matrix.rows(); } int _cols() const { return 1; } diff --git a/src/internal/ScalarOps.h b/src/internal/ScalarOps.h index 6d7de3bed..cff51fa09 100644 --- a/src/internal/ScalarOps.h +++ b/src/internal/ScalarOps.h @@ -31,8 +31,9 @@ template class EiScalarProduct { public: typedef typename MatrixType::Scalar Scalar; - typedef typename MatrixType::Ref MatRef; + typedef typename MatrixType::ConstRef MatRef; typedef EiScalarProduct Ref; + typedef EiScalarProduct ConstRef; friend class EiObject >; static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime, @@ -48,6 +49,7 @@ template class EiScalarProduct private: const Ref& _ref() const { return *this; } + const ConstRef& _constRef() const { return *this; } int _rows() const { return m_matrix.rows(); } int _cols() const { return m_matrix.cols(); } @@ -61,13 +63,13 @@ template class EiScalarProduct const Scalar m_scalar; }; -#define EI_MAKE_SCALAR_OPS(OtherScalar) \ +#define EI_MAKE_SCALAR_OPS(OtherScalar) \ template \ EiScalarProduct \ operator*(const EiObject& matrix, \ OtherScalar scalar) \ { \ - return EiScalarProduct(matrix.ref(), scalar); \ + return EiScalarProduct(matrix.constRef(), scalar); \ } \ \ template \ @@ -75,7 +77,7 @@ EiScalarProduct \ operator*(OtherScalar scalar, \ const EiObject& matrix) \ { \ - return EiScalarProduct(matrix.ref(), scalar); \ + return EiScalarProduct(matrix.constRef(), scalar); \ } \ \ template \ @@ -88,7 +90,7 @@ operator/(const EiObject& matrix, \ \ template \ Derived & \ -EiObject::operator*=(const OtherScalar &other) \ +EiObject::operator*=(const OtherScalar &other) \ { \ *this = *this * other; \ return *static_cast(this); \ @@ -96,7 +98,7 @@ EiObject::operator*=(const OtherScalar &other) \ \ template \ Derived & \ -EiObject::operator/=(const OtherScalar &other) \ +EiObject::operator/=(const OtherScalar &other) \ { \ *this = *this / other; \ return *static_cast(this); \ diff --git a/src/internal/Util.h b/src/internal/Util.h index 03df6b097..d00a309e4 100644 --- a/src/internal/Util.h +++ b/src/internal/Util.h @@ -43,6 +43,7 @@ //forward declarations template class EiMatrix; template class EiMatrixRef; +template class EiMatrixConstRef; template class EiRow; template class EiColumn; template class EiMinor; @@ -56,12 +57,14 @@ template class EiEval; template struct EiForwardDecl { typedef T Ref; + typedef T ConstRef; }; template struct EiForwardDecl > { typedef EiMatrixRef > Ref; + typedef EiMatrixConstRef > ConstRef; }; const int EiDynamic = -1; @@ -70,7 +73,7 @@ const int EiDynamic = -1; #ifdef __GNUC__ # define EI_ALWAYS_INLINE __attribute__((always_inline)) -# define EI_RESTRICT __restrict__ +# define EI_RESTRICT /*__restrict__*/ #else # define EI_ALWAYS_INLINE # define EI_RESTRICT -- cgit v1.2.3