From e1e728daad07600fca7bc1811c92fb05065884c6 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 7 Apr 2010 16:55:01 +0200 Subject: O(n^2) to O(n) optimization in QTreeWidget::selectedItems() As we need to ensure that there will be no duplicates in the list returned by QTreeWidget::selectedItems() (see commit ea75d434e40100a9abf56195b646a8efdf3c1288), we now use a QSet to ensure unicity while keeping the same order as before. Reviewed-by: Olivier Task-number: QTBUG-9485 --- src/gui/itemviews/qtreewidget.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/gui/itemviews/qtreewidget.cpp b/src/gui/itemviews/qtreewidget.cpp index 19ed10b..4c80325 100644 --- a/src/gui/itemviews/qtreewidget.cpp +++ b/src/gui/itemviews/qtreewidget.cpp @@ -3039,10 +3039,14 @@ QList QTreeWidget::selectedItems() const Q_D(const QTreeWidget); QModelIndexList indexes = selectionModel()->selectedIndexes(); QList items; + items.reserve(indexes.count()); + QSet seen; + seen.reserve(indexes.count()); for (int i = 0; i < indexes.count(); ++i) { QTreeWidgetItem *item = d->item(indexes.at(i)); - if (isItemHidden(item) || items.contains(item)) // ### slow, optimize later + if (isItemHidden(item) || seen.contains(item)) continue; + seen.insert(item); items.append(item); } return items; -- cgit v0.12