From 48f1ebc0404983381ddd2cc2a3b5ecc1a12785c3 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 4 Dec 2013 14:11:39 +0100 Subject: Add QVarLengthArray::{indexOf,lastIndexOf,contains} functions [ChangeLog][QtCore][QVarLengthArray] Added the indexOf, lastIndexOf and contains functions to QVarLengthArray. These functions make the class more similar to QVector. Change-Id: I9bd2b22bd8b7151c2d17aede36e5f2126570600b Reviewed-by: Oswald Buddenhagen Reviewed-by: Olivier Goffart --- src/corelib/tools/qvarlengtharray.h | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/corelib/tools/qvarlengtharray.h') diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 309d2a2dca..bfa0bbbbb1 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -107,6 +107,10 @@ public: inline int capacity() const { return a; } inline void reserve(int size); + inline int indexOf(const T &t, int from = 0) const; + inline int lastIndexOf(const T &t, int from = -1) const; + inline bool contains(const T &t) const; + inline T &operator[](int idx) { Q_ASSERT(idx >= 0 && idx < s); return ptr[idx]; @@ -228,6 +232,51 @@ template Q_INLINE_TEMPLATE void QVarLengthArray::reserve(int asize) { if (asize > a) realloc(s, asize); } +template +Q_INLINE_TEMPLATE int QVarLengthArray::indexOf(const T &t, int from) const +{ + if (from < 0) + from = qMax(from + s, 0); + if (from < s) { + T *n = ptr + from - 1; + T *e = ptr + s; + while (++n != e) + if (*n == t) + return n - ptr; + } + return -1; +} + +template +Q_INLINE_TEMPLATE int QVarLengthArray::lastIndexOf(const T &t, int from) const +{ + if (from < 0) + from += s; + else if (from >= s) + from = s - 1; + if (from >= 0) { + T *b = ptr; + T *n = ptr + from + 1; + while (n != b) { + if (*--n == t) + return n - b; + } + } + return -1; +} + +template +Q_INLINE_TEMPLATE bool QVarLengthArray::contains(const T &t) const +{ + T *b = ptr; + T *i = ptr + s; + while (i != b) { + if (*--i == t) + return true; + } + return false; +} + template Q_OUTOFLINE_TEMPLATE void QVarLengthArray::append(const T *abuf, int increment) { -- cgit v1.2.3