From 34c6fbf846b12fa11a863e5c0d3ad5651eb5db48 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 27 Mar 2015 20:56:44 +0100 Subject: 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 Reviewed-by: Thiago Macieira --- src/corelib/tools/qvector.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ src/corelib/tools/qvector.h | 30 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index d10f82fbb4..de29fb3cf5 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -290,6 +290,54 @@ \sa operator==() */ +/*! \fn bool operator<(const QVector &lhs, const QVector &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 &lhs, const QVector &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 &lhs, const QVector &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 &lhs, const QVector &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 Returns the number of items in the vector. diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index a48269cfaa..c8210281a7 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -865,6 +865,36 @@ QList QList::fromVector(const QVector &vector) Q_DECLARE_SEQUENTIAL_ITERATOR(Vector) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector) +template +bool operator<(const QVector &lhs, const QVector &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 +inline bool operator>(const QVector &lhs, const QVector &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return rhs < lhs; +} + +template +inline bool operator<=(const QVector &lhs, const QVector &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs > rhs); +} + +template +inline bool operator>=(const QVector &lhs, const QVector &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs < rhs); +} + /* ### Qt 5: ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because -- cgit v1.2.3