summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qlist.h
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@nokia.com>2010-01-28 21:07:18 +0100
committerOswald Buddenhagen <oswald.buddenhagen@nokia.com>2010-02-16 19:10:38 +0100
commitc03b21d272f617b037ad9d0ccaa2f442638bf302 (patch)
tree81ee508df0a38dc2decd6017f39e777af0416574 /src/corelib/tools/qlist.h
parent5723f1bcb538a5624cbed871c34507ff26285878 (diff)
optimize QList::mid()
instead of append()ing each element separately, reserve enough space and do a low-level node copy. obviously, the gain for small simple types is the biggest. Reviewed-by: joao Reviewed-by: denis
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r--src/corelib/tools/qlist.h15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h
index 3b9eefa1f8..8c75c985a4 100644
--- a/src/corelib/tools/qlist.h
+++ b/src/corelib/tools/qlist.h
@@ -589,11 +589,20 @@ Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int alength) const
alength = size() - pos;
if (pos == 0 && alength == size())
return *this;
- QList<T> cpy;
if (pos + alength > size())
alength = size() - pos;
- for (int i = pos; i < pos + alength; ++i)
- cpy += at(i);
+ QList<T> cpy;
+ cpy.reserve(alength);
+ cpy.d->end = alength;
+ QT_TRY {
+ cpy.node_copy(reinterpret_cast<Node *>(cpy.p.begin()),
+ reinterpret_cast<Node *>(cpy.p.end()),
+ reinterpret_cast<Node *>(p.begin() + pos));
+ } QT_CATCH(...) {
+ // restore the old end
+ cpy.d->end = 0;
+ QT_RETHROW;
+ }
return cpy;
}