QGraphicsSceneBspTreeIndex: simplify the code of items()

The old code dealt with a lot of special cases, probably to
avoid detaching. But the only case where deep copies are
avoided is if
  a) there're no freeItemIndexes
  b) there're no unindexedItems
  c) the sort order is neither AcendingOrder nor DescendingOrder,
     which is funny, since those are the only two values for
     Qt::SortOrder. The code checks for SortOrder(-1), but
     nowhere in Qt is such a sort order created.
Ergo, the deep copy was _never_ avoided.

So simplify the code by always building the result list from
the two input lists by copying all non-null items.

Saves over 2KiB in text size on optimized GCC 4.9 Linux AMD64
builds.

Change-Id: I8e739fb78896b2ad0bec45d05e86a76fe1ede04a
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
bb10
Marc Mutz 2015-12-21 16:23:12 +01:00
parent 3690bcfda3
commit 7353be1c67
1 changed files with 13 additions and 16 deletions

View File

@ -77,6 +77,8 @@
#include <QtCore/qmath.h>
#include <QtCore/qdebug.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
static inline int intmaxlog(int n)
@ -545,23 +547,18 @@ QList<QGraphicsItem *> QGraphicsSceneBspTreeIndex::items(Qt::SortOrder order) co
Q_D(const QGraphicsSceneBspTreeIndex);
const_cast<QGraphicsSceneBspTreeIndexPrivate*>(d)->purgeRemovedItems();
QList<QGraphicsItem *> itemList;
itemList.reserve(d->indexedItems.size() + d->unindexedItems.size());
// Rebuild the list of items to avoid holes. ### We could also just
// compress the item lists at this point.
QGraphicsItem *null = nullptr; // work-around for (at least) MSVC 2012 emitting
// warning C4100 for its own header <algorithm>
// when passing nullptr directly to remove_copy:
std::remove_copy(d->indexedItems.cbegin(), d->indexedItems.cend(),
std::back_inserter(itemList), null);
std::remove_copy(d->unindexedItems.cbegin(), d->unindexedItems.cend(),
std::back_inserter(itemList), null);
// If freeItemIndexes is empty, we know there are no holes in indexedItems and
// unindexedItems.
if (d->freeItemIndexes.isEmpty()) {
if (d->unindexedItems.isEmpty()) {
itemList = d->indexedItems;
} else {
itemList = d->indexedItems + d->unindexedItems;
}
} else {
// Rebuild the list of items to avoid holes. ### We could also just
// compress the item lists at this point.
foreach (QGraphicsItem *item, d->indexedItems + d->unindexedItems) {
if (item)
itemList << item;
}
}
d->sortItems(&itemList, order, d->sortCacheEnabled);
return itemList;
}