Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nyalldawson committed Feb 13, 2016
1 parent 62f272e commit c62a6e7
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 85 deletions.
15 changes: 9 additions & 6 deletions src/core/composer/qgscomposertablev2.cpp
Expand Up @@ -137,11 +137,12 @@ bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool i

//cell styles
QDomElement stylesElem = doc.createElement( "cellStyles" );
Q_FOREACH ( CellStyleGroup group, mCellStyleNames.keys() )
QMap< CellStyleGroup, QString >::const_iterator it = mCellStyleNames.constBegin();
for ( ; it != mCellStyleNames.constEnd(); ++it )
{
QString styleName = mCellStyleNames.value( group );
QString styleName = it.value();
QDomElement styleElem = doc.createElement( styleName );
mCellStyles.value( group )->writeXML( styleElem, doc );
mCellStyles.value( it.key() )->writeXML( styleElem, doc );
stylesElem.appendChild( styleElem );
}
elem.appendChild( stylesElem );
Expand Down Expand Up @@ -209,14 +210,16 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
if ( !stylesList.isEmpty() )
{
QDomElement stylesElem = stylesList.at( 0 ).toElement();
Q_FOREACH ( CellStyleGroup group, mCellStyleNames.keys() )

QMap< CellStyleGroup, QString >::const_iterator it = mCellStyleNames.constBegin();
for ( ; it != mCellStyleNames.constEnd(); ++it )
{
QString styleName = mCellStyleNames.value( group );
QString styleName = it.value();
QDomNodeList styleList = stylesElem.elementsByTagName( styleName );
if ( !styleList.isEmpty() )
{
QDomElement styleElem = styleList.at( 0 ).toElement();
mCellStyles.value( group )->readXML( styleElem );
mCellStyles.value( it.key() )->readXML( styleElem );
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/core/effects/qgspainteffectregistry.cpp
Expand Up @@ -53,11 +53,7 @@ QgsPaintEffectRegistry::QgsPaintEffectRegistry()

QgsPaintEffectRegistry::~QgsPaintEffectRegistry()
{
Q_FOREACH ( const QString& name, mMetadata.keys() )
{
delete mMetadata[name];
}
mMetadata.clear();
qDeleteAll( mMetadata );
}

QgsPaintEffectRegistry* QgsPaintEffectRegistry::instance()
Expand Down
10 changes: 6 additions & 4 deletions src/core/geometry/qgswkbtypes.cpp
Expand Up @@ -32,12 +32,14 @@ QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>* QgsWKBTypes::entries()
QgsWKBTypes::Type QgsWKBTypes::parseType( const QString &wktStr )
{
QString typestr = wktStr.left( wktStr.indexOf( '(' ) ).simplified().remove( ' ' );
Q_FOREACH ( Type type, entries()->keys() )

QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>* knownTypes = entries();
QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry>::const_iterator it = knownTypes->constBegin();
for ( ; it != knownTypes->constEnd(); ++it )
{
QMap< Type, wkbEntry >::const_iterator it = entries()->constFind( type );
if ( it != entries()->constEnd() && it.value().mName.compare( typestr, Qt::CaseInsensitive ) == 0 )
if ( it.value().mName.compare( typestr, Qt::CaseInsensitive ) == 0 )
{
return type;
return it.key();
}
}
return Unknown;
Expand Down
4 changes: 3 additions & 1 deletion src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -1345,8 +1345,10 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::layerOriginalLegendNodes(

QgsLayerTreeModelLegendNode* QgsLayerTreeModel::findLegendNode( const QString& layerId, const QString& ruleKey ) const
{
Q_FOREACH ( QgsLayerTreeLayer* layer, mLegend.keys() )
QMap<QgsLayerTreeLayer*, LayerLegendData>::const_iterator it = mLegend.constBegin();
for ( ; it != mLegend.constEnd(); ++it )
{
QgsLayerTreeLayer* layer = it.key();
if ( layer->layerId() == layerId )
{
Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, mLegend.value( layer ).activeNodes )
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/pal.cpp
Expand Up @@ -271,7 +271,7 @@ Problem* Pal::extract( double lambda_min, double phi_min, double lambda_max, dou
QStringList layersWithFeaturesInBBox;

mMutex.lock();
Q_FOREACH ( Layer* layer, mLayers.values() )
Q_FOREACH ( Layer* layer, mLayers )
{
if ( !layer )
{
Expand Down
7 changes: 4 additions & 3 deletions src/core/qgsconditionalstyle.cpp
Expand Up @@ -60,11 +60,12 @@ bool QgsConditionalLayerStyles::writeXml( QDomNode &node, QDomDocument &doc ) co
stylesel.appendChild( rowel );

QDomElement fieldsel = doc.createElement( "fieldstyles" );
Q_FOREACH ( const QString field, mFieldStyles.keys() )
QHash<QString, QgsConditionalStyles>::const_iterator it = mFieldStyles.constBegin();
for ( ; it != mFieldStyles.constEnd(); ++it )
{
QDomElement fieldel = doc.createElement( "fieldstyle" );
fieldel.setAttribute( "fieldname", field );
QgsConditionalStyles styles = mFieldStyles[field];
fieldel.setAttribute( "fieldname", it.key() );
QgsConditionalStyles styles = it.value();
Q_FOREACH ( const QgsConditionalStyle& style, styles )
{
style.writeXml( fieldel, doc );
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -1838,9 +1838,10 @@ int QgsCoordinateReferenceSystem::syncDb()

sql = "DELETE FROM tbl_srs WHERE auth_name='EPSG' AND NOT auth_id IN (";
QString delim;
Q_FOREACH ( int i, wkts.keys() )
QHash<int, QString>::const_iterator it = wkts.constBegin();
for ( ; it != wkts.constEnd(); ++it )
{
sql += delim + QString::number( i );
sql += delim + QString::number( it.key() );
delim = ',';
}
sql += ") AND NOT noupdate";
Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsexpression.cpp
Expand Up @@ -96,9 +96,11 @@ QgsExpression::Interval QgsExpression::Interval::fromString( const QString& stri
}

bool matched = false;
Q_FOREACH ( int duration, map.keys() )
QMap<int, QStringList>::const_iterator it = map.constBegin();
for ( ; it != map.constEnd(); ++it )
{
Q_FOREACH ( const QString& name, map[duration] )
int duration = it.key();
Q_FOREACH ( const QString& name, it.value() )
{
if ( match.contains( name, Qt::CaseInsensitive ) )
{
Expand Down
45 changes: 26 additions & 19 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -52,9 +52,10 @@ QgsExpressionContextScope::QgsExpressionContextScope( const QgsExpressionContext
: mName( other.mName )
, mVariables( other.mVariables )
{
Q_FOREACH ( const QString& key, other.mFunctions.keys() )
QHash<QString, QgsScopedExpressionFunction* >::const_iterator it = other.mFunctions.constBegin();
for ( ; it != other.mFunctions.constEnd(); ++it )
{
mFunctions.insert( key, other.mFunctions.value( key )->clone() );
mFunctions.insert( it.key(), it.value()->clone() );
}
}

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

qDeleteAll( mFunctions );
mFunctions.clear();
Q_FOREACH ( const QString& key, other.mFunctions.keys() )
QHash<QString, QgsScopedExpressionFunction* >::const_iterator it = other.mFunctions.constBegin();
for ( ; it != other.mFunctions.constEnd(); ++it )
{
mFunctions.insert( key, other.mFunctions.value( key )->clone() );
mFunctions.insert( it.key(), it.value()->clone() );
}

return *this;
Expand Down Expand Up @@ -503,10 +505,11 @@ void QgsExpressionContextUtils::setGlobalVariables( const QgsStringMap &variable
QList< QVariant > customVariableVariants;
QList< QVariant > customVariableNames;

Q_FOREACH ( const QString& variable, variables.keys() )
QMap< QString, QString >::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
customVariableNames << variable;
customVariableVariants << variables.value( variable );
customVariableNames << it.key();
customVariableVariants << it.value();
}

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

Q_FOREACH ( const QString& variable, variables.keys() )
QMap< QString, QString >::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << variable;
variableValues << variables.value( variable );
variableNames << it.key();
variableValues << it.value();
}

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

Q_FOREACH ( const QString& variable, variables.keys() )
QMap< QString, QString >::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << variable;
variableValues << variables.value( variable );
variableNames << it.key();
variableValues << it.value();
}

layer->setCustomProperty( "variableNames", variableNames );
Expand Down Expand Up @@ -797,10 +802,11 @@ void QgsExpressionContextUtils::setCompositionVariables( QgsComposition* composi
QStringList variableNames;
QStringList variableValues;

Q_FOREACH ( const QString& variable, variables.keys() )
QMap< QString, QString >::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << variable;
variableValues << variables.value( variable );
variableNames << it.key();
variableValues << it.value();
}

composition->setCustomProperty( "variableNames", variableNames );
Expand Down Expand Up @@ -899,10 +905,11 @@ void QgsExpressionContextUtils::setComposerItemVariables( QgsComposerItem* compo
QStringList variableNames;
QStringList variableValues;

Q_FOREACH ( const QString& variable, variables.keys() )
QMap< QString, QString >::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << variable;
variableValues << variables.value( variable );
variableNames << it.key();
variableValues << it.value();
}

composerItem->setCustomProperty( "variableNames", variableNames );
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslayerdefinition.cpp
Expand Up @@ -237,9 +237,9 @@ void QgsLayerDefinition::DependencySorter::init( const QDomDocument& doc )
}

// check that all dependencies are present
Q_FOREACH ( const QString& id, dependencies.keys() )
Q_FOREACH ( const QVector< QString >& ids, dependencies )
{
Q_FOREACH ( const QString& depId, dependencies[id] )
Q_FOREACH ( const QString& depId, ids )
{
if ( !dependencies.contains( depId ) )
{
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsmaprenderercache.cpp
Expand Up @@ -35,9 +35,10 @@ void QgsMapRendererCache::clearInternal()
mScale = 0;

// make sure we are disconnected from all layers
Q_FOREACH ( const QString& layerId, mCachedImages.keys() )
QMap<QString, QImage>::const_iterator it = mCachedImages.constBegin();
for ( ; it != mCachedImages.constEnd(); ++it )
{
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( it.key() );
if ( layer )
{
disconnect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
Expand Down
7 changes: 4 additions & 3 deletions src/core/qgsmaprenderercustompainterjob.cpp
Expand Up @@ -357,10 +357,11 @@ void QgsMapRendererJob::drawNewLabeling( const QgsMapSettings& settings, QgsRend

void QgsMapRendererJob::updateLayerGeometryCaches()
{
Q_FOREACH ( const QString& id, mGeometryCaches.keys() )
QMap<QString, QgsGeometryCache>::const_iterator it = mGeometryCaches.constBegin();
for ( ; it != mGeometryCaches.constEnd(); ++it )
{
const QgsGeometryCache& cache = mGeometryCaches[id];
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( id ) ) )
const QgsGeometryCache& cache = it.value();
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( it.key() ) ) )
* vl->cache() = cache;
}
mGeometryCaches.clear();
Expand Down
7 changes: 5 additions & 2 deletions src/core/qgspluginlayerregistry.cpp
Expand Up @@ -75,8 +75,11 @@ QgsPluginLayerRegistry::~QgsPluginLayerRegistry()
if ( !mPluginLayerTypes.isEmpty() )
{
QgsDebugMsg( "QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty" );
Q_FOREACH ( const QString& typeName, mPluginLayerTypes.keys() )
removePluginLayerType( typeName );
PluginLayerTypes::const_iterator it = mPluginLayerTypes.constBegin();
for ( ; it != mPluginLayerTypes.constEnd(); ++it )
{
removePluginLayerType( it.key() );
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsrulebasedlabeling.cpp
Expand Up @@ -31,7 +31,7 @@ QgsRuleBasedLabelProvider::~QgsRuleBasedLabelProvider()

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

// populate sub-providers
Expand All @@ -48,7 +48,7 @@ void QgsRuleBasedLabelProvider::registerFeature( QgsFeature& feature, QgsRenderC
QList<QgsAbstractLabelProvider*> QgsRuleBasedLabelProvider::subProviders()
{
QList<QgsAbstractLabelProvider*> lst;
Q_FOREACH ( QgsVectorLayerLabelProvider* subprovider, mSubProviders.values() )
Q_FOREACH ( QgsVectorLayerLabelProvider* subprovider, mSubProviders )
lst << subprovider;
return lst;
}
Expand Down
10 changes: 6 additions & 4 deletions src/core/qgsvectorlayereditbuffer.cpp
Expand Up @@ -607,9 +607,10 @@ QString QgsVectorLayerEditBuffer::dumpEditBuffer()
void QgsVectorLayerEditBuffer::handleAttributeAdded( int index )
{
// go through the changed attributes map and adapt indices
Q_FOREACH ( QgsFeatureId fid, mChangedAttributeValues.keys() )
QgsChangedAttributesMap::iterator it = mChangedAttributeValues.begin();
for ( ; it != mChangedAttributeValues.end(); ++it )
{
updateAttributeMapIndex( mChangedAttributeValues[fid], index, + 1 );
updateAttributeMapIndex( it.value(), index, + 1 );
}

// go through added features and adapt attributes
Expand All @@ -625,9 +626,10 @@ void QgsVectorLayerEditBuffer::handleAttributeAdded( int index )
void QgsVectorLayerEditBuffer::handleAttributeDeleted( int index )
{
// go through the changed attributes map and adapt indices
Q_FOREACH ( QgsFeatureId fid, mChangedAttributeValues.keys() )
QgsChangedAttributesMap::iterator it = mChangedAttributeValues.begin();
for ( ; it != mChangedAttributeValues.end(); ++it )
{
QgsAttributeMap& attrMap = mChangedAttributeValues[fid];
QgsAttributeMap& attrMap = it.value();
// remove the attribute
if ( attrMap.contains( index ) )
attrMap.remove( index );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerimport.cpp
Expand Up @@ -87,7 +87,7 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
return;
}

Q_FOREACH ( int idx, mOldToNewAttrIdx.values() )
Q_FOREACH ( int idx, mOldToNewAttrIdx )
{
if ( idx > mAttributeCount )
mAttributeCount = idx;
Expand Down

0 comments on commit c62a6e7

Please sign in to comment.