Resizes the matrix to a \a rows x \a cols matrix leaving old values untouched. * * If the sizes of the matrix are decreased, then the matrix is turned to \b uncompressed-mode * and the storage of the out of bounds coefficients is kept and reserved. * Call makeCompressed() to pack the entries and squeeze extra memory. * * \sa reserve(), setZero(), makeCompressed(
| 551 | * \sa reserve(), setZero(), makeCompressed() |
| 552 | */ |
| 553 | void conservativeResize(Index rows, Index cols) |
| 554 | { |
| 555 | // No change |
| 556 | if (this->rows() == rows && this->cols() == cols) return; |
| 557 | |
| 558 | // If one dimension is null, then there is nothing to be preserved |
| 559 | if(rows==0 || cols==0) return resize(rows,cols); |
| 560 | |
| 561 | Index innerChange = IsRowMajor ? cols - this->cols() : rows - this->rows(); |
| 562 | Index outerChange = IsRowMajor ? rows - this->rows() : cols - this->cols(); |
| 563 | StorageIndex newInnerSize = convert_index(IsRowMajor ? cols : rows); |
| 564 | |
| 565 | // Deals with inner non zeros |
| 566 | if (m_innerNonZeros) |
| 567 | { |
| 568 | // Resize m_innerNonZeros |
| 569 | StorageIndex *newInnerNonZeros = static_cast<StorageIndex*>(std::realloc(m_innerNonZeros, (m_outerSize + outerChange) * sizeof(StorageIndex))); |
| 570 | if (!newInnerNonZeros) internal::throw_std_bad_alloc(); |
| 571 | m_innerNonZeros = newInnerNonZeros; |
| 572 | |
| 573 | for(Index i=m_outerSize; i<m_outerSize+outerChange; i++) |
| 574 | m_innerNonZeros[i] = 0; |
| 575 | } |
| 576 | else if (innerChange < 0) |
| 577 | { |
| 578 | // Inner size decreased: allocate a new m_innerNonZeros |
| 579 | m_innerNonZeros = static_cast<StorageIndex*>(std::malloc((m_outerSize+outerChange+1) * sizeof(StorageIndex))); |
| 580 | if (!m_innerNonZeros) internal::throw_std_bad_alloc(); |
| 581 | for(Index i = 0; i < m_outerSize; i++) |
| 582 | m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i]; |
| 583 | } |
| 584 | |
| 585 | // Change the m_innerNonZeros in case of a decrease of inner size |
| 586 | if (m_innerNonZeros && innerChange < 0) |
| 587 | { |
| 588 | for(Index i = 0; i < m_outerSize + (std::min)(outerChange, Index(0)); i++) |
| 589 | { |
| 590 | StorageIndex &n = m_innerNonZeros[i]; |
| 591 | StorageIndex start = m_outerIndex[i]; |
| 592 | while (n > 0 && m_data.index(start+n-1) >= newInnerSize) --n; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | m_innerSize = newInnerSize; |
| 597 | |
| 598 | // Re-allocate outer index structure if necessary |
| 599 | if (outerChange == 0) |
| 600 | return; |
| 601 | |
| 602 | StorageIndex *newOuterIndex = static_cast<StorageIndex*>(std::realloc(m_outerIndex, (m_outerSize + outerChange + 1) * sizeof(StorageIndex))); |
| 603 | if (!newOuterIndex) internal::throw_std_bad_alloc(); |
| 604 | m_outerIndex = newOuterIndex; |
| 605 | if (outerChange > 0) |
| 606 | { |
| 607 | StorageIndex last = m_outerSize == 0 ? 0 : m_outerIndex[m_outerSize]; |
| 608 | for(Index i=m_outerSize; i<m_outerSize+outerChange+1; i++) |
| 609 | m_outerIndex[i] = last; |
| 610 | } |
nothing calls this directly
no test coverage detected