This commit is contained in:
chriseth 2022-08-14 22:10:13 +02:00
parent d308cf8d96
commit 64e41febf2
2 changed files with 17 additions and 36 deletions

View File

@ -53,10 +53,7 @@ void SparseMatrix::addMultipleOfRow(size_t _sourceRow, size_t _targetRow, ration
target = next; target = next;
} }
} }
else if (!target) target = prependInRow(target, _targetRow, source->col, _factor * source->value)->next_in_row;
target = appendToRow(_targetRow, source->col, _factor * source->value)->next_in_row;
else
target = prependInRow(*target, source->col, _factor * source->value)->next_in_row;
source = source->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; 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>(Entry{ m_elements.emplace_back(make_unique<Entry>(Entry{
move(_value), move(_value),
_row, _row,
_column, _column,
m_row_end[_row],
nullptr, nullptr,
_successor,
nullptr, nullptr,
nullptr nullptr
})); }));
Entry* e = m_elements.back().get(); Entry* e = m_elements.back().get();
if (m_row_end[_row]) if (_successor)
m_row_end[_row]->next_in_row = e; {
m_row_end[_row] = e; e->prev_in_row = _successor->prev_in_row;
_successor->prev_in_row = e;
if (!m_row_start[_row]) }
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; m_row_start[_row] = e;
adjustColumnProperties(*e); adjustColumnProperties(*e);
return 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>(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) void SparseMatrix::adjustColumnProperties(Entry& _entry)
{ {
size_t column = _entry.col; size_t column = _entry.col;

View File

@ -211,8 +211,7 @@ public:
private: private:
void remove(Entry& _entry); void remove(Entry& _entry);
Entry* appendToRow(size_t _row, size_t _column, rational _value); Entry* prependInRow(Entry* _successor, size_t _row, size_t _column, rational _value);
Entry* prependInRow(Entry& _successor, size_t _column, rational _value);
void adjustColumnProperties(Entry& _entry); void adjustColumnProperties(Entry& _entry);
std::vector<std::unique_ptr<Entry>> m_elements; std::vector<std::unique_ptr<Entry>> m_elements;