third pass of const-correctness fixes (bug #54), hopefully the last one...

This commit is contained in:
Benoit Jacob 2011-01-07 05:16:01 -05:00
parent c7baf07a3e
commit 98f0274305
7 changed files with 82 additions and 19 deletions

View File

@ -119,7 +119,7 @@ template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool H
/** Column or Row constructor /** Column or Row constructor
*/ */
inline Block(const XprType& xpr, Index i) inline Block(typename internal::as_argument<XprType>::type xpr, Index i)
: m_xpr(xpr), : m_xpr(xpr),
// It is a row if and only if BlockRows==1 and BlockCols==XprType::ColsAtCompileTime, // 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, // and it is a column if and only if BlockRows==XprType::RowsAtCompileTime and BlockCols==1,
@ -137,7 +137,7 @@ template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool H
/** Fixed-size constructor /** Fixed-size constructor
*/ */
inline Block(const XprType& xpr, Index startRow, Index startCol) inline Block(typename internal::as_argument<XprType>::type xpr, Index startRow, Index startCol)
: m_xpr(xpr), m_startRow(startRow), m_startCol(startCol), : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
m_blockRows(BlockRows), m_blockCols(BlockCols) m_blockRows(BlockRows), m_blockCols(BlockCols)
{ {
@ -148,7 +148,7 @@ template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool H
/** Dynamic-size constructor /** Dynamic-size constructor
*/ */
inline Block(const XprType& xpr, inline Block(typename internal::as_argument<XprType>::type xpr,
Index startRow, Index startCol, Index startRow, Index startCol,
Index blockRows, Index blockCols) Index blockRows, Index blockCols)
: m_xpr(xpr), m_startRow(startRow), m_startCol(startCol), : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
@ -265,10 +265,10 @@ class Block<XprType,BlockRows,BlockCols, InnerPanel,true>
/** Column or Row constructor /** Column or Row constructor
*/ */
inline Block(XprType& xpr, Index i) inline Block(typename internal::as_argument<XprType>::type xpr, Index i)
: Base(&xpr.coeffRef( : Base(internal::const_cast_ptr(&xpr.coeffRef(
(BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0, (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(), BlockRows==1 ? 1 : xpr.rows(),
BlockCols==1 ? 1 : xpr.cols()), BlockCols==1 ? 1 : xpr.cols()),
m_xpr(xpr) m_xpr(xpr)
@ -281,8 +281,8 @@ class Block<XprType,BlockRows,BlockCols, InnerPanel,true>
/** Fixed-size constructor /** Fixed-size constructor
*/ */
inline Block(XprType& xpr, Index startRow, Index startCol) inline Block(typename internal::as_argument<XprType>::type xpr, Index startRow, Index startCol)
: Base(&xpr.coeffRef(startRow,startCol)), m_xpr(xpr) : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol))), m_xpr(xpr)
{ {
eigen_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= xpr.rows() eigen_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= xpr.rows()
&& startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= xpr.cols()); && startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= xpr.cols());
@ -291,10 +291,10 @@ class Block<XprType,BlockRows,BlockCols, InnerPanel,true>
/** Dynamic-size constructor /** Dynamic-size constructor
*/ */
inline Block(XprType& xpr, inline Block(typename internal::as_argument<XprType>::type xpr,
Index startRow, Index startCol, Index startRow, Index startCol,
Index blockRows, Index blockCols) 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) m_xpr(xpr)
{ {
eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows) eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
@ -326,7 +326,7 @@ class Block<XprType,BlockRows,BlockCols, InnerPanel,true>
#ifndef EIGEN_PARSED_BY_DOXYGEN #ifndef EIGEN_PARSED_BY_DOXYGEN
/** \internal used by allowAligned() */ /** \internal used by allowAligned() */
inline Block(const XprType& xpr, const Scalar* data, Index blockRows, Index blockCols) inline Block(typename internal::as_argument<XprType>::type xpr, const Scalar* data, Index blockRows, Index blockCols)
: Base(data, blockRows, blockCols), m_xpr(xpr) : Base(data, blockRows, blockCols), m_xpr(xpr)
{ {
init(); init();

View File

@ -82,7 +82,7 @@ template<typename MatrixType, int DiagIndex> class Diagonal
typedef typename internal::dense_xpr_base<Diagonal>::type Base; typedef typename internal::dense_xpr_base<Diagonal>::type Base;
EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal) 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<MatrixType>::type matrix, Index index = DiagIndex) : m_matrix(matrix), m_index(index) {}
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)

View File

@ -75,7 +75,7 @@ template<typename MatrixType> class Transpose
typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base; typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose) EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
inline Transpose(const MatrixType& matrix) : m_matrix(matrix) {} inline Transpose(typename internal::as_argument<MatrixType>::type matrix) : m_matrix(matrix) {}
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
@ -222,7 +222,7 @@ template<typename Derived>
inline const typename DenseBase<Derived>::ConstTransposeReturnType inline const typename DenseBase<Derived>::ConstTransposeReturnType
DenseBase<Derived>::transpose() const DenseBase<Derived>::transpose() const
{ {
return derived(); return ConstTransposeReturnType(derived());
} }
/** \returns an expression of the adjoint (i.e. conjugate transpose) of *this. /** \returns an expression of the adjoint (i.e. conjugate transpose) of *this.

View File

@ -232,8 +232,8 @@ struct blas_traits<Transpose<NestedXpr> >
typedef typename NestedXpr::Scalar Scalar; typedef typename NestedXpr::Scalar Scalar;
typedef blas_traits<NestedXpr> Base; typedef blas_traits<NestedXpr> Base;
typedef Transpose<NestedXpr> XprType; typedef Transpose<NestedXpr> XprType;
typedef Transpose<typename Base::_ExtractType> ExtractType; typedef Transpose<const typename Base::_ExtractType> ExtractType; // const to get rid of a compile error; anyway blas traits are only used on the RHS
typedef Transpose<typename Base::_ExtractType> _ExtractType; typedef Transpose<const typename Base::_ExtractType> _ExtractType;
typedef typename conditional<bool(Base::HasUsableDirectAccess), typedef typename conditional<bool(Base::HasUsableDirectAccess),
ExtractType, ExtractType,
typename ExtractType::PlainObject typename ExtractType::PlainObject

View File

@ -333,6 +333,52 @@ template<typename T, int n=1, typename PlainObject = typename eval<T>::type> str
>::type type; >::type type;
}; };
template<typename ExpressionType>
struct as_argument
{
typedef typename nested<ExpressionType>::type type;
};
template<typename ExpressionType>
struct as_argument<const ExpressionType>
{
typedef const typename nested<ExpressionType>::type type;
};
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct as_argument<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
{
typedef Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> MatrixType;
typedef MatrixType& type;
};
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct as_argument<const Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
{
typedef Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> MatrixType;
typedef const MatrixType& type;
};
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct as_argument<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
{
typedef Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> ArrayType;
typedef ArrayType& type;
};
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct as_argument<const Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
{
typedef Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> ArrayType;
typedef const ArrayType& type;
};
template<typename T>
T* const_cast_ptr(const T* ptr)
{
return const_cast<T*>(ptr);
}
template<typename Derived, typename XprKind = typename traits<Derived>::XprKind> template<typename Derived, typename XprKind = typename traits<Derived>::XprKind>
struct dense_xpr_base struct dense_xpr_base
{ {

View File

@ -61,6 +61,22 @@ struct traits<SparseMatrix<_Scalar, _Options, _Index> >
SupportedAccessPatterns = InnerRandomAccessPattern SupportedAccessPatterns = InnerRandomAccessPattern
}; };
}; };
template<typename _Scalar, int _Options, typename _Index>
struct as_argument<SparseMatrix<_Scalar, _Options, _Index> >
{
typedef SparseMatrix<_Scalar, _Options, _Index> MatrixType;
typedef MatrixType& type;
};
template<typename _Scalar, int _Options, typename _Index>
struct as_argument<const SparseMatrix<_Scalar, _Options, _Index> >
{
typedef SparseMatrix<_Scalar, _Options, _Index> MatrixType;
typedef const MatrixType& type;
};
} // end namespace internal } // end namespace internal
template<typename _Scalar, int _Options, typename _Index> template<typename _Scalar, int _Options, typename _Index>

View File

@ -118,10 +118,11 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
// typedef SparseCwiseUnaryOp<internal::scalar_imag_op<Scalar>, Derived> ImagReturnType; // typedef SparseCwiseUnaryOp<internal::scalar_imag_op<Scalar>, Derived> ImagReturnType;
/** \internal the return type of MatrixBase::adjoint() */ /** \internal the return type of MatrixBase::adjoint() */
typedef typename internal::conditional<NumTraits<Scalar>::IsComplex, typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, Eigen::Transpose<Derived> >, CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, Eigen::Transpose<const Derived> >,
Transpose<Derived> Transpose<const Derived>
>::type AdjointReturnType; >::type AdjointReturnType;
typedef SparseMatrix<Scalar, Flags&RowMajorBit ? RowMajor : ColMajor> PlainObject; typedef SparseMatrix<Scalar, Flags&RowMajorBit ? RowMajor : ColMajor> PlainObject;
#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
@ -424,7 +425,7 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
// void normalize(); // void normalize();
Transpose<Derived> transpose() { return derived(); } Transpose<Derived> transpose() { return derived(); }
const Transpose<Derived> transpose() const { return derived(); } const Transpose<const Derived> transpose() const { return derived(); }
// void transposeInPlace(); // void transposeInPlace();
const AdjointReturnType adjoint() const { return transpose(); } const AdjointReturnType adjoint() const { return transpose(); }