Skip to content

Commit c62a6e7

Browse files
committed
Optimise some loops over QMap/QHash objects
Avoid iterating over keys() and then later retrieving the matching values, and instead use QMap/QHash iterators to retrieve both the keys and values at the same time
1 parent 62f272e commit c62a6e7

20 files changed

+103
-85
lines changed

src/core/composer/qgscomposertablev2.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool i
137137

138138
//cell styles
139139
QDomElement stylesElem = doc.createElement( "cellStyles" );
140-
Q_FOREACH ( CellStyleGroup group, mCellStyleNames.keys() )
140+
QMap< CellStyleGroup, QString >::const_iterator it = mCellStyleNames.constBegin();
141+
for ( ; it != mCellStyleNames.constEnd(); ++it )
141142
{
142-
QString styleName = mCellStyleNames.value( group );
143+
QString styleName = it.value();
143144
QDomElement styleElem = doc.createElement( styleName );
144-
mCellStyles.value( group )->writeXML( styleElem, doc );
145+
mCellStyles.value( it.key() )->writeXML( styleElem, doc );
145146
stylesElem.appendChild( styleElem );
146147
}
147148
elem.appendChild( stylesElem );
@@ -209,14 +210,16 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
209210
if ( !stylesList.isEmpty() )
210211
{
211212
QDomElement stylesElem = stylesList.at( 0 ).toElement();
212-
Q_FOREACH ( CellStyleGroup group, mCellStyleNames.keys() )
213+
214+
QMap< CellStyleGroup, QString >::const_iterator it = mCellStyleNames.constBegin();
215+
for ( ; it != mCellStyleNames.constEnd(); ++it )
213216
{
214-
QString styleName = mCellStyleNames.value( group );
217+
QString styleName = it.value();
215218
QDomNodeList styleList = stylesElem.elementsByTagName( styleName );
216219
if ( !styleList.isEmpty() )
217220
{
218221
QDomElement styleElem = styleList.at( 0 ).toElement();
219-
mCellStyles.value( group )->readXML( styleElem );
222+
mCellStyles.value( it.key() )->readXML( styleElem );
220223
}
221224
}
222225
}

src/core/effects/qgspainteffectregistry.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ QgsPaintEffectRegistry::QgsPaintEffectRegistry()
5353

5454
QgsPaintEffectRegistry::~QgsPaintEffectRegistry()
5555
{
56-
Q_FOREACH ( const QString& name, mMetadata.keys() )
57-
{
58-
delete mMetadata[name];
59-
}
60-
mMetadata.clear();
56+
qDeleteAll( mMetadata );
6157
}
6258

6359
QgsPaintEffectRegistry* QgsPaintEffectRegistry::instance()

