diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 6a699aa197..3b9eb154ed 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -378,6 +378,29 @@ public: inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; } QList mid(qsizetype pos, qsizetype len = -1) const; + QList first(qsizetype n) const + { + Q_ASSERT(size_t(n) <= size_t(size())); + return QList(begin(), begin() + n); + } + QList last(qsizetype n) const + { + Q_ASSERT(size_t(n) <= size_t(size())); + return QList(end() - n, end()); + } + QList sliced(qsizetype pos) const + { + Q_ASSERT(size_t(pos) <= size_t(size())); + return QList(begin() + pos, end()); + } + QList sliced(qsizetype pos, qsizetype n) const + { + Q_ASSERT(size_t(pos) <= size_t(size())); + Q_ASSERT(n >= 0); + Q_ASSERT(pos + n <= size()); + return QList(begin() + pos, begin() + pos + n); + } + T value(qsizetype i) const { return value(i, T()); } T value(qsizetype i, const T &defaultValue) const; diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc index 550663ed0e..047c418469 100644 --- a/src/corelib/tools/qlist.qdoc +++ b/src/corelib/tools/qlist.qdoc @@ -202,6 +202,55 @@ are included. */ +/*! + \fn template QList QList::first(qsizetype n) const + \since 6.0 + + Returns a sub-list that contains the first \a n elements + of this list. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa last(), sliced() +*/ + +/*! + \fn template QList QList::last(qsizetype n) const + \since 6.0 + + Returns a sub-list that contains the last \a n elements of this list. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa first(), sliced() +*/ + +/*! + \fn template QList QList::sliced(qsizetype pos, qsizetype n) const + \since 6.0 + + Returns a sub-list that contains \a n elements of this list, + starting at position \a pos. + + \note The behavior is undefined when \a pos < 0, \a n < 0, + or \a pos + \a n > size(). + + \sa first(), last() +*/ + +/*! + \fn template QList QList::sliced(qsizetype pos) const + \since 6.0 + \overload + + Returns a sub-list that contains the elements of this list starting at + position \a pos and extending to its end. + + \note The behavior is undefined when \a pos < 0 or \a pos > size(). + + \sa first(), last() +*/ + /*! \fn template QList::QList() diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index ddf5e9c30a..fbd5cf6402 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -267,6 +267,7 @@ private slots: void last() const; void lastIndexOf() const; void mid() const; + void sliced() const; void moveInt() const; void moveMovable() const; void moveCustom() const; @@ -1287,6 +1288,13 @@ void tst_QList::first() const myvec.prepend(23); QCOMPARE(myvec.first(), 23); QCOMPARE(myvec.constFirst(), 23); + + + QCOMPARE(QList().first(0), QList()); + QCOMPARE(myvec.first(0), QList()); + QCOMPARE(myvec.first(1), (QList{23})); + QCOMPARE(myvec.first(2), (QList{23, 42})); + QCOMPARE(myvec.first(3), myvec); } void tst_QList::constFirst() const @@ -1546,6 +1554,12 @@ void tst_QList::last() const myvec.remove(3); QCOMPARE(myvec.last(), QLatin1String("C")); QCOMPARE(myvec.constLast(), QLatin1String("C")); + + QCOMPARE(QList().last(0), QList()); + QCOMPARE(myvec.last(0), QList()); + QCOMPARE(myvec.last(1), (QList{QLatin1String("C")})); + QCOMPARE(myvec.last(2), (QList{QLatin1String("B"), QLatin1String("C")})); + QCOMPARE(myvec.last(3), myvec); } void tst_QList::constLast() const @@ -1648,6 +1662,22 @@ void tst_QList::mid() const QCOMPARE(list.mid(4), QList() << "buck" << "hello" << "kitty"); } +void tst_QList::sliced() const +{ + QList list; + list << "foo" << "bar" << "baz" << "bak" << "buck" << "hello" << "kitty"; + + QCOMPARE(QList().sliced(0), QList()); + QCOMPARE(QList().sliced(0, 0), QList()); + QCOMPARE(list.sliced(3, 3), QList() << "bak" << "buck" << "hello"); + QCOMPARE(list.sliced(3), QList() << "bak" << "buck" << "hello" << "kitty"); + QCOMPARE(list.sliced(6, 1), QList() << "kitty"); + QCOMPARE(list.sliced(6), QList() << "kitty"); + QCOMPARE(list.sliced(0, list.size()), list); + QCOMPARE(list.sliced(0), list); + QCOMPARE(list.sliced(4), QList() << "buck" << "hello" << "kitty"); +} + template void tst_QList::qhash() const {