QVector: add some functions missing for QList compat

Eases migration from QList to QVector.

Had to rename the 'length' parameter to mid() to suppress
-Wshadow warnings.

Task-number: QTBUG-3781

Change-Id: I755c6caefe4de81ea42a81b1c76aab728e639613
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Marc Mutz 2013-10-05 03:48:45 +02:00 committed by The Qt Project
parent 0e96e47deb
commit 059857d4d5
2 changed files with 52 additions and 9 deletions

View File

@ -562,6 +562,44 @@
\sa insert(), replace(), fill()
*/
/*! \fn void QVector::removeAt(int i)
\since 5.2
Equivalent to
\code
remove(i);
\endcode
Provided for compatibility with QList.
\sa remove(int), QList::removeAt(int)
*/
/*! \fn int QVector::length() const
\since 5.2
Same as size() and count().
Provided for compatibility with QList.
\sa size(), count(), QList::length()
*/
/*! \fn T QVector::takeAt(int i)
\since 5.2
Equivalent to
\code
T t = at(i);
remove(i);
return t;
\endcode
Provided for compatibility with QList.
\sa takeFirst(), takeLast(), QList::takeAt(int)
*/
/*! \fn void QVector::removeFirst()
\since 5.1
Removes the first item in the vector. Calling this function is

View File

@ -152,6 +152,11 @@ public:
bool contains(const T &t) const;
int count(const T &t) const;
// QList compatibility
void removeAt(int i) { remove(i); }
int length() const { return size(); }
T takeAt(int i) { T t = at(i); remove(i); return t; }
// STL-style
typedef typename Data::iterator iterator;
typedef typename Data::const_iterator const_iterator;
@ -187,7 +192,7 @@ public:
inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
QVector<T> mid(int pos, int length = -1) const;
QVector<T> mid(int pos, int len = -1) const;
T value(int i) const;
T value(int i, const T &defaultValue) const;
@ -772,17 +777,17 @@ int QVector<T>::count(const T &t) const
}
template <typename T>
Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const
Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int len) const
{
if (length < 0)
length = size() - pos;
if (pos == 0 && length == size())
if (len < 0)
len = size() - pos;
if (pos == 0 && len == size())
return *this;
if (pos + length > size())
length = size() - pos;
if (pos + len > size())
len = size() - pos;
QVector<T> copy;
copy.reserve(length);
for (int i = pos; i < pos + length; ++i)
copy.reserve(len);
for (int i = pos; i < pos + len; ++i)
copy += at(i);
return copy;
}