QtBase: avoid uses of Java-style iterators [QHash, QMap]

Java-style iterators are slower than STL-style ones, so
they should not be used in library code.

Replaced them with C++11 range-for, STL iterators or, in
one case, qDeleteAll().

In one case, avoid a double hash lookup by using erase(it)
instead of remove(it.key()), which we can now do without
detaching, due to the new erase() taking const_iterator.

Change-Id: I96174657fed70f76120b2c9d8190b4e70d5d8179
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2016-01-28 16:48:36 +01:00
parent 1d3503b8f3
commit fdfd63053a
3 changed files with 6 additions and 20 deletions

View File

@ -685,9 +685,7 @@ void VcprojGenerator::writeSubDirs(QTextStream &t)
t << _slnGlobalBeg;
QHashIterator<VcsolutionDepend *, QStringList> extraIt(extraSubdirs);
while (extraIt.hasNext()) {
extraIt.next();
for (auto extraIt = extraSubdirs.cbegin(), end = extraSubdirs.cend(); extraIt != end; ++extraIt) {
for (const QString &depend : extraIt.value()) {
if (!projGuids[depend].isEmpty()) {
extraIt.key()->dependencies << projGuids[depend];

View File

@ -114,11 +114,7 @@ QNetworkDiskCache::QNetworkDiskCache(QObject *parent)
QNetworkDiskCache::~QNetworkDiskCache()
{
Q_D(QNetworkDiskCache);
QHashIterator<QIODevice*, QCacheItem*> it(d->inserting);
while (it.hasNext()) {
it.next();
delete it.value();
}
qDeleteAll(d->inserting);
}
/*!
@ -319,13 +315,11 @@ bool QNetworkDiskCache::remove(const QUrl &url)
Q_D(QNetworkDiskCache);
// remove is also used to cancel insertions, not a common operation
QHashIterator<QIODevice*, QCacheItem*> it(d->inserting);
while (it.hasNext()) {
it.next();
for (auto it = d->inserting.cbegin(), end = d->inserting.cend(); it != end; ++it) {
QCacheItem *item = it.value();
if (item && item->metaData.url() == url) {
delete item;
d->inserting.remove(it.key());
d->inserting.erase(it);
return true;
}
}
@ -559,10 +553,7 @@ qint64 QNetworkDiskCache::expire()
QFile file(name);
if (name.contains(PREPARED_SLASH)) {
QHashIterator<QIODevice*, QCacheItem*> iterator(d->inserting);
while (iterator.hasNext()) {
iterator.next();
QCacheItem *item = iterator.value();
for (QCacheItem *item : qAsConst(d->inserting)) {
if (item && item->file && item->file->fileName() == name) {
delete item->file;
item->file = 0;

View File

@ -793,11 +793,8 @@ void QColumnView::initializeColumn(QAbstractItemView *column) const
column->setModel(model());
// Copy the custom delegate per row
QMapIterator<int, QPointer<QAbstractItemDelegate> > i(d->rowDelegates);
while (i.hasNext()) {
i.next();
for (auto i = d->rowDelegates.cbegin(), end = d->rowDelegates.cend(); i != end; ++i)
column->setItemDelegateForRow(i.key(), i.value());
}
// set the delegate to be the columnview delegate
QAbstractItemDelegate *delegate = column->itemDelegate();