Add qHash(QVector)

QVectors can be compared for equality, so qHash should be overloaded, too.

[ChangeLog][QtCore][QVector] Added qHash(QVector).

Change-Id: I2aacce55d416abf2492631a504a02c6e8fc4ff1c
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2015-04-08 19:44:48 +02:00
parent f9063758cb
commit adfe9ea1e0
3 changed files with 37 additions and 0 deletions

View File

@ -338,6 +338,17 @@
of \c operator<().
*/
/*!
\fn uint qHash(const QVector<T> &key, uint seed = 0)
\since 5.6
\relates QVector
Returns the hash value for \a key,
using \a seed to seed the calculation.
This function requires qHash() to be overloaded for the value type \c T.
*/
/*! \fn int QVector::size() const
Returns the number of items in the vector.

View File

@ -39,6 +39,7 @@
#include <QtCore/qlist.h>
#include <QtCore/qrefcount.h>
#include <QtCore/qarraydata.h>
#include <QtCore/qhashfunctions.h>
#include <iterator>
#include <vector>
@ -873,6 +874,13 @@ QList<T> QList<T>::fromVector(const QVector<T> &vector)
Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
template <typename T>
uint qHash(const QVector<T> &key, uint seed = 0)
Q_DECL_NOEXCEPT_EXPR(noexcept(qHashRange(key.cbegin(), key.cend(), seed)))
{
return qHashRange(key.cbegin(), key.cend(), seed);
}
template <typename T>
bool operator<(const QVector<T> &lhs, const QVector<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),

View File

@ -86,6 +86,8 @@ private:
}
};
inline uint qHash(const Movable &key, uint seed = 0) { return qHash(key.i, seed); }
QAtomicInt Movable::counter = 0;
QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(Movable, Q_MOVABLE_TYPE);
@ -155,6 +157,8 @@ private:
};
QAtomicInt Custom::counter = 0;
inline uint qHash(const Custom &key, uint seed = 0) { return qHash(key.i, seed); }
Q_DECLARE_METATYPE(Custom);
// tests depends on the fact that:
@ -237,6 +241,9 @@ private slots:
void prependInt() const;
void prependMovable() const;
void prependCustom() const;
void qhashInt() const { qhash<int>(); }
void qhashMovable() const { qhash<Movable>(); }
void qhashCustom() const { qhash<Custom>(); }
void removeInt() const;
void removeMovable() const;
void removeCustom() const;
@ -302,6 +309,7 @@ private:
template<typename T> void fill() const;
template<typename T> void fromList() const;
template<typename T> void insert() const;
template<typename T> void qhash() const;
template<typename T> void prepend() const;
template<typename T> void remove() const;
template<typename T> void size() const;
@ -1445,6 +1453,16 @@ void tst_QVector::mid() const
QCOMPARE(list.mid(4), QVector<QString>() << "buck" << "hello" << "kitty");
}
template <typename T>
void tst_QVector::qhash() const
{
QVector<T> l1, l2;
QCOMPARE(qHash(l1), qHash(l2));
l1 << SimpleValue<T>::at(0);
l2 << SimpleValue<T>::at(0);
QCOMPARE(qHash(l1), qHash(l2));
}
template<typename T>
void tst_QVector::prepend() const
{