Skip to content
Permalink
Browse files

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 c62a6e76d59ffc648af1aaf1ade025faeb62e691
@@ -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 );
@@ -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 );
}
}
}
@@ -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()
@@ -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;
@@ -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 )
@@ -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 )
{
@@ -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 );
@@ -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";
@@ -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 ) )
{
@@ -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() );
}
}

@@ -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;
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 ) )
{
@@ -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() ) );
@@ -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();
@@ -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() );
}
}
}

@@ -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
@@ -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;
}
@@ -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
@@ -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 );
@@ -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;

0 comments on commit c62a6e7

Please sign in to comment.
You can’t perform that action at this time.