From 1e4d70b5b626f70c6d36731c08c2367febf95f27 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 5 Oct 2013 01:12:36 +0200 Subject: [PATCH] QMdiArea: replace dubious use of QSet with QVector The code populated two QSets with some x and y coordinates and went on to get the values as a list. Since QSet is unordered_set, those lists were not sorted, so the code did that manually. It then went and created the cross product of the two lists as a list of QPoints. Since QSet is a node-based container and not even an ordered one, the code pays a hefty price just for ensuring uniqueness of values prior to sorting. The new code just cramms everything into vectors, duplicates and all, then sorts the vectors and only then removes duplicates using std::unique. Since the sizes of all containers involved are known ahead of time, make liberal use of reserve() to trim the whole container business down to three memory allocations. Change-Id: I7004b1b90b3142acb12d7bb12bcd0014d54e6e02 Reviewed-by: Friedemann Kleint --- src/widgets/widgets/qmdiarea.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/widgets/widgets/qmdiarea.cpp b/src/widgets/widgets/qmdiarea.cpp index cb5c8751cf..f06370bbcf 100644 --- a/src/widgets/widgets/qmdiarea.cpp +++ b/src/widgets/widgets/qmdiarea.cpp @@ -450,22 +450,28 @@ QRect MinOverlapPlacer::findMinOverlapRect(const QVector &source, const Q void MinOverlapPlacer::getCandidatePlacements(const QSize &size, const QVector &rects, const QRect &domain,QVector &candidates) { - QSet xset; - QSet yset; - xset << domain.left() << domain.right() - size.width() + 1; - yset << domain.top(); + QVector xlist; + xlist.reserve(2 + rects.size()); + xlist << domain.left() << domain.right() - size.width() + 1; + + QVector ylist; + ylist.reserve(2 + rects.size()); + ylist << domain.top(); if (domain.bottom() - size.height() + 1 >= 0) - yset << domain.bottom() - size.height() + 1; + ylist << domain.bottom() - size.height() + 1; + foreach (const QRect &rect, rects) { - xset << rect.right() + 1; - yset << rect.bottom() + 1; + xlist << rect.right() + 1; + ylist << rect.bottom() + 1; } - QList xlist = xset.values(); std::sort(xlist.begin(), xlist.end()); - QList ylist = yset.values(); - std::sort(ylist.begin(), ylist.end()); + xlist.erase(std::unique(xlist.begin(), xlist.end()), xlist.end()); + std::sort(ylist.begin(), ylist.end()); + ylist.erase(std::unique(ylist.begin(), ylist.end()), ylist.end()); + + candidates.reserve(candidates.size() + ylist.size() * xlist.size()); foreach (int y, ylist) foreach (int x, xlist) candidates << QRect(QPoint(x, y), size);