From 510660080de6fab87e117de2663c33eff5ae451b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Wed, 13 Feb 2013 17:48:49 +0100 Subject: [PATCH] QVector - add remove functions This patch adds the functions removeFirst() and removeLast(). Functions that QList has. Beside making these functions, pop_back and pop_front are redirected to these rather than calling erase. Change-Id: Ifc5f8a78e33f436f06f21095a920ec5d4311fd6f Reviewed-by: Thiago Macieira --- src/corelib/tools/qvector.cpp | 24 +++++- src/corelib/tools/qvector.h | 6 +- .../corelib/tools/qvector/tst_qvector.cpp | 76 +++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index efc4b6a446..5ece011f0b 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -544,6 +544,26 @@ \sa insert(), replace(), fill() */ +/*! \fn void QVector::removeFirst() + \since 5.1 + Removes the first item in the vector. Calling this function is + equivalent to calling remove(0). The vector must not be empty. If + the vector can be empty, call isEmpty() before calling this + function. + + \sa remove(), isEmpty() +*/ + +/*! \fn void QVector::removeLast() + \since 5.1 + Removes the last item in the vector. Calling this function is + equivalent to calling remove(size() - 1). The vector must not be + empty. If the vector can be empty, call isEmpty() before calling + this function. + + \sa remove(), removeFirst(), isEmpty() +*/ + /*! \fn QVector &QVector::fill(const T &value, int size = -1) Assigns \a value to all items in the vector. If \a size is @@ -773,13 +793,13 @@ /*! \fn void QVector::pop_front() This function is provided for STL compatibility. It is equivalent - to erase(begin()). + to removeFirst(). */ /*! \fn void QVector::pop_back() This function is provided for STL compatibility. It is equivalent - to erase(end() - 1). + to removeLast(). */ /*! \fn T& QVector::front() diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 7d5cf91324..f6fe316a15 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -138,6 +138,8 @@ public: void replace(int i, const T &t); void remove(int i); void remove(int i, int n); + inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(d->begin()); } + inline void removeLast() { Q_ASSERT(!isEmpty()); erase(d->end() - 1); } QVector &fill(const T &t, int size = -1); @@ -198,8 +200,8 @@ public: typedef int size_type; inline void push_back(const T &t) { append(t); } inline void push_front(const T &t) { prepend(t); } - void pop_back() { Q_ASSERT(!isEmpty()); erase(d->end() - 1); } - void pop_front() { Q_ASSERT(!isEmpty()); erase(d->begin()); } + void pop_back() { removeLast(); } + void pop_front() { removeFirst(); } inline bool empty() const { return d->size == 0; } inline T& front() { return first(); } diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 7738a2c797..3acd5ddb10 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -236,6 +236,7 @@ private slots: void removeInt() const; void removeMovable() const; void removeCustom() const; + void removeFirstLast() const; void resizePOD_data() const; void resizePOD() const; void resizeComplexMovable_data() const; @@ -1393,6 +1394,81 @@ void tst_QVector::removeCustom() const QCOMPARE(instancesCount, Custom::counter.loadAcquire()); } +struct RemoveLastTestClass +{ + RemoveLastTestClass() { other = 0; deleted = false; } + RemoveLastTestClass *other; + bool deleted; + ~RemoveLastTestClass() + { + deleted = true; + if (other) + other->other = 0; + } +}; + +void tst_QVector::removeFirstLast() const +{ + // pop_pack - pop_front + QVector t; + t.append(1); + t.append(2); + t.append(3); + t.append(4); + t.pop_front(); + QCOMPARE(t.size(), 3); + QCOMPARE(t.at(0), 2); + t.pop_back(); + QCOMPARE(t.size(), 2); + QCOMPARE(t.at(0), 2); + QCOMPARE(t.at(1), 3); + + // remove first + QVector x, y; + x.append(1); + x.append(2); + y = x; + x.removeFirst(); + QCOMPARE(x.size(), 1); + QCOMPARE(y.size(), 2); + QCOMPARE(x.at(0), 2); + + // remove Last + QVector v; + v.resize(2); + v[0].other = &(v[1]); + v[1].other = &(v[0]); + // Check dtor - complex type + QVERIFY(v.at(0).other != 0); + v.removeLast(); + QVERIFY(v.at(0).other == 0); + QCOMPARE(v.at(0).deleted, false); + // check iterator + int count = 0; + for (QVector::const_iterator i = v.constBegin(); i != v.constEnd(); ++i) { + ++count; + QVERIFY(i->other == 0); + QCOMPARE(i->deleted, false); + } + // Check size + QCOMPARE(count, 1); + QCOMPARE(v.size(), 1); + v.removeLast(); + QCOMPARE(v.size(), 0); + // Check if we do correct realloc + QVector v2, v3; + v2.append(1); + v2.append(2); + v3 = v2; // shared + v2.removeLast(); + QCOMPARE(v2.size(), 1); + QCOMPARE(v3.size(), 2); + QCOMPARE(v2.at(0), 1); + QCOMPARE(v3.at(0), 1); + QCOMPARE(v3.at(1), 2); +} + + void tst_QVector::resizePOD_data() const { QTest::addColumn >("vector");