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

View File

@ -45,6 +45,8 @@
#include <QtGui/private/qopenglcontext_p.h> #include <QtGui/private/qopenglcontext_p.h>
#include <QtCore/qthreadstorage.h> #include <QtCore/qthreadstorage.h>
#include <algorithm>
#if defined(QT_DEBUG) #if defined(QT_DEBUG)
#include <QMetaEnum> #include <QMetaEnum>
#endif #endif
@ -475,15 +477,16 @@ QOpenGLEngineShaderProg *QOpenGLEngineSharedShaders::findProgramInCache(const QO
void QOpenGLEngineSharedShaders::cleanupCustomStage(QOpenGLCustomShaderStage* stage) void QOpenGLEngineSharedShaders::cleanupCustomStage(QOpenGLCustomShaderStage* stage)
{ {
// Remove any shader programs which has this as the custom shader src: auto hasStageAsCustomShaderSouce = [stage](QOpenGLEngineShaderProg *cachedProg) -> bool {
for (int i = 0; i < cachedPrograms.size(); ++i) {
QOpenGLEngineShaderProg *cachedProg = cachedPrograms[i];
if (cachedProg->customStageSource == stage->source()) { if (cachedProg->customStageSource == stage->source()) {
delete cachedProg; delete cachedProg;
cachedPrograms.removeAt(i); return true;
i--;
} }
} 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; return;
QTextFrameData *fd = data(current); QTextFrameData *fd = data(current);
for (int i = 0; i < fd->floats.size(); ++i) { // float got removed in editing operation
QTextFrame *f = fd->floats[i]; QTextFrame *null = nullptr; // work-around for (at least) MSVC 2012 emitting
if (!f) { // warning C4100 for its own header <algorithm>
// float got removed in editing operation // when passing nullptr directly to std::remove
fd->floats.removeAt(i); fd->floats.erase(std::remove(fd->floats.begin(), fd->floats.end(), null),
--i; fd->floats.end());
}
}
fd->layoutDirty = true; fd->layoutDirty = true;
fd->sizeDirty = true; fd->sizeDirty = true;

View File

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

View File

@ -48,6 +48,9 @@
#include "qiosintegration.h" #include "qiosintegration.h"
#include "qiostextresponder.h" #include "qiostextresponder.h"
#include <algorithm>
#include <iterator>
// m_currentMenu points to the currently visible menu. // m_currentMenu points to the currently visible menu.
// Only one menu will be visible at a time, and if a second 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. // 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 QIOSMenu::visibleMenuItems() const
{ {
QIOSMenuItemList visibleMenuItems = m_menuItems; QIOSMenuItemList visibleMenuItems;
visibleMenuItems.reserve(m_menuItems.size());
for (int i = visibleMenuItems.count() - 1; i >= 0; --i) { std::copy_if(m_menuItems.begin(), m_menuItems.end(), std::back_inserter(visibleMenuItems),
QIOSMenuItem *item = visibleMenuItems.at(i); [](QIOSMenuItem *item) { return item->m_enabled && item->m_visible && !item->m_separator; });
if (!item->m_enabled || !item->m_visible || item->m_separator)
visibleMenuItems.removeAt(i);
}
return visibleMenuItems; return visibleMenuItems;
} }

View File

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

View File

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

View File

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