From 9eff0dd19d6507ec400f036dd27efe960e4e5f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Martins?= Date: Mon, 25 May 2015 18:32:35 +0100 Subject: [PATCH] QSet: Introduce intersects(). The pattern "mySet.intersect(other).isEmpty()" has been spotted in the wild and in Qt codebase. intersects() is much cheaper because it bails out as soon as we find one common item and doesn't do any allocations. [ChangeLog][QtCore][QSet] Added intersects(). Change-Id: I44a350dc4cdb9deb835a23eee99fc99d6ca24c82 Reviewed-by: Marc Mutz --- src/corelib/tools/qhash.h | 1 + src/corelib/tools/qset.h | 30 ++++++++++++++++++++++ src/corelib/tools/qset.qdoc | 12 ++++++++- tests/auto/corelib/tools/qset/tst_qset.cpp | 27 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 3d713146fe..2080a22e23 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -353,6 +353,7 @@ public: class const_iterator { friend class iterator; + friend class QSet; QHashData::Node *i; public: diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index e4688711d6..5a9c75fe07 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -137,6 +137,7 @@ public: typedef QHash Hash; typename Hash::const_iterator i; friend class iterator; + friend class QSet; public: typedef std::bidirectional_iterator_tag iterator_category; @@ -191,6 +192,7 @@ public: inline const_iterator constFind(const T &value) const { return find(value); } QSet &unite(const QSet &other); QSet &intersect(const QSet &other); + bool intersects(const QSet &other) const; QSet &subtract(const QSet &other); // STL compatibility @@ -283,6 +285,34 @@ Q_INLINE_TEMPLATE QSet &QSet::intersect(const QSet &other) return *this; } +template +Q_INLINE_TEMPLATE bool QSet::intersects(const QSet &other) const +{ + const bool otherIsBigger = other.size() > size(); + const QSet &smallestSet = otherIsBigger ? *this : other; + const QSet &biggestSet = otherIsBigger ? other : *this; + const bool equalSeeds = q_hash.d->seed == other.q_hash.d->seed; + typename QSet::const_iterator i = smallestSet.cbegin(); + typename QSet::const_iterator e = smallestSet.cend(); + + if (Q_LIKELY(equalSeeds)) { + // If seeds are equal we take the fast path so no hash is recalculated. + while (i != e) { + if (*biggestSet.q_hash.findNode(*i, i.i.i->h) != biggestSet.q_hash.e) + return true; + ++i; + } + } else { + while (i != e) { + if (biggestSet.contains(*i)) + return true; + ++i; + } + } + + return false; +} + template Q_INLINE_TEMPLATE QSet &QSet::subtract(const QSet &other) { diff --git a/src/corelib/tools/qset.qdoc b/src/corelib/tools/qset.qdoc index 94cfa729f5..c4f33deed6 100644 --- a/src/corelib/tools/qset.qdoc +++ b/src/corelib/tools/qset.qdoc @@ -490,7 +490,17 @@ Removes all items from this set that are not contained in the \a other set. A reference to this set is returned. - \sa operator&=(), unite(), subtract() + \sa intersects(), operator&=(), unite(), subtract() +*/ + +/*! + \fn bool QSet::intersects(const QSet &other) const + \since 5.6 + + Returns \c true if this set has at least one item in common with + \a other. + + \sa contains(), intersect() */ /*! diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index f13d69514a..ef0ebabd66 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -73,6 +73,7 @@ private slots: void makeSureTheComfortFunctionsCompile(); void initializerList(); void qhash(); + void intersects(); }; struct IdentityTracker { @@ -1030,6 +1031,32 @@ void tst_QSet::qhash() } } +void tst_QSet::intersects() +{ + QSet s1; + QSet s2; + + QVERIFY(!s1.intersects(s1)); + QVERIFY(!s1.intersects(s2)); + + s1 << 100; + QVERIFY(s1.intersects(s1)); + QVERIFY(!s1.intersects(s2)); + + s2 << 200; + QVERIFY(!s1.intersects(s2)); + + s1 << 200; + QVERIFY(s1.intersects(s2)); + + const QtQHashSeedSaver seedSaver(0x10101010); + QSet s3; + s3 << 500; + QVERIFY(!s1.intersects(s3)); + s3 << 200; + QVERIFY(s1.intersects(s3)); +} + QTEST_APPLESS_MAIN(tst_QSet) #include "tst_qset.moc"