diff options
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qhash.h | 3 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 34 | ||||
-rw-r--r-- | src/corelib/tools/qmap.h | 3 |
3 files changed, 28 insertions, 12 deletions
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index f1030ae..3374c80 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -625,6 +625,7 @@ template <class Key, class T> Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::uniqueKeys() const { QList<Key> res; + res.reserve(size()); // May be too much, but assume short lifetime const_iterator i = begin(); if (i != end()) { for (;;) { @@ -644,6 +645,7 @@ template <class Key, class T> Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::keys() const { QList<Key> res; + res.reserve(size()); const_iterator i = begin(); while (i != end()) { res.append(i.key()); @@ -688,6 +690,7 @@ template <class Key, class T> Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values() const { QList<T> res; + res.reserve(size()); const_iterator i = begin(); while (i != end()) { res.append(i.value()); diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 3a29e13..c6dd106 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -525,7 +525,8 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t) PodNode cpy = *reinterpret_cast<const PodNode *>(&t); Node *n = reinterpret_cast<Node *>(p.append()); QT_TRY { - node_construct(n, *reinterpret_cast<const T *>(&cpy)); + void *ptr = &cpy; + node_construct(n, *reinterpret_cast<T *>(ptr)); } QT_CATCH(...) { --d->end; QT_RETHROW; @@ -559,7 +560,8 @@ inline void QList<T>::prepend(const T &t) PodNode cpy = *reinterpret_cast<const PodNode *>(&t); Node *n = reinterpret_cast<Node *>(p.prepend()); QT_TRY { - node_construct(n, *reinterpret_cast<const T *>(&cpy)); + void *ptr = &cpy; + node_construct(n, *reinterpret_cast<T *>(ptr)); } QT_CATCH(...) { ++d->begin; QT_RETHROW; @@ -593,7 +595,8 @@ inline void QList<T>::insert(int i, const T &t) PodNode cpy = *reinterpret_cast<const PodNode *>(&t); Node *n = reinterpret_cast<Node *>(p.insert(i)); QT_TRY { - node_construct(n, *reinterpret_cast<const T *>(&cpy)); + void *ptr = &cpy; + node_construct(n, *reinterpret_cast<T *>(ptr)); } QT_CATCH(...) { p.remove(i); QT_RETHROW; @@ -808,15 +811,22 @@ Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList< template <typename T> Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l) { - Node *n = (d->ref != 1) - ? detach_helper_grow(INT_MAX, l.size()) - : reinterpret_cast<Node *>(p.append2(l.p)); - QT_TRY{ - node_copy(n, reinterpret_cast<Node *>(p.end()), reinterpret_cast<Node *>(l.p.begin())); - } QT_CATCH(...) { - // restore the old end - d->end -= int(reinterpret_cast<Node *>(p.end()) - n); - QT_RETHROW; + if (!l.isEmpty()) { + if (isEmpty()) { + *this = l; + } else { + Node *n = (d->ref != 1) + ? detach_helper_grow(INT_MAX, l.size()) + : reinterpret_cast<Node *>(p.append2(l.p)); + QT_TRY { + node_copy(n, reinterpret_cast<Node *>(p.end()), + reinterpret_cast<Node *>(l.p.begin())); + } QT_CATCH(...) { + // restore the old end + d->end -= int(reinterpret_cast<Node *>(p.end()) - n); + QT_RETHROW; + } + } } return *this; } diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 2e21547..df0ae46 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -773,6 +773,7 @@ template <class Key, class T> Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::uniqueKeys() const { QList<Key> res; + res.reserve(size()); // May be too much, but assume short lifetime const_iterator i = begin(); if (i != end()) { for (;;) { @@ -792,6 +793,7 @@ template <class Key, class T> Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const { QList<Key> res; + res.reserve(size()); const_iterator i = begin(); while (i != end()) { res.append(i.key()); @@ -836,6 +838,7 @@ template <class Key, class T> Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values() const { QList<T> res; + res.reserve(size()); const_iterator i = begin(); while (i != end()) { res.append(i.value()); |