From 5e01d3f64265867284e6bba8d1881e10c5d7605c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 1 Oct 2014 10:10:29 +0200 Subject: [PATCH] QList: iterate forward in count()/contains() After much head-scratching, we found no reason for the backwards iteration. Indeed, forward iteration should be slightly faster than backwards, because it operates in the direction in which cache-lines are filled, usually. This is in preparation of using std algorithms instead of hand-written loops. It avoids having to use std::reverse_iterator. Change-Id: Ib62cf0a6f2a33d186cb174b23b0d6bb2891b6c63 Reviewed-by: Thiago Macieira --- src/corelib/tools/qlist.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index b56afe15c2..9704c7b953 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -921,9 +921,9 @@ Q_OUTOFLINE_TEMPLATE int QList::lastIndexOf(const T &t, int from) const template Q_OUTOFLINE_TEMPLATE bool QList::contains(const T &t) const { - Node *b = reinterpret_cast(p.begin()); - Node *i = reinterpret_cast(p.end()); - while (i-- != b) + Node *e = reinterpret_cast(p.end()); + Node *i = reinterpret_cast(p.begin()); + for (; i != e; ++i) if (i->t() == t) return true; return false; @@ -933,9 +933,9 @@ template Q_OUTOFLINE_TEMPLATE int QList::count(const T &t) const { int c = 0; - Node *b = reinterpret_cast(p.begin()); - Node *i = reinterpret_cast(p.end()); - while (i-- != b) + Node *e = reinterpret_cast(p.end()); + Node *i = reinterpret_cast(p.begin()); + for (; i != e; ++i) if (i->t() == t) ++c; return c;