2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
2014-03-17 14:44:29 +00:00
|
|
|
** Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
|
2016-01-15 07:08:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** 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.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QHASH_H
|
|
|
|
#define QHASH_H
|
|
|
|
|
|
|
|
#include <QtCore/qchar.h>
|
|
|
|
#include <QtCore/qiterator.h>
|
|
|
|
#include <QtCore/qlist.h>
|
2011-09-09 09:18:28 +00:00
|
|
|
#include <QtCore/qrefcount.h>
|
2015-04-04 18:46:05 +00:00
|
|
|
#include <QtCore/qhashfunctions.h>
|
2016-03-03 19:20:42 +00:00
|
|
|
#include <QtCore/qcontainertools_impl.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-04-25 14:58:57 +00:00
|
|
|
#include <algorithm>
|
2019-04-04 16:13:32 +00:00
|
|
|
#include <initializer_list>
|
2017-04-25 14:58:57 +00:00
|
|
|
|
2014-03-07 12:18:31 +00:00
|
|
|
#if defined(Q_CC_MSVC)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4311 ) // disable pointer truncation warning
|
|
|
|
#pragma warning( disable : 4127 ) // conditional expression is constant
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
struct Q_CORE_EXPORT QHashData
|
|
|
|
{
|
|
|
|
struct Node {
|
|
|
|
Node *next;
|
|
|
|
uint h;
|
|
|
|
};
|
|
|
|
|
|
|
|
Node *fakeNext;
|
|
|
|
Node **buckets;
|
2011-09-09 09:18:28 +00:00
|
|
|
QtPrivate::RefCount ref;
|
2011-04-27 10:05:43 +00:00
|
|
|
int size;
|
|
|
|
int nodeSize;
|
|
|
|
short userNumBits;
|
|
|
|
short numBits;
|
|
|
|
int numBuckets;
|
2012-03-24 08:36:52 +00:00
|
|
|
uint seed;
|
2011-04-27 10:05:43 +00:00
|
|
|
uint sharable : 1;
|
|
|
|
uint strictAlignment : 1;
|
|
|
|
uint reserved : 30;
|
|
|
|
|
|
|
|
void *allocateNode(int nodeAlign);
|
|
|
|
void freeNode(void *node);
|
2011-09-09 10:45:18 +00:00
|
|
|
QHashData *detach_helper(void (*node_duplicate)(Node *, void *), void (*node_delete)(Node *),
|
|
|
|
int nodeSize, int nodeAlign);
|
2011-04-27 10:05:43 +00:00
|
|
|
bool willGrow();
|
|
|
|
void hasShrunk();
|
|
|
|
void rehash(int hint);
|
|
|
|
void free_helper(void (*node_delete)(Node *));
|
|
|
|
Node *firstNode();
|
|
|
|
#ifdef QT_QHASH_DEBUG
|
|
|
|
void dump();
|
|
|
|
void checkSanity();
|
|
|
|
#endif
|
|
|
|
static Node *nextNode(Node *node);
|
|
|
|
static Node *previousNode(Node *node);
|
|
|
|
|
2011-09-09 09:18:28 +00:00
|
|
|
static const QHashData shared_null;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool QHashData::willGrow()
|
|
|
|
{
|
|
|
|
if (size >= numBuckets) {
|
|
|
|
rehash(numBits + 1);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void QHashData::hasShrunk()
|
|
|
|
{
|
|
|
|
if (size <= (numBuckets >> 3) && numBits > userNumBits) {
|
|
|
|
QT_TRY {
|
|
|
|
rehash(qMax(int(numBits) - 2, int(userNumBits)));
|
|
|
|
} QT_CATCH(const std::bad_alloc &) {
|
|
|
|
// ignore bad allocs - shrinking shouldn't throw. rehash is exception safe.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QHashData::Node *QHashData::firstNode()
|
|
|
|
{
|
|
|
|
Node *e = reinterpret_cast<Node *>(this);
|
|
|
|
Node **bucket = buckets;
|
|
|
|
int n = numBuckets;
|
|
|
|
while (n--) {
|
|
|
|
if (*bucket != e)
|
|
|
|
return *bucket;
|
|
|
|
++bucket;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct QHashDummyValue
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2019-07-08 19:55:14 +00:00
|
|
|
constexpr bool operator==(const QHashDummyValue &, const QHashDummyValue &) noexcept
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_DECLARE_TYPEINFO(QHashDummyValue, Q_MOVABLE_TYPE | Q_DUMMY_TYPE);
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2012-04-30 09:01:05 +00:00
|
|
|
struct QHashNode
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-04-30 09:01:05 +00:00
|
|
|
QHashNode *next;
|
2012-11-10 16:32:49 +00:00
|
|
|
const uint h;
|
|
|
|
const Key key;
|
2012-04-30 09:01:05 +00:00
|
|
|
T value;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-04-30 09:01:05 +00:00
|
|
|
inline QHashNode(const Key &key0, const T &value0, uint hash, QHashNode *n)
|
|
|
|
: next(n), h(hash), key(key0), value(value0) {}
|
2012-11-10 16:32:49 +00:00
|
|
|
inline bool same_key(uint h0, const Key &key0) const { return h0 == h && key0 == key; }
|
2014-03-07 12:18:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QHashNode)
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2014-07-03 11:38:59 +00:00
|
|
|
// Specialize for QHashDummyValue in order to save some memory
|
|
|
|
template <class Key>
|
|
|
|
struct QHashNode<Key, QHashDummyValue>
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2014-07-03 11:38:59 +00:00
|
|
|
union {
|
|
|
|
QHashNode *next;
|
|
|
|
QHashDummyValue value;
|
|
|
|
};
|
2012-11-10 16:32:49 +00:00
|
|
|
const uint h;
|
|
|
|
const Key key;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-07-03 11:38:59 +00:00
|
|
|
inline QHashNode(const Key &key0, const QHashDummyValue &, uint hash, QHashNode *n)
|
|
|
|
: next(n), h(hash), key(key0) {}
|
|
|
|
inline bool same_key(uint h0, const Key &key0) const { return h0 == h && key0 == key; }
|
2014-03-07 12:18:31 +00:00
|
|
|
|
|
|
|
private:
|
2014-07-03 11:38:59 +00:00
|
|
|
Q_DISABLE_COPY(QHashNode)
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2012-04-30 09:01:05 +00:00
|
|
|
|
2012-04-13 17:59:58 +00:00
|
|
|
#if 0
|
|
|
|
// ###
|
|
|
|
// The introduction of the QHash random seed breaks this optimization, as it
|
|
|
|
// relies on qHash(int i) = i. If the hash value is salted, then the hash
|
|
|
|
// table becomes corrupted.
|
|
|
|
//
|
|
|
|
// A bit more research about whether it makes sense or not to salt integer
|
|
|
|
// keys (and in general keys whose hash value is easy to invert)
|
|
|
|
// is needed, or about how keep this optimization and the seed (f.i. by
|
|
|
|
// specializing QHash for integer values, and re-apply the seed during lookup).
|
|
|
|
//
|
|
|
|
// Be aware that such changes can easily be binary incompatible, and therefore
|
|
|
|
// cannot be made during the Qt 5 lifetime.
|
2011-04-27 10:05:43 +00:00
|
|
|
#define Q_HASH_DECLARE_INT_NODES(key_type) \
|
|
|
|
template <class T> \
|
|
|
|
struct QHashDummyNode<key_type, T> { \
|
|
|
|
QHashDummyNode *next; \
|
|
|
|
union { uint h; key_type key; }; \
|
|
|
|
\
|
|
|
|
inline QHashDummyNode(key_type /* key0 */) {} \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
template <class T> \
|
|
|
|
struct QHashNode<key_type, T> { \
|
|
|
|
QHashNode *next; \
|
|
|
|
union { uint h; key_type key; }; \
|
|
|
|
T value; \
|
|
|
|
\
|
|
|
|
inline QHashNode(key_type /* key0 */) {} \
|
|
|
|
inline QHashNode(key_type /* key0 */, const T &value0) : value(value0) {} \
|
2012-11-10 16:32:49 +00:00
|
|
|
inline bool same_key(uint h0, key_type) const { return h0 == h; } \
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(Q_BYTE_ORDER) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
|
|
|
|
Q_HASH_DECLARE_INT_NODES(short);
|
|
|
|
Q_HASH_DECLARE_INT_NODES(ushort);
|
|
|
|
#endif
|
|
|
|
Q_HASH_DECLARE_INT_NODES(int);
|
|
|
|
Q_HASH_DECLARE_INT_NODES(uint);
|
|
|
|
#undef Q_HASH_DECLARE_INT_NODES
|
2012-04-13 17:59:58 +00:00
|
|
|
#endif // #if 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
class QHash
|
|
|
|
{
|
|
|
|
typedef QHashNode<Key, T> Node;
|
|
|
|
|
|
|
|
union {
|
|
|
|
QHashData *d;
|
|
|
|
QHashNode<Key, T> *e;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline Node *concrete(QHashData::Node *node) {
|
|
|
|
return reinterpret_cast<Node *>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int alignOfNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(Node)); }
|
|
|
|
|
|
|
|
public:
|
2019-04-02 08:54:54 +00:00
|
|
|
inline QHash() noexcept : d(const_cast<QHashData *>(&QHashData::shared_null)) { }
|
2013-01-10 12:45:18 +00:00
|
|
|
inline QHash(std::initializer_list<std::pair<Key,T> > list)
|
|
|
|
: d(const_cast<QHashData *>(&QHashData::shared_null))
|
|
|
|
{
|
2014-01-24 14:37:29 +00:00
|
|
|
reserve(int(list.size()));
|
2013-01-10 12:45:18 +00:00
|
|
|
for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it)
|
|
|
|
insert(it->first, it->second);
|
|
|
|
}
|
2015-01-08 23:39:34 +00:00
|
|
|
QHash(const QHash &other) : d(other.d) { d->ref.ref(); if (!d->sharable) detach(); }
|
|
|
|
~QHash() { if (!d->ref.deref()) freeData(d); }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-08 23:39:34 +00:00
|
|
|
QHash &operator=(const QHash &other);
|
2019-04-02 08:54:54 +00:00
|
|
|
QHash(QHash &&other) noexcept : d(other.d) { other.d = const_cast<QHashData *>(&QHashData::shared_null); }
|
|
|
|
QHash &operator=(QHash &&other) noexcept
|
2015-07-03 11:24:31 +00:00
|
|
|
{ QHash moved(std::move(other)); swap(moved); return *this; }
|
2016-03-03 19:20:42 +00:00
|
|
|
#ifdef Q_QDOC
|
|
|
|
template <typename InputIterator>
|
|
|
|
QHash(InputIterator f, InputIterator l);
|
|
|
|
#else
|
|
|
|
template <typename InputIterator, QtPrivate::IfAssociativeIteratorHasKeyAndValue<InputIterator> = true>
|
|
|
|
QHash(InputIterator f, InputIterator l)
|
|
|
|
: QHash()
|
|
|
|
{
|
|
|
|
QtPrivate::reserveIfForwardIterator(this, f, l);
|
|
|
|
for (; f != l; ++f)
|
|
|
|
insert(f.key(), f.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename InputIterator, QtPrivate::IfAssociativeIteratorHasFirstAndSecond<InputIterator> = true>
|
|
|
|
QHash(InputIterator f, InputIterator l)
|
|
|
|
: QHash()
|
|
|
|
{
|
|
|
|
QtPrivate::reserveIfForwardIterator(this, f, l);
|
|
|
|
for (; f != l; ++f)
|
|
|
|
insert(f->first, f->second);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
2019-04-02 08:54:54 +00:00
|
|
|
void swap(QHash &other) noexcept { qSwap(d, other.d); }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-08 23:39:34 +00:00
|
|
|
bool operator==(const QHash &other) const;
|
|
|
|
bool operator!=(const QHash &other) const { return !(*this == other); }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
inline int size() const { return d->size; }
|
|
|
|
|
|
|
|
inline bool isEmpty() const { return d->size == 0; }
|
|
|
|
|
|
|
|
inline int capacity() const { return d->numBuckets; }
|
|
|
|
void reserve(int size);
|
|
|
|
inline void squeeze() { reserve(1); }
|
|
|
|
|
2012-01-12 14:39:17 +00:00
|
|
|
inline void detach() { if (d->ref.isShared()) detach_helper(); }
|
|
|
|
inline bool isDetached() const { return !d->ref.isShared(); }
|
2016-04-13 19:36:28 +00:00
|
|
|
#if !defined(QT_NO_UNSHARABLE_CONTAINERS)
|
2011-10-03 13:21:02 +00:00
|
|
|
inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QHashData::shared_null) d->sharable = sharable; }
|
2014-03-06 21:01:20 +00:00
|
|
|
#endif
|
2015-01-08 23:39:34 +00:00
|
|
|
bool isSharedWith(const QHash &other) const { return d == other.d; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
int remove(const Key &key);
|
|
|
|
T take(const Key &key);
|
|
|
|
|
|
|
|
bool contains(const Key &key) const;
|
|
|
|
const Key key(const T &value) const;
|
|
|
|
const Key key(const T &value, const Key &defaultKey) const;
|
|
|
|
const T value(const Key &key) const;
|
|
|
|
const T value(const Key &key, const T &defaultValue) const;
|
|
|
|
T &operator[](const Key &key);
|
|
|
|
const T operator[](const Key &key) const;
|
|
|
|
|
|
|
|
QList<Key> uniqueKeys() const;
|
|
|
|
QList<Key> keys() const;
|
|
|
|
QList<Key> keys(const T &value) const;
|
|
|
|
QList<T> values() const;
|
|
|
|
QList<T> values(const Key &key) const;
|
|
|
|
int count(const Key &key) const;
|
|
|
|
|
|
|
|
class const_iterator;
|
|
|
|
|
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
friend class const_iterator;
|
2011-09-09 10:45:18 +00:00
|
|
|
friend class QHash<Key, T>;
|
2015-12-03 11:56:56 +00:00
|
|
|
friend class QSet<Key>;
|
2011-04-27 10:05:43 +00:00
|
|
|
QHashData::Node *i;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::bidirectional_iterator_tag iterator_category;
|
|
|
|
typedef qptrdiff difference_type;
|
|
|
|
typedef T value_type;
|
|
|
|
typedef T *pointer;
|
|
|
|
typedef T &reference;
|
|
|
|
|
2017-09-18 09:49:52 +00:00
|
|
|
inline iterator() : i(nullptr) { }
|
2011-04-27 10:05:43 +00:00
|
|
|
explicit inline iterator(void *node) : i(reinterpret_cast<QHashData::Node *>(node)) { }
|
|
|
|
|
|
|
|
inline const Key &key() const { return concrete(i)->key; }
|
|
|
|
inline T &value() const { return concrete(i)->value; }
|
|
|
|
inline T &operator*() const { return concrete(i)->value; }
|
|
|
|
inline T *operator->() const { return &concrete(i)->value; }
|
|
|
|
inline bool operator==(const iterator &o) const { return i == o.i; }
|
|
|
|
inline bool operator!=(const iterator &o) const { return i != o.i; }
|
|
|
|
|
|
|
|
inline iterator &operator++() {
|
|
|
|
i = QHashData::nextNode(i);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline iterator operator++(int) {
|
|
|
|
iterator r = *this;
|
|
|
|
i = QHashData::nextNode(i);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
inline iterator &operator--() {
|
|
|
|
i = QHashData::previousNode(i);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline iterator operator--(int) {
|
|
|
|
iterator r = *this;
|
|
|
|
i = QHashData::previousNode(i);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
inline iterator operator+(int j) const
|
|
|
|
{ iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
|
|
|
|
inline iterator operator-(int j) const { return operator+(-j); }
|
|
|
|
inline iterator &operator+=(int j) { return *this = *this + j; }
|
|
|
|
inline iterator &operator-=(int j) { return *this = *this - j; }
|
2018-03-05 15:59:11 +00:00
|
|
|
friend inline iterator operator+(int j, iterator k) { return k + j; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-25 09:51:48 +00:00
|
|
|
#ifndef QT_STRICT_ITERATORS
|
2011-04-27 10:05:43 +00:00
|
|
|
public:
|
|
|
|
inline bool operator==(const const_iterator &o) const
|
|
|
|
{ return i == o.i; }
|
|
|
|
inline bool operator!=(const const_iterator &o) const
|
|
|
|
{ return i != o.i; }
|
2012-05-25 09:51:48 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
friend class iterator;
|
|
|
|
|
|
|
|
class const_iterator
|
|
|
|
{
|
|
|
|
friend class iterator;
|
2015-12-03 11:56:56 +00:00
|
|
|
friend class QHash<Key, T>;
|
2015-05-25 17:32:35 +00:00
|
|
|
friend class QSet<Key>;
|
2011-04-27 10:05:43 +00:00
|
|
|
QHashData::Node *i;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::bidirectional_iterator_tag iterator_category;
|
|
|
|
typedef qptrdiff difference_type;
|
|
|
|
typedef T value_type;
|
|
|
|
typedef const T *pointer;
|
|
|
|
typedef const T &reference;
|
|
|
|
|
2017-09-18 09:49:52 +00:00
|
|
|
Q_DECL_CONSTEXPR inline const_iterator() : i(nullptr) { }
|
2011-04-27 10:05:43 +00:00
|
|
|
explicit inline const_iterator(void *node)
|
|
|
|
: i(reinterpret_cast<QHashData::Node *>(node)) { }
|
|
|
|
#ifdef QT_STRICT_ITERATORS
|
|
|
|
explicit inline const_iterator(const iterator &o)
|
|
|
|
#else
|
|
|
|
inline const_iterator(const iterator &o)
|
|
|
|
#endif
|
|
|
|
{ i = o.i; }
|
|
|
|
|
|
|
|
inline const Key &key() const { return concrete(i)->key; }
|
|
|
|
inline const T &value() const { return concrete(i)->value; }
|
|
|
|
inline const T &operator*() const { return concrete(i)->value; }
|
|
|
|
inline const T *operator->() const { return &concrete(i)->value; }
|
2017-07-13 06:50:18 +00:00
|
|
|
Q_DECL_CONSTEXPR inline bool operator==(const const_iterator &o) const { return i == o.i; }
|
|
|
|
Q_DECL_CONSTEXPR inline bool operator!=(const const_iterator &o) const { return i != o.i; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
inline const_iterator &operator++() {
|
|
|
|
i = QHashData::nextNode(i);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const_iterator operator++(int) {
|
|
|
|
const_iterator r = *this;
|
|
|
|
i = QHashData::nextNode(i);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
inline const_iterator &operator--() {
|
|
|
|
i = QHashData::previousNode(i);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const_iterator operator--(int) {
|
|
|
|
const_iterator r = *this;
|
|
|
|
i = QHashData::previousNode(i);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
inline const_iterator operator+(int j) const
|
|
|
|
{ const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
|
|
|
|
inline const_iterator operator-(int j) const { return operator+(-j); }
|
|
|
|
inline const_iterator &operator+=(int j) { return *this = *this + j; }
|
|
|
|
inline const_iterator &operator-=(int j) { return *this = *this - j; }
|
2018-03-05 15:59:11 +00:00
|
|
|
friend inline const_iterator operator+(int j, const_iterator k) { return k + j; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// ### Qt 5: not sure this is necessary anymore
|
|
|
|
#ifdef QT_STRICT_ITERATORS
|
|
|
|
private:
|
|
|
|
inline bool operator==(const iterator &o) const { return operator==(const_iterator(o)); }
|
|
|
|
inline bool operator!=(const iterator &o) const { return operator!=(const_iterator(o)); }
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
friend class const_iterator;
|
|
|
|
|
2015-06-22 16:21:34 +00:00
|
|
|
class key_iterator
|
|
|
|
{
|
|
|
|
const_iterator i;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef typename const_iterator::iterator_category iterator_category;
|
|
|
|
typedef typename const_iterator::difference_type difference_type;
|
|
|
|
typedef Key value_type;
|
|
|
|
typedef const Key *pointer;
|
|
|
|
typedef const Key &reference;
|
|
|
|
|
2017-03-16 21:07:57 +00:00
|
|
|
key_iterator() = default;
|
2015-06-22 16:21:34 +00:00
|
|
|
explicit key_iterator(const_iterator o) : i(o) { }
|
|
|
|
|
|
|
|
const Key &operator*() const { return i.key(); }
|
|
|
|
const Key *operator->() const { return &i.key(); }
|
|
|
|
bool operator==(key_iterator o) const { return i == o.i; }
|
|
|
|
bool operator!=(key_iterator o) const { return i != o.i; }
|
|
|
|
|
|
|
|
inline key_iterator &operator++() { ++i; return *this; }
|
|
|
|
inline key_iterator operator++(int) { return key_iterator(i++);}
|
|
|
|
inline key_iterator &operator--() { --i; return *this; }
|
|
|
|
inline key_iterator operator--(int) { return key_iterator(i--); }
|
|
|
|
const_iterator base() const { return i; }
|
|
|
|
};
|
|
|
|
|
2016-11-29 10:06:24 +00:00
|
|
|
typedef QKeyValueIterator<const Key&, const T&, const_iterator> const_key_value_iterator;
|
|
|
|
typedef QKeyValueIterator<const Key&, T&, iterator> key_value_iterator;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// STL style
|
|
|
|
inline iterator begin() { detach(); return iterator(d->firstNode()); }
|
|
|
|
inline const_iterator begin() const { return const_iterator(d->firstNode()); }
|
2011-08-29 15:49:48 +00:00
|
|
|
inline const_iterator cbegin() const { return const_iterator(d->firstNode()); }
|
2011-04-27 10:05:43 +00:00
|
|
|
inline const_iterator constBegin() const { return const_iterator(d->firstNode()); }
|
|
|
|
inline iterator end() { detach(); return iterator(e); }
|
|
|
|
inline const_iterator end() const { return const_iterator(e); }
|
2011-08-29 15:49:48 +00:00
|
|
|
inline const_iterator cend() const { return const_iterator(e); }
|
2011-04-27 10:05:43 +00:00
|
|
|
inline const_iterator constEnd() const { return const_iterator(e); }
|
2015-06-22 16:21:34 +00:00
|
|
|
inline key_iterator keyBegin() const { return key_iterator(begin()); }
|
|
|
|
inline key_iterator keyEnd() const { return key_iterator(end()); }
|
2016-11-29 10:06:24 +00:00
|
|
|
inline key_value_iterator keyValueBegin() { return key_value_iterator(begin()); }
|
|
|
|
inline key_value_iterator keyValueEnd() { return key_value_iterator(end()); }
|
|
|
|
inline const_key_value_iterator keyValueBegin() const { return const_key_value_iterator(begin()); }
|
|
|
|
inline const_key_value_iterator constKeyValueBegin() const { return const_key_value_iterator(begin()); }
|
|
|
|
inline const_key_value_iterator keyValueEnd() const { return const_key_value_iterator(end()); }
|
|
|
|
inline const_key_value_iterator constKeyValueEnd() const { return const_key_value_iterator(end()); }
|
2015-06-22 16:21:34 +00:00
|
|
|
|
2016-02-07 02:49:33 +00:00
|
|
|
QPair<iterator, iterator> equal_range(const Key &key);
|
2019-04-02 08:54:54 +00:00
|
|
|
QPair<const_iterator, const_iterator> equal_range(const Key &key) const noexcept;
|
2015-12-03 11:56:56 +00:00
|
|
|
iterator erase(iterator it) { return erase(const_iterator(it.i)); }
|
|
|
|
iterator erase(const_iterator it);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// more Qt
|
|
|
|
typedef iterator Iterator;
|
|
|
|
typedef const_iterator ConstIterator;
|
|
|
|
inline int count() const { return d->size; }
|
|
|
|
iterator find(const Key &key);
|
|
|
|
const_iterator find(const Key &key) const;
|
|
|
|
const_iterator constFind(const Key &key) const;
|
|
|
|
iterator insert(const Key &key, const T &value);
|
|
|
|
iterator insertMulti(const Key &key, const T &value);
|
2015-01-08 23:39:34 +00:00
|
|
|
QHash &unite(const QHash &other);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// STL compatibility
|
|
|
|
typedef T mapped_type;
|
|
|
|
typedef Key key_type;
|
|
|
|
typedef qptrdiff difference_type;
|
|
|
|
typedef int size_type;
|
|
|
|
|
|
|
|
inline bool empty() const { return isEmpty(); }
|
|
|
|
|
|
|
|
#ifdef QT_QHASH_DEBUG
|
|
|
|
inline void dump() const { d->dump(); }
|
|
|
|
inline void checkSanity() const { d->checkSanity(); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
void detach_helper();
|
|
|
|
void freeData(QHashData *d);
|
2017-09-18 09:49:52 +00:00
|
|
|
Node **findNode(const Key &key, uint *hp = nullptr) const;
|
2015-05-27 10:20:57 +00:00
|
|
|
Node **findNode(const Key &key, uint h) const;
|
2011-04-27 10:05:43 +00:00
|
|
|
Node *createNode(uint h, const Key &key, const T &value, Node **nextNode);
|
|
|
|
void deleteNode(Node *node);
|
|
|
|
static void deleteNode2(QHashData::Node *node);
|
|
|
|
|
|
|
|
static void duplicateNode(QHashData::Node *originalNode, void *newNode);
|
2013-05-03 14:13:55 +00:00
|
|
|
|
2019-04-02 08:54:54 +00:00
|
|
|
bool isValidIterator(const iterator &it) const noexcept
|
2015-12-03 11:56:56 +00:00
|
|
|
{ return isValidNode(it.i); }
|
2019-04-02 08:54:54 +00:00
|
|
|
bool isValidIterator(const const_iterator &it) const noexcept
|
2015-12-03 11:56:56 +00:00
|
|
|
{ return isValidNode(it.i); }
|
2019-04-02 08:54:54 +00:00
|
|
|
bool isValidNode(QHashData::Node *node) const noexcept
|
2013-05-03 14:13:55 +00:00
|
|
|
{
|
|
|
|
#if defined(QT_DEBUG) && !defined(Q_HASH_NO_ITERATOR_DEBUG)
|
|
|
|
while (node->next)
|
|
|
|
node = node->next;
|
2013-08-20 09:12:18 +00:00
|
|
|
return (static_cast<void *>(node) == d);
|
2013-05-03 14:13:55 +00:00
|
|
|
#else
|
2015-12-03 11:56:56 +00:00
|
|
|
Q_UNUSED(node);
|
2013-05-03 14:13:55 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
2013-05-03 04:45:10 +00:00
|
|
|
friend class QSet<Key>;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE void QHash<Key, T>::deleteNode(Node *node)
|
|
|
|
{
|
|
|
|
deleteNode2(reinterpret_cast<QHashData::Node*>(node));
|
|
|
|
d->freeNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE void QHash<Key, T>::deleteNode2(QHashData::Node *node)
|
|
|
|
{
|
|
|
|
#ifdef Q_CC_BOR
|
|
|
|
concrete(node)->~QHashNode<Key, T>();
|
|
|
|
#else
|
|
|
|
concrete(node)->~Node();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE void QHash<Key, T>::duplicateNode(QHashData::Node *node, void *newNode)
|
|
|
|
{
|
|
|
|
Node *concreteNode = concrete(node);
|
2017-09-18 09:49:52 +00:00
|
|
|
new (newNode) Node(concreteNode->key, concreteNode->value, concreteNode->h, nullptr);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE typename QHash<Key, T>::Node *
|
|
|
|
QHash<Key, T>::createNode(uint ah, const Key &akey, const T &avalue, Node **anextNode)
|
|
|
|
{
|
2014-07-03 11:38:59 +00:00
|
|
|
Node *node = new (d->allocateNode(alignOfNode())) Node(akey, avalue, ah, *anextNode);
|
2011-04-27 10:05:43 +00:00
|
|
|
*anextNode = node;
|
|
|
|
++d->size;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2015-01-08 23:39:34 +00:00
|
|
|
Q_INLINE_TEMPLATE QHash<Key, T> &QHash<Key, T>::unite(const QHash &other)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-04-25 12:43:43 +00:00
|
|
|
if (d == &QHashData::shared_null) {
|
|
|
|
*this = other;
|
|
|
|
} else {
|
|
|
|
QHash copy(other);
|
|
|
|
const_iterator it = copy.constEnd();
|
|
|
|
while (it != copy.constBegin()) {
|
|
|
|
--it;
|
|
|
|
insertMulti(it.key(), it.value());
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE void QHash<Key, T>::freeData(QHashData *x)
|
|
|
|
{
|
|
|
|
x->free_helper(deleteNode2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE void QHash<Key, T>::clear()
|
|
|
|
{
|
2015-01-08 23:39:34 +00:00
|
|
|
*this = QHash();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE void QHash<Key, T>::detach_helper()
|
|
|
|
{
|
2014-07-03 11:38:59 +00:00
|
|
|
QHashData *x = d->detach_helper(duplicateNode, deleteNode2, sizeof(Node), alignOfNode());
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!d->ref.deref())
|
|
|
|
freeData(d);
|
|
|
|
d = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2015-01-08 23:39:34 +00:00
|
|
|
Q_INLINE_TEMPLATE QHash<Key, T> &QHash<Key, T>::operator=(const QHash &other)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (d != other.d) {
|
|
|
|
QHashData *o = other.d;
|
|
|
|
o->ref.ref();
|
|
|
|
if (!d->ref.deref())
|
|
|
|
freeData(d);
|
|
|
|
d = o;
|
|
|
|
if (!d->sharable)
|
|
|
|
detach_helper();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey) const
|
|
|
|
{
|
|
|
|
Node *node;
|
|
|
|
if (d->size == 0 || (node = *findNode(akey)) == e) {
|
|
|
|
return T();
|
|
|
|
} else {
|
|
|
|
return node->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey, const T &adefaultValue) const
|
|
|
|
{
|
|
|
|
Node *node;
|
|
|
|
if (d->size == 0 || (node = *findNode(akey)) == e) {
|
|
|
|
return adefaultValue;
|
|
|
|
} else {
|
|
|
|
return node->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::uniqueKeys() const
|
|
|
|
{
|
|
|
|
QList<Key> res;
|
|
|
|
res.reserve(size()); // May be too much, but assume short lifetime
|
|
|
|
const_iterator i = begin();
|
|
|
|
if (i != end()) {
|
|
|
|
for (;;) {
|
|
|
|
const Key &aKey = i.key();
|
|
|
|
res.append(aKey);
|
|
|
|
do {
|
|
|
|
if (++i == end())
|
|
|
|
goto break_out_of_outer_loop;
|
|
|
|
} while (aKey == i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break_out_of_outer_loop:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::keys() const
|
|
|
|
{
|
|
|
|
QList<Key> res;
|
|
|
|
res.reserve(size());
|
|
|
|
const_iterator i = begin();
|
|
|
|
while (i != end()) {
|
|
|
|
res.append(i.key());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::keys(const T &avalue) const
|
|
|
|
{
|
|
|
|
QList<Key> res;
|
|
|
|
const_iterator i = begin();
|
|
|
|
while (i != end()) {
|
|
|
|
if (i.value() == avalue)
|
|
|
|
res.append(i.key());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE const Key QHash<Key, T>::key(const T &avalue) const
|
|
|
|
{
|
|
|
|
return key(avalue, Key());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE const Key QHash<Key, T>::key(const T &avalue, const Key &defaultValue) const
|
|
|
|
{
|
|
|
|
const_iterator i = begin();
|
|
|
|
while (i != end()) {
|
|
|
|
if (i.value() == avalue)
|
|
|
|
return i.key();
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values() const
|
|
|
|
{
|
|
|
|
QList<T> res;
|
|
|
|
res.reserve(size());
|
|
|
|
const_iterator i = begin();
|
|
|
|
while (i != end()) {
|
|
|
|
res.append(i.value());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values(const Key &akey) const
|
|
|
|
{
|
|
|
|
QList<T> res;
|
|
|
|
Node *node = *findNode(akey);
|
|
|
|
if (node != e) {
|
|
|
|
do {
|
|
|
|
res.append(node->value);
|
|
|
|
} while ((node = node->next) != e && node->key == akey);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE int QHash<Key, T>::count(const Key &akey) const
|
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
Node *node = *findNode(akey);
|
|
|
|
if (node != e) {
|
|
|
|
do {
|
|
|
|
++cnt;
|
|
|
|
} while ((node = node->next) != e && node->key == akey);
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE const T QHash<Key, T>::operator[](const Key &akey) const
|
|
|
|
{
|
|
|
|
return value(akey);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE T &QHash<Key, T>::operator[](const Key &akey)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
|
|
|
|
uint h;
|
|
|
|
Node **node = findNode(akey, &h);
|
|
|
|
if (*node == e) {
|
|
|
|
if (d->willGrow())
|
2016-11-09 13:36:08 +00:00
|
|
|
node = findNode(akey, h);
|
2011-04-27 10:05:43 +00:00
|
|
|
return createNode(h, akey, T(), node)->value;
|
|
|
|
}
|
|
|
|
return (*node)->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insert(const Key &akey,
|
|
|
|
const T &avalue)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
|
|
|
|
uint h;
|
|
|
|
Node **node = findNode(akey, &h);
|
|
|
|
if (*node == e) {
|
|
|
|
if (d->willGrow())
|
2016-11-09 13:36:08 +00:00
|
|
|
node = findNode(akey, h);
|
2011-04-27 10:05:43 +00:00
|
|
|
return iterator(createNode(h, akey, avalue, node));
|
|
|
|
}
|
|
|
|
|
2016-08-05 08:12:12 +00:00
|
|
|
if (!std::is_same<T, QHashDummyValue>::value)
|
2011-04-27 10:05:43 +00:00
|
|
|
(*node)->value = avalue;
|
|
|
|
return iterator(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insertMulti(const Key &akey,
|
|
|
|
const T &avalue)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
d->willGrow();
|
|
|
|
|
|
|
|
uint h;
|
|
|
|
Node **nextNode = findNode(akey, &h);
|
|
|
|
return iterator(createNode(h, akey, avalue, nextNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE int QHash<Key, T>::remove(const Key &akey)
|
|
|
|
{
|
|
|
|
if (isEmpty()) // prevents detaching shared null
|
|
|
|
return 0;
|
|
|
|
detach();
|
|
|
|
|
|
|
|
int oldSize = d->size;
|
|
|
|
Node **node = findNode(akey);
|
|
|
|
if (*node != e) {
|
|
|
|
bool deleteNext = true;
|
|
|
|
do {
|
|
|
|
Node *next = (*node)->next;
|
|
|
|
deleteNext = (next != e && next->key == (*node)->key);
|
|
|
|
deleteNode(*node);
|
|
|
|
*node = next;
|
|
|
|
--d->size;
|
|
|
|
} while (deleteNext);
|
|
|
|
d->hasShrunk();
|
|
|
|
}
|
|
|
|
return oldSize - d->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE T QHash<Key, T>::take(const Key &akey)
|
|
|
|
{
|
|
|
|
if (isEmpty()) // prevents detaching shared null
|
|
|
|
return T();
|
|
|
|
detach();
|
|
|
|
|
|
|
|
Node **node = findNode(akey);
|
|
|
|
if (*node != e) {
|
2017-05-21 18:18:34 +00:00
|
|
|
T t = std::move((*node)->value);
|
2011-04-27 10:05:43 +00:00
|
|
|
Node *next = (*node)->next;
|
|
|
|
deleteNode(*node);
|
|
|
|
*node = next;
|
|
|
|
--d->size;
|
|
|
|
d->hasShrunk();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2015-12-03 11:56:56 +00:00
|
|
|
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::erase(const_iterator it)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-08-27 15:10:02 +00:00
|
|
|
Q_ASSERT_X(isValidIterator(it), "QHash::erase", "The specified iterator argument 'it' is invalid");
|
2013-05-03 14:13:55 +00:00
|
|
|
|
2015-12-03 11:56:56 +00:00
|
|
|
if (it == const_iterator(e))
|
|
|
|
return iterator(it.i);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-12 17:17:33 +00:00
|
|
|
if (d->ref.isShared()) {
|
2015-12-03 11:56:56 +00:00
|
|
|
// save 'it' across the detach:
|
2013-07-12 17:17:33 +00:00
|
|
|
int bucketNum = (it.i->h % d->numBuckets);
|
2015-12-03 11:56:56 +00:00
|
|
|
const_iterator bucketIterator(*(d->buckets + bucketNum));
|
2013-07-12 17:17:33 +00:00
|
|
|
int stepsFromBucketStartToIte = 0;
|
|
|
|
while (bucketIterator != it) {
|
|
|
|
++stepsFromBucketStartToIte;
|
|
|
|
++bucketIterator;
|
|
|
|
}
|
|
|
|
detach();
|
2015-12-03 11:56:56 +00:00
|
|
|
it = const_iterator(*(d->buckets + bucketNum));
|
2013-07-12 17:17:33 +00:00
|
|
|
while (stepsFromBucketStartToIte > 0) {
|
|
|
|
--stepsFromBucketStartToIte;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 11:56:56 +00:00
|
|
|
iterator ret(it.i);
|
2011-04-27 10:05:43 +00:00
|
|
|
++ret;
|
|
|
|
|
2011-09-09 10:45:18 +00:00
|
|
|
Node *node = concrete(it.i);
|
2011-04-27 10:05:43 +00:00
|
|
|
Node **node_ptr = reinterpret_cast<Node **>(&d->buckets[node->h % d->numBuckets]);
|
|
|
|
while (*node_ptr != node)
|
|
|
|
node_ptr = &(*node_ptr)->next;
|
|
|
|
*node_ptr = node->next;
|
|
|
|
deleteNode(node);
|
|
|
|
--d->size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE void QHash<Key, T>::reserve(int asize)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
d->rehash(-qMax(asize, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE typename QHash<Key, T>::const_iterator QHash<Key, T>::find(const Key &akey) const
|
|
|
|
{
|
|
|
|
return const_iterator(*findNode(akey));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE typename QHash<Key, T>::const_iterator QHash<Key, T>::constFind(const Key &akey) const
|
|
|
|
{
|
|
|
|
return const_iterator(*findNode(akey));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::find(const Key &akey)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
return iterator(*findNode(akey));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE bool QHash<Key, T>::contains(const Key &akey) const
|
|
|
|
{
|
|
|
|
return *findNode(akey) != e;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2015-05-27 10:20:57 +00:00
|
|
|
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::Node **QHash<Key, T>::findNode(const Key &akey, uint h) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
Node **node;
|
|
|
|
|
|
|
|
if (d->numBuckets) {
|
|
|
|
node = reinterpret_cast<Node **>(&d->buckets[h % d->numBuckets]);
|
|
|
|
Q_ASSERT(*node == e || (*node)->next);
|
|
|
|
while (*node != e && !(*node)->same_key(h, akey))
|
|
|
|
node = &(*node)->next;
|
|
|
|
} else {
|
|
|
|
node = const_cast<Node **>(reinterpret_cast<const Node * const *>(&e));
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2015-05-27 10:20:57 +00:00
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::Node **QHash<Key, T>::findNode(const Key &akey,
|
|
|
|
uint *ahp) const
|
|
|
|
{
|
|
|
|
uint h = 0;
|
|
|
|
|
|
|
|
if (d->numBuckets || ahp) {
|
|
|
|
h = qHash(akey, d->seed);
|
|
|
|
if (ahp)
|
|
|
|
*ahp = h;
|
|
|
|
}
|
|
|
|
return findNode(akey, h);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
template <class Key, class T>
|
2015-01-08 23:39:34 +00:00
|
|
|
Q_OUTOFLINE_TEMPLATE bool QHash<Key, T>::operator==(const QHash &other) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (d == other.d)
|
|
|
|
return true;
|
2019-07-10 17:40:06 +00:00
|
|
|
if (size() != other.size())
|
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const_iterator it = begin();
|
|
|
|
|
|
|
|
while (it != end()) {
|
2017-04-25 14:58:57 +00:00
|
|
|
// Build two equal ranges for i.key(); one for *this and one for other.
|
|
|
|
// For *this we can avoid a lookup via equal_range, as we know the beginning of the range.
|
2019-07-10 17:40:06 +00:00
|
|
|
auto thisEqualRangeStart = it;
|
|
|
|
const Key &thisEqualRangeKey = it.key();
|
|
|
|
size_type n = 0;
|
|
|
|
do {
|
|
|
|
++it;
|
|
|
|
++n;
|
|
|
|
} while (it != end() && it.key() == thisEqualRangeKey);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-07-10 17:40:06 +00:00
|
|
|
const auto otherEqualRange = other.equal_range(thisEqualRangeKey);
|
2017-04-25 14:58:57 +00:00
|
|
|
|
2019-07-10 17:40:06 +00:00
|
|
|
if (n != std::distance(otherEqualRange.first, otherEqualRange.second))
|
2017-04-25 14:58:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Keys in the ranges are equal by construction; this checks only the values.
|
2019-07-10 17:46:45 +00:00
|
|
|
if (!qt_is_permutation(thisEqualRangeStart, it, otherEqualRange.first, otherEqualRange.second))
|
2017-04-25 14:58:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2017-04-25 14:58:57 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 02:49:33 +00:00
|
|
|
template <class Key, class T>
|
|
|
|
QPair<typename QHash<Key, T>::iterator, typename QHash<Key, T>::iterator> QHash<Key, T>::equal_range(const Key &akey)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
auto pair = qAsConst(*this).equal_range(akey);
|
|
|
|
return qMakePair(iterator(pair.first.i), iterator(pair.second.i));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2019-04-02 08:54:54 +00:00
|
|
|
QPair<typename QHash<Key, T>::const_iterator, typename QHash<Key, T>::const_iterator> QHash<Key, T>::equal_range(const Key &akey) const noexcept
|
2016-02-07 02:49:33 +00:00
|
|
|
{
|
2016-11-09 13:36:08 +00:00
|
|
|
Node *node = *findNode(akey);
|
2016-02-07 02:49:33 +00:00
|
|
|
const_iterator firstIt = const_iterator(node);
|
|
|
|
|
|
|
|
if (node != e) {
|
|
|
|
// equal keys must hash to the same value and so they all
|
|
|
|
// end up in the same bucket. So we can use node->next,
|
|
|
|
// which only works within a bucket, instead of (out-of-line)
|
|
|
|
// QHashData::nextNode()
|
|
|
|
while (node->next != e && node->next->key == akey)
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
// 'node' may be the last node in the bucket. To produce the end iterator, we'd
|
|
|
|
// need to enter the next bucket in this case, so we need to use
|
|
|
|
// QHashData::nextNode() here, which, unlike node->next above, can move between
|
|
|
|
// buckets.
|
|
|
|
node = concrete(QHashData::nextNode(reinterpret_cast<QHashData::Node *>(node)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return qMakePair(firstIt, const_iterator(node));
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
template <class Key, class T>
|
|
|
|
class QMultiHash : public QHash<Key, T>
|
|
|
|
{
|
|
|
|
public:
|
2019-04-02 08:54:54 +00:00
|
|
|
QMultiHash() noexcept {}
|
2013-01-10 12:45:18 +00:00
|
|
|
inline QMultiHash(std::initializer_list<std::pair<Key,T> > list)
|
|
|
|
{
|
2014-01-24 14:37:29 +00:00
|
|
|
this->reserve(int(list.size()));
|
2013-01-10 12:45:18 +00:00
|
|
|
for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it)
|
|
|
|
insert(it->first, it->second);
|
|
|
|
}
|
2016-03-03 19:20:42 +00:00
|
|
|
#ifdef Q_QDOC
|
|
|
|
template <typename InputIterator>
|
|
|
|
QMultiHash(InputIterator f, InputIterator l);
|
|
|
|
#else
|
|
|
|
template <typename InputIterator, QtPrivate::IfAssociativeIteratorHasKeyAndValue<InputIterator> = true>
|
|
|
|
QMultiHash(InputIterator f, InputIterator l)
|
|
|
|
{
|
|
|
|
QtPrivate::reserveIfForwardIterator(this, f, l);
|
|
|
|
for (; f != l; ++f)
|
|
|
|
insert(f.key(), f.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename InputIterator, QtPrivate::IfAssociativeIteratorHasFirstAndSecond<InputIterator> = true>
|
|
|
|
QMultiHash(InputIterator f, InputIterator l)
|
|
|
|
{
|
|
|
|
QtPrivate::reserveIfForwardIterator(this, f, l);
|
|
|
|
for (; f != l; ++f)
|
|
|
|
insert(f->first, f->second);
|
|
|
|
}
|
2013-01-10 12:45:18 +00:00
|
|
|
#endif
|
2015-07-06 14:55:15 +00:00
|
|
|
// compiler-generated copy/move ctors/assignment operators are fine!
|
|
|
|
// compiler-generated destructor is fine!
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QMultiHash(const QHash<Key, T> &other) : QHash<Key, T>(other) {}
|
2019-04-02 08:54:54 +00:00
|
|
|
QMultiHash(QHash<Key, T> &&other) noexcept : QHash<Key, T>(std::move(other)) {}
|
|
|
|
void swap(QMultiHash &other) noexcept { QHash<Key, T>::swap(other); } // prevent QMultiHash<->QHash swaps
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
inline typename QHash<Key, T>::iterator replace(const Key &key, const T &value)
|
|
|
|
{ return QHash<Key, T>::insert(key, value); }
|
|
|
|
|
|
|
|
inline typename QHash<Key, T>::iterator insert(const Key &key, const T &value)
|
|
|
|
{ return QHash<Key, T>::insertMulti(key, value); }
|
|
|
|
|
|
|
|
inline QMultiHash &operator+=(const QMultiHash &other)
|
|
|
|
{ this->unite(other); return *this; }
|
|
|
|
inline QMultiHash operator+(const QMultiHash &other) const
|
|
|
|
{ QMultiHash result = *this; result += other; return result; }
|
|
|
|
|
|
|
|
using QHash<Key, T>::contains;
|
|
|
|
using QHash<Key, T>::remove;
|
|
|
|
using QHash<Key, T>::count;
|
|
|
|
using QHash<Key, T>::find;
|
|
|
|
using QHash<Key, T>::constFind;
|
|
|
|
|
|
|
|
bool contains(const Key &key, const T &value) const;
|
|
|
|
|
|
|
|
int remove(const Key &key, const T &value);
|
|
|
|
|
|
|
|
int count(const Key &key, const T &value) const;
|
|
|
|
|
|
|
|
typename QHash<Key, T>::iterator find(const Key &key, const T &value) {
|
|
|
|
typename QHash<Key, T>::iterator i(find(key));
|
|
|
|
typename QHash<Key, T>::iterator end(this->end());
|
|
|
|
while (i != end && i.key() == key) {
|
|
|
|
if (i.value() == value)
|
|
|
|
return i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
typename QHash<Key, T>::const_iterator find(const Key &key, const T &value) const {
|
|
|
|
typename QHash<Key, T>::const_iterator i(constFind(key));
|
|
|
|
typename QHash<Key, T>::const_iterator end(QHash<Key, T>::constEnd());
|
|
|
|
while (i != end && i.key() == key) {
|
|
|
|
if (i.value() == value)
|
|
|
|
return i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
typename QHash<Key, T>::const_iterator constFind(const Key &key, const T &value) const
|
|
|
|
{ return find(key, value); }
|
|
|
|
private:
|
|
|
|
T &operator[](const Key &key);
|
|
|
|
const T operator[](const Key &key) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE bool QMultiHash<Key, T>::contains(const Key &key, const T &value) const
|
|
|
|
{
|
|
|
|
return constFind(key, value) != QHash<Key, T>::constEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE int QMultiHash<Key, T>::remove(const Key &key, const T &value)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
typename QHash<Key, T>::iterator i(find(key));
|
|
|
|
typename QHash<Key, T>::iterator end(QHash<Key, T>::end());
|
|
|
|
while (i != end && i.key() == key) {
|
|
|
|
if (i.value() == value) {
|
|
|
|
i = this->erase(i);
|
|
|
|
++n;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_INLINE_TEMPLATE int QMultiHash<Key, T>::count(const Key &key, const T &value) const
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
typename QHash<Key, T>::const_iterator i(constFind(key));
|
|
|
|
typename QHash<Key, T>::const_iterator end(QHash<Key, T>::constEnd());
|
|
|
|
while (i != end && i.key() == key) {
|
|
|
|
if (i.value() == value)
|
|
|
|
++n;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_DECLARE_ASSOCIATIVE_ITERATOR(Hash)
|
|
|
|
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Hash)
|
|
|
|
|
2016-07-11 09:14:34 +00:00
|
|
|
template <class Key, class T>
|
|
|
|
uint qHash(const QHash<Key, T> &key, uint seed = 0)
|
2019-04-04 14:46:41 +00:00
|
|
|
noexcept(noexcept(qHash(std::declval<Key&>())) && noexcept(qHash(std::declval<T&>())))
|
2016-07-11 09:14:34 +00:00
|
|
|
{
|
|
|
|
QtPrivate::QHashCombineCommutative hash;
|
|
|
|
for (auto it = key.begin(), end = key.end(); it != end; ++it) {
|
|
|
|
const Key &k = it.key();
|
|
|
|
const T &v = it.value();
|
|
|
|
seed = hash(seed, std::pair<const Key&, const T&>(k, v));
|
|
|
|
}
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
inline uint qHash(const QMultiHash<Key, T> &key, uint seed = 0)
|
2019-04-04 14:46:41 +00:00
|
|
|
noexcept(noexcept(qHash(std::declval<Key&>())) && noexcept(qHash(std::declval<T&>())))
|
2016-07-11 09:14:34 +00:00
|
|
|
{
|
|
|
|
const QHash<Key, T> &key2 = key;
|
|
|
|
return qHash(key2, seed);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
2014-03-07 12:18:31 +00:00
|
|
|
#if defined(Q_CC_MSVC)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif // QHASH_H
|