diff options
Diffstat (limited to 'src/corelib/tools/qset.h')
-rw-r--r-- | src/corelib/tools/qset.h | 86 |
1 files changed, 30 insertions, 56 deletions
diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index 681ce9cbe2..7330b5e91c 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QSET_H #define QSET_H @@ -71,12 +35,17 @@ public: inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); } +#ifndef Q_QDOC template <typename U = T> - QTypeTraits::compare_eq_result<U> operator==(const QSet<T> &other) const + QTypeTraits::compare_eq_result_container<QSet, U> operator==(const QSet<T> &other) const { return q_hash == other.q_hash; } template <typename U = T> - QTypeTraits::compare_eq_result<U> operator!=(const QSet<T> &other) const + QTypeTraits::compare_eq_result_container<QSet, U> operator!=(const QSet<T> &other) const { return q_hash != other.q_hash; } +#else + bool operator==(const QSet &other) const; + bool operator!=(const QSet &other) const; +#endif inline qsizetype size() const { return q_hash.size(); } @@ -182,7 +151,7 @@ public: // more Qt typedef iterator Iterator; typedef const_iterator ConstIterator; - inline qsizetype count() const { return q_hash.count(); } + inline qsizetype count() const { return q_hash.size(); } inline iterator insert(const T &value) { return q_hash.insert(value, QHashDummyValue()); } inline iterator insert(T &&value) @@ -220,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; @@ -235,12 +208,10 @@ private: Hash q_hash; }; -#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 template <typename InputIterator, typename ValueType = typename std::iterator_traits<InputIterator>::value_type, QtPrivate::IfIsInputIterator<InputIterator> = true> QSet(InputIterator, InputIterator) -> QSet<ValueType>; -#endif template <typename T> size_t qHash(const QSet<T> &key, size_t seed = 0) @@ -257,10 +228,13 @@ Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize) template <class T> Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other) { - if (!q_hash.isSharedWith(other.q_hash)) { - for (const T &e : other) - insert(e); - } + if (q_hash.isSharedWith(other.q_hash)) + return *this; + QSet<T> tmp = other; + if (size() < other.size()) + swap(tmp); + for (const auto &e : std::as_const(tmp)) + insert(e); return *this; } @@ -277,7 +251,7 @@ Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other) copy2 = *this; *this = copy1; } - for (const auto &e : qAsConst(copy1)) { + for (const auto &e : std::as_const(copy1)) { if (!copy2.contains(e)) remove(e); } |