QVector: add relational operators <,<=,>,>=

std::vector has them, too.

[ChangeLog][QtCore][QVector] Added relational operators <, <=, >, >= if the element
type supports operator<.

Change-Id: I0bcb22dfcc43cb0362f17b4e06154ce18646580a
Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2015-03-27 20:56:44 +01:00
parent 929cc27ffe
commit 34c6fbf846
3 changed files with 98 additions and 0 deletions

View File

@ -290,6 +290,54 @@
\sa operator==() \sa operator==()
*/ */
/*! \fn bool operator<(const QVector<T> &lhs, const QVector<T> &rhs)
\since 5.6
\relates QVector
Returns \c true if vector \a lhs is
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
{lexicographically less than} \a rhs; otherwise returns \c false.
This function requires the value type to have an implementation
of \c operator<().
*/
/*! \fn bool operator<=(const QVector<T> &lhs, const QVector<T> &rhs)
\since 5.6
\relates QVector
Returns \c true if vector \a lhs is
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
{lexicographically less than or equal to} \a rhs; otherwise returns \c false.
This function requires the value type to have an implementation
of \c operator<().
*/
/*! \fn bool operator>(const QVector<T> &lhs, const QVector<T> &rhs)
\since 5.6
\relates QVector
Returns \c true if vector \a lhs is
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
{lexicographically greater than} \a rhs; otherwise returns \c false.
This function requires the value type to have an implementation
of \c operator<().
*/
/*! \fn bool operator>=(const QVector<T> &lhs, const QVector<T> &rhs)
\since 5.6
\relates QVector
Returns \c true if vector \a lhs is
\l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare}
{lexicographically greater than or equal to} \a rhs; otherwise returns \c false.
This function requires the value type to have an implementation
of \c operator<().
*/
/*! \fn int QVector::size() const /*! \fn int QVector::size() const
Returns the number of items in the vector. Returns the number of items in the vector.

View File

@ -865,6 +865,36 @@ QList<T> QList<T>::fromVector(const QVector<T> &vector)
Q_DECLARE_SEQUENTIAL_ITERATOR(Vector) Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
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(),
rhs.begin(), rhs.end())))
{
return std::lexicographical_compare(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end());
}
template <typename T>
inline bool operator>(const QVector<T> &lhs, const QVector<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return rhs < lhs;
}
template <typename T>
inline bool operator<=(const QVector<T> &lhs, const QVector<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return !(lhs > rhs);
}
template <typename T>
inline bool operator>=(const QVector<T> &lhs, const QVector<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return !(lhs < rhs);
}
/* /*
### Qt 5: ### Qt 5:
### This needs to be removed for next releases of Qt. It is a workaround for vc++ because ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because

View File

@ -123,6 +123,13 @@ struct Custom {
return i == other.i; return i == other.i;
} }
bool operator<(const Custom &other) const
{
check(&other);
check(this);
return i < other.i;
}
Custom &operator=(const Custom &other) Custom &operator=(const Custom &other)
{ {
check(&other); check(&other);
@ -2076,6 +2083,19 @@ void tst_QVector::testOperators() const
// == // ==
QVERIFY(myvec == combined); QVERIFY(myvec == combined);
// <, >, <=, >=
QVERIFY(!(myvec < combined));
QVERIFY(!(myvec > combined));
QVERIFY( myvec <= combined);
QVERIFY( myvec >= combined);
combined.push_back("G");
QVERIFY( myvec < combined);
QVERIFY(!(myvec > combined));
QVERIFY( myvec <= combined);
QVERIFY(!(myvec >= combined));
QVERIFY(combined > myvec);
QVERIFY(combined >= myvec);
// [] // []
QCOMPARE(myvec[0], QLatin1String("A")); QCOMPARE(myvec[0], QLatin1String("A"));
QCOMPARE(myvec[1], QLatin1String("B")); QCOMPARE(myvec[1], QLatin1String("B"));