Skip to content

Commit

Permalink
Moved from QMap to QHash for the row-id maps and feature map in models.
Browse files Browse the repository at this point in the history
There might be a tiny speed up in attribute table loading...


git-svn-id: http://svn.osgeo.org/qgis/trunk@11138 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jul 21, 2009
1 parent 8b1fb78 commit 8a803aa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/app/attributetable/qgsattributetablememorymodel.cpp
Expand Up @@ -32,6 +32,8 @@ void QgsAttributeTableMemoryModel::loadLayer()
QgsAttributeTableModel::loadLayer();
mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), false );

mFeatureMap.reserve( mLayer->pendingFeatureCount() + 50 );

QgsFeature f;
while ( mLayer->nextFeature( f ) )
mFeatureMap.insert( f.id(), f );
Expand Down
3 changes: 2 additions & 1 deletion src/app/attributetable/qgsattributetablememorymodel.h
Expand Up @@ -20,6 +20,7 @@
#include <QAbstractTableModel>
#include <QModelIndex>
#include <QObject>
#include <QHash>

//QGIS Includes
#include "qgsfeature.h" //QgsAttributeMap
Expand Down Expand Up @@ -98,7 +99,7 @@ class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
*/
virtual void loadLayer();

QMap<int, QgsFeature> mFeatureMap;
QHash<int, QgsFeature> mFeatureMap;
};

#endif //QGSATTRIBUTETABLEMEMORYMODEL_H
11 changes: 8 additions & 3 deletions src/app/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -82,7 +82,7 @@ void QgsAttributeTableModel::featureDeleted( int fid )
#endif

QgsDebugMsg( "id->row" );
QMap<int, int>::iterator it;
QHash<int, int>::iterator it;
for ( it = mIdRowMap.begin(); it != mIdRowMap.end(); ++it )
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );

Expand Down Expand Up @@ -175,6 +175,10 @@ void QgsAttributeTableModel::loadLayer()

mLayer->select( QgsAttributeList(), QgsRectangle(), false );

// preallocate data before inserting
mRowIdMap.reserve(pendingFeatureCount + 50);
mIdRowMap.reserve(pendingFeatureCount + 50);

for ( int i = 0; mLayer->nextFeature( f ); ++i )
{
mRowIdMap.insert( i, f.id() );
Expand All @@ -198,7 +202,7 @@ void QgsAttributeTableModel::loadLayer()

#if 0
QgsDebugMsg( "id->row" );
QMap<int, int>::iterator it;
QHash<int, int>::iterator it;
for ( it = mIdRowMap.begin(); it != mIdRowMap.end(); ++it )
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );

Expand Down Expand Up @@ -248,7 +252,8 @@ int QgsAttributeTableModel::rowToId( const int id ) const
if ( !mRowIdMap.contains( id ) )
{
QgsDebugMsg( QString( "rowToId: row %1 not in the map" ).arg( id ) );
return -1;
// return negative infinite (to avoid collision with newly added features)
return -999999;
}

return mRowIdMap[id];
Expand Down
5 changes: 3 additions & 2 deletions src/app/attributetable/qgsattributetablemodel.h
Expand Up @@ -20,6 +20,7 @@
#include <QAbstractTableModel>
#include <QModelIndex>
#include <QObject>
#include <QHash>

//QGIS Includes
#include "qgsfeature.h" //QgsAttributeMap
Expand Down Expand Up @@ -185,8 +186,8 @@ class QgsAttributeTableModel: public QAbstractTableModel
QgsAttributeList mAttributes;

QList<QgsAttributeTableIdColumnPair> mSortList;
QMap<int, int> mIdRowMap;
QMap<int, int> mRowIdMap;
QHash<int, int> mIdRowMap;
QHash<int, int> mRowIdMap;

/**
* Initializes id <-> row maps
Expand Down

0 comments on commit 8a803aa

Please sign in to comment.