diff options
author | Marc Mutz <marc.mutz@qt.io> | 2023-10-11 14:00:10 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2023-10-12 21:38:54 +0200 |
commit | 05888490db0c331bea8dc31e4c9160b51d697b0a (patch) | |
tree | a2e55ddc266265e89b249cfb6ef6e026fdedb72e /src/corelib/tools/qset.h | |
parent | 4a7c76d4a5365343178ea29c8a8e9b8d8acb89b5 (diff) |
QSet: de-pessimize binary operators
Overload the binary QSet operators |, &, + and - for rvalue LHSs, so
chained operations like e.g. in qgesturemanager.cpp:
QSet<QGesture *> 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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qset.h')
-rw-r--r-- | src/corelib/tools/qset.h | 20 |
1 files changed, 12 insertions, 8 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<T> &operator+=(const T &value) { insert(value); return *this; } inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; } inline QSet<T> &operator-=(const T &value) { remove(value); return *this; } - inline QSet<T> operator|(const QSet<T> &other) const - { QSet<T> result = *this; result |= other; return result; } - inline QSet<T> operator&(const QSet<T> &other) const - { QSet<T> result = *this; result &= other; return result; } - inline QSet<T> operator+(const QSet<T> &other) const - { QSet<T> result = *this; result += other; return result; } - inline QSet<T> operator-(const QSet<T> &other) const - { QSet<T> 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<T> values() const; |