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 <thiago.macieira@intel.com>
This commit is contained in:
parent
d1308232d3
commit
510660080d
@ -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()
|
||||
|
@ -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<T> &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(); }
|
||||
|
@ -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<int> 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<int> 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<RemoveLastTestClass> 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<RemoveLastTestClass>::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<int> 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<QVector<int> >("vector");
|
||||
|
Loading…
Reference in New Issue
Block a user