Skip to content
Permalink
Browse files

Fix clazy reserve-candidates suggestions

Avoids repeated memory allocations
  • Loading branch information
nyalldawson committed Oct 24, 2016
1 parent 904b004 commit bdc39ff6595439b812b98bb1e72118185506ec51
@@ -3860,6 +3860,8 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )

QgsSublayersDialog::LayerDefinitionList layers;
QStringList names;
names.reserve( sublayers.size() );
layers.reserve( sublayers.size() );
for ( int i = 0; i < sublayers.size(); i++ )
{
// simplify raster sublayer name - should add a function in gdal provider for this?
@@ -1052,6 +1052,7 @@ void QgsVectorLayerProperties::on_mButtonAddJoin_clicked()

QList<QgsMapLayer*> joinedLayers;
const QList< QgsVectorJoinInfo >& joins = mLayer->vectorJoins();
joinedLayers.reserve( joins.size() );
for ( int i = 0; i < joins.size(); ++i )
{
joinedLayers.append( QgsMapLayerRegistry::instance()->mapLayer( joins[i].joinLayerId ) );
@@ -104,6 +104,7 @@ void CostCalculator::setPolygonCandidatesCost( int nblp, QList< LabelPosition* >
// IMPORTANT - only want to sort first nblp positions. The rest have not had the cost
// calculated so will have nonsense values
QList< LabelPosition* > toSort;
toSort.reserve( nblp );
for ( int i = 0; i < nblp; ++i )
{
toSort << lPos.at( i );
@@ -784,6 +784,7 @@ QgsColorRamp* QgsPresetSchemeColorRamp::create( const QgsStringMap& properties )
QList<QColor> QgsPresetSchemeColorRamp::colors() const
{
QList< QColor > l;
l.reserve( mColors.count() );
for ( int i = 0; i < mColors.count(); ++i )
{
l << mColors.at( i ).first;
@@ -2589,6 +2589,7 @@ static QVariant fcnOrderParts( const QVariantList& values, const QgsExpressionCo
QgsExpressionSorter sorter( orderBy );

QList<QgsFeature> partFeatures;
partFeatures.reserve( collection->partCount() );
for ( int i = 0; i < collection->partCount(); ++i )
{
f.setGeometry( QgsGeometry( collection->geometryN( i )->clone() ) );
@@ -220,11 +220,12 @@ void QgsLayerDefinition::DependencySorter::init( const QDomDocument& doc )
QStringList layerIds;

QDomNodeList nl = doc.elementsByTagName( "maplayer" );
layerIds.reserve( nl.count() );
QVector<QString> deps; //avoid expensive allocation for list for every iteration
for ( int i = 0; i < nl.count(); i++ )
{
QVector<QString> deps;
deps.resize( 0 ); // preserve capacity - don't use clear
QDomNode node = nl.item( i );
QDomElement element = node.toElement();

QString id = node.namedItem( "id" ).toElement().text();
layerIds << id;
@@ -185,6 +185,10 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( const Qgs
QList<bool> destHasNoDataValueList;
QList<double> destNoDataValueList;
QList<Qgis::DataType> destDataTypeList;
destDataTypeList.reserve( nBands );
destHasNoDataValueList.reserve( nBands );
destNoDataValueList.reserve( nBands );

for ( int bandNo = 1; bandNo <= nBands; bandNo++ )
{
QgsRasterNuller *nuller = pipe->nuller();
@@ -1348,6 +1348,7 @@ bool QgsSimpleMarkerSymbolLayer::writeDxf( QgsDxfExport& e, double mmMapUnitScal
polygon = t.map( polygon );

QgsPointSequence p;
p.reserve( polygon.size() );
for ( int i = 0; i < polygon.size(); i++ )
p << QgsPointV2( polygon[i] );
p << p[0];
@@ -271,6 +271,7 @@ void QgsAttributeTableFilterModel::setFilteredFeatures( const QgsFeatureIds& ids
QgsFeatureIds QgsAttributeTableFilterModel::filteredFeatures()
{
QgsFeatureIds ids;
ids.reserve( rowCount() );
for ( int i = 0; i < rowCount(); ++i )
{
QModelIndex row = index( i, 0 );
@@ -109,6 +109,7 @@ QgsOrganizeTableColumnsDialog::~QgsOrganizeTableColumnsDialog()
QgsAttributeTableConfig QgsOrganizeTableColumnsDialog::config() const
{
QVector<QgsAttributeTableConfig::ColumnConfig> columns;
columns.reserve( mFieldsList->count() );

for ( int i = 0 ; i < mFieldsList->count() ; i++ )
{
@@ -272,6 +272,7 @@ void QgsValueRelationSearchWidgetWrapper::initWidget( QWidget* editor )
else if ( mLineEdit )
{
QStringList values;
values.reserve( mCache.size() );
Q_FOREACH ( const ValueRelationItem& i, mCache )
{
values << i.second;
@@ -141,6 +141,7 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget* editor )
else if ( mLineEdit )
{
QStringList values;
values.reserve( mCache.size() );
Q_FOREACH ( const ValueRelationItem& i, mCache )
{
values << i.second;
@@ -151,6 +151,7 @@ QList< int > QgsDatumTransformDialog::selectedDatumTransform()
QTreeWidgetItem * item = mDatumTransformTreeWidget->currentItem();
if ( item )
{
list.reserve( 2 );
for ( int i = 0; i < 2; ++i )
{
int transformNr = item->data( i, Qt::UserRole ).toInt();
@@ -406,6 +406,7 @@ void QgsRasterTransparencyWidget::apply()
{
QgsRasterTransparency::TransparentThreeValuePixel myTransparentPixel;
QList<QgsRasterTransparency::TransparentThreeValuePixel> myTransparentThreeValuePixelList;
myTransparentThreeValuePixelList.reserve( tableTransparency->rowCount() );
for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
{
myTransparentPixel.red = transparencyCellValue( myListRunner, 0 );
@@ -420,6 +421,7 @@ void QgsRasterTransparencyWidget::apply()
{
QgsRasterTransparency::TransparentSingleValuePixel myTransparentPixel;
QList<QgsRasterTransparency::TransparentSingleValuePixel> myTransparentSingleValuePixelList;
myTransparentSingleValuePixelList.reserve( tableTransparency->rowCount() );
for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
{
myTransparentPixel.min = transparencyCellValue( myListRunner, 0 );
@@ -306,6 +306,7 @@ QgsSvgSelectorGroupsModel::QgsSvgSelectorGroupsModel( QObject* parent )
QStringList svgPaths = QgsApplication::svgPaths();
QStandardItem *parentItem = invisibleRootItem();
QStringList parentPaths;
parentPaths.reserve( svgPaths.size() );

for ( int i = 0; i < svgPaths.size(); i++ )
{
@@ -538,7 +539,7 @@ void QgsSvgSelectorWidget::populateList()
//-- QgsSvgSelectorDialog

QgsSvgSelectorDialog::QgsSvgSelectorDialog( QWidget *parent, Qt::WindowFlags fl,
const QDialogButtonBox::StandardButtons& buttons,
QDialogButtonBox::StandardButtons buttons,
Qt::Orientation orientation )
: QDialog( parent, fl )
{
@@ -172,6 +172,7 @@ bool QgsAfsProvider::getFeature( const QgsFeatureId &id, QgsFeature &f, bool fet
// When fetching from server, fetch all attributes and geometry by default so that we can cache them
QStringList fetchAttribNames;
QList<int> fetchAttribIdx;
fetchAttribIdx.reserve( mFields.size() );
for ( int idx = 0, n = mFields.size(); idx < n; ++idx )
{
fetchAttribNames.append( mFields.at( idx ).name() );
@@ -183,6 +184,7 @@ bool QgsAfsProvider::getFeature( const QgsFeatureId &id, QgsFeature &f, bool fet
int startId = ( id / 100 ) * 100;
int stopId = qMin( startId + 100, mObjectIds.length() );
QList<quint32> objectIds;
objectIds.reserve( stopId );
for ( int i = startId; i < stopId; ++i )
{
objectIds.append( mObjectIds[i] );
@@ -242,6 +242,7 @@ void QgsDb2TableModel::setGeometryTypesForTable( QgsDb2LayerProperty layerProper

QList<QStandardItem *> row;

row.reserve( dbtmColumns );
for ( int j = 0; j < dbtmColumns; j++ )
{
row << itemFromIndex( currentChildIndex.sibling( i, j ) );
@@ -241,6 +241,7 @@ void QgsMssqlTableModel::setGeometryTypesForTable( QgsMssqlLayerProperty layerPr
}

QList<QStandardItem *> row;
row.reserve( dbtmColumns );

for ( int j = 0; j < dbtmColumns; j++ )
{
@@ -272,6 +272,8 @@ QgsWcsProvider::QgsWcsProvider( const QString& uri )
// Get types
// TODO: we are using the same data types like GDAL (not wider like GDAL provider)
// with expectation to replace 'no data' values by NaN
mSrcGdalDataType.reserve( mBandCount );
mGdalDataType.reserve( mBandCount );
for ( int i = 1; i <= mBandCount; i++ )
{
GDALRasterBandH gdalBand = GDALGetRasterBand( mCachedGdalDataset, i );
@@ -47,6 +47,7 @@ QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QStr
QDomNodeList layerNodeList = mXMLDoc->elementsByTagName( "maplayer" );
QDomElement currentElement;
int nNodes = layerNodeList.size();
mProjectLayerElements.reserve( nNodes );
for ( int i = 0; i < nNodes; ++i )
{
currentElement = layerNodeList.at( i ).toElement();
@@ -1470,6 +1471,7 @@ QList< QPair< QString, QgsLayerCoordinateTransform > > QgsServerProjectParser::l
}

QDomNodeList layerTransformNodeList = coordTransformInfoElem.elementsByTagName( "layer_coordinate_transform" );
layerTransformList.reserve( layerTransformNodeList.size() );
for ( int i = 0; i < layerTransformNodeList.size(); ++i )
{
QPair< QString, QgsLayerCoordinateTransform > layerEntry;
@@ -996,6 +996,7 @@ void QgsWmsProjectParser::addDrawingOrder( QDomElement& parentElem, QDomDocument
if ( !layerList.isEmpty() )
{
QStringList reversedList;
reversedList.reserve( layerList.size() );
for ( int i = layerList.size() - 1; i >= 0; --i )
reversedList << layerList[ i ];

0 comments on commit bdc39ff

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