Use QHash<K,V> instead of QMap<K,V> when K is a pointer type
authorGiuseppe D'Angelo <dangelog@gmail.com>
Thu, 1 Mar 2012 23:47:36 +0000 (23:47 +0000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 2 Mar 2012 22:16:25 +0000 (23:16 +0100)
Changes various internal usages of QMap when the key is a pointer type.
Being ordered by the pointer value itself, it makes very little sense
to use QMap<K*, V> (esp. in cases where the key is actually allocated
on the heap).

The usages have been found with the following script:

 #!/usr/bin/perl

 use strict;
 use warnings;
 use Regexp::Common;
 use File::Find;
 use feature ':5.10';

 my $container = qr/(?:QMap)/;

 sub process {
     return unless (-f and -r and /(\.c|\.cpp|\.h|\.txt)$/);
     open my $fh, "<", $_ or die "Cannot open $_: $!";
     while (my $line = <$fh>) {
         chomp $line;
         while ($line =~ /($container\s*$RE{balanced}{-parens=>"<>"})/g) {
             my $tmp = $1;
             $tmp =~ s/\s+//g;
             say "$_:$.: $line" if $tmp =~ /^$container\s*<[^,]+\*,/;
         }
     }
     close $fh;
 }

 find( { no_chdir => 1, wanted => \&process }, @ARGV );

Change-Id: Idd9819c3f4c48f98ef92831d5e8e5ac0fa42283c
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/widgets/graphicsview/qgraphicsitem.cpp
src/widgets/graphicsview/qgraphicsitem_p.h
src/widgets/graphicsview/qgraphicsscene_p.h
src/widgets/graphicsview/qgraphicswidget.cpp
src/widgets/kernel/qapplication_p.h
src/widgets/kernel/qgesturemanager.cpp
src/widgets/kernel/qgesturemanager_p.h
src/widgets/styles/qgtkstyle_p.cpp
src/widgets/styles/qgtkstyle_p.h
src/widgets/widgets/qabstractbutton.cpp
src/widgets/widgets/qwidgetanimator_p.h

index d88183f..dab1965 100644 (file)
@@ -783,7 +783,7 @@ static inline void _q_adjustRect(QRect *rect)
 class QGraphicsItemCustomDataStore
 {
 public:
-    QMap<const QGraphicsItem *, QMap<int, QVariant> > data;
+    QHash<const QGraphicsItem *, QMap<int, QVariant> > data;
 };
 Q_GLOBAL_STATIC(QGraphicsItemCustomDataStore, qt_dataStore)
 
@@ -1379,7 +1379,7 @@ void QGraphicsItemCache::purge()
 {
     QPixmapCache::remove(key);
     key = QPixmapCache::Key();
-    QMutableMapIterator<QPaintDevice *, DeviceData> it(deviceData);
+    QMutableHashIterator<QPaintDevice *, DeviceData> it(deviceData);
     while (it.hasNext()) {
         DeviceData &data = it.next().value();
         QPixmapCache::remove(data.key);
@@ -7124,7 +7124,7 @@ void QGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
     if ((event->buttons() & Qt::LeftButton) && (flags() & ItemIsMovable)) {
         // Determine the list of items that need to be moved.
         QList<QGraphicsItem *> selectedItems;
-        QMap<QGraphicsItem *, QPointF> initialPositions;
+        QHash<QGraphicsItem *, QPointF> initialPositions;
         if (d_ptr->scene) {
             selectedItems = d_ptr->scene->selectedItems();
             initialPositions = d_ptr->scene->d_func()->movingItemsInitialPositions;
index aebb22a..1783fdb 100644 (file)
@@ -145,7 +145,7 @@ public:
         QPoint cacheIndent;
         QPixmapCache::Key key;
     };
-    QMap<QPaintDevice *, DeviceData> deviceData;
+    QHash<QPaintDevice *, DeviceData> deviceData;
 
     // List of logical exposed rects
     QVector<QRectF> exposed;
@@ -508,7 +508,7 @@ public:
 
     QRectF childrenBoundingRect;
     QRectF needsRepaint;
-    QMap<QWidget *, QRect> paintedViewBoundingRects;
+    QHash<QWidget *, QRect> paintedViewBoundingRects;
     QPointF pos;
     qreal z;
     qreal opacity;
index b0410e3..0f5a0a6 100644 (file)
@@ -128,7 +128,7 @@ public:
     QVector<QGraphicsItem *> unpolishedItems;
     QList<QGraphicsItem *> topLevelItems;
 
-    QMap<QGraphicsItem *, QPointF> movingItemsInitialPositions;
+    QHash<QGraphicsItem *, QPointF> movingItemsInitialPositions;
     void registerTopLevelItem(QGraphicsItem *item);
     void unregisterTopLevelItem(QGraphicsItem *item);
     void _q_updateLater();
index da4ccb1..4ad8513 100644 (file)
@@ -222,7 +222,7 @@ public:
     }
 
 private:
-    QMap<const QGraphicsWidget *, QStyle *> styles;
+    QHash<const QGraphicsWidget *, QStyle *> styles;
     mutable QMutex mutex;
 };
 Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles)
index 7a88098..ae4f0c2 100644 (file)
@@ -313,7 +313,7 @@ public:
     QPointer<QWSManager> last_manager;
     QWSServerCleaner qwsServerCleaner;
 # ifndef QT_NO_DIRECTPAINTER
-    QMap<WId, QDirectPainter *> *directPainters;
+    QHash<WId, QDirectPainter *> *directPainters;
 # endif
     QRect maxWindowRect(const QScreen *screen) const { return maxWindowRects[screen]; }
     void setMaxWindowRect(const QScreen *screen, int screenNo, const QRect &rect);
@@ -393,7 +393,7 @@ public:
 
 private:
 #ifdef Q_WS_QWS
-    QMap<const QScreen*, QRect> maxWindowRects;
+    QHash<const QScreen*, QRect> maxWindowRects;
 #endif
 
     static QApplicationPrivate *self;
index 2e9edb1..296d8d3 100644 (file)
@@ -541,8 +541,8 @@ bool QGestureManager::filterEvent(QObject *receiver, QEvent *event)
 }
 
 void QGestureManager::getGestureTargets(const QSet<QGesture*> &gestures,
-    QMap<QWidget *, QList<QGesture *> > *conflicts,
-    QMap<QWidget *, QList<QGesture *> > *normal)
+    QHash<QWidget *, QList<QGesture *> > *conflicts,
+    QHash<QWidget *, QList<QGesture *> > *normal)
 {
     typedef QHash<Qt::GestureType, QHash<QWidget *, QGesture *> > GestureByTypes;
     GestureByTypes gestureByTypes;
@@ -588,7 +588,7 @@ void QGestureManager::deliverEvents(const QSet<QGesture *> &gestures,
     if (gestures.isEmpty())
         return;
 
-    typedef QMap<QWidget *, QList<QGesture *> > GesturesPerWidget;
+    typedef QHash<QWidget *, QList<QGesture *> > GesturesPerWidget;
     GesturesPerWidget conflictedGestures;
     GesturesPerWidget normalStartedGestures;
 
index 68945a7..9f59550 100644 (file)
@@ -138,8 +138,8 @@ private:
     void deliverEvents(const QSet<QGesture *> &gestures,
                        QSet<QGesture *> *undeliveredGestures);
     void getGestureTargets(const QSet<QGesture*> &gestures,
-                           QMap<QWidget *, QList<QGesture *> > *conflicts,
-                           QMap<QWidget *, QList<QGesture *> > *normal);
+                           QHash<QWidget *, QList<QGesture *> > *conflicts,
+                           QHash<QWidget *, QList<QGesture *> > *normal);
 
     void cancelGesturesForChildren(QGesture *originatingGesture);
 };
