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>
bb10
Marc Mutz 2023-10-11 14:00:10 +02:00
parent 4a7c76d4a5
commit 05888490db
2 changed files with 23 additions and 18 deletions

View File

@ -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;

View File

@ -567,29 +567,30 @@
*/
/*!
\fn template <class T> QSet<T> QSet<T>::operator|(const QSet<T> &other) const
\fn template <class T> QSet<T> QSet<T>::operator+(const QSet<T> &other) const
\fn template <class T> QSet<T> QSet<T>::operator|(const QSet &lhs, const QSet &rhs)
\fn template <class T> QSet<T> QSet<T>::operator|(QSet &&lhs, const QSet &rhs)
\fn template <class T> QSet<T> QSet<T>::operator+(const QSet &lhs, const QSet &rhs)
\fn template <class T> QSet<T> QSet<T>::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 <class T> QSet<T> QSet<T>::operator&(const QSet<T> &other) const
\fn template <class T> QSet<T> QSet<T>::operator&(const QSet &lhs, const QSet &rhs)
\fn template <class T> QSet<T> QSet<T>::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 <class T> QSet<T> QSet<T>::operator-(const QSet<T> &other) const
\fn template <class T> QSet<T> QSet<T>::operator-(const QSet &lhs, const QSet &rhs)
\fn template <class T> QSet<T> QSet<T>::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&()
*/