diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index b57954dc03..77afe9d00e 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -688,6 +688,16 @@ \sa takeFirst(), takeLast(), QList::takeAt() */ +/*! \fn void QVector::move(int from, int to) + \since 5.6 + + Moves the item at index position \a from to index position \a to. + + Provided for compatibility with QList. + + \sa QList::move() +*/ + /*! \fn void QVector::removeFirst() \since 5.1 Removes the first item in the vector. Calling this function is diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 2adf2d4522..d13ae9dccd 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -172,6 +172,19 @@ public: } int length() const { return size(); } T takeAt(int i) { T t = at(i); remove(i); return t; } + void move(int from, int to) + { + Q_ASSERT_X(from >= 0 && from < size(), "QVector::move(int,int)", "'from' is out-of-range"); + Q_ASSERT_X(to >= 0 && to < size(), "QVector::move(int,int)", "'to' is out-of-range"); + if (from == to) // don't detach when no-op + return; + detach(); + T * const b = d->begin(); + if (from < to) + std::rotate(b + from, b + from + 1, b + to + 1); + else + std::rotate(b + to, b + from, b + from + 1); + } // STL-style typedef typename Data::iterator iterator; diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index f9f9ac472a..e6f008d623 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -240,6 +240,9 @@ private slots: void last() const; void lastIndexOf() const; void mid() const; + void moveInt() const; + void moveMovable() const; + void moveCustom() const; void prependInt() const; void prependMovable() const; void prependCustom() const; @@ -312,6 +315,7 @@ private: template void fromList() const; template void insert() const; template void qhash() const; + template void move() const; template void prepend() const; template void remove() const; template void size() const; @@ -350,6 +354,14 @@ const Movable SimpleValue::Values[] = { 110, 105, 101, 114, 111, 98 }; template<> const Custom SimpleValue::Values[] = { 110, 105, 101, 114, 111, 98 }; +// Make some macros for the tests to use in order to be slightly more readable... +#define T_FOO SimpleValue::at(0) +#define T_BAR SimpleValue::at(1) +#define T_BAZ SimpleValue::at(2) +#define T_CAT SimpleValue::at(3) +#define T_DOG SimpleValue::at(4) +#define T_BLAH SimpleValue::at(5) + void tst_QVector::constructors_empty() const { QVector emptyInt; @@ -1604,6 +1616,44 @@ void tst_QVector::qhash() const QCOMPARE(qHash(l1), qHash(l2)); } +template +void tst_QVector::move() const +{ + QVector list; + list << T_FOO << T_BAR << T_BAZ; + + // move an item + list.move(0, list.count() - 1); + QCOMPARE(list, QVector() << T_BAR << T_BAZ << T_FOO); + + // move it back + list.move(list.count() - 1, 0); + QCOMPARE(list, QVector() << T_FOO << T_BAR << T_BAZ); + + // move an item in the middle + list.move(1, 0); + QCOMPARE(list, QVector() << T_BAR << T_FOO << T_BAZ); +} + +void tst_QVector::moveInt() const +{ + move(); +} + +void tst_QVector::moveMovable() const +{ + const int instancesCount = Movable::counter.loadAcquire(); + move(); + QCOMPARE(instancesCount, Movable::counter.loadAcquire()); +} + +void tst_QVector::moveCustom() const +{ + const int instancesCount = Custom::counter.loadAcquire(); + move(); + QCOMPARE(instancesCount, Custom::counter.loadAcquire()); +} + template void tst_QVector::prepend() const {