QVector: add {const_,}reverse_iterator, {c,}r{begin,end}()

[ChangeLog][QtCore][QVector] Added rbegin(), crbegin(), rend(), crend(),
and reverse_iterator and const_reverse_iterator typedefs.

Task-number: QTBUG-25919
Change-Id: I8dea52a08e7b1a4442e034c22b83be4d25dc2303
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2015-03-31 00:14:24 +02:00
parent 34c6fbf846
commit ffedd0cf64
3 changed files with 102 additions and 0 deletions

View File

@ -871,6 +871,52 @@
\sa constBegin(), end()
*/
/*! \fn QVector::reverse_iterator QVector::rbegin()
\since 5.6
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
item in the vector, in reverse order.
\sa begin(), crbegin(), rend()
*/
/*! \fn QVector::const_reverse_iterator QVector::rbegin() const
\since 5.6
\overload
*/
/*! \fn QVector::const_reverse_iterator QVector::crbegin() const
\since 5.6
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
item in the vector, in reverse order.
\sa begin(), rbegin(), rend()
*/
/*! \fn QVector::reverse_iterator QVector::rend()
\since 5.6
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
the last item in the vector, in reverse order.
\sa end(), crend(), rbegin()
*/
/*! \fn QVector::const_reverse_iterator QVector::rend() const
\since 5.6
\overload
*/
/*! \fn QVector::const_reverse_iterator QVector::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 vector, in reverse order.
\sa end(), rend(), rbegin()
*/
/*! \fn QVector::iterator QVector::erase(iterator pos)
Removes the item pointed to by the iterator \a pos from the
@ -1061,6 +1107,38 @@
\sa QVector::constBegin(), QVector::constEnd(), QVector::iterator, QVectorIterator
*/
/*! \typedef QVector::reverse_iterator
\since 5.6
The QVector::reverse_iterator typedef provides an STL-style non-const
reverse iterator for QVector.
It is simply a typedef for \c{std::reverse_iterator<T*>}.
\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 QVector::rbegin(), QVector::rend(), QVector::const_reverse_iterator, QVector::iterator
*/
/*! \typedef QVector::const_reverse_iterator
\since 5.6
The QVector::const_reverse_iterator typedef provides an STL-style const
reverse iterator for QVector.
It is simply a typedef for \c{std::reverse_iterator<const T*>}.
\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 QVector::rbegin(), QVector::rend(), QVector::reverse_iterator, QVector::const_iterator
*/
/*! \typedef QVector::Iterator
Qt-style synonym for QVector::iterator.

View File

@ -175,6 +175,8 @@ public:
// STL-style
typedef typename Data::iterator iterator;
typedef typename Data::const_iterator const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#if !defined(QT_STRICT_ITERATORS) || defined(Q_QDOC)
inline iterator begin() { detach(); return d->begin(); }
inline const_iterator begin() const { return d->constBegin(); }
@ -194,6 +196,12 @@ public:
inline const_iterator cend(const_iterator = const_iterator()) const { return d->constEnd(); }
inline const_iterator constEnd(const_iterator = const_iterator()) const { return d->constEnd(); }
#endif
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, int n, const T &x);
inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
iterator erase(iterator begin, iterator end);

View File

@ -248,6 +248,7 @@ private slots:
void resizeComplex_data() const;
void resizeComplex() const;
void resizeCtorAndDtor() const;
void reverseIterators() const;
void sizeInt() const;
void sizeMovable() const;
void sizeCustom() const;
@ -1914,6 +1915,21 @@ void tst_QVector::resizeCtorAndDtor() const
QCOMPARE(Custom::counter.loadAcquire(), items);
}
void tst_QVector::reverseIterators() const
{
QVector<int> v;
v << 1 << 2 << 3 << 4;
QVector<int> vr = v;
std::reverse(vr.begin(), vr.end());
const QVector<int> &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()));
}
template<typename T>
void tst_QVector::size() const
{