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

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

Task-number: QTBUG-25919
Change-Id: Ifda5d420802a3594c3181f54036279f16a7da16e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Marc Mutz 2015-03-31 00:14:24 +02:00
parent f276dd5b7f
commit cc164f9ac7
3 changed files with 85 additions and 0 deletions

View File

@ -45,6 +45,7 @@
#ifdef Q_COMPILER_INITIALIZER_LISTS
#include <initializer_list>
#endif
#include <iterator>
QT_BEGIN_NAMESPACE
@ -165,6 +166,8 @@ public:
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
inline iterator begin() { return ptr; }
inline const_iterator begin() const { return ptr; }
@ -174,6 +177,12 @@ public:
inline const_iterator end() const { return ptr + s; }
inline const_iterator cend() const { return ptr + s; }
inline const_iterator constEnd() const { return ptr + s; }
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(const_iterator before, int n, const T &x);
inline iterator insert(const_iterator before, const T &x) { return insert(before, 1, x); }
iterator erase(const_iterator begin, const_iterator end);

View File

@ -459,6 +459,20 @@
Typedef for T *. Provided for STL compatibility.
*/
/*!
\typedef QVarLengthArray::const_reverse_iterator
\since 5.6
Typedef for \c{std::reverse_iterator<const T*>}. Provided for STL compatibility.
*/
/*!
\typedef QVarLengthArray::reverse_iterator
\since 5.6
Typedef for \c{std::reverse_iterator<T*>}. Provided for STL compatibility.
*/
/*! \fn void QVarLengthArray::prepend(const T &value)
\since 4.8
@ -573,6 +587,52 @@
\sa constBegin(), end()
*/
/*! \fn QVarLengthArray::reverse_iterator QVarLengthArray::rbegin()
\since 5.6
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
item in the variable length array, in reverse order.
\sa begin(), crbegin(), rend()
*/
/*! \fn QVarLengthArray::const_reverse_iterator QVarLengthArray::rbegin() const
\since 5.6
\overload
*/
/*! \fn QVarLengthArray::const_reverse_iterator QVarLengthArray::crbegin() const
\since 5.6
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
item in the variable length array, in reverse order.
\sa begin(), rbegin(), rend()
*/
/*! \fn QVarLengthArray::reverse_iterator QVarLengthArray::rend()
\since 5.6
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
the last item in the variable length array, in reverse order.
\sa end(), crend(), rbegin()
*/
/*! \fn QVarLengthArray::const_reverse_iterator QVarLengthArray::rend() const
\since 5.6
\overload
*/
/*! \fn QVarLengthArray::const_reverse_iterator QVarLengthArray::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 variable length array, in reverse order.
\sa end(), rend(), rbegin()
*/
/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(const_iterator pos)
\since 4.8

View File

@ -47,6 +47,7 @@ private slots:
void appendCausingRealloc();
void resize();
void realloc();
void reverseIterators();
void count();
void first();
void last();
@ -563,6 +564,21 @@ void tst_QVarLengthArray::realloc()
QVERIFY(reallocTestProceed);
}
void tst_QVarLengthArray::reverseIterators()
{
QVarLengthArray<int> v;
v << 1 << 2 << 3 << 4;
QVarLengthArray<int> vr = v;
std::reverse(vr.begin(), vr.end());
const QVarLengthArray<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()));
}
void tst_QVarLengthArray::count()
{
// tests size(), count() and length(), since they're the same thing