summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qvector.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qvector.h')
-rw-r--r--src/corelib/tools/qvector.h36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h
index 4ff2b9f8e0..3d86440fcd 100644
--- a/src/corelib/tools/qvector.h
+++ b/src/corelib/tools/qvector.h
@@ -56,6 +56,8 @@
#include <initializer_list>
#endif
+#include <algorithm>
+
QT_BEGIN_NAMESPACE
class QRegion;
@@ -227,18 +229,22 @@ public:
static QVector<T> fromList(const QList<T> &list);
static inline QVector<T> fromStdVector(const std::vector<T> &vector)
- { QVector<T> tmp; tmp.reserve(int(vector.size())); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
+ { QVector<T> tmp; tmp.reserve(int(vector.size())); std::copy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
inline std::vector<T> toStdVector() const
- { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
+ { std::vector<T> tmp; tmp.reserve(size()); std::copy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
private:
friend class QRegion; // Optimization for QRegion::rects()
void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default);
+ void reallocData(const int sz) { reallocData(sz, d->alloc); }
void freeData(Data *d);
void defaultConstruct(T *from, T *to);
void copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom);
void destruct(T *from, T *to);
-
+ bool isValidIterator(const iterator &i) const
+ {
+ return (i <= d->end()) && (d->begin() <= i);
+ }
class AlignmentDummy { Data header; T array[1]; };
};
@@ -532,7 +538,7 @@ void QVector<T>::reallocData(const int asize, const int aalloc, QArrayData::Allo
template<typename T>
Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
{
- if (i < 0 || i >= d->size) {
+ if (uint(i) >= uint(d->size)) {
return T();
}
return d->begin()[i];
@@ -540,7 +546,7 @@ Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
template<typename T>
Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
{
- return ((i < 0 || i >= d->size) ? defaultValue : d->begin()[i]);
+ return uint(i) >= uint(d->size) ? defaultValue : d->begin()[i];
}
template <typename T>
@@ -560,24 +566,25 @@ void QVector<T>::append(const T &t)
}
template <typename T>
-inline void QVector<T>::removeLast()
+void QVector<T>::removeLast()
{
Q_ASSERT(!isEmpty());
+ Q_ASSERT(d->alloc);
- if (d->alloc) {
- if (d->ref.isShared()) {
- reallocData(d->size - 1, int(d->alloc));
- return;
- }
- if (QTypeInfo<T>::isComplex)
- (d->data() + d->size - 1)->~T();
+ if (!d->ref.isShared()) {
--d->size;
+ if (QTypeInfo<T>::isComplex)
+ (d->data() + d->size)->~T();
+ } else {
+ reallocData(d->size - 1);
}
}
template <typename T>
typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
{
+ Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid");
+
int offset = std::distance(d->begin(), before);
if (n != 0) {
const T copy(t);
@@ -611,6 +618,9 @@ typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, c
template <typename T>
typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
{
+ Q_ASSERT_X(isValidIterator(abegin), "QVector::erase", "The specified iterator argument 'abegin' is invalid");
+ Q_ASSERT_X(isValidIterator(aend), "QVector::erase", "The specified iterator argument 'aend' is invalid");
+
const int itemsToErase = aend - abegin;
if (!itemsToErase)