From e1c9276c778b66d5f7fd71f01c26338f3d6f2c5e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 2 Jul 2019 10:16:43 +0200 Subject: Optimize QSet set operations Replace the identity check with a check for the underlying QHash objects being shared and replace backwards iteration, which is really forwards iteration with wrapping at bucket boundaries, with forward iteration. QSet cannot contain duplicates, so the order in which the RHS elements are presented to the algorithms does not matter. Change-Id: Iad8528e3a9501b14cb85601b221a848aad91480c Reviewed-by: Lars Knoll --- src/corelib/tools/qset.h | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/corelib/tools/qset.h') diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index 83e574bf1c..98caabfb57 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -284,11 +284,9 @@ Q_INLINE_TEMPLATE void QSet::reserve(int asize) { q_hash.reserve(asize); } template Q_INLINE_TEMPLATE QSet &QSet::unite(const QSet &other) { - QSet copy(other); - typename QSet::const_iterator i = copy.constEnd(); - while (i != copy.constBegin()) { - --i; - insert(*i); + if (!q_hash.isSharedWith(other.q_hash)) { + for (const T &e : other) + insert(e); } return *this; } @@ -306,11 +304,9 @@ Q_INLINE_TEMPLATE QSet &QSet::intersect(const QSet &other) copy2 = *this; *this = copy1; } - typename QSet::const_iterator i = copy1.constEnd(); - while (i != copy1.constBegin()) { - --i; - if (!copy2.contains(*i)) - remove(*i); + for (const auto &e : qAsConst(copy1)) { + if (!copy2.contains(e)) + remove(e); } return *this; } @@ -346,14 +342,11 @@ Q_INLINE_TEMPLATE bool QSet::intersects(const QSet &other) const template Q_INLINE_TEMPLATE QSet &QSet::subtract(const QSet &other) { - if (&other == this) { + if (q_hash.isSharedWith(other.q_hash)) { clear(); } else { - auto i = other.constEnd(); - while (i != other.constBegin()) { - --i; - remove(*i); - } + for (const auto &e : other) + remove(e); } return *this; } -- cgit v1.2.3