QVector: add move(int,int) for QList compat
Change-Id: I67948621313f2e7c69abe7ef95ee82ca64c6512a Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
parent
5867e0a7ab
commit
6f530fe4d6
@ -688,6 +688,16 @@
|
|||||||
\sa takeFirst(), takeLast(), QList::takeAt()
|
\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()
|
/*! \fn void QVector::removeFirst()
|
||||||
\since 5.1
|
\since 5.1
|
||||||
Removes the first item in the vector. Calling this function is
|
Removes the first item in the vector. Calling this function is
|
||||||
|
@ -172,6 +172,19 @@ public:
|
|||||||
}
|
}
|
||||||
int length() const { return size(); }
|
int length() const { return size(); }
|
||||||
T takeAt(int i) { T t = at(i); remove(i); return t; }
|
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
|
// STL-style
|
||||||
typedef typename Data::iterator iterator;
|
typedef typename Data::iterator iterator;
|
||||||
|
@ -240,6 +240,9 @@ private slots:
|
|||||||
void last() const;
|
void last() const;
|
||||||
void lastIndexOf() const;
|
void lastIndexOf() const;
|
||||||
void mid() const;
|
void mid() const;
|
||||||
|
void moveInt() const;
|
||||||
|
void moveMovable() const;
|
||||||
|
void moveCustom() const;
|
||||||
void prependInt() const;
|
void prependInt() const;
|
||||||
void prependMovable() const;
|
void prependMovable() const;
|
||||||
void prependCustom() const;
|
void prependCustom() const;
|
||||||
@ -312,6 +315,7 @@ private:
|
|||||||
template<typename T> void fromList() const;
|
template<typename T> void fromList() const;
|
||||||
template<typename T> void insert() const;
|
template<typename T> void insert() const;
|
||||||
template<typename T> void qhash() const;
|
template<typename T> void qhash() const;
|
||||||
|
template<typename T> void move() const;
|
||||||
template<typename T> void prepend() const;
|
template<typename T> void prepend() const;
|
||||||
template<typename T> void remove() const;
|
template<typename T> void remove() const;
|
||||||
template<typename T> void size() const;
|
template<typename T> void size() const;
|
||||||
@ -350,6 +354,14 @@ const Movable SimpleValue<Movable>::Values[] = { 110, 105, 101, 114, 111, 98 };
|
|||||||
template<>
|
template<>
|
||||||
const Custom SimpleValue<Custom>::Values[] = { 110, 105, 101, 114, 111, 98 };
|
const Custom SimpleValue<Custom>::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<T>::at(0)
|
||||||
|
#define T_BAR SimpleValue<T>::at(1)
|
||||||
|
#define T_BAZ SimpleValue<T>::at(2)
|
||||||
|
#define T_CAT SimpleValue<T>::at(3)
|
||||||
|
#define T_DOG SimpleValue<T>::at(4)
|
||||||
|
#define T_BLAH SimpleValue<T>::at(5)
|
||||||
|
|
||||||
void tst_QVector::constructors_empty() const
|
void tst_QVector::constructors_empty() const
|
||||||
{
|
{
|
||||||
QVector<int> emptyInt;
|
QVector<int> emptyInt;
|
||||||
@ -1604,6 +1616,44 @@ void tst_QVector::qhash() const
|
|||||||
QCOMPARE(qHash(l1), qHash(l2));
|
QCOMPARE(qHash(l1), qHash(l2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void tst_QVector::move() const
|
||||||
|
{
|
||||||
|
QVector<T> list;
|
||||||
|
list << T_FOO << T_BAR << T_BAZ;
|
||||||
|
|
||||||
|
// move an item
|
||||||
|
list.move(0, list.count() - 1);
|
||||||
|
QCOMPARE(list, QVector<T>() << T_BAR << T_BAZ << T_FOO);
|
||||||
|
|
||||||
|
// move it back
|
||||||
|
list.move(list.count() - 1, 0);
|
||||||
|
QCOMPARE(list, QVector<T>() << T_FOO << T_BAR << T_BAZ);
|
||||||
|
|
||||||
|
// move an item in the middle
|
||||||
|
list.move(1, 0);
|
||||||
|
QCOMPARE(list, QVector<T>() << T_BAR << T_FOO << T_BAZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QVector::moveInt() const
|
||||||
|
{
|
||||||
|
move<int>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QVector::moveMovable() const
|
||||||
|
{
|
||||||
|
const int instancesCount = Movable::counter.loadAcquire();
|
||||||
|
move<Movable>();
|
||||||
|
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QVector::moveCustom() const
|
||||||
|
{
|
||||||
|
const int instancesCount = Custom::counter.loadAcquire();
|
||||||
|
move<Custom>();
|
||||||
|
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void tst_QVector::prepend() const
|
void tst_QVector::prepend() const
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user