summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-06-27 11:14:32 +0200
committerLars Knoll <lars.knoll@qt.io>2020-07-06 21:31:25 +0200
commit792cb3989af2918f6a2878c22db51a009310756f (patch)
tree463b61ee3fc5deea2eb58fc8ecb30f8a5638ae74 /src
parent9a9a1a2a2e0c8bcbbaa178be3d67add3876df9b7 (diff)
Add support for first(n), last(n) and sliced() to QList
This keeps the API symmetric with what we have in our string classes. Change-Id: I94c5b39b718ca2472f9ca645e7a42e4314636f67 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qlist.h23
-rw-r--r--src/corelib/tools/qlist.qdoc49
2 files changed, 72 insertions, 0 deletions
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<T> mid(qsizetype pos, qsizetype len = -1) const;
+ QList<T> first(qsizetype n) const
+ {
+ Q_ASSERT(size_t(n) <= size_t(size()));
+ return QList<T>(begin(), begin() + n);
+ }
+ QList<T> last(qsizetype n) const
+ {
+ Q_ASSERT(size_t(n) <= size_t(size()));
+ return QList<T>(end() - n, end());
+ }
+ QList<T> sliced(qsizetype pos) const
+ {
+ Q_ASSERT(size_t(pos) <= size_t(size()));
+ return QList<T>(begin() + pos, end());
+ }
+ QList<T> 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<T>(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 <typename T> QList<T> QList<T>::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 <typename T> QList<T> QList<T>::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 <typename T> QList<T> QList<T>::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 <typename T> QList<T> QList<T>::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 <typename T> QList<T>::QList()