summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Kudryavtsev <a.kudryavtsev@netris.ru>2016-02-15 11:16:39 +0300
committerAnton Kudryavtsev <a.kudryavtsev@netris.ru>2016-04-08 19:35:26 +0000
commit89034600939bbfe241ba3d6136abf7bb6961db52 (patch)
tree569ed773351a496ddac552fff66859967695351f
parentf931e5e72d4617023bbea46cba2c0d61bb1efa4f (diff)
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>
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.cpp14
-rw-r--r--src/gui/opengl/qopenglengineshadermanager.cpp15
-rw-r--r--src/gui/text/qtextdocumentlayout.cpp14
-rw-r--r--src/opengl/gl2paintengineex/qglengineshadermanager.cpp15
-rw-r--r--src/plugins/platforms/ios/qiosmenu.mm15
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp12
-rw-r--r--src/widgets/styles/qwindowsstyle.cpp13
-rw-r--r--src/widgets/widgets/qabstractbutton.cpp13
8 files changed, 60 insertions, 51 deletions
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp
index b2aacfa5aa..56df8fd55a 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.cpp
+++ b/src/corelib/itemmodels/qitemselectionmodel.cpp
@@ -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;
}
diff --git a/src/gui/opengl/qopenglengineshadermanager.cpp b/src/gui/opengl/qopenglengineshadermanager.cpp
index 1bcb1c6760..4e3d14ba37 100644
--- a/src/gui/opengl/qopenglengineshadermanager.cpp
+++ b/src/gui/opengl/qopenglengineshadermanager.cpp
@@ -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());
}
diff --git a/src/gui/text/qtextdocumentlayout.cpp b/src/gui/text/qtextdocumentlayout.cpp
index c26fd08c41..9863955a55 100644
--- a/src/gui/text/qtextdocumentlayout.cpp
+++ b/src/gui/text/qtextdocumentlayout.cpp
@@ -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;
diff --git a/src/opengl/gl2paintengineex/qglengineshadermanager.cpp b/src/opengl/gl2paintengineex/qglengineshadermanager.cpp
index b239356c4a..fff64506ab 100644
--- a/src/opengl/gl2paintengineex/qglengineshadermanager.cpp
+++ b/src/opengl/gl2paintengineex/qglengineshadermanager.cpp
@@ -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());
}
diff --git a/src/plugins/platforms/ios/qiosmenu.mm b/src/plugins/platforms/ios/qiosmenu.mm
index 84707319d7..db1b5b8cc8 100644
--- a/src/plugins/platforms/ios/qiosmenu.mm
+++ b/src/plugins/platforms/ios/qiosmenu.mm
@@ -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;
}
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index 6c38c53a77..9bb4497811 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -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;
}
diff --git a/src/widgets/styles/qwindowsstyle.cpp b/src/widgets/styles/qwindowsstyle.cpp
index 23f886e5b1..f6323bb41c 100644
--- a/src/widgets/styles/qwindowsstyle.cpp
+++ b/src/widgets/styles/qwindowsstyle.cpp
@@ -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;
diff --git a/src/widgets/widgets/qabstractbutton.cpp b/src/widgets/widgets/qabstractbutton.cpp
index 44123d4dda..1a5b8db33c 100644
--- a/src/widgets/widgets/qabstractbutton.cpp
+++ b/src/widgets/widgets/qabstractbutton.cpp
@@ -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;
}