src/core/geometry/qgswkbtypes.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>* QgsWKBTypes::entries()
3232
QgsWKBTypes::Type QgsWKBTypes::parseType( const QString &wktStr )
3333
{
3434
QString typestr = wktStr.left( wktStr.indexOf( '(' ) ).simplified().remove( ' ' );
35-
Q_FOREACH ( Type type, entries()->keys() )
35+
36+
QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>* knownTypes = entries();
37+
QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>::const_iterator it = knownTypes->constBegin();
38+
for ( ; it != knownTypes->constEnd(); ++it )
3639
{
37-
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
38-
if ( it != entries()->constEnd() && it.value().mName.compare( typestr, Qt::CaseInsensitive ) == 0 )
40+
if ( it.value().mName.compare( typestr, Qt::CaseInsensitive ) == 0 )
3941
{
40-
return type;
42+
return it.key();
4143
}
4244
}
4345
return Unknown;

src/core/layertree/qgslayertreemodel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,10 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::layerOriginalLegendNodes(
13451345

13461346
QgsLayerTreeModelLegendNode* QgsLayerTreeModel::findLegendNode( const QString& layerId, const QString& ruleKey ) const
13471347
{
1348-
Q_FOREACH ( QgsLayerTreeLayer* layer, mLegend.keys() )
1348+
QMap<QgsLayerTreeLayer*, LayerLegendData>::const_iterator it = mLegend.constBegin();
1349+
for ( ; it != mLegend.constEnd(); ++it )
13491350
{
1351+
QgsLayerTreeLayer* layer = it.key();
13501352
if ( layer->layerId() == layerId )
13511353
{
13521354
Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, mLegend.value( layer ).activeNodes )

src/core/pal/pal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ Problem* Pal::extract( double lambda_min, double phi_min, double lambda_max, dou
271271
QStringList layersWithFeaturesInBBox;
272272

273273
mMutex.lock();
274-
Q_FOREACH ( Layer* layer, mLayers.values() )
274+
Q_FOREACH ( Layer* layer, mLayers )
275275
{
276276
if ( !layer )
277277
{

src/core/qgsconditionalstyle.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ bool QgsConditionalLayerStyles::writeXml( QDomNode &node, QDomDocument &doc ) co
6060
stylesel.appendChild( rowel );
6161

6262
QDomElement fieldsel = doc.createElement( "fieldstyles" );
63-
Q_FOREACH ( const QString field, mFieldStyles.keys() )
63+
QHash<QString, QgsConditionalStyles>::const_iterator it = mFieldStyles.constBegin();
64+
for ( ; it != mFieldStyles.constEnd(); ++it )
6465
{
6566
QDomElement fieldel = doc.createElement( "fieldstyle" );
66-
fieldel.setAttribute( "fieldname", field );
67-
QgsConditionalStyles styles = mFieldStyles[field];
67+
fieldel.setAttribute( "fieldname", it.key() );
68+
QgsConditionalStyles styles = it.value();
6869
Q_FOREACH ( const QgsConditionalStyle& style, styles )
6970
{
7071
style.writeXml( fieldel, doc );

src/core/qgscoordinatereferencesystem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,9 +1838,10 @@ int QgsCoordinateReferenceSystem::syncDb()
18381838

18391839
sql = "DELETE FROM tbl_srs WHERE auth_name='EPSG' AND NOT auth_id IN (";
18401840
QString delim;
1841-
Q_FOREACH ( int i, wkts.keys() )
1841+
QHash<int, QString>::const_iterator it = wkts.constBegin();
1842+
for ( ; it != wkts.constEnd(); ++it )
18421843
{
1843-
sql += delim + QString::number( i );
1844+
sql += delim + QString::number( it.key() );
18441845
delim = ',';
18451846
}
18461847
sql += ") AND NOT noupdate";

src/core/qgsexpression.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ QgsExpression::Interval QgsExpression::Interval::fromString( const QString& stri
9696
}
9797

9898
bool matched = false;
99-
Q_FOREACH ( int duration, map.keys() )
99+
QMap<int, QStringList>::const_iterator it = map.constBegin();
100+
for ( ; it != map.constEnd(); ++it )
100101
{
101-
Q_FOREACH ( const QString& name, map[duration] )
102+
int duration = it.key();
103+
Q_FOREACH ( const QString& name, it.value() )
102104
{
103105
if ( match.contains( name, Qt::CaseInsensitive ) )
104106
{

src/core/qgsexpressioncontext.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ QgsExpressionContextScope::QgsExpressionContextScope( const QgsExpressionContext
5252
: mName( other.mName )
5353
, mVariables( other.mVariables )
5454
{
55-
Q_FOREACH ( const QString& key, other.mFunctions.keys() )
55+
QHash<QString, QgsScopedExpressionFunction* >::const_iterator it = other.mFunctions.constBegin();
56+
for ( ; it != other.mFunctions.constEnd(); ++it )
5657
{
57-
mFunctions.insert( key, other.mFunctions.value( key )->clone() );
58+
mFunctions.insert( it.key(), it.value()->clone() );
5859
}
5960
}
6061

@@ -65,9 +66,10 @@ QgsExpressionContextScope& QgsExpressionContextScope::operator=( const QgsExpres
6566

6667
qDeleteAll( mFunctions );
6768
mFunctions.clear();
68-
Q_FOREACH ( const QString& key, other.mFunctions.keys() )
69+
QHash<QString, QgsScopedExpressionFunction* >::const_iterator it = other.mFunctions.constBegin();
70+
for ( ; it != other.mFunctions.constEnd(); ++it )
6971
{
70-
mFunctions.insert( key, other.mFunctions.value( key )->clone() );
72+
mFunctions.insert( it.key(), it.value()->clone() );
7173
}
7274

7375
return *this;
@@ -503,10 +505,11 @@ void QgsExpressionContextUtils::setGlobalVariables( const QgsStringMap &variable
503505
QList< QVariant > customVariableVariants;
504506
QList< QVariant > customVariableNames;
505507

506-
Q_FOREACH ( const QString& variable, variables.keys() )
508+
QMap< QString, QString >::const_iterator it = variables.constBegin();
509+
for ( ; it != variables.constEnd(); ++it )
507510
{
508-
customVariableNames << variable;
509-
customVariableVariants << variables.value( variable );
511+
customVariableNames << it.key();
512+
customVariableVariants << it.value();
510513
}
511514

512515
settings.setValue( QString( "/variables/names" ), customVariableNames );
@@ -622,10 +625,11 @@ void QgsExpressionContextUtils::setProjectVariables( const QgsStringMap &variabl
622625
QStringList variableNames;
623626
QStringList variableValues;
624627

625-
Q_FOREACH ( const QString& variable, variables.keys() )
628+
QMap< QString, QString >::const_iterator it = variables.constBegin();
629+
for ( ; it != variables.constEnd(); ++it )
626630
{
627-
variableNames << variable;
628-
variableValues << variables.value( variable );
631+
variableNames << it.key();
632+
variableValues << it.value();
629633
}
630634

631635
project->writeEntry( "Variables", "/variableNames", variableNames );
@@ -696,10 +700,11 @@ void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer* layer, const Qgs
696700
QStringList variableNames;
697701
QStringList variableValues;
698702

699-
Q_FOREACH ( const QString& variable, variables.keys() )
703+
QMap< QString, QString >::const_iterator it = variables.constBegin();
704+
for ( ; it != variables.constEnd(); ++it )
700705
{
701-
variableNames << variable;
702-
variableValues << variables.value( variable );
706+
variableNames << it.key();
707+
variableValues << it.value();
703708
}
704709

705710
layer->setCustomProperty( "variableNames", variableNames );
@@ -797,10 +802,11 @@ void QgsExpressionContextUtils::setCompositionVariables( QgsComposition* composi
797802
QStringList variableNames;
798803
QStringList variableValues;
799804

800-
Q_FOREACH ( const QString& variable, variables.keys() )
805+
QMap< QString, QString >::const_iterator it = variables.constBegin();
806+
for ( ; it != variables.constEnd(); ++it )
801807
{
802-
variableNames << variable;
803-
variableValues << variables.value( variable );
808+
variableNames << it.key();
809+
variableValues << it.value();
804810
}
805811

806812
composition->setCustomProperty( "variableNames", variableNames );
@@ -899,10 +905,11 @@ void QgsExpressionContextUtils::setComposerItemVariables( QgsComposerItem* compo
899905
QStringList variableNames;
900906
QStringList variableValues;
901907

902-
Q_FOREACH ( const QString& variable, variables.keys() )
908+
QMap< QString, QString >::const_iterator it = variables.constBegin();
909+
for ( ; it != variables.constEnd(); ++it )
903910
{
904-
variableNames << variable;
905-
variableValues << variables.value( variable );
911+
variableNames << it.key();
912+
variableValues << it.value();
906913
}
907914

908915
composerItem->setCustomProperty( "variableNames", variableNames );

src/core/qgslayerdefinition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ void QgsLayerDefinition::DependencySorter::init( const QDomDocument& doc )
237237
}
238238

239239
// check that all dependencies are present
240-
Q_FOREACH ( const QString& id, dependencies.keys() )
240+
Q_FOREACH ( const QVector< QString >& ids, dependencies )
241241
{
242-
Q_FOREACH ( const QString& depId, dependencies[id] )
242+
Q_FOREACH ( const QString& depId, ids )
243243
{
244244
if ( !dependencies.contains( depId ) )
245245
{

src/core/qgsmaprenderercache.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ void QgsMapRendererCache::clearInternal()
3535
mScale = 0;
3636

3737
// make sure we are disconnected from all layers
38-
Q_FOREACH ( const QString& layerId, mCachedImages.keys() )
38+
QMap<QString, QImage>::const_iterator it = mCachedImages.constBegin();
39+
for ( ; it != mCachedImages.constEnd(); ++it )
3940
{
40-
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
41+
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( it.key() );
4142
if ( layer )
4243
{
4344
disconnect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );

src/core/qgsmaprenderercustompainterjob.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,11 @@ void QgsMapRendererJob::drawNewLabeling( const QgsMapSettings& settings, QgsRend
357357

358358
void QgsMapRendererJob::updateLayerGeometryCaches()
359359
{
360-
Q_FOREACH ( const QString& id, mGeometryCaches.keys() )
360+
QMap<QString, QgsGeometryCache>::const_iterator it = mGeometryCaches.constBegin();
361+
for ( ; it != mGeometryCaches.constEnd(); ++it )
361362
{
362-
const QgsGeometryCache& cache = mGeometryCaches[id];
363-
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( id ) ) )
363+
const QgsGeometryCache& cache = it.value();
364+
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( it.key() ) ) )
364365
* vl->cache() = cache;
365366
}
366367
mGeometryCaches.clear();

src/core/qgspluginlayerregistry.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ QgsPluginLayerRegistry::~QgsPluginLayerRegistry()
7575
if ( !mPluginLayerTypes.isEmpty() )
7676
{
7777
QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
78-
Q_FOREACH ( const QString& typeName, mPluginLayerTypes.keys() )
79-
removePluginLayerType( typeName );
78+
PluginLayerTypes::const_iterator it = mPluginLayerTypes.constBegin();
79+
for ( ; it != mPluginLayerTypes.constEnd(); ++it )
80+
{
81+
removePluginLayerType( it.key() );
82+
}
8083
}
8184
}
8285

src/core/qgsrulebasedlabeling.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ QgsRuleBasedLabelProvider::~QgsRuleBasedLabelProvider()
3131

3232
bool QgsRuleBasedLabelProvider::prepare( const QgsRenderContext& context, QStringList& attributeNames )
3333
{
34-
Q_FOREACH ( QgsVectorLayerLabelProvider* provider, mSubProviders.values() )
34+
Q_FOREACH ( QgsVectorLayerLabelProvider* provider, mSubProviders )
3535
provider->setEngine( mEngine );
3636

3737
// populate sub-providers
@@ -48,7 +48,7 @@ void QgsRuleBasedLabelProvider::registerFeature( QgsFeature& feature, QgsRenderC
4848
QList<QgsAbstractLabelProvider*> QgsRuleBasedLabelProvider::subProviders()
4949
{
5050
QList<QgsAbstractLabelProvider*> lst;
51-
Q_FOREACH ( QgsVectorLayerLabelProvider* subprovider, mSubProviders.values() )
51+
Q_FOREACH ( QgsVectorLayerLabelProvider* subprovider, mSubProviders )
5252
lst << subprovider;
5353
return lst;
5454
}

src/core/qgsvectorlayereditbuffer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,10 @@ QString QgsVectorLayerEditBuffer::dumpEditBuffer()
607607
void QgsVectorLayerEditBuffer::handleAttributeAdded( int index )
608608
{
609609
// go through the changed attributes map and adapt indices
610-
Q_FOREACH ( QgsFeatureId fid, mChangedAttributeValues.keys() )
610+
QgsChangedAttributesMap::iterator it = mChangedAttributeValues.begin();
611+
for ( ; it != mChangedAttributeValues.end(); ++it )
611612
{
612-
updateAttributeMapIndex( mChangedAttributeValues[fid], index, + 1 );
613+
updateAttributeMapIndex( it.value(), index, + 1 );
613614
}
614615

615616
// go through added features and adapt attributes
@@ -625,9 +626,10 @@ void QgsVectorLayerEditBuffer::handleAttributeAdded( int index )
625626
void QgsVectorLayerEditBuffer::handleAttributeDeleted( int index )
626627
{
627628
// go through the changed attributes map and adapt indices
628-
Q_FOREACH ( QgsFeatureId fid, mChangedAttributeValues.keys() )
629+
QgsChangedAttributesMap::iterator it = mChangedAttributeValues.begin();
630+
for ( ; it != mChangedAttributeValues.end(); ++it )
629631
{
630-
QgsAttributeMap& attrMap = mChangedAttributeValues[fid];
632+
QgsAttributeMap& attrMap = it.value();
631633
// remove the attribute
632634
if ( attrMap.contains( index ) )
633635
attrMap.remove( index );

src/core/qgsvectorlayerimport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
8787
return;
8888
}
8989

90-
Q_FOREACH ( int idx, mOldToNewAttrIdx.values() )
90+
Q_FOREACH ( int idx, mOldToNewAttrIdx )
9191
{
9292
if ( idx > mAttributeCount )
9393
mAttributeCount = idx;

0 commit comments

Comments
 (0)