diff options
Diffstat (limited to 'src/corelib/tools/qvector.h')
-rw-r--r-- | src/corelib/tools/qvector.h | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 6f7c534085..f09f1a3c41 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -156,6 +156,24 @@ public: // QList compatibility void removeAt(int i) { remove(i); } + int removeAll(const T &t) + { + const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t); + if (cit == ce) + return 0; + const iterator e = end(), it = std::remove(c2m(cit), e, t); + const int result = std::distance(it, e); + erase(it, e); + return result; + } + bool removeOne(const T &t) + { + const int i = indexOf(t); + if (i < 0) + return false; + remove(i); + return true; + } int length() const { return size(); } T takeAt(int i) { T t = at(i); remove(i); return t; } @@ -252,6 +270,7 @@ private: { return (i <= d->end()) && (d->begin() <= i); } + iterator c2m(const_iterator it) { return begin() + (it - cbegin()); } class AlignmentDummy { Data header; T array[1]; }; }; @@ -791,29 +810,30 @@ bool QVector<T>::contains(const T &t) const template <typename T> int QVector<T>::count(const T &t) const { - int c = 0; - T* b = d->begin(); - T* i = d->end(); - while (i != b) - if (*--i == t) - ++c; - return c; + return int(std::count(cbegin(), cend(), t)); } template <typename T> Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int len) const { - if (len < 0) - len = size() - pos; - if (pos == 0 && len == size()) + using namespace QtPrivate; + switch (QContainerImplHelper::mid(d->size, &pos, &len)) { + case QContainerImplHelper::Null: + case QContainerImplHelper::Empty: + return QVector<T>(); + case QContainerImplHelper::Full: return *this; - if (pos + len > size()) - len = size() - pos; - QVector<T> copy; - copy.reserve(len); - for (int i = pos; i < pos + len; ++i) - copy += at(i); - return copy; + case QContainerImplHelper::Subset: + break; + } + + QVector<T> midResult; + midResult.reallocData(0, len); + T *srcFrom = d->begin() + pos; + T *srcTo = d->begin() + pos + len; + midResult.copyConstruct(srcFrom, srcTo, midResult.data()); + midResult.d->size = len; + return midResult; } template <typename T> |