From 05888490db0c331bea8dc31e4c9160b51d697b0a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 11 Oct 2023 14:00:10 +0200 Subject: [PATCH] QSet: de-pessimize binary operators Overload the binary QSet operators |, &, + and - for rvalue LHSs, so chained operations like e.g. in qgesturemanager.cpp: QSet endedGestures = finishedGestures + canceledGestures + undeliveredGestures + maybeToCanceledGestures; become as efficient as chained op+= calls. Make the operators hidden friends as a drive-by. [ChangeLog][QtCore][QSet] The binary operators &, |, + and - are now hidden friends, and chaining them has been made a lot more efficient. Change-Id: I55d2247b11d088eb1ef88608f89d2bf9e1daeb58 Reviewed-by: Thiago Macieira --- src/corelib/tools/qset.h | 20 ++++++++++++-------- src/corelib/tools/qset.qdoc | 21 +++++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index cdd9ceda20..b75f19f242 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -189,14 +189,18 @@ public: inline QSet &operator+=(const T &value) { insert(value); return *this; } inline QSet &operator-=(const QSet &other) { subtract(other); return *this; } inline QSet &operator-=(const T &value) { remove(value); return *this; } - inline QSet operator|(const QSet &other) const - { QSet result = *this; result |= other; return result; } - inline QSet operator&(const QSet &other) const - { QSet result = *this; result &= other; return result; } - inline QSet operator+(const QSet &other) const - { QSet result = *this; result += other; return result; } - inline QSet operator-(const QSet &other) const - { QSet result = *this; result -= other; return result; } + + friend QSet operator|(const QSet &lhs, const QSet &rhs) { return QSet(lhs) |= rhs; } + friend QSet operator|(QSet &&lhs, const QSet &rhs) { lhs |= rhs; return std::move(lhs); } + + friend QSet operator&(const QSet &lhs, const QSet &rhs) { return QSet(lhs) &= rhs; } + friend QSet operator&(QSet &&lhs, const QSet &rhs) { lhs &= rhs; return std::move(lhs); } + + friend QSet operator+(const QSet &lhs, const QSet &rhs) { return QSet(lhs) += rhs; } + friend QSet operator+(QSet &&lhs, const QSet &rhs) { lhs += rhs; return std::move(lhs); } + + friend QSet operator-(const QSet &lhs, const QSet &rhs) { return QSet(lhs) -= rhs; } + friend QSet operator-(QSet &&lhs, const QSet &rhs) { lhs -= rhs; return std::move(lhs); } QList values() const; diff --git a/src/corelib/tools/qset.qdoc b/src/corelib/tools/qset.qdoc index e0d4278935..cd233ed6c4 100644 --- a/src/corelib/tools/qset.qdoc +++ b/src/corelib/tools/qset.qdoc @@ -567,29 +567,30 @@ */ /*! - \fn template QSet QSet::operator|(const QSet &other) const - \fn template QSet QSet::operator+(const QSet &other) const + \fn template QSet QSet::operator|(const QSet &lhs, const QSet &rhs) + \fn template QSet QSet::operator|(QSet &&lhs, const QSet &rhs) + \fn template QSet QSet::operator+(const QSet &lhs, const QSet &rhs) + \fn template QSet QSet::operator+(QSet &&lhs, const QSet &rhs) - Returns a new QSet that is the union of this set and the - \a other set. + Returns a new QSet that is the union of sets \a lhs and \a rhs. \sa unite(), operator|=(), operator&(), operator-() */ /*! - \fn template QSet QSet::operator&(const QSet &other) const + \fn template QSet QSet::operator&(const QSet &lhs, const QSet &rhs) + \fn template QSet QSet::operator&(QSet &&lhs, const QSet &rhs) - Returns a new QSet that is the intersection of this set and the - \a other set. + Returns a new QSet that is the intersection of sets \a lhs and \a rhs. \sa intersect(), operator&=(), operator|(), operator-() */ /*! - \fn template QSet QSet::operator-(const QSet &other) const + \fn template QSet QSet::operator-(const QSet &lhs, const QSet &rhs) + \fn template QSet QSet::operator-(QSet &&lhs, const QSet &rhs) - Returns a new QSet that is the set difference of this set and - the \a other set, i.e., this set - \a other set. + Returns a new QSet that is the set difference of sets \a lhs and \a rhs. \sa subtract(), operator-=(), operator|(), operator&() */