diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 255816d0ba..5d6cf9fe15 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -377,6 +377,12 @@ private: { return (constBegin().i <= i.i) && (i.i <= constEnd().i); } + +private: + inline bool contains_impl(const T &, QListData::NotArrayCompatibleLayout) const; + inline bool contains_impl(const T &, QListData::ArrayCompatibleLayout) const; + inline int count_impl(const T &, QListData::NotArrayCompatibleLayout) const; + inline int count_impl(const T &, QListData::ArrayCompatibleLayout) const; }; #if defined(Q_CC_BOR) @@ -940,6 +946,12 @@ Q_OUTOFLINE_TEMPLATE int QList::lastIndexOf(const T &t, int from) const template Q_OUTOFLINE_TEMPLATE bool QList::contains(const T &t) const +{ + return contains_impl(t, MemoryLayout()); +} + +template +inline bool QList::contains_impl(const T &t, QListData::NotArrayCompatibleLayout) const { Node *e = reinterpret_cast(p.end()); Node *i = reinterpret_cast(p.begin()); @@ -949,8 +961,22 @@ Q_OUTOFLINE_TEMPLATE bool QList::contains(const T &t) const return false; } +template +inline bool QList::contains_impl(const T &t, QListData::ArrayCompatibleLayout) const +{ + const T *b = reinterpret_cast(p.begin()); + const T *e = reinterpret_cast(p.end()); + return std::find(b, e, t) != e; +} + template Q_OUTOFLINE_TEMPLATE int QList::count(const T &t) const +{ + return this->count_impl(t, MemoryLayout()); +} + +template +inline int QList::count_impl(const T &t, QListData::NotArrayCompatibleLayout) const { int c = 0; Node *e = reinterpret_cast(p.end()); @@ -961,6 +987,14 @@ Q_OUTOFLINE_TEMPLATE int QList::count(const T &t) const return c; } +template +inline int QList::count_impl(const T &t, QListData::ArrayCompatibleLayout) const +{ + return int(std::count(reinterpret_cast(p.begin()), + reinterpret_cast(p.end()), + t)); +} + Q_DECLARE_SEQUENTIAL_ITERATOR(List) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List)