From 059857d4d5dc5862d0135c839e7dabaa7331e4b5 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 5 Oct 2013 03:48:45 +0200 Subject: 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 --- src/corelib/tools/qvector.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/corelib/tools/qvector.h | 23 ++++++++++++++--------- 2 files changed, 52 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index 69b656c191..11990d30b2 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -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 diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 3d86440fcd..f56511edbf 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -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 mid(int pos, int length = -1) const; + QVector 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::count(const T &t) const } template -Q_OUTOFLINE_TEMPLATE QVector QVector::mid(int pos, int length) const +Q_OUTOFLINE_TEMPLATE QVector QVector::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 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; } -- cgit v1.2.3