Fix SparseMatrix::conservativeResize() when one dimension is null

This commit is contained in:
Gael Guennebaud 2013-07-12 14:10:02 +02:00
parent 444c09e313
commit 5431473d67

View File

@ -534,6 +534,9 @@ class SparseMatrix
// No change
if (this->rows() == rows && this->cols() == cols) return;
// If one dimension is null, then there is nothing to be preserved
if(rows==0 || cols==0) return resize(rows,cols);
Index innerChange = IsRowMajor ? cols - this->cols() : rows - this->rows();
Index outerChange = IsRowMajor ? rows - this->rows() : cols - this->cols();
Index newInnerSize = IsRowMajor ? cols : rows;
@ -559,7 +562,8 @@ class SparseMatrix
}
// Change the m_innerNonZeros in case of a decrease of inner size
if (m_innerNonZeros && innerChange < 0) {
if (m_innerNonZeros && innerChange < 0)
{
for(Index i = 0; i < m_outerSize + (std::min)(outerChange, Index(0)); i++)
{
Index &n = m_innerNonZeros[i];
@ -577,13 +581,13 @@ class SparseMatrix
Index *newOuterIndex = static_cast<Index*>(std::realloc(m_outerIndex, (m_outerSize + outerChange + 1) * sizeof(Index)));
if (!newOuterIndex) internal::throw_std_bad_alloc();
m_outerIndex = newOuterIndex;
if (outerChange > 0) {
if (outerChange > 0)
{
Index last = m_outerSize == 0 ? 0 : m_outerIndex[m_outerSize];
for(Index i=m_outerSize; i<m_outerSize+outerChange+1; i++)
m_outerIndex[i] = last;
}
m_outerSize += outerChange;
}
/** Resizes the matrix to a \a rows x \a cols matrix and initializes it to zero.