Skip to content

Commit 36a294e

Browse files
author
wonder
committed
Moved from QMap to QHash for the row-id maps and feature map in models.
There might be a tiny speed up in attribute table loading... git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11138 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2f68613 commit 36a294e

4 files changed

+15
-6
lines changed

src/app/attributetable/qgsattributetablememorymodel.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void QgsAttributeTableMemoryModel::loadLayer()
3232
QgsAttributeTableModel::loadLayer();
3333
mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), false );
3434

35+
mFeatureMap.reserve( mLayer->pendingFeatureCount() + 50 );
36+
3537
QgsFeature f;
3638
while ( mLayer->nextFeature( f ) )
3739
mFeatureMap.insert( f.id(), f );

src/app/attributetable/qgsattributetablememorymodel.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QAbstractTableModel>
2121
#include <QModelIndex>
2222
#include <QObject>
23+
#include <QHash>
2324

2425
//QGIS Includes
2526
#include "qgsfeature.h" //QgsAttributeMap
@@ -98,7 +99,7 @@ class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
9899
*/
99100
virtual void loadLayer();
100101

101-
QMap<int, QgsFeature> mFeatureMap;
102+
QHash<int, QgsFeature> mFeatureMap;
102103
};
103104

104105
#endif //QGSATTRIBUTETABLEMEMORYMODEL_H

src/app/attributetable/qgsattributetablemodel.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void QgsAttributeTableModel::featureDeleted( int fid )
8282
#endif
8383

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

@@ -175,6 +175,10 @@ void QgsAttributeTableModel::loadLayer()
175175

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

178+
// preallocate data before inserting
179+
mRowIdMap.reserve(pendingFeatureCount + 50);
180+
mIdRowMap.reserve(pendingFeatureCount + 50);
181+
178182
for ( int i = 0; mLayer->nextFeature( f ); ++i )
179183
{
180184
mRowIdMap.insert( i, f.id() );
@@ -198,7 +202,7 @@ void QgsAttributeTableModel::loadLayer()
198202

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

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

254259
return mRowIdMap[id];

src/app/attributetable/qgsattributetablemodel.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QAbstractTableModel>
2121
#include <QModelIndex>
2222
#include <QObject>
23+
#include <QHash>
2324

2425
//QGIS Includes
2526
#include "qgsfeature.h" //QgsAttributeMap
@@ -185,8 +186,8 @@ class QgsAttributeTableModel: public QAbstractTableModel
185186
QgsAttributeList mAttributes;
186187

187188
QList<QgsAttributeTableIdColumnPair> mSortList;
188-
QMap<int, int> mIdRowMap;
189-
QMap<int, int> mRowIdMap;
189+
QHash<int, int> mIdRowMap;
190+
QHash<int, int> mRowIdMap;
190191

191192
/**
192193
* Initializes id <-> row maps

0 commit comments

Comments
 (0)