Use (new) erase()/erase_if() algorithms

Change-Id: I45c18fd45c20b226e44d16315e3ebb6c305d4ab0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2017-05-18 14:49:04 +02:00 committed by Giuseppe D'Angelo
parent f19266bd02
commit 105a66e654
20 changed files with 28 additions and 84 deletions

View File

@ -113,6 +113,7 @@ qt_internal_add_tool(${target_name}
../src/corelib/time/qgregoriancalendar.cpp ../src/corelib/time/qgregoriancalendar_p.h # special case ../src/corelib/time/qgregoriancalendar.cpp ../src/corelib/time/qgregoriancalendar_p.h # special case
../src/corelib/time/qromancalendar.cpp ../src/corelib/time/qromancalendar_p.h # special case ../src/corelib/time/qromancalendar.cpp ../src/corelib/time/qromancalendar_p.h # special case
../src/corelib/time/qdatetime.cpp ../src/corelib/time/qdatetime.h ../src/corelib/time/qdatetime_p.h # special case ../src/corelib/time/qdatetime.cpp ../src/corelib/time/qdatetime.h ../src/corelib/time/qdatetime_p.h # special case
../src/corelib/tools/qduplicatetracker_p.h
../src/corelib/tools/qhash.cpp ../src/corelib/tools/qhash.h ../src/corelib/tools/qhash.cpp ../src/corelib/tools/qhash.h
../src/corelib/tools/qlist.h ../src/corelib/tools/qlist.h
../src/corelib/tools/qmap.h ../src/corelib/tools/qmap.h

View File

@ -940,9 +940,7 @@ MakefileGenerator::filterIncludedFiles(const char *var)
auto isIncluded = [this](const ProString &input) { auto isIncluded = [this](const ProString &input) {
return QMakeSourceFileInfo::included(input.toQString()) > 0; return QMakeSourceFileInfo::included(input.toQString()) > 0;
}; };
inputs.erase(std::remove_if(inputs.begin(), inputs.end(), inputs.removeIf(isIncluded);
isIncluded),
inputs.end());
} }
static QString static QString

View File

@ -32,6 +32,7 @@
#include <qset.h> #include <qset.h>
#include <qstringlist.h> #include <qstringlist.h>
#include <qtextstream.h> #include <qtextstream.h>
#include <private/qduplicatetracker_p.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -403,21 +404,9 @@ void ProStringList::removeEmpty()
void ProStringList::removeDuplicates() void ProStringList::removeDuplicates()
{ {
int n = size(); QDuplicateTracker<ProString> seen;
int j = 0; seen.reserve(size());
QSet<ProString> seen; removeIf([&](const ProString &s) { return seen.hasSeen(s); });
seen.reserve(n);
for (int i = 0; i < n; ++i) {
const ProString &s = at(i);
if (seen.contains(s))
continue;
seen.insert(s);
if (j != i)
(*this)[j] = s;
++j;
}
if (n != j)
erase(begin() + j, end());
} }
void ProStringList::insertUnique(const ProStringList &value) void ProStringList::insertUnique(const ProStringList &value)

View File

@ -187,6 +187,7 @@ HEADERS += \
qcryptographichash.h \ qcryptographichash.h \
qdatetime.h \ qdatetime.h \
qdatetime_p.h \ qdatetime_p.h \
qduplicatetracker_p.h \
qdir.h \ qdir.h \
qdir_p.h \ qdir_p.h \
qdiriterator.h \ qdiriterator.h \

View File

@ -764,8 +764,7 @@ void QUrlQuery::removeAllQueryItems(const QString &key)
auto firstEqualsEncodedKey = [&encodedKey](const QPair<QString, QString> &item) { auto firstEqualsEncodedKey = [&encodedKey](const QPair<QString, QString> &item) {
return item.first == encodedKey; return item.first == encodedKey;
}; };
const auto end = p->itemList.end(); p->itemList.removeIf(firstEqualsEncodedKey);
p->itemList.erase(std::remove_if(p->itemList.begin(), end, firstEqualsEncodedKey), end);
} }
} }

View File

@ -1252,9 +1252,7 @@ void QItemSelectionModel::select(const QItemSelection &selection, QItemSelection
// be too late if another model observer is connected to the same modelReset slot and is invoked first // be too late if another model observer is connected to the same modelReset slot and is invoked first
// it might call select() on this selection model before any such QItemSelectionModelPrivate::_q_modelReset() slot // it might call select() on this selection model before any such QItemSelectionModelPrivate::_q_modelReset() slot
// is invoked, so it would not be cleared yet. We clear it invalid ranges in it here. // is invoked, so it would not be cleared yet. We clear it invalid ranges in it here.
using namespace QtFunctionObjects; d->ranges.removeIf(QtFunctionObjects::IsNotValid());
d->ranges.erase(std::remove_if(d->ranges.begin(), d->ranges.end(), IsNotValid()),
d->ranges.end());
QItemSelection old = d->ranges; QItemSelection old = d->ranges;
old.merge(d->currentSelection, d->currentCommand); old.merge(d->currentSelection, d->currentCommand);
@ -1755,10 +1753,7 @@ const QItemSelection QItemSelectionModel::selection() const
selected.merge(d->currentSelection, d->currentCommand); selected.merge(d->currentSelection, d->currentCommand);
// 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
using namespace QtFunctionObjects; selected.removeIf(QtFunctionObjects::IsNotValid());
selected.erase(std::remove_if(selected.begin(), selected.end(),
IsNotValid()),
selected.end());
return selected; return selected;
} }

