From 64e41febf279c76bc434fdadaa7e1d6d8e99e060 Mon Sep 17 00:00:00 2001 From: chriseth Date: Sun, 14 Aug 2022 22:10:13 +0200 Subject: [PATCH] simplify --- libsolutil/LinearExpression.cpp | 50 +++++++++++---------------------- libsolutil/LinearExpression.h | 3 +- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/libsolutil/LinearExpression.cpp b/libsolutil/LinearExpression.cpp index 7b7668cff..b89e8038d 100644 --- a/libsolutil/LinearExpression.cpp +++ b/libsolutil/LinearExpression.cpp @@ -53,10 +53,7 @@ void SparseMatrix::addMultipleOfRow(size_t _sourceRow, size_t _targetRow, ration target = next; } } - else if (!target) - target = appendToRow(_targetRow, source->col, _factor * source->value)->next_in_row; - else - target = prependInRow(*target, source->col, _factor * source->value)->next_in_row; + target = prependInRow(target, _targetRow, source->col, _factor * source->value)->next_in_row; source = source->next_in_row; } @@ -95,52 +92,37 @@ void SparseMatrix::remove(SparseMatrix::Entry& _e) m_col_end[_e.col] = _e.prev_in_col; } -SparseMatrix::Entry* SparseMatrix::appendToRow(size_t _row, size_t _column, rational _value) +SparseMatrix::Entry* SparseMatrix::prependInRow(Entry* _successor, size_t _row, size_t _column, rational _value) { - // TODO could be combined with prependInRow - // with successor being nullptr m_elements.emplace_back(make_unique(Entry{ move(_value), _row, _column, - m_row_end[_row], nullptr, + _successor, nullptr, nullptr })); Entry* e = m_elements.back().get(); - if (m_row_end[_row]) - m_row_end[_row]->next_in_row = e; - m_row_end[_row] = e; - - if (!m_row_start[_row]) + if (_successor) + { + e->prev_in_row = _successor->prev_in_row; + _successor->prev_in_row = e; + } + else + { + e->prev_in_row = m_row_end[_row]; + m_row_end[_row] = e; + } + if (e->prev_in_row) + e->prev_in_row->next_in_row = e; + else m_row_start[_row] = e; adjustColumnProperties(*e); return e; } -SparseMatrix::Entry* SparseMatrix::prependInRow(Entry& _successor, size_t _column, rational _value) -{ - size_t row = _successor.row; - m_elements.emplace_back(make_unique(Entry{ - move(_value), - row, - _column, - _successor.prev_in_row, - &_successor, - nullptr, - nullptr - })); - Entry* e = m_elements.back().get(); - _successor.prev_in_row = e; - if (m_row_start[row] == &_successor) - m_row_start[row] = e; - - adjustColumnProperties(*e); - return e; -} - void SparseMatrix::adjustColumnProperties(Entry& _entry) { size_t column = _entry.col; diff --git a/libsolutil/LinearExpression.h b/libsolutil/LinearExpression.h index 2c48d7869..65586555d 100644 --- a/libsolutil/LinearExpression.h +++ b/libsolutil/LinearExpression.h @@ -211,8 +211,7 @@ public: private: void remove(Entry& _entry); - Entry* appendToRow(size_t _row, size_t _column, rational _value); - Entry* prependInRow(Entry& _successor, size_t _column, rational _value); + Entry* prependInRow(Entry* _successor, size_t _row, size_t _column, rational _value); void adjustColumnProperties(Entry& _entry); std::vector> m_elements;