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

std::vector has them, too.

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

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

View File

@ -480,6 +480,36 @@ bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T,
return !(l == r);
}
template <typename T, int Prealloc1, int Prealloc2>
bool operator<(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &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, int Prealloc1, int Prealloc2>
inline bool operator>(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return rhs < lhs;
}
template <typename T, int Prealloc1, int Prealloc2>
inline bool operator<=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return !(lhs > rhs);
}
template <typename T, int Prealloc1, int Prealloc2>
inline bool operator>=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs))
{
return !(lhs < rhs);
}
QT_END_NAMESPACE
#endif // QVARLENGTHARRAY_H

View File

@ -667,6 +667,54 @@
\sa operator==()
*/
/*! \fn bool operator<(const QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
\since 5.6
\relates QVarLengthArray
Returns \c true if variable length array \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 QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
\since 5.6
\relates QVarLengthArray
Returns \c true if variable length array \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 QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
\since 5.6
\relates QVarLengthArray
Returns \c true if variable length array \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 QVarLengthArray<T,Prealloc1> &lhs, const QVarLengthArray<T,Prealloc2> &rhs)
\since 5.6
\relates QVarLengthArray
Returns \c true if variable length array \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 QVarLengthArray &QVarLengthArray::operator<<(const T &value)
\since 4.8

View File

@ -51,6 +51,7 @@ private slots:
void first();
void last();
void squeeze();
void operators();
void indexOf();
void lastIndexOf();
void contains();
@ -690,6 +691,49 @@ void tst_QVarLengthArray::squeeze()
QCOMPARE(list.capacity(), sizeOnHeap);
}
void tst_QVarLengthArray::operators()
{
QVarLengthArray<QString> myvla;
myvla << "A" << "B" << "C";
QVarLengthArray<QString> myvlatwo;
myvlatwo << "D" << "E" << "F";
QVarLengthArray<QString> combined;
combined << "A" << "B" << "C" << "D" << "E" << "F";
// !=
QVERIFY(myvla != myvlatwo);
// +=: not provided, emulate
//myvla += myvlatwo;
Q_FOREACH (const QString &s, myvlatwo)
myvla.push_back(s);
QCOMPARE(myvla, combined);
// ==
QVERIFY(myvla == combined);
// <, >, <=, >=
QVERIFY(!(myvla < combined));
QVERIFY(!(myvla > combined));
QVERIFY( myvla <= combined);
QVERIFY( myvla >= combined);
combined.push_back("G");
QVERIFY( myvla < combined);
QVERIFY(!(myvla > combined));
QVERIFY( myvla <= combined);
QVERIFY(!(myvla >= combined));
QVERIFY(combined > myvla);
QVERIFY(combined >= myvla);
// []
QCOMPARE(myvla[0], QLatin1String("A"));
QCOMPARE(myvla[1], QLatin1String("B"));
QCOMPARE(myvla[2], QLatin1String("C"));
QCOMPARE(myvla[3], QLatin1String("D"));
QCOMPARE(myvla[4], QLatin1String("E"));
QCOMPARE(myvla[5], QLatin1String("F"));
}
void tst_QVarLengthArray::indexOf()
{
QVarLengthArray<QString> myvec;