Skip to content
Permalink
Browse files

Sorting of raster color ramp items: better and faster.

... 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
wonder committed Apr 20, 2009
1 parent e202703 commit 969e9f0d5af1d031d25ec56e594632d86782cc02
Showing with 9 additions and 30 deletions.
  1. +6 −30 src/app/qgsrasterlayerproperties.cpp
  2. +3 −0 src/core/raster/qgscolorrampshader.h
@@ -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 )
@@ -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 );
@@ -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

0 comments on commit 969e9f0

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