QListView: use erase and std::remove_if with QVector

... instead of using erase in a loop, with quadratic complexity.

Change-Id: I9686d117e092f5d74c6e74a462adf503a7b7ae79
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2015-12-24 14:15:07 +03:00
parent 2ccacfb5c9
commit 2a2c582306
2 changed files with 13 additions and 9 deletions

View File

@ -52,6 +52,8 @@
#include <qaccessible.h> #include <qaccessible.h>
#endif #endif
#include <algorithm>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event);
@ -1844,6 +1846,16 @@ bool QListViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QMo
} }
#endif #endif
void QListViewPrivate::removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const
{
auto isCurrentOrDisabled = [=](const QModelIndex &index) {
return !isIndexEnabled(index) || index == current;
};
indexes->erase(std::remove_if(indexes->begin(), indexes->end(),
isCurrentOrDisabled),
indexes->end());
}
/* /*
* Common ListView Implementation * Common ListView Implementation
*/ */

View File

@ -372,15 +372,7 @@ public:
} }
inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); } inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); }
inline void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const { void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const;
QVector<QModelIndex>::iterator it = indexes->begin();
while (it != indexes->end()) {
if (!isIndexEnabled(*it) || (*it) == current)
indexes->erase(it);
else
++it;
}
}
void scrollElasticBandBy(int dx, int dy); void scrollElasticBandBy(int dx, int dy);