From 9f4630b993dbe531e5b11f6bedc53927f6f942ae Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 18 Aug 2022 17:44:16 +0200 Subject: [PATCH] ensure size --- libsolutil/LinearExpression.cpp | 27 ++++++++++++++++----------- libsolutil/LinearExpression.h | 1 + 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/libsolutil/LinearExpression.cpp b/libsolutil/LinearExpression.cpp index dd75b9ba1..412b1d23e 100644 --- a/libsolutil/LinearExpression.cpp +++ b/libsolutil/LinearExpression.cpp @@ -103,6 +103,7 @@ void SparseMatrix::addMultipleOfRow(size_t _sourceRow, size_t _targetRow, ration SparseMatrix::Entry& SparseMatrix::entry(size_t _row, size_t _column) { + ensureSize(_row, _column); Entry* successor = entryOrSuccessorInRow(_row, _column); if (successor && successor->col == _column) return *successor; @@ -112,17 +113,7 @@ SparseMatrix::Entry& SparseMatrix::entry(size_t _row, size_t _column) void SparseMatrix::insert(size_t _row, size_t _column, rational _value) { - if (_column >= m_col_start.size()) - { - m_col_start.resize(_column + 1); - m_col_end.resize(_column + 1); - } - if (_row >= m_row_start.size()) - { - m_row_start.resize(_row + 1); - m_row_end.resize(_row + 1); - } - + ensureSize(_row, _column); prependInRow(entryOrSuccessorInRow(_row, _column), _row, _column, move(_value)); } @@ -138,6 +129,20 @@ void SparseMatrix::appendRow(LinearExpression const& _entries) } } +void SparseMatrix::ensureSize(size_t _row, size_t _column) +{ + if (_column >= m_col_start.size()) + { + m_col_start.resize(_column + 1); + m_col_end.resize(_column + 1); + } + if (_row >= m_row_start.size()) + { + m_row_start.resize(_row + 1); + m_row_end.resize(_row + 1); + } +} + SparseMatrix::Entry* SparseMatrix::entryOrSuccessorInRow(size_t _row, size_t _column) { Entry* successor = nullptr; diff --git a/libsolutil/LinearExpression.h b/libsolutil/LinearExpression.h index 973804074..06b0bf157 100644 --- a/libsolutil/LinearExpression.h +++ b/libsolutil/LinearExpression.h @@ -256,6 +256,7 @@ public: void appendRow(LinearExpression const& _entries); private: + void ensureSize(size_t _row, size_t _column); /// @returns the entry at the row/column if it exists or its successor in the row. Entry* entryOrSuccessorInRow(size_t _row, size_t _column);