diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-11-04 11:10:56 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-11-09 17:35:59 +0100 |
commit | f43828687581c2d89549225d1cdc7518bde43b65 (patch) | |
tree | 908ea1f22f0e6ad39a7e1e41b2dbf4e6d0e33668 /src/corelib/tools/qlist.h | |
parent | 13fcd02ff906a00593fabffe3620348da8e42755 (diff) |
Implement fast paths for removeFirst() and removeLast()
This avoids lots of the code and checks in remove() making the
methods a lot faster.
Change-Id: If99c39f5b55672b341f9331b5903bf77e9e67477
Reviewed-by: Andrei Golubev <andrei.golubev@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 | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index b4070a27fc..6fe3522f6f 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -377,8 +377,8 @@ public: } void remove(qsizetype i, qsizetype n = 1); - void removeFirst() { Q_ASSERT(!isEmpty()); remove(0); } - void removeLast() { Q_ASSERT(!isEmpty()); remove(size() - 1); } + void removeFirst(); + void removeLast(); value_type takeFirst() { Q_ASSERT(!isEmpty()); value_type v = std::move(first()); remove(0); return v; } value_type takeLast() { Q_ASSERT(!isEmpty()); value_type v = std::move(last()); remove(size() - 1); return v; } @@ -655,8 +655,26 @@ inline void QList<T>::remove(qsizetype i, qsizetype n) // we're detached and we can just move data around d->erase(d->begin() + i, d->begin() + i + n); } + +template <typename T> +inline void QList<T>::removeFirst() +{ + Q_ASSERT(!isEmpty()); + if (d->needsDetach()) + d.detach(); + d->eraseFirst(); } +template <typename T> +inline void QList<T>::removeLast() +{ + Q_ASSERT(!isEmpty()); + if (d->needsDetach()) + detach(); + d->eraseLast(); +} + + template<typename T> inline T QList<T>::value(qsizetype i, parameter_type defaultValue) const { |