diff options
Diffstat (limited to 'src/corelib/tools/qmap.h')
-rw-r--r-- | src/corelib/tools/qmap.h | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index f2f1bc96f4..7ee0be1e51 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -1,46 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> -** Copyright (C) 2021 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) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> +// Copyright (C) 2021 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 QMAP_H #define QMAP_H +#include <QtCore/qhashfunctions.h> #include <QtCore/qiterator.h> #include <QtCore/qlist.h> #include <QtCore/qrefcount.h> @@ -238,7 +203,7 @@ public: void swap(QMap<Key, T> &other) noexcept { - qSwap(d, other.d); + d.swap(other.d); } QMap(std::initializer_list<std::pair<Key, T>> list) @@ -276,7 +241,7 @@ public: return {}; } -#ifndef Q_CLANG_QDOC +#ifndef Q_QDOC template <typename AKey = Key, typename AT = T> friend QTypeTraits::compare_eq_result_container<QMap, AKey, AT> operator==(const QMap &lhs, const QMap &rhs) { @@ -297,7 +262,7 @@ public: #else friend bool operator==(const QMap &lhs, const QMap &rhs); friend bool operator!=(const QMap &lhs, const QMap &rhs); -#endif // Q_CLANG_QDOC +#endif // Q_QDOC size_type size() const { return d ? size_type(d->m.size()) : size_type(0); } @@ -646,6 +611,10 @@ public: const_key_value_iterator constKeyValueBegin() const { return const_key_value_iterator(begin()); } const_key_value_iterator keyValueEnd() const { return const_key_value_iterator(end()); } const_key_value_iterator constKeyValueEnd() const { return const_key_value_iterator(end()); } + auto asKeyValueRange() & { return QtPrivate::QKeyValueRange(*this); } + auto asKeyValueRange() const & { return QtPrivate::QKeyValueRange(*this); } + auto asKeyValueRange() && { return QtPrivate::QKeyValueRange(std::move(*this)); } + auto asKeyValueRange() const && { return QtPrivate::QKeyValueRange(std::move(*this)); } iterator erase(const_iterator it) { @@ -794,7 +763,7 @@ public: return isEmpty(); } - QPair<iterator, iterator> equal_range(const Key &akey) + std::pair<iterator, iterator> equal_range(const Key &akey) { const auto copy = d.isShared() ? *this : QMap(); // keep `key` alive across the detach detach(); @@ -802,13 +771,38 @@ public: return {iterator(result.first), iterator(result.second)}; } - QPair<const_iterator, const_iterator> equal_range(const Key &akey) const + std::pair<const_iterator, const_iterator> equal_range(const Key &akey) const { if (!d) return {}; auto result = d->m.equal_range(akey); return {const_iterator(result.first), const_iterator(result.second)}; } + +private: +#ifdef Q_QDOC + friend size_t qHash(const QMap &key, size_t seed = 0); +#else +# if defined(Q_CC_GHS) || defined (Q_CC_MSVC) + // GHS and MSVC tries to intantiate qHash() for the noexcept running into a + // non-SFINAE'ed hard error... Create an artificial SFINAE context as a + // work-around: + template <typename M, std::enable_if_t<std::is_same_v<M, QMap>, bool> = true> + friend QtPrivate::QHashMultiReturnType<typename M::key_type, typename M::mapped_type> +# else + using M = QMap; + friend size_t +# endif + qHash(const M &key, size_t seed = 0) + noexcept(QHashPrivate::noexceptPairHash<typename M::key_type, typename M::mapped_type>()) + { + if (!key.d) + return seed; + // don't use qHashRange to avoid its compile-time overhead: + return std::accumulate(key.d->m.begin(), key.d->m.end(), seed, + QtPrivate::QHashCombine{}); + } +#endif // !Q_QDOC }; Q_DECLARE_ASSOCIATIVE_ITERATOR(Map) @@ -820,6 +814,7 @@ qsizetype erase_if(QMap<Key, T> &map, Predicate pred) return QtPrivate::associative_erase_if(map, pred); } + // // QMultiMap // @@ -849,7 +844,7 @@ public: void swap(QMultiMap<Key, T> &other) noexcept { - qSwap(d, other.d); + d.swap(other.d); } explicit QMultiMap(const QMap<Key, T> &other) @@ -917,7 +912,7 @@ public: return {}; } -#ifndef Q_CLANG_QDOC +#ifndef Q_QDOC template <typename AKey = Key, typename AT = T> friend QTypeTraits::compare_eq_result_container<QMultiMap, AKey, AT> operator==(const QMultiMap &lhs, const QMultiMap &rhs) { @@ -938,7 +933,7 @@ public: #else friend bool operator==(const QMultiMap &lhs, const QMultiMap &rhs); friend bool operator!=(const QMultiMap &lhs, const QMultiMap &rhs); -#endif // Q_CLANG_QDOC +#endif // Q_QDOC size_type size() const { return d ? size_type(d->m.size()) : size_type(0); } @@ -1341,6 +1336,10 @@ public: const_key_value_iterator constKeyValueBegin() const { return const_key_value_iterator(begin()); } const_key_value_iterator keyValueEnd() const { return const_key_value_iterator(end()); } const_key_value_iterator constKeyValueEnd() const { return const_key_value_iterator(end()); } + auto asKeyValueRange() & { return QtPrivate::QKeyValueRange(*this); } + auto asKeyValueRange() const & { return QtPrivate::QKeyValueRange(*this); } + auto asKeyValueRange() && { return QtPrivate::QKeyValueRange(std::move(*this)); } + auto asKeyValueRange() const && { return QtPrivate::QKeyValueRange(std::move(*this)); } iterator erase(const_iterator it) { @@ -1520,7 +1519,7 @@ public: // STL compatibility inline bool empty() const { return isEmpty(); } - QPair<iterator, iterator> equal_range(const Key &akey) + std::pair<iterator, iterator> equal_range(const Key &akey) { const auto copy = d.isShared() ? *this : QMultiMap(); // keep `key` alive across the detach detach(); @@ -1528,7 +1527,7 @@ public: return {iterator(result.first), iterator(result.second)}; } - QPair<const_iterator, const_iterator> equal_range(const Key &akey) const + std::pair<const_iterator, const_iterator> equal_range(const Key &akey) const { if (!d) return {}; |