index 18e120a..0402859 100644 (file)
@@ -897,7 +897,7 @@ extern QStringList qt_make_filter_list(const QString &filter);
 void QGtkStylePrivate::setupGtkFileChooser(GtkWidget* gtkFileChooser, QWidget *parent,
                                 const QString &dir, const QString &filter, QString *selectedFilter,
                                 QFileDialog::Options options, bool isSaveDialog,
-                                QMap<GtkFileFilter *, QString> *filterMap)
+                                QHash<GtkFileFilter *, QString> *filterMap)
 {
     g_object_set(gtkFileChooser, "do-overwrite-confirmation", gboolean(!(options & QFileDialog::DontConfirmOverwrite)), NULL);
     g_object_set(gtkFileChooser, "local_only", gboolean(true), NULL);
@@ -969,7 +969,7 @@ void QGtkStylePrivate::setupGtkFileChooser(GtkWidget* gtkFileChooser, QWidget *p
 QString QGtkStylePrivate::openFilename(QWidget *parent, const QString &caption, const QString &dir, const QString &filter,
                             QString *selectedFilter, QFileDialog::Options options)
 {
-    QMap<GtkFileFilter *, QString> filterMap;
+    QHash<GtkFileFilter *, QString> filterMap;
     GtkWidget *gtkFileChooser = QGtkStylePrivate::gtk_file_chooser_dialog_new (qPrintable(caption),
                                                              NULL,
                                                              GTK_FILE_CHOOSER_ACTION_OPEN,
@@ -1003,7 +1003,7 @@ QString QGtkStylePrivate::openFilename(QWidget *parent, const QString &caption,
 
 QString QGtkStylePrivate::openDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)
 {
-    QMap<GtkFileFilter *, QString> filterMap;
+    QHash<GtkFileFilter *, QString> filterMap;
     GtkWidget *gtkFileChooser = QGtkStylePrivate::gtk_file_chooser_dialog_new (qPrintable(caption),
                                                              NULL,
                                                              GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
@@ -1033,7 +1033,7 @@ QStringList QGtkStylePrivate::openFilenames(QWidget *parent, const QString &capt
                                  QString *selectedFilter, QFileDialog::Options options)
 {
     QStringList filenames;
-    QMap<GtkFileFilter *, QString> filterMap;
+    QHash<GtkFileFilter *, QString> filterMap;
     GtkWidget *gtkFileChooser = QGtkStylePrivate::gtk_file_chooser_dialog_new (qPrintable(caption),
                                                              NULL,
                                                              GTK_FILE_CHOOSER_ACTION_OPEN,
@@ -1068,7 +1068,7 @@ QStringList QGtkStylePrivate::openFilenames(QWidget *parent, const QString &capt
 QString QGtkStylePrivate::saveFilename(QWidget *parent, const QString &caption, const QString &dir, const QString &filter,
                            QString *selectedFilter, QFileDialog::Options options)
 {
-    QMap<GtkFileFilter *, QString> filterMap;
+    QHash<GtkFileFilter *, QString> filterMap;
     GtkWidget *gtkFileChooser = QGtkStylePrivate::gtk_file_chooser_dialog_new (qPrintable(caption),
                                                              NULL,
                                                              GTK_FILE_CHOOSER_ACTION_SAVE,
index 95988fa..02d659c 100644 (file)
@@ -344,7 +344,7 @@ public:
     static void setupGtkFileChooser(GtkWidget* gtkFileChooser, QWidget *parent,
             const QString &dir, const QString &filter, QString *selectedFilter,
             QFileDialog::Options options, bool isSaveDialog = false,
-            QMap<GtkFileFilter *, QString> *filterMap = 0);
+            QHash<GtkFileFilter *, QString> *filterMap = 0);
 
     static QString openFilename(QWidget *parent, const QString &caption, const QString &dir, const QString &filter,
                                 QString *selectedFilter, QFileDialog::Options options);
index 2c51e89..80e1259 100644 (file)
@@ -190,7 +190,7 @@ public:
     void detectCheckedButton();
     void notifyChecked(QAbstractButton *button);
     bool exclusive;
-    QMap<QAbstractButton*, int> mapping;
+    QHash<QAbstractButton*, int> mapping;
 };
 
 QButtonGroup::QButtonGroup(QObject *parent)
index e205440..82f81a7 100644 (file)
@@ -54,7 +54,7 @@
 //
 
 #include <qobject.h>
-#include <qmap.h>
+#include <qhash.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -79,7 +79,7 @@ private Q_SLOTS:
 #endif
 
 private:
-    typedef QMap<QWidget*, QPropertyAnimation*> AnimationMap;
+    typedef QHash<QWidget*, QPropertyAnimation*> AnimationMap;
     AnimationMap m_animation_map;
     QMainWindowLayout *m_mainWindowLayout;
 };