summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qvarlengtharray.h49
-rw-r--r--src/corelib/tools/qvarlengtharray.qdoc39
-rw-r--r--tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp65
3 files changed, 153 insertions, 0 deletions
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];
@@ -229,6 +233,51 @@ Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
{ if (asize > a) realloc(s, asize); }
template <class T, int Prealloc>
+Q_INLINE_TEMPLATE int QVarLengthArray<T, Prealloc>::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 <class T, int Prealloc>
+Q_INLINE_TEMPLATE int QVarLengthArray<T, Prealloc>::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 <class T, int Prealloc>
+Q_INLINE_TEMPLATE bool QVarLengthArray<T, Prealloc>::contains(const T &t) const
+{
+ T *b = ptr;
+ T *i = ptr + s;
+ while (i != b) {
+ if (*--i == t)
+ return true;
+ }
+ return false;
+}
+
+template <class T, int Prealloc>
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int increment)
{
Q_ASSERT(abuf);
diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc
index db435739fc..05d9258c86 100644
--- a/src/corelib/tools/qvarlengtharray.qdoc
+++ b/src/corelib/tools/qvarlengtharray.qdoc
@@ -671,3 +671,42 @@
\sa append(), operator<<()
*/
+/*! \fn int QVarLengthArray::indexOf(const T &value, int from = 0) const
+
+ \since 5.3
+ Returns the index position of the first occurrence of \a value in
+ the array, searching forward from index position \a from.
+ Returns -1 if no item matched.
+
+ This function requires the value type to have an implementation of
+ \c operator==().
+
+ \sa lastIndexOf(), contains()
+*/
+
+/*! \fn int QVarLengthArray::lastIndexOf(const T &value, int from = -1) const
+
+ \since 5.3
+ Returns the index position of the last occurrence of the value \a
+ value in the array, searching backward from index position \a
+ from. If \a from is -1 (the default), the search starts at the
+ last item. Returns -1 if no item matched.
+
+ This function requires the value type to have an implementation of
+ \c operator==().
+
+ \sa indexOf(), contains()
+*/
+
+/*! \fn bool QVarLengthArray::contains(const T &value) const
+
+ \since 5.3
+ Returns \c true if the array contains an occurrence of \a value;
+ otherwise returns \c false.
+
+ This function requires the value type to have an implementation of
+ \c operator==().
+
+ \sa indexOf(), lastIndexOf()
+*/
+
diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
index c19080e345..1507fc7007 100644
--- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
+++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
@@ -59,6 +59,9 @@ private slots:
void first();
void last();
void squeeze();
+ void indexOf();
+ void lastIndexOf();
+ void contains();
};
int fooCtor = 0;
@@ -677,5 +680,67 @@ void tst_QVarLengthArray::squeeze()
QCOMPARE(list.capacity(), sizeOnHeap);
}
+void tst_QVarLengthArray::indexOf()
+{
+ QVarLengthArray<QString> myvec;
+ myvec << "A" << "B" << "C" << "B" << "A";
+
+ QVERIFY(myvec.indexOf("B") == 1);
+ QVERIFY(myvec.indexOf("B", 1) == 1);
+ QVERIFY(myvec.indexOf("B", 2) == 3);
+ QVERIFY(myvec.indexOf("X") == -1);
+ QVERIFY(myvec.indexOf("X", 2) == -1);
+
+ // add an X
+ myvec << "X";
+ QVERIFY(myvec.indexOf("X") == 5);
+ QVERIFY(myvec.indexOf("X", 5) == 5);
+ QVERIFY(myvec.indexOf("X", 6) == -1);
+
+ // remove first A
+ myvec.remove(0);
+ QVERIFY(myvec.indexOf("A") == 3);
+ QVERIFY(myvec.indexOf("A", 3) == 3);
+ QVERIFY(myvec.indexOf("A", 4) == -1);
+}
+
+void tst_QVarLengthArray::lastIndexOf()
+{
+ QVarLengthArray<QString> myvec;
+ myvec << "A" << "B" << "C" << "B" << "A";
+
+ QVERIFY(myvec.lastIndexOf("B") == 3);
+ QVERIFY(myvec.lastIndexOf("B", 2) == 1);
+ QVERIFY(myvec.lastIndexOf("X") == -1);
+ QVERIFY(myvec.lastIndexOf("X", 2) == -1);
+
+ // add an X
+ myvec << "X";
+ QVERIFY(myvec.lastIndexOf("X") == 5);
+ QVERIFY(myvec.lastIndexOf("X", 5) == 5);
+ QVERIFY(myvec.lastIndexOf("X", 3) == -1);
+
+ // remove first A
+ myvec.remove(0);
+ QVERIFY(myvec.lastIndexOf("A") == 3);
+ QVERIFY(myvec.lastIndexOf("A", 3) == 3);
+ QVERIFY(myvec.lastIndexOf("A", 2) == -1);
+}
+
+void tst_QVarLengthArray::contains()
+{
+ QVarLengthArray<QString> myvec;
+ myvec << "aaa" << "bbb" << "ccc";
+
+ QVERIFY(myvec.contains(QLatin1String("aaa")));
+ QVERIFY(myvec.contains(QLatin1String("bbb")));
+ QVERIFY(myvec.contains(QLatin1String("ccc")));
+ QVERIFY(!myvec.contains(QLatin1String("I don't exist")));
+
+ // add it and make sure it does :)
+ myvec.append(QLatin1String("I don't exist"));
+ QVERIFY(myvec.contains(QLatin1String("I don't exist")));
+}
+
QTEST_APPLESS_MAIN(tst_QVarLengthArray)
#include "tst_qvarlengtharray.moc"