VCS: Generalize selections preserving on model refresh

Default implementation is between O(n) (for identical models) to
O(n*m) (for disjoint models). If sort order is known, this
can be reduced to O(n+m), like in Git implementation.

Change-Id: I44662a22961311cb882601d20efa9d445f74748b
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Orgad Shaneh
2013-01-02 23:21:30 +02:00
committed by hjk
parent 01084aaa38
commit 2396f34fda
4 changed files with 62 additions and 25 deletions

View File

@@ -150,4 +150,28 @@ unsigned int SubmitFileModel::filterFiles(const QStringList &filter)
return rc;
}
/*! Updates user selections from \a source model.
*
* Assumption: Both model are sorted with the same order, and there
* are no duplicate entries.
*/
void SubmitFileModel::updateSelections(SubmitFileModel *source)
{
Q_ASSERT(source);
int rows = rowCount();
int sourceRows = source->rowCount();
int lastMatched = 0;
for (int i = 0; i < rows; ++i) {
// Since both models are sorted with the same order, there is no need
// to test rows earlier than latest match found
for (int j = lastMatched; j < sourceRows; ++j) {
if (file(i) == source->file(j) && state(i) == source->state(j)) {
setChecked(i, source->checked(j));
lastMatched = j + 1; // No duplicates, start on next entry
break;
}
}
}
}
} // namespace VcsBase