From f7b5f0cfd292baad7a18f1b83567133756ff1d2c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 31 Mar 2015 00:14:24 +0200 Subject: [PATCH] QList: add {const_,reverse_iterator}, {c,}r{begin,end}() [ChangeLog][QtCore][QList] Added rbegin(), crbegin(), rend(), crend(), and reverse_iterator and const_reverse_iterator typedefs. Task-number: QTBUG-25919 Change-Id: Icce870c22931e68cdcedd1519651bfa374ac44af Reviewed-by: Thiago Macieira --- src/corelib/tools/qlist.cpp | 78 ++++++++++++++++++++ src/corelib/tools/qlist.h | 8 ++ tests/auto/corelib/tools/qlist/tst_qlist.cpp | 41 ++++++++++ 3 files changed, 127 insertions(+) diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index ef47ec1c05..126147716c 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -1048,6 +1048,52 @@ void **QListData::erase(void **xi) \sa constBegin(), end() */ +/*! \fn QList::reverse_iterator QList::rbegin() + \since 5.6 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + item in the list, in reverse order. + + \sa begin(), crbegin(), rend() +*/ + +/*! \fn QList::const_reverse_iterator QList::rbegin() const + \since 5.6 + \overload +*/ + +/*! \fn QList::const_reverse_iterator QList::crbegin() const + \since 5.6 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + item in the list, in reverse order. + + \sa begin(), rbegin(), rend() +*/ + +/*! \fn QList::reverse_iterator QList::rend() + \since 5.6 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past + the last item in the list, in reverse order. + + \sa end(), crend(), rbegin() +*/ + +/*! \fn QList::const_reverse_iterator QList::rend() const + \since 5.6 + \overload +*/ + +/*! \fn QList::const_reverse_iterator QList::crend() const + \since 5.6 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one + past the last item in the list, in reverse order. + + \sa end(), rend(), rbegin() +*/ + /*! \fn QList::iterator QList::erase(iterator pos) Removes the item associated with the iterator \a pos from the @@ -1118,6 +1164,38 @@ void **QListData::erase(void **xi) Typedef for const T &. Provided for STL compatibility. */ +/*! \typedef QList::reverse_iterator + \since 5.6 + + The QList::reverse_iterator typedef provides an STL-style non-const + reverse iterator for QList. + + It is simply a typedef for \c{std::reverse_iterator}. + + \warning Iterators on implicitly shared containers do not work + exactly like STL-iterators. You should avoid copying a container + while iterators are active on that container. For more information, + read \l{Implicit sharing iterator problem}. + + \sa QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator +*/ + +/*! \typedef QList::const_reverse_iterator + \since 5.6 + + The QList::const_reverse_iterator typedef provides an STL-style const + reverse iterator for QList. + + It is simply a typedef for \c{std::reverse_iterator}. + + \warning Iterators on implicitly shared containers do not work + exactly like STL-iterators. You should avoid copying a container + while iterators are active on that container. For more information, + read \l{Implicit sharing iterator problem}. + + \sa QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator +*/ + /*! \fn int QList::count() const Returns the number of items in the list. This is effectively the diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 7d90f72876..31c9c36e3d 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -291,6 +291,8 @@ public: friend class const_iterator; // stl style + typedef std::reverse_iterator reverse_iterator; + typedef std::reverse_iterator const_reverse_iterator; inline iterator begin() { detach(); return reinterpret_cast(p.begin()); } inline const_iterator begin() const { return reinterpret_cast(p.begin()); } inline const_iterator cbegin() const { return reinterpret_cast(p.begin()); } @@ -299,6 +301,12 @@ public: inline const_iterator end() const { return reinterpret_cast(p.end()); } inline const_iterator cend() const { return reinterpret_cast(p.end()); } inline const_iterator constEnd() const { return reinterpret_cast(p.end()); } + reverse_iterator rbegin() { return reverse_iterator(end()); } + reverse_iterator rend() { return reverse_iterator(begin()); } + const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } + const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } + const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } iterator insert(iterator before, const T &t); iterator erase(iterator pos); iterator erase(iterator first, iterator last); diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 6ec3064564..9b12083b18 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -349,6 +349,9 @@ private slots: void replaceOptimal() const; void replaceMovable() const; void replaceComplex() const; + void reverseIteratorsOptimal() const; + void reverseIteratorsMovable() const; + void reverseIteratorsComplex() const; void startsWithOptimal() const; void startsWithMovable() const; void startsWithComplex() const; @@ -416,6 +419,7 @@ private: template void removeAt() const; template void removeOne() const; template void replace() const; + template void reverseIterators() const; template void startsWith() const; template void swap() const; template void takeAt() const; @@ -1239,6 +1243,43 @@ void tst_QList::replaceComplex() const QCOMPARE(liveCount, Complex::getLiveCount()); } +template +void tst_QList::reverseIterators() const +{ + QList v; + v << T_CAT << T_DOG << T_BLAH << T_BAZ; + QList vr = v; + std::reverse(vr.begin(), vr.end()); + const QList &cvr = vr; + QVERIFY(std::equal(v.begin(), v.end(), vr.rbegin())); + QVERIFY(std::equal(v.begin(), v.end(), vr.crbegin())); + QVERIFY(std::equal(v.begin(), v.end(), cvr.rbegin())); + QVERIFY(std::equal(vr.rbegin(), vr.rend(), v.begin())); + QVERIFY(std::equal(vr.crbegin(), vr.crend(), v.begin())); + QVERIFY(std::equal(cvr.rbegin(), cvr.rend(), v.begin())); +} + +void tst_QList::reverseIteratorsOptimal() const +{ + const int liveCount = Optimal::getLiveCount(); + reverseIterators(); + QCOMPARE(liveCount, Optimal::getLiveCount()); +} + +void tst_QList::reverseIteratorsMovable() const +{ + const int liveCount = Movable::getLiveCount(); + reverseIterators(); + QCOMPARE(liveCount, Movable::getLiveCount()); +} + +void tst_QList::reverseIteratorsComplex() const +{ + const int liveCount = Complex::getLiveCount(); + reverseIterators(); + QCOMPARE(liveCount, Complex::getLiveCount()); +} + template void tst_QList::startsWith() const {