From 84c10500b1730e8d947732728d190fde612fc840 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 8 Oct 2013 13:17:52 +0200 Subject: [PATCH] QVector: add removeOne(), removeAll() for QList compatibility Eases migration from QList to QVector. [ChangeLog][QtCore][QVector] Added removeOne(), removeAll() for QList compatibility. Change-Id: I4211afb2e077c187d0a39f0ac4528f0c66721fb3 Reviewed-by: Olivier Goffart --- src/corelib/tools/qvector.cpp | 22 +++++++++++++++++++ src/corelib/tools/qvector.h | 19 ++++++++++++++++ .../corelib/tools/qvector/tst_qvector.cpp | 19 ++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index 89396540e0..b2b4315545 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -576,6 +576,28 @@ \sa remove(), QList::removeAt() */ +/*! \fn int QVector::removeAll(const T &t) + \since 5.4 + + Removes all elements that compare equal to \a t from the + vector. Returns the number of elements removed, if any. + + Provided for compatibility with QList. + + \sa removeOne(), QList::removeAll() +*/ + +/*! \fn bool QVector::removeOne(const T &t) + \since 5.4 + + Removes the first element that compares equal to \a t from the + vector. Returns whether an element was, in fact, removed. + + Provided for compatibility with QList. + + \sa removeAll(), QList::removeOne() +*/ + /*! \fn int QVector::length() const \since 5.2 diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 075e8e83e8..d1420088a4 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -154,6 +154,24 @@ public: // QList compatibility void removeAt(int i) { remove(i); } + int removeAll(const T &t) + { + const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t); + if (cit == ce) + return 0; + const iterator e = end(), it = std::remove(c2m(cit), e, t); + const int result = std::distance(it, e); + erase(it, e); + return result; + } + bool removeOne(const T &t) + { + const int i = indexOf(t); + if (i < 0) + return false; + remove(i); + return true; + } int length() const { return size(); } T takeAt(int i) { T t = at(i); remove(i); return t; } @@ -250,6 +268,7 @@ private: { return (i <= d->end()) && (d->begin() <= i); } + iterator c2m(const_iterator it) { return begin() + (it - cbegin()); } class AlignmentDummy { Data header; T array[1]; }; }; diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index e1e65a77c8..bd0ed06382 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -1423,13 +1423,28 @@ void tst_QVector::remove() const T val1 = SimpleValue::at(1); T val2 = SimpleValue::at(2); T val3 = SimpleValue::at(3); + T val4 = SimpleValue::at(4); + myvec << val1 << val2 << val3; + myvec << val1 << val2 << val3; myvec << val1 << val2 << val3; // remove middle myvec.remove(1); - QCOMPARE(myvec, QVector() << val1 << val3); + QCOMPARE(myvec, QVector() << val1 << val3 << val1 << val2 << val3 << val1 << val2 << val3); + + // removeOne() + QVERIFY(!myvec.removeOne(val4)); + QVERIFY(myvec.removeOne(val2)); + QCOMPARE(myvec, QVector() << val1 << val3 << val1 << val3 << val1 << val2 << val3); + + // removeAll() + QCOMPARE(myvec.removeAll(val4), 0); + QCOMPARE(myvec.removeAll(val1), 3); + QCOMPARE(myvec, QVector() << val3 << val3 << val2 << val3); + QCOMPARE(myvec.removeAll(val2), 1); + QCOMPARE(myvec, QVector() << val3 << val3 << val3); // remove rest - myvec.remove(0, 2); + myvec.remove(0, 3); QCOMPARE(myvec, QVector()); }