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

std::vector has them, too.

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

Change-Id: Id2bd905e92c0365ad9f439d49908045c8df309c3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2015-03-27 20:56:44 +01:00
parent a03c633e4f
commit dfb00bd479
3 changed files with 111 additions and 0 deletions

View File

@ -584,6 +584,54 @@ void **QListData::erase(void **xi)
\sa operator==()
*/
/*! \fn bool operator<(const QList<T> &lhs, const QList<T> &rhs)
\since 5.6
\relates QList
Returns \c true if list \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 QList<T> &lhs, const QList<T> &rhs)
\since 5.6
\relates QList
Returns \c true if list \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 QList<T> &lhs, const QList<T> &rhs)
\since 5.6
\relates QList
Returns \c true if list \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 QList<T> &lhs, const QList<T> &rhs)
\since 5.6
\relates QList
Returns \c true if list \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 QList::size() const

View File

@ -1015,6 +1015,36 @@ inline int QList<T>::count_impl(const T &t, QListData::ArrayCompatibleLayout) co
Q_DECLARE_SEQUENTIAL_ITERATOR(List)
Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List)
template <typename T>
bool operator<(const QList<T> &lhs, const QList<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 QList<T> &lhs, const QList<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return rhs < lhs;
}
template <typename T>
inline bool operator<=(const QList<T> &lhs, const QList<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return !(lhs > rhs);
}
template <typename T>
inline bool operator>=(const QList<T> &lhs, const QList<T> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return !(lhs < rhs);
}
QT_END_NAMESPACE
#include <QtCore/qbytearraylist.h>

View File

@ -76,6 +76,13 @@ struct Movable {
return i == other.i;
}
bool operator<(const Movable &other) const
{
check(state, Constructed);
check(other.state, Constructed);
return i < other.i;
}
Movable &operator=(const Movable &other)
{
check(state, Constructed);
@ -144,6 +151,13 @@ struct Optimal
return i == other.i;
}
bool operator<(const Optimal &other) const
{
check(state, Constructed);
check(other.state, Constructed);
return i < other.i;
}
Optimal &operator=(const Optimal &other)
{
check(state, Constructed);
@ -220,6 +234,12 @@ struct Complex
return value == other.value;
}
bool operator<(Complex const &other) const
{
check(); other.check();
return value < other.value;
}
void check() const
{
QVERIFY(this == checkSum);
@ -1576,6 +1596,19 @@ void tst_QList::testOperators() const
// []
QCOMPARE(list[0], T_FOO);
QCOMPARE(list[list.size() - 1], T_CAT);
// <, >, <=, >=
QVERIFY(!(list < listtwo));
QVERIFY(!(list > listtwo));
QVERIFY( list <= listtwo);
QVERIFY( list >= listtwo);
listtwo.push_back(T_CAT);
QVERIFY( list < listtwo);
QVERIFY(!(list > listtwo));
QVERIFY( list <= listtwo);
QVERIFY(!(list >= listtwo));
QVERIFY(listtwo > list);
QVERIFY(listtwo >= list);
}
void tst_QList::testOperatorsOptimal() const