From 98f027430565fab169bee441e8c5db64b53709c7 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Fri, 7 Jan 2011 05:16:01 -0500 Subject: [PATCH] third pass of const-correctness fixes (bug #54), hopefully the last one... --- Eigen/src/Core/Block.h | 22 +++++++------- Eigen/src/Core/Diagonal.h | 2 +- Eigen/src/Core/Transpose.h | 4 +-- Eigen/src/Core/util/BlasUtil.h | 4 +-- Eigen/src/Core/util/XprHelper.h | 46 +++++++++++++++++++++++++++++ Eigen/src/Sparse/SparseMatrix.h | 16 ++++++++++ Eigen/src/Sparse/SparseMatrixBase.h | 7 +++-- 7 files changed, 82 insertions(+), 19 deletions(-) diff --git a/Eigen/src/Core/Block.h b/Eigen/src/Core/Block.h index de384b0257..99e0cdc1f8 100644 --- a/Eigen/src/Core/Block.h +++ b/Eigen/src/Core/Block.h @@ -119,7 +119,7 @@ template::type xpr, Index i) : m_xpr(xpr), // It is a row if and only if BlockRows==1 and BlockCols==XprType::ColsAtCompileTime, // and it is a column if and only if BlockRows==XprType::RowsAtCompileTime and BlockCols==1, @@ -137,7 +137,7 @@ template::type xpr, Index startRow, Index startCol) : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(BlockRows), m_blockCols(BlockCols) { @@ -148,7 +148,7 @@ template::type xpr, Index startRow, Index startCol, Index blockRows, Index blockCols) : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol), @@ -265,10 +265,10 @@ class Block /** Column or Row constructor */ - inline Block(XprType& xpr, Index i) - : Base(&xpr.coeffRef( + inline Block(typename internal::as_argument::type xpr, Index i) + : Base(internal::const_cast_ptr(&xpr.coeffRef( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0, - (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0), + (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0)), BlockRows==1 ? 1 : xpr.rows(), BlockCols==1 ? 1 : xpr.cols()), m_xpr(xpr) @@ -281,8 +281,8 @@ class Block /** Fixed-size constructor */ - inline Block(XprType& xpr, Index startRow, Index startCol) - : Base(&xpr.coeffRef(startRow,startCol)), m_xpr(xpr) + inline Block(typename internal::as_argument::type xpr, Index startRow, Index startCol) + : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol))), m_xpr(xpr) { eigen_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= xpr.rows() && startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= xpr.cols()); @@ -291,10 +291,10 @@ class Block /** Dynamic-size constructor */ - inline Block(XprType& xpr, + inline Block(typename internal::as_argument::type xpr, Index startRow, Index startCol, Index blockRows, Index blockCols) - : Base(&xpr.coeffRef(startRow,startCol), blockRows, blockCols), + : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol)), blockRows, blockCols), m_xpr(xpr) { eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows) @@ -326,7 +326,7 @@ class Block #ifndef EIGEN_PARSED_BY_DOXYGEN /** \internal used by allowAligned() */ - inline Block(const XprType& xpr, const Scalar* data, Index blockRows, Index blockCols) + inline Block(typename internal::as_argument::type xpr, const Scalar* data, Index blockRows, Index blockCols) : Base(data, blockRows, blockCols), m_xpr(xpr) { init(); diff --git a/Eigen/src/Core/Diagonal.h b/Eigen/src/Core/Diagonal.h index 30818f6d12..3fea583a59 100644 --- a/Eigen/src/Core/Diagonal.h +++ b/Eigen/src/Core/Diagonal.h @@ -82,7 +82,7 @@ template class Diagonal typedef typename internal::dense_xpr_base::type Base; EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal) - inline Diagonal(const MatrixType& matrix, Index index = DiagIndex) : m_matrix(matrix), m_index(index) {} + inline Diagonal(typename internal::as_argument::type matrix, Index index = DiagIndex) : m_matrix(matrix), m_index(index) {} EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal) diff --git a/Eigen/src/Core/Transpose.h b/Eigen/src/Core/Transpose.h index a5687790fe..9f80fefd88 100644 --- a/Eigen/src/Core/Transpose.h +++ b/Eigen/src/Core/Transpose.h @@ -75,7 +75,7 @@ template class Transpose typedef typename TransposeImpl::StorageKind>::Base Base; EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose) - inline Transpose(const MatrixType& matrix) : m_matrix(matrix) {} + inline Transpose(typename internal::as_argument::type matrix) : m_matrix(matrix) {} EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose) @@ -222,7 +222,7 @@ template inline const typename DenseBase::ConstTransposeReturnType DenseBase::transpose() const { - return derived(); + return ConstTransposeReturnType(derived()); } /** \returns an expression of the adjoint (i.e. conjugate transpose) of *this. diff --git a/Eigen/src/Core/util/BlasUtil.h b/Eigen/src/Core/util/BlasUtil.h index b15110baa7..4ae410a786 100644 --- a/Eigen/src/Core/util/BlasUtil.h +++ b/Eigen/src/Core/util/BlasUtil.h @@ -232,8 +232,8 @@ struct blas_traits > typedef typename NestedXpr::Scalar Scalar; typedef blas_traits Base; typedef Transpose XprType; - typedef Transpose ExtractType; - typedef Transpose _ExtractType; + typedef Transpose ExtractType; // const to get rid of a compile error; anyway blas traits are only used on the RHS + typedef Transpose _ExtractType; typedef typename conditional::type> str >::type type; }; +template +struct as_argument +{ + typedef typename nested::type type; +}; + +template +struct as_argument +{ + typedef const typename nested::type type; +}; + +template +struct as_argument > +{ + typedef Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> MatrixType; + typedef MatrixType& type; +}; + +template +struct as_argument > +{ + typedef Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> MatrixType; + typedef const MatrixType& type; +}; + +template +struct as_argument > +{ + typedef Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> ArrayType; + typedef ArrayType& type; +}; + +template +struct as_argument > +{ + typedef Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> ArrayType; + typedef const ArrayType& type; +}; + +template +T* const_cast_ptr(const T* ptr) +{ + return const_cast(ptr); +} + template::XprKind> struct dense_xpr_base { diff --git a/Eigen/src/Sparse/SparseMatrix.h b/Eigen/src/Sparse/SparseMatrix.h index 2346a0c2e9..73ca5945b7 100644 --- a/Eigen/src/Sparse/SparseMatrix.h +++ b/Eigen/src/Sparse/SparseMatrix.h @@ -61,6 +61,22 @@ struct traits > SupportedAccessPatterns = InnerRandomAccessPattern }; }; + + +template +struct as_argument > +{ + typedef SparseMatrix<_Scalar, _Options, _Index> MatrixType; + typedef MatrixType& type; +}; + +template +struct as_argument > +{ + typedef SparseMatrix<_Scalar, _Options, _Index> MatrixType; + typedef const MatrixType& type; +}; + } // end namespace internal template diff --git a/Eigen/src/Sparse/SparseMatrixBase.h b/Eigen/src/Sparse/SparseMatrixBase.h index 6ad428324a..5901dacc23 100644 --- a/Eigen/src/Sparse/SparseMatrixBase.h +++ b/Eigen/src/Sparse/SparseMatrixBase.h @@ -118,10 +118,11 @@ template class SparseMatrixBase : public EigenBase // typedef SparseCwiseUnaryOp, Derived> ImagReturnType; /** \internal the return type of MatrixBase::adjoint() */ typedef typename internal::conditional::IsComplex, - CwiseUnaryOp, Eigen::Transpose >, - Transpose + CwiseUnaryOp, Eigen::Transpose >, + Transpose >::type AdjointReturnType; + typedef SparseMatrix PlainObject; #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase @@ -424,7 +425,7 @@ template class SparseMatrixBase : public EigenBase // void normalize(); Transpose transpose() { return derived(); } - const Transpose transpose() const { return derived(); } + const Transpose transpose() const { return derived(); } // void transposeInPlace(); const AdjointReturnType adjoint() const { return transpose(); }