View File

@ -128,7 +128,7 @@ public:
auto isMimeTypeEqual = [&mimeType](const QMimeGlobPattern &pattern) { auto isMimeTypeEqual = [&mimeType](const QMimeGlobPattern &pattern) {
return pattern.mimeType() == mimeType; return pattern.mimeType() == mimeType;
}; };
erase(std::remove_if(begin(), end(), isMimeTypeEqual), end()); removeIf(isMimeTypeEqual);
} }
void match(QMimeGlobMatchResult &result, const QString &fileName) const; void match(QMimeGlobMatchResult &result, const QString &fileName) const;

View File

@ -648,22 +648,9 @@ qsizetype QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QReg
*/ */
qsizetype QtPrivate::QStringList_removeDuplicates(QStringList *that) qsizetype QtPrivate::QStringList_removeDuplicates(QStringList *that)
{ {
qsizetype n = that->size();
qsizetype j = 0;
QDuplicateTracker<QString> seen; QDuplicateTracker<QString> seen;
seen.reserve(n); seen.reserve(that->size());
for (qsizetype i = 0; i < n; ++i) { return that->removeIf([&](const QString &s) { return seen.hasSeen(s); });
const QString &s = that->at(i);
if (seen.hasSeen(s))
continue;
if (j != i)
that->swapItemsAt(i, j);
++j;
}
if (n != j)
that->erase(that->begin() + j, that->end());
return n - j;
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -286,8 +286,7 @@ QStringList QCommandLineOptionPrivate::removeInvalidNames(QStringList nameList)
if (Q_UNLIKELY(nameList.isEmpty())) if (Q_UNLIKELY(nameList.isEmpty()))
qWarning("QCommandLineOption: Options must have at least one name"); qWarning("QCommandLineOption: Options must have at least one name");
else else
nameList.erase(std::remove_if(nameList.begin(), nameList.end(), IsInvalidName()), nameList.removeIf(IsInvalidName());
nameList.end());
return nameList; return nameList;
} }

View File

@ -593,11 +593,8 @@ static void huntAndDestroy(QObject *needle, QDBusConnectionPrivate::ObjectTreeNo
for (auto &node : haystack.children) for (auto &node : haystack.children)
huntAndDestroy(needle, node); huntAndDestroy(needle, node);
auto isInactive = [](QDBusConnectionPrivate::ObjectTreeNode &node) { return !node.isActive(); }; auto isInactive = [](const QDBusConnectionPrivate::ObjectTreeNode &node) { return !node.isActive(); };
haystack.children.removeIf(isInactive);
haystack.children.erase(std::remove_if(haystack.children.begin(), haystack.children.end(),
isInactive),
haystack.children.end());
if (needle == haystack.obj) { if (needle == haystack.obj) {
haystack.obj = nullptr; haystack.obj = nullptr;

View File

@ -1022,7 +1022,7 @@ QList<QByteArray> QMovie::supportedFormats()
return !QImageReader(&buffer, format).supportsOption(QImageIOHandler::Animation); return !QImageReader(&buffer, format).supportsOption(QImageIOHandler::Animation);
}; };
list.erase(std::remove_if(list.begin(), list.end(), doesntSupportAnimation), list.end()); list.removeIf(doesntSupportAnimation);
return list; return list;
} }

View File

@ -109,12 +109,8 @@ void QSyntaxHighlighterPrivate::applyFormatChanges()
return range.start < preeditAreaStart return range.start < preeditAreaStart
|| range.start + range.length > preeditAreaStart + preeditAreaLength; || range.start + range.length > preeditAreaStart + preeditAreaLength;
}; };
const auto it = std::remove_if(ranges.begin(), ranges.end(), if (ranges.removeIf(isOutsidePreeditArea) > 0)
isOutsidePreeditArea);
if (it != ranges.end()) {
ranges.erase(it, ranges.end());
formatsChanged = true; formatsChanged = true;
}
} else if (!ranges.isEmpty()) { } else if (!ranges.isEmpty()) {
ranges.clear(); ranges.clear();
formatsChanged = true; formatsChanged = true;

View File

@ -3756,11 +3756,7 @@ static void markFrames(QTextFrame *current, int from, int oldLength, int length)
QTextFrameData *fd = data(current); QTextFrameData *fd = data(current);
// float got removed in editing operation // float got removed in editing operation
QTextFrame *null = nullptr; // work-around for (at least) MSVC 2012 emitting fd->floats.removeAll(nullptr);
// 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->layoutDirty = true;
fd->sizeDirty = true; fd->sizeDirty = true;

View File

@ -106,9 +106,7 @@ void QHttpNetworkHeaderPrivate::setHeaderField(const QByteArray &name, const QBy
auto firstEqualsName = [&name](const QPair<QByteArray, QByteArray> &header) { auto firstEqualsName = [&name](const QPair<QByteArray, QByteArray> &header) {
return name.compare(header.first, Qt::CaseInsensitive) == 0; return name.compare(header.first, Qt::CaseInsensitive) == 0;
}; };
fields.erase(std::remove_if(fields.begin(), fields.end(), fields.removeIf(firstEqualsName);
firstEqualsName),
fields.end());
fields.append(qMakePair(name, data)); fields.append(qMakePair(name, data));
} }

