QtBase: use erase and std::remove_if with QList and QVector

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

Change-Id: I38b49e56b12c396db9fc0f1b75d8fb43c503a7f6
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2016-02-15 11:16:39 +03:00
parent f931e5e72d
commit 8903460093
8 changed files with 60 additions and 51 deletions

View File

@ -1717,15 +1717,15 @@ const QItemSelection QItemSelectionModel::selection() const
Q_D(const QItemSelectionModel);
QItemSelection selected = d->ranges;
selected.merge(d->currentSelection, d->currentCommand);
int i = 0;
// make sure we have no invalid ranges
// ### should probably be handled more generic somewhere else
while (i<selected.count()) {
if (selected.at(i).isValid())
++i;
else
(selected.removeAt(i));
}
auto isNotValid = [](const QItemSelectionRange& range) {
return !range.isValid();
};
selected.erase(std::remove_if(selected.begin(), selected.end(),
isNotValid),
selected.end());
return selected;
}

View File

@ -45,6 +45,8 @@
#include <QtGui/private/qopenglcontext_p.h>
#include <QtCore/qthreadstorage.h>
#include <algorithm>
#if defined(QT_DEBUG)
#include <QMetaEnum>
#endif
@ -475,15 +477,16 @@ QOpenGLEngineShaderProg *QOpenGLEngineSharedShaders::findProgramInCache(const QO
void QOpenGLEngineSharedShaders::cleanupCustomStage(QOpenGLCustomShaderStage* stage)
{
// Remove any shader programs which has this as the custom shader src:
for (int i = 0; i < cachedPrograms.size(); ++i) {
QOpenGLEngineShaderProg *cachedProg = cachedPrograms[i];
auto hasStageAsCustomShaderSouce = [stage](QOpenGLEngineShaderProg *cachedProg) -> bool {
if (cachedProg->customStageSource == stage->source()) {
delete cachedProg;
cachedPrograms.removeAt(i);
i--;
return true;
}
}
return false;
};
cachedPrograms.erase(std::remove_if(cachedPrograms.begin(), cachedPrograms.end(),
hasStageAsCustomShaderSouce),
cachedPrograms.end());
}

View File

@ -2901,14 +2901,12 @@ static void markFrames(QTextFrame *current, int from, int oldLength, int length)
return;
QTextFrameData *fd = data(current);
for (int i = 0; i < fd->floats.size(); ++i) {
QTextFrame *f = fd->floats[i];
if (!f) {
// float got removed in editing operation
fd->floats.removeAt(i);
--i;
}
}
// float got removed in editing operation
QTextFrame *null = nullptr; // work-around for (at least) MSVC 2012 emitting
// warning C4100 for its own header <algorithm>
// when passing nullptr directly to std::remove
fd->floats.erase(std::remove(fd->floats.begin(), fd->floats.end(), null),
fd->floats.end());
fd->layoutDirty = true;
fd->sizeDirty = true;

View File

@ -48,6 +48,8 @@
#include <QMetaEnum>
#endif
#include <algorithm>
// #define QT_GL_SHARED_SHADER_DEBUG
QT_BEGIN_NAMESPACE
@ -472,15 +474,16 @@ QGLEngineShaderProg *QGLEngineSharedShaders::findProgramInCache(const QGLEngineS
void QGLEngineSharedShaders::cleanupCustomStage(QGLCustomShaderStage* stage)
{
// Remove any shader programs which has this as the custom shader src:
for (int i = 0; i < cachedPrograms.size(); ++i) {
QGLEngineShaderProg *cachedProg = cachedPrograms[i];
auto hasStageAsCustomShaderSouce = [stage](QGLEngineShaderProg *cachedProg) -> bool {
if (cachedProg->customStageSource == stage->source()) {
delete cachedProg;
cachedPrograms.removeAt(i);
i--;
return true;
}
}
return false;
};
cachedPrograms.erase(std::remove_if(cachedPrograms.begin(), cachedPrograms.end(),
hasStageAsCustomShaderSouce),
cachedPrograms.end());
}

View File

@ -48,6 +48,9 @@
#include "qiosintegration.h"
#include "qiostextresponder.h"
#include <algorithm>
#include <iterator>
// m_currentMenu points to the currently visible menu.
// Only one menu will be visible at a time, and if a second menu
// is shown on top of a first, the first one will be told to hide.
@ -525,14 +528,10 @@ bool QIOSMenu::eventFilter(QObject *obj, QEvent *event)
QIOSMenuItemList QIOSMenu::visibleMenuItems() const
{
QIOSMenuItemList visibleMenuItems = m_menuItems;
for (int i = visibleMenuItems.count() - 1; i >= 0; --i) {
QIOSMenuItem *item = visibleMenuItems.at(i);
if (!item->m_enabled || !item->m_visible || item->m_separator)
visibleMenuItems.removeAt(i);
}
QIOSMenuItemList visibleMenuItems;
visibleMenuItems.reserve(m_menuItems.size());
std::copy_if(m_menuItems.begin(), m_menuItems.end(), std::back_inserter(visibleMenuItems),
[](QIOSMenuItem *item) { return item->m_enabled && item->m_visible && !item->m_separator; });
return visibleMenuItems;
}

View File

@ -68,6 +68,8 @@
# include <qscroller.h>
#endif
#include <algorithm>
QT_BEGIN_NAMESPACE
QAbstractItemViewPrivate::QAbstractItemViewPrivate()
@ -4459,10 +4461,12 @@ QModelIndexList QAbstractItemViewPrivate::selectedDraggableIndexes() const
{
Q_Q(const QAbstractItemView);
QModelIndexList indexes = q->selectedIndexes();
for(int i = indexes.count() - 1 ; i >= 0; --i) {
if (!isIndexDragEnabled(indexes.at(i)))
indexes.removeAt(i);
}
auto isNotDragEnabled = [this](const QModelIndex &index) {
return !isIndexDragEnabled(index);
};
indexes.erase(std::remove_if(indexes.begin(), indexes.end(),
isNotDragEnabled),
indexes.end());
return indexes;
}

View File

@ -71,6 +71,8 @@
#include <private/qstylehelper_p.h>
#include <private/qstyleanimation_p.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
#if defined(Q_OS_WIN)
@ -159,12 +161,11 @@ bool QWindowsStyle::eventFilter(QObject *o, QEvent *e)
// Alt has been pressed - find all widgets that care
QList<QWidget *> l = widget->findChildren<QWidget *>();
for (int pos=0 ; pos < l.size() ; ++pos) {
QWidget *w = l.at(pos);
if (w->isWindow() || !w->isVisible() ||
w->style()->styleHint(SH_UnderlineShortcut, 0, w))
l.removeAt(pos);
}
auto ignorable = [](QWidget *w) {
return w->isWindow() || !w->isVisible()
|| w->style()->styleHint(SH_UnderlineShortcut, 0, w);
};
l.erase(std::remove_if(l.begin(), l.end(), ignorable), l.end());
// Update states before repainting
d->seenAlt.append(widget);
d->alt_down = true;

View File

@ -188,15 +188,16 @@ QList<QAbstractButton *>QAbstractButtonPrivate::queryButtonList() const
QList<QAbstractButton*>candidates = parent->findChildren<QAbstractButton *>();
if (autoExclusive) {
for (int i = candidates.count() - 1; i >= 0; --i) {
QAbstractButton *candidate = candidates.at(i);
if (!candidate->autoExclusive()
auto isNoMemberOfMyAutoExclusiveGroup = [](QAbstractButton *candidate) {
return !candidate->autoExclusive()
#ifndef QT_NO_BUTTONGROUP
|| candidate->group()
#endif
)
candidates.removeAt(i);
}
;
};
candidates.erase(std::remove_if(candidates.begin(), candidates.end(),
isNoMemberOfMyAutoExclusiveGroup),
candidates.end());
}
return candidates;
}