diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-06-27 11:14:32 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-06 21:31:25 +0200 |
commit | 792cb3989af2918f6a2878c22db51a009310756f (patch) | |
tree | 463b61ee3fc5deea2eb58fc8ecb30f8a5638ae74 /src/corelib/tools/qlist.h | |
parent | 9a9a1a2a2e0c8bcbbaa178be3d67add3876df9b7 (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/corelib/tools/qlist.h')
-rw-r--r-- | src/corelib/tools/qlist.h | 23 |
1 files changed, 23 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; |