View File

@ -1324,9 +1324,7 @@ void QNetworkHeadersPrivate::setRawHeaderInternal(const QByteArray &key, const Q
auto firstEqualsKey = [&key](const RawHeaderPair &header) { auto firstEqualsKey = [&key](const RawHeaderPair &header) {
return header.first.compare(key, Qt::CaseInsensitive) == 0; return header.first.compare(key, Qt::CaseInsensitive) == 0;
}; };
rawHeaders.erase(std::remove_if(rawHeaders.begin(), rawHeaders.end(), rawHeaders.removeIf(firstEqualsKey);
firstEqualsKey),
rawHeaders.end());
if (value.isNull()) if (value.isNull())
return; // only wanted to erase key return; // only wanted to erase key

View File

@ -493,9 +493,7 @@ void QOpenGLEngineSharedShaders::cleanupCustomStage(QOpenGLCustomShaderStage* st
} }
return false; return false;
}; };
cachedPrograms.erase(std::remove_if(cachedPrograms.begin(), cachedPrograms.end(), cachedPrograms.removeIf(hasStageAsCustomShaderSouce);
hasStageAsCustomShaderSouce),
cachedPrograms.end());
} }

View File

@ -2642,8 +2642,7 @@ QModelIndexList QAbstractItemView::selectedIndexes() const
auto isHidden = [this](const QModelIndex &idx) { auto isHidden = [this](const QModelIndex &idx) {
return isIndexHidden(idx); return isIndexHidden(idx);
}; };
const auto end = indexes.end(); indexes.removeIf(isHidden);
indexes.erase(std::remove_if(indexes.begin(), end, isHidden), end);
} }
return indexes; return indexes;
} }
@ -4489,9 +4488,7 @@ QModelIndexList QAbstractItemViewPrivate::selectedDraggableIndexes() const
auto isNotDragEnabled = [this](const QModelIndex &index) { auto isNotDragEnabled = [this](const QModelIndex &index) {
return !isIndexDragEnabled(index); return !isIndexDragEnabled(index);
}; };
indexes.erase(std::remove_if(indexes.begin(), indexes.end(), indexes.removeIf(isNotDragEnabled);
isNotDragEnabled),
indexes.end());
return indexes; return indexes;
} }

View File

@ -1522,8 +1522,7 @@ QModelIndexList QListView::selectedIndexes() const
auto ignorable = [this, d](const QModelIndex &index) { auto ignorable = [this, d](const QModelIndex &index) {
return index.column() != d->column || index.parent() != d->root || isIndexHidden(index); return index.column() != d->column || index.parent() != d->root || isIndexHidden(index);
}; };
viewSelected.erase(std::remove_if(viewSelected.begin(), viewSelected.end(), ignorable), viewSelected.removeIf(ignorable);
viewSelected.end());
return viewSelected; return viewSelected;
} }
@ -1960,9 +1959,7 @@ void QListViewPrivate::removeCurrentAndDisabled(QList<QModelIndex> *indexes,
auto isCurrentOrDisabled = [this, current](const QModelIndex &index) { auto isCurrentOrDisabled = [this, current](const QModelIndex &index) {
return !isIndexEnabled(index) || index == current; return !isIndexEnabled(index) || index == current;
}; };
indexes->erase(std::remove_if(indexes->begin(), indexes->end(), indexes->removeIf(isCurrentOrDisabled);
isCurrentOrDisabled),
indexes->end());
} }
/* /*

View File

@ -173,7 +173,7 @@ bool QWindowsStyle::eventFilter(QObject *o, QEvent *e)
return w->isWindow() || !w->isVisible() return w->isWindow() || !w->isVisible()
|| w->style()->styleHint(SH_UnderlineShortcut, nullptr, w); || w->style()->styleHint(SH_UnderlineShortcut, nullptr, w);
}; };
l.erase(std::remove_if(l.begin(), l.end(), ignorable), l.end()); l.removeIf(ignorable);
// 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

@ -199,9 +199,7 @@ QList<QAbstractButton *>QAbstractButtonPrivate::queryButtonList() const
#endif #endif
; ;
}; };
candidates.erase(std::remove_if(candidates.begin(), candidates.end(), candidates.removeIf(isNoMemberOfMyAutoExclusiveGroup);
isNoMemberOfMyAutoExclusiveGroup),
candidates.end());
} }
return candidates; return candidates;
} }