Skip to content

Commit

Permalink
Simplify QgsVectorDataProvider::fillMinMaxCache()
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 20, 2018
1 parent 10ceac6 commit c6f7a07
Showing 1 changed file with 46 additions and 38 deletions.
84 changes: 46 additions & 38 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -483,18 +483,19 @@ void QgsVectorDataProvider::fillMinMaxCache() const
{
if ( flds.at( i ).type() == QVariant::Int )
{
mCacheMinValues[i] = QVariant( INT_MAX );
mCacheMaxValues[i] = QVariant( INT_MIN );
mCacheMinValues[i] = QVariant( std::numeric_limits<int>::max() );
mCacheMaxValues[i] = QVariant( std::numeric_limits<int>::lowest() );
}
else if ( flds.at( i ).type() == QVariant::LongLong )
{
mCacheMinValues[i] = QVariant( std::numeric_limits<qlonglong>::max() );
mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::min() );
mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::lowest() );
}
else if ( flds.at( i ).type() == QVariant::Double )
{
mCacheMinValues[i] = QVariant( DBL_MAX );
mCacheMaxValues[i] = QVariant( -DBL_MAX );
mCacheMinValues[i] = QVariant( std::numeric_limits<double>::max() );
mCacheMaxValues[i] = QVariant( std::numeric_limits<double>::lowest() );

}
else
{
Expand All @@ -504,54 +505,61 @@ void QgsVectorDataProvider::fillMinMaxCache() const
}

QgsFeature f;
QgsAttributeList keys = mCacheMinValues.keys();
const QgsAttributeList keys = mCacheMinValues.keys();
QgsFeatureIterator fi = getFeatures( QgsFeatureRequest().setSubsetOfAttributes( keys )
.setFlags( QgsFeatureRequest::NoGeometry ) );

while ( fi.nextFeature( f ) )
{
QgsAttributes attrs = f.attributes();
for ( QgsAttributeList::const_iterator it = keys.constBegin(); it != keys.constEnd(); ++it )
for ( int attributeIndex : keys )
{
const QVariant &varValue = attrs.at( *it );
const QVariant &varValue = attrs.at( attributeIndex );

if ( varValue.isNull() )
continue;

if ( flds.at( *it ).type() == QVariant::Int )
{
int value = varValue.toInt();
if ( value < mCacheMinValues[*it].toInt() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toInt() )
mCacheMaxValues[*it] = value;
}
else if ( flds.at( *it ).type() == QVariant::LongLong )
switch ( flds.at( attributeIndex ).type() )
{
qlonglong value = varValue.toLongLong();
if ( value < mCacheMinValues[*it].toLongLong() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toLongLong() )
mCacheMaxValues[*it] = value;
}
else if ( flds.at( *it ).type() == QVariant::Double )
{
double value = varValue.toDouble();
if ( value < mCacheMinValues[*it].toDouble() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toDouble() )
mCacheMaxValues[*it] = value;
}
else
{
QString value = varValue.toString();
if ( mCacheMinValues[*it].isNull() || value < mCacheMinValues[*it].toString() )
case QVariant::Int:
{
int value = varValue.toInt();
if ( value < mCacheMinValues[ attributeIndex ].toInt() )
mCacheMinValues[ attributeIndex ] = value;
if ( value > mCacheMaxValues[ attributeIndex ].toInt() )
mCacheMaxValues[ attributeIndex ] = value;
break;
}
case QVariant::LongLong:
{
qlonglong value = varValue.toLongLong();
if ( value < mCacheMinValues[ attributeIndex ].toLongLong() )
mCacheMinValues[ attributeIndex ] = value;
if ( value > mCacheMaxValues[ attributeIndex ].toLongLong() )
mCacheMaxValues[ attributeIndex ] = value;
break;
}
case QVariant::Double:
{
mCacheMinValues[*it] = value;
double value = varValue.toDouble();
if ( value < mCacheMinValues[ attributeIndex ].toDouble() )
mCacheMinValues[attributeIndex ] = value;
if ( value > mCacheMaxValues[ attributeIndex ].toDouble() )
mCacheMaxValues[ attributeIndex ] = value;
break;
}
if ( mCacheMaxValues[*it].isNull() || value > mCacheMaxValues[*it].toString() )
default:
{
mCacheMaxValues[*it] = value;
QString value = varValue.toString();
if ( mCacheMinValues[ attributeIndex ].isNull() || value < mCacheMinValues[attributeIndex ].toString() )
{
mCacheMinValues[attributeIndex] = value;
}
if ( mCacheMaxValues[attributeIndex].isNull() || value > mCacheMaxValues[attributeIndex].toString() )
{
mCacheMaxValues[attributeIndex] = value;
}
break;
}
}
}
Expand Down

0 comments on commit c6f7a07

Please sign in to comment.