QTableView: replace QLinkedList with std::list

The object is never copied, so there's no point in using a cow'ed list implementation.

Apart from the usual API adaptions (isEmpty() -> empty(), append() -> push_back()),
alse replaced two foreach-loops with ranged-for. The first one does not call into
out-of-line functions, and doesn't modify the container it iterates over, so is
safe.

The second does call into out-of-line functions, but they are const. The loop does
not modify the container it iterates over, either, so is also safe (except for some
fishy const_cast somewhere, or const being lost due to shallowness of const).

Also replaced explicit-iterator loops with ranged-for where possible.

Change-Id: I60b0f2d356846d527bfbaa6a0ecbb8395013b852
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2019-04-30 15:34:08 +02:00
parent b703279235
commit 68eea0196e
2 changed files with 22 additions and 21 deletions

View File

@ -58,6 +58,8 @@
#include <qaccessible.h> #include <qaccessible.h>
#endif #endif
#include <algorithm>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
/** \internal /** \internal
@ -65,7 +67,7 @@ QT_BEGIN_NAMESPACE
*/ */
void QSpanCollection::addSpan(QSpanCollection::Span *span) void QSpanCollection::addSpan(QSpanCollection::Span *span)
{ {
spans.append(span); spans.push_back(span);
Index::iterator it_y = index.lowerBound(-span->top()); Index::iterator it_y = index.lowerBound(-span->top());
if (it_y == index.end() || it_y.key() != -span->top()) { if (it_y == index.end() || it_y.key() != -span->top()) {
//there is no spans that starts with the row in the index, so create a sublist for it. //there is no spans that starts with the row in the index, so create a sublist for it.
@ -132,7 +134,7 @@ void QSpanCollection::updateSpan(QSpanCollection::Span *span, int old_height)
} }
if (span->width() == 0 && span->height() == 0) { if (span->width() == 0 && span->height() == 0) {
spans.removeOne(span); spans.remove(span);
delete span; delete span;
} }
} }
@ -212,15 +214,14 @@ void QSpanCollection::updateInsertedRows(int start, int end)
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug() << start << end << Qt::endl << index; qDebug() << start << end << Qt::endl << index;
#endif #endif
if (spans.isEmpty()) if (spans.empty())
return; return;
int delta = end - start + 1; int delta = end - start + 1;
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug("Before"); qDebug("Before");
#endif #endif
for (SpanList::iterator it = spans.begin(); it != spans.end(); ++it) { for (Span *span : spans) {
Span *span = *it;
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug() << span << *span; qDebug() << span << *span;
#endif #endif
@ -260,15 +261,14 @@ void QSpanCollection::updateInsertedColumns(int start, int end)
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug() << start << end << Qt::endl << index; qDebug() << start << end << Qt::endl << index;
#endif #endif
if (spans.isEmpty()) if (spans.empty())
return; return;
int delta = end - start + 1; int delta = end - start + 1;
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug("Before"); qDebug("Before");
#endif #endif
for (SpanList::iterator it = spans.begin(); it != spans.end(); ++it) { for (Span *span : spans) {
Span *span = *it;
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug() << span << *span; qDebug() << span << *span;
#endif #endif
@ -341,7 +341,7 @@ void QSpanCollection::updateRemovedRows(int start, int end)
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug() << start << end << Qt::endl << index; qDebug() << start << end << Qt::endl << index;
#endif #endif
if (spans.isEmpty()) if (spans.empty())
return; return;
SpanList spansToBeDeleted; SpanList spansToBeDeleted;
@ -377,7 +377,7 @@ void QSpanCollection::updateRemovedRows(int start, int end)
if (span->m_top == span->m_bottom && span->m_left == span->m_right) if (span->m_top == span->m_bottom && span->m_left == span->m_right)
span->will_be_deleted = true; span->will_be_deleted = true;
if (span->will_be_deleted) { if (span->will_be_deleted) {
spansToBeDeleted.append(span); spansToBeDeleted.push_back(span);
it = spans.erase(it); it = spans.erase(it);
} else { } else {
++it; ++it;
@ -389,7 +389,7 @@ void QSpanCollection::updateRemovedRows(int start, int end)
foreach (QSpanCollection::Span *span, spans) foreach (QSpanCollection::Span *span, spans)
qDebug() << span << *span; qDebug() << span << *span;
#endif #endif
if (spans.isEmpty()) { if (spans.empty()) {
qDeleteAll(spansToBeDeleted); qDeleteAll(spansToBeDeleted);
index.clear(); index.clear();
return; return;
@ -468,7 +468,7 @@ void QSpanCollection::updateRemovedColumns(int start, int end)
#ifdef DEBUG_SPAN_UPDATE #ifdef DEBUG_SPAN_UPDATE
qDebug() << start << end << Qt::endl << index; qDebug() << start << end << Qt::endl << index;
#endif #endif
if (spans.isEmpty()) if (spans.empty())
return; return;
SpanList toBeDeleted; SpanList toBeDeleted;
@ -504,7 +504,7 @@ void QSpanCollection::updateRemovedColumns(int start, int end)
if (span->m_top == span->m_bottom && span->m_left == span->m_right) if (span->m_top == span->m_bottom && span->m_left == span->m_right)
span->will_be_deleted = true; span->will_be_deleted = true;
if (span->will_be_deleted) { if (span->will_be_deleted) {
toBeDeleted.append(span); toBeDeleted.push_back(span);
it = spans.erase(it); it = spans.erase(it);
} else { } else {
++it; ++it;
@ -516,7 +516,7 @@ void QSpanCollection::updateRemovedColumns(int start, int end)
foreach (QSpanCollection::Span *span, spans) foreach (QSpanCollection::Span *span, spans)
qDebug() << span << *span; qDebug() << span << *span;
#endif #endif
if (spans.isEmpty()) { if (spans.empty()) {
qDeleteAll(toBeDeleted); qDeleteAll(toBeDeleted);
index.clear(); index.clear();
return; return;
@ -552,13 +552,13 @@ bool QSpanCollection::checkConsistency() const
for (SubIndex::const_iterator it = subIndex.begin(); it != subIndex.end(); ++it) { for (SubIndex::const_iterator it = subIndex.begin(); it != subIndex.end(); ++it) {
int x = -it.key(); int x = -it.key();
Span *span = it.value(); Span *span = it.value();
if (!spans.contains(span) || span->left() != x const bool contains = std::find(spans.begin(), spans.end(), span) != spans.end();
|| y < span->top() || y > span->bottom()) if (!contains || span->left() != x || y < span->top() || y > span->bottom())
return false; return false;
} }
} }
foreach (const Span *span, spans) { for (const Span *span : spans) {
if (span->width() < 1 || span->height() < 1 if (span->width() < 1 || span->height() < 1
|| (span->width() == 1 && span->height() == 1)) || (span->width() == 1 && span->height() == 1))
return false; return false;
@ -1923,7 +1923,7 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF
int right = qMax(d->visualColumn(tl.column()), d->visualColumn(br.column())); int right = qMax(d->visualColumn(tl.column()), d->visualColumn(br.column()));
do { do {
expanded = false; expanded = false;
foreach (QSpanCollection::Span *it, d->spans.spans) { for (QSpanCollection::Span *it : d->spans.spans) {
const QSpanCollection::Span &span = *it; const QSpanCollection::Span &span = *it;
int t = d->visualRow(span.top()); int t = d->visualRow(span.top());
int l = d->visualColumn(span.left()); int l = d->visualColumn(span.left());

View File

@ -53,12 +53,13 @@
#include <QtWidgets/private/qtwidgetsglobal_p.h> #include <QtWidgets/private/qtwidgetsglobal_p.h>
#include <QtCore/QList> #include <QtCore/QList>
#include <QtCore/QLinkedList>
#include <QtCore/QMap> #include <QtCore/QMap>
#include <QtCore/QSet> #include <QtCore/QSet>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include "private/qabstractitemview_p.h" #include "private/qabstractitemview_p.h"
#include <list>
QT_REQUIRE_CONFIG(tableview); QT_REQUIRE_CONFIG(tableview);
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -115,7 +116,7 @@ public:
bool checkConsistency() const; bool checkConsistency() const;
#endif #endif
typedef QLinkedList<Span *> SpanList; typedef std::list<Span *> SpanList;
SpanList spans; //lists of all spans SpanList spans; //lists of all spans
private: private:
//the indexes are negative so the QMap::lowerBound do what i need. //the indexes are negative so the QMap::lowerBound do what i need.
@ -210,7 +211,7 @@ public:
return span(row, column).width(); return span(row, column).width();
} }
inline bool hasSpans() const { inline bool hasSpans() const {
return !spans.spans.isEmpty(); return !spans.spans.empty();
} }
inline int rowSpanHeight(int row, int span) const { inline int rowSpanHeight(int row, int span) const {
return sectionSpanSize(verticalHeader, row, span); return sectionSpanSize(verticalHeader, row, span);