Skip to content

Commit

Permalink
Sorting of raster color ramp items: better and faster.
Browse files Browse the repository at this point in the history
... why to use insert sort if we have quicksort for free ...? :)

(was taking minutes for one raster layer)


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10604 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Apr 20, 2009
1 parent e202703 commit 969e9f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
36 changes: 6 additions & 30 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -1371,8 +1371,6 @@ void QgsRasterLayerProperties::apply()
//iterate through mColormapTreeWidget and set colormap info of layer
QList<QgsColorRampShader::ColorRampItem> myColorRampItems;

bool inserted = false;
int myCurrentIndex = 0;
int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* myCurrentItem;
for ( int i = 0; i < myTopLevelItemCount; ++i )
Expand All @@ -1386,35 +1384,13 @@ void QgsRasterLayerProperties::apply()
myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble();
myNewColorRampItem.color = myCurrentItem->background( 1 ).color();
myNewColorRampItem.label = myCurrentItem->text( 2 );

//Simple insertion sort - speed is not a huge factor here
inserted = false;
myCurrentIndex = 0;
while ( !inserted )
{
if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
{
myColorRampItems.push_back( myNewColorRampItem );
inserted = true;
}
else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
{
myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
inserted = true;
}
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 )
{
myColorRampItems.push_back( myNewColorRampItem );
inserted = true;
}
else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value )
{
myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
inserted = true;
}
myCurrentIndex++;
}

myColorRampItems.append( myNewColorRampItem );
}

// sort the shader items
qSort(myColorRampItems);

myRasterShaderFunction->setColorRampItemList( myColorRampItems );
//Reload table in GUI because it may have been sorted or contained invalid values
populateColorMapTable( myColorRampItems );
Expand Down
3 changes: 3 additions & 0 deletions src/core/raster/qgscolorrampshader.h
Expand Up @@ -44,6 +44,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
QString label;
double value;
QColor color;

// compare operator for sorting
bool operator<(const ColorRampItem& other) const { return value < other.value; }
};

enum ColorRamp_TYPE
Expand Down

0 comments on commit 969e9f0

Please sign in to comment.