From dfb00bd479ded5bc4e86424c9678412c5ff72282 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 27 Mar 2015 20:56:44 +0100 Subject: 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 --- src/corelib/tools/qlist.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/corelib/tools/qlist.h | 30 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index db00dcb458..ef47ec1c05 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -584,6 +584,54 @@ void **QListData::erase(void **xi) \sa operator==() */ +/*! \fn bool operator<(const QList &lhs, const QList &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 &lhs, const QList &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 &lhs, const QList &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 &lhs, const QList &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 diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 57e67d52d7..7d90f72876 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -1015,6 +1015,36 @@ inline int QList::count_impl(const T &t, QListData::ArrayCompatibleLayout) co Q_DECLARE_SEQUENTIAL_ITERATOR(List) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) +template +bool operator<(const QList &lhs, const QList &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 QList &lhs, const QList &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return rhs < lhs; +} + +template +inline bool operator<=(const QList &lhs, const QList &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs > rhs); +} + +template +inline bool operator>=(const QList &lhs, const QList &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs < rhs); +} + QT_END_NAMESPACE #include -- cgit v1.2.3