From 510660080de6fab87e117de2663c33eff5ae451b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Wed, 13 Feb 2013 17:48:49 +0100 Subject: QVector - add remove functions This patch adds the functions removeFirst() and removeLast(). Functions that QList has. Beside making these functions, pop_back and pop_front are redirected to these rather than calling erase. Change-Id: Ifc5f8a78e33f436f06f21095a920ec5d4311fd6f Reviewed-by: Thiago Macieira --- src/corelib/tools/qvector.cpp | 24 ++++++++++++++++++++++-- src/corelib/tools/qvector.h | 6 ++++-- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index efc4b6a446..5ece011f0b 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -544,6 +544,26 @@ \sa insert(), replace(), fill() */ +/*! \fn void QVector::removeFirst() + \since 5.1 + Removes the first item in the vector. Calling this function is + equivalent to calling remove(0). The vector must not be empty. If + the vector can be empty, call isEmpty() before calling this + function. + + \sa remove(), isEmpty() +*/ + +/*! \fn void QVector::removeLast() + \since 5.1 + Removes the last item in the vector. Calling this function is + equivalent to calling remove(size() - 1). The vector must not be + empty. If the vector can be empty, call isEmpty() before calling + this function. + + \sa remove(), removeFirst(), isEmpty() +*/ + /*! \fn QVector &QVector::fill(const T &value, int size = -1) Assigns \a value to all items in the vector. If \a size is @@ -773,13 +793,13 @@ /*! \fn void QVector::pop_front() This function is provided for STL compatibility. It is equivalent - to erase(begin()). + to removeFirst(). */ /*! \fn void QVector::pop_back() This function is provided for STL compatibility. It is equivalent - to erase(end() - 1). + to removeLast(). */ /*! \fn T& QVector::front() diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 7d5cf91324..f6fe316a15 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -138,6 +138,8 @@ public: void replace(int i, const T &t); void remove(int i); void remove(int i, int n); + inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(d->begin()); } + inline void removeLast() { Q_ASSERT(!isEmpty()); erase(d->end() - 1); } QVector &fill(const T &t, int size = -1); @@ -198,8 +200,8 @@ public: typedef int size_type; inline void push_back(const T &t) { append(t); } inline void push_front(const T &t) { prepend(t); } - void pop_back() { Q_ASSERT(!isEmpty()); erase(d->end() - 1); } - void pop_front() { Q_ASSERT(!isEmpty()); erase(d->begin()); } + void pop_back() { removeLast(); } + void pop_front() { removeFirst(); } inline bool empty() const { return d->size == 0; } inline T& front() { return first(); } -- cgit v1.2.3