1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qalgorithms.h>
46 #include <QtCore/qiterator.h>
47 #include <QtCore/qlist.h>
48 #include <QtCore/qrefcount.h>
56 #ifdef Q_COMPILER_INITIALIZER_LISTS
57 #include <initializer_list>
65 struct Q_CORE_EXPORT QVectorData
67 QtPrivate::RefCount ref;
70 #if defined(QT_ARCH_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED)
71 // workaround for bug in gcc 3.4.2
81 static const QVectorData shared_null;
82 // ### Qt 5: rename to 'allocate()'. The current name causes problems for
83 // some debugges when the QVector is member of a class within an unnamed namespace.
84 // ### Qt 5: can be removed completely. (Ralf)
85 static QVectorData *malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init);
86 static QVectorData *allocate(int size, int alignment);
87 static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
88 static void free(QVectorData *data, int alignment);
89 static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive);
93 struct QVectorTypedData : private QVectorData
94 { // private inheritance as we must not access QVectorData member thought QVectorTypedData
95 // as this would break strict aliasing rules. (in the case of shared_null)
98 static inline void free(QVectorTypedData<T> *x, int alignment) { QVectorData::free(static_cast<QVectorData *>(x), alignment); }
103 template <typename T>
106 typedef QVectorTypedData<T> Data;
109 #if defined(Q_CC_SUN) && (__SUNPRO_CC <= 0x550)
110 QVectorTypedData<T> *p;
117 // ### Qt 5: Consider making QVector non-shared to get at least one
118 // "really fast" container. See tests/benchmarks/corelib/tools/qvector/
119 inline QVector() : d(const_cast<QVectorData *>(&QVectorData::shared_null)) { }
120 explicit QVector(int size);
121 QVector(int size, const T &t);
122 inline QVector(const QVector<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
123 inline ~QVector() { if (!d) return; if (!d->ref.deref()) free(p); }
124 QVector<T> &operator=(const QVector<T> &v);
125 #ifdef Q_COMPILER_RVALUE_REFS
126 inline QVector<T> operator=(QVector<T> &&other)
127 { qSwap(p, other.p); return *this; }
129 inline void swap(QVector<T> &other) { qSwap(d, other.d); }
130 #ifdef Q_COMPILER_INITIALIZER_LISTS
131 inline QVector(std::initializer_list<T> args);
133 bool operator==(const QVector<T> &v) const;
134 inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
136 inline int size() const { return d->size; }
138 inline bool isEmpty() const { return d->size == 0; }
140 void resize(int size);
142 inline int capacity() const { return d->alloc; }
143 void reserve(int size);
144 inline void squeeze() { realloc(d->size, d->size); d->capacity = 0; }
146 inline void detach() { if (d->ref != 1) detach_helper(); }
147 inline bool isDetached() const { return d->ref == 1; }
148 inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QVectorData::shared_null) d->sharable = sharable; }
149 inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
151 inline T *data() { detach(); return p->array; }
152 inline const T *data() const { return p->array; }
153 inline const T *constData() const { return p->array; }
156 const T &at(int i) const;
157 T &operator[](int i);
158 const T &operator[](int i) const;
159 void append(const T &t);
160 void prepend(const T &t);
161 void insert(int i, const T &t);
162 void insert(int i, int n, const T &t);
163 void replace(int i, const T &t);
165 void remove(int i, int n);
167 QVector<T> &fill(const T &t, int size = -1);
169 int indexOf(const T &t, int from = 0) const;
170 int lastIndexOf(const T &t, int from = -1) const;
171 bool contains(const T &t) const;
172 int count(const T &t) const;
174 #ifdef QT_STRICT_ITERATORS
178 typedef std::random_access_iterator_tag iterator_category;
179 typedef qptrdiff difference_type;
180 typedef T value_type;
182 typedef T &reference;
184 inline iterator() : i(0) {}
185 inline iterator(T *n) : i(n) {}
186 inline iterator(const iterator &o): i(o.i){}
187 inline T &operator*() const { return *i; }
188 inline T *operator->() const { return i; }
189 inline T &operator[](int j) const { return *(i + j); }
190 inline bool operator==(const iterator &o) const { return i == o.i; }
191 inline bool operator!=(const iterator &o) const { return i != o.i; }
192 inline bool operator<(const iterator& other) const { return i < other.i; }
193 inline bool operator<=(const iterator& other) const { return i <= other.i; }
194 inline bool operator>(const iterator& other) const { return i > other.i; }
195 inline bool operator>=(const iterator& other) const { return i >= other.i; }
196 inline iterator &operator++() { ++i; return *this; }
197 inline iterator operator++(int) { T *n = i; ++i; return n; }
198 inline iterator &operator--() { i--; return *this; }
199 inline iterator operator--(int) { T *n = i; i--; return n; }
200 inline iterator &operator+=(int j) { i+=j; return *this; }
201 inline iterator &operator-=(int j) { i-=j; return *this; }
202 inline iterator operator+(int j) const { return iterator(i+j); }
203 inline iterator operator-(int j) const { return iterator(i-j); }
204 inline int operator-(iterator j) const { return i - j.i; }
206 friend class iterator;
208 class const_iterator {
211 typedef std::random_access_iterator_tag iterator_category;
212 typedef qptrdiff difference_type;
213 typedef T value_type;
214 typedef const T *pointer;
215 typedef const T &reference;
217 inline const_iterator() : i(0) {}
218 inline const_iterator(T *n) : i(n) {}
219 inline const_iterator(const const_iterator &o): i(o.i) {}
220 inline explicit const_iterator(const iterator &o): i(o.i) {}
221 inline const T &operator*() const { return *i; }
222 inline const T *operator->() const { return i; }
223 inline const T &operator[](int j) const { return *(i + j); }
224 inline bool operator==(const const_iterator &o) const { return i == o.i; }
225 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
226 inline bool operator<(const const_iterator& other) const { return i < other.i; }
227 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
228 inline bool operator>(const const_iterator& other) const { return i > other.i; }
229 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
230 inline const_iterator &operator++() { ++i; return *this; }
231 inline const_iterator operator++(int) { T *n = i; ++i; return n; }
232 inline const_iterator &operator--() { i--; return *this; }
233 inline const_iterator operator--(int) { T *n = i; i--; return n; }
234 inline const_iterator &operator+=(int j) { i+=j; return *this; }
235 inline const_iterator &operator-=(int j) { i-=j; return *this; }
236 inline const_iterator operator+(int j) const { return const_iterator(i+j); }
237 inline const_iterator operator-(int j) const { return const_iterator(i-j); }
238 inline int operator-(const_iterator j) const { return i - j.i; }
240 friend class const_iterator;
244 typedef const T* const_iterator;
246 inline iterator begin() { detach(); return p->array; }
247 inline const_iterator begin() const { return p->array; }
248 inline const_iterator constBegin() const { return p->array; }
249 inline iterator end() { detach(); return p->array + d->size; }
250 inline const_iterator end() const { return p->array + d->size; }
251 inline const_iterator constEnd() const { return p->array + d->size; }
252 iterator insert(iterator before, int n, const T &x);
253 inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
254 iterator erase(iterator begin, iterator end);
255 inline iterator erase(iterator pos) { return erase(pos, pos+1); }
258 inline int count() const { return d->size; }
259 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
260 inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); }
261 inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); }
262 inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
263 inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
264 inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
265 QVector<T> mid(int pos, int length = -1) const;
267 T value(int i) const;
268 T value(int i, const T &defaultValue) const;
271 typedef T value_type;
272 typedef value_type* pointer;
273 typedef const value_type* const_pointer;
274 typedef value_type& reference;
275 typedef const value_type& const_reference;
276 typedef qptrdiff difference_type;
277 typedef iterator Iterator;
278 typedef const_iterator ConstIterator;
279 typedef int size_type;
280 inline void push_back(const T &t) { append(t); }
281 inline void push_front(const T &t) { prepend(t); }
282 void pop_back() { Q_ASSERT(!isEmpty()); erase(end()-1); }
283 void pop_front() { Q_ASSERT(!isEmpty()); erase(begin()); }
284 inline bool empty() const
285 { return d->size == 0; }
286 inline T& front() { return first(); }
287 inline const_reference front() const { return first(); }
288 inline reference back() { return last(); }
289 inline const_reference back() const { return last(); }
292 QVector<T> &operator+=(const QVector<T> &l);
293 inline QVector<T> operator+(const QVector<T> &l) const
294 { QVector n = *this; n += l; return n; }
295 inline QVector<T> &operator+=(const T &t)
296 { append(t); return *this; }
297 inline QVector<T> &operator<< (const T &t)
298 { append(t); return *this; }
299 inline QVector<T> &operator<<(const QVector<T> &l)
300 { *this += l; return *this; }
302 QList<T> toList() const;
304 static QVector<T> fromList(const QList<T> &list);
307 static inline QVector<T> fromStdVector(const std::vector<T> &vector)
308 { QVector<T> tmp; tmp.reserve(int(vector.size())); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
309 inline std::vector<T> toStdVector() const
310 { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
313 friend class QRegion; // Optimization for QRegion::rects()
315 void detach_helper();
316 QVectorData *malloc(int alloc);
317 void realloc(int size, int alloc);
319 int sizeOfTypedData() {
320 // this is more or less the same as sizeof(Data), except that it doesn't
321 // count the padding at the end
322 return reinterpret_cast<const char *>(&(reinterpret_cast<const Data *>(this))->array[1]) - reinterpret_cast<const char *>(this);
324 inline int alignOfTypedData() const
327 return qMax<int>(sizeof(void*), Q_ALIGNOF(Data));
334 template <typename T>
335 void QVector<T>::detach_helper()
336 { realloc(d->size, d->alloc); }
337 template <typename T>
338 void QVector<T>::reserve(int asize)
339 { if (asize > d->alloc) realloc(d->size, asize); if (d->ref == 1) d->capacity = 1; }
340 template <typename T>
341 void QVector<T>::resize(int asize)
342 { realloc(asize, (asize > d->alloc || (!d->capacity && asize < d->size && asize < (d->alloc >> 1))) ?
343 QVectorData::grow(sizeOfTypedData(), asize, sizeof(T), QTypeInfo<T>::isStatic)
345 template <typename T>
346 inline void QVector<T>::clear()
347 { *this = QVector<T>(); }
348 template <typename T>
349 inline const T &QVector<T>::at(int i) const
350 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
351 return p->array[i]; }
352 template <typename T>
353 inline const T &QVector<T>::operator[](int i) const
354 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
355 return p->array[i]; }
356 template <typename T>
357 inline T &QVector<T>::operator[](int i)
358 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
360 template <typename T>
361 inline void QVector<T>::insert(int i, const T &t)
362 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
363 insert(begin() + i, 1, t); }
364 template <typename T>
365 inline void QVector<T>::insert(int i, int n, const T &t)
366 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
367 insert(begin() + i, n, t); }
368 template <typename T>
369 inline void QVector<T>::remove(int i, int n)
370 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
371 erase(begin() + i, begin() + i + n); }
372 template <typename T>
373 inline void QVector<T>::remove(int i)
374 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
375 erase(begin() + i, begin() + i + 1); }
376 template <typename T>
377 inline void QVector<T>::prepend(const T &t)
378 { insert(begin(), 1, t); }
380 template <typename T>
381 inline void QVector<T>::replace(int i, const T &t)
383 Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
388 template <typename T>
389 QVector<T> &QVector<T>::operator=(const QVector<T> &v)
391 QVectorData *o = v.d;
401 template <typename T>
402 inline QVectorData *QVector<T>::malloc(int aalloc)
404 QVectorData *vectordata = QVectorData::allocate(sizeOfTypedData() + (aalloc - 1) * sizeof(T), alignOfTypedData());
405 Q_CHECK_PTR(vectordata);
409 template <typename T>
410 QVector<T>::QVector(int asize)
414 d->alloc = d->size = asize;
417 if (QTypeInfo<T>::isComplex) {
419 T* i = p->array + d->size;
423 qMemSet(p->array, 0, asize * sizeof(T));
427 template <typename T>
428 QVector<T>::QVector(int asize, const T &t)
432 d->alloc = d->size = asize;
435 T* i = p->array + d->size;
436 while (i != p->array)
440 #ifdef Q_COMPILER_INITIALIZER_LISTS
441 template <typename T>
442 QVector<T>::QVector(std::initializer_list<T> args)
444 d = malloc(int(args.size()));
446 d->alloc = d->size = int(args.size());
449 T* i = p->array + d->size;
450 auto it = args.end();
451 while (i != p->array)
452 new (--i) T(*(--it));
456 template <typename T>
457 void QVector<T>::free(Data *x)
459 if (QTypeInfo<T>::isComplex) {
461 union { QVectorData *d; Data *p; } u;
463 T* i = b + u.d->size;
467 x->free(x, alignOfTypedData());
470 template <typename T>
471 void QVector<T>::realloc(int asize, int aalloc)
473 Q_ASSERT(asize <= aalloc);
476 union { QVectorData *d; Data *p; } x;
479 if (QTypeInfo<T>::isComplex && asize < d->size && d->ref == 1 ) {
480 // call the destructor on all objects that need to be
481 // destroyed when shrinking
482 pOld = p->array + d->size;
483 pNew = p->array + asize;
484 while (asize < d->size) {
490 if (aalloc != d->alloc || d->ref != 1) {
491 // (re)allocate memory
492 if (QTypeInfo<T>::isStatic) {
493 x.d = malloc(aalloc);
496 } else if (d->ref != 1) {
497 x.d = malloc(aalloc);
499 if (QTypeInfo<T>::isComplex) {
502 ::memcpy(x.p, p, sizeOfTypedData() + (qMin(aalloc, d->alloc) - 1) * sizeof(T));
507 QVectorData *mem = QVectorData::reallocate(d, sizeOfTypedData() + (aalloc - 1) * sizeof(T),
508 sizeOfTypedData() + (d->alloc - 1) * sizeof(T), alignOfTypedData());
512 } QT_CATCH (const std::bad_alloc &) {
513 if (aalloc > d->alloc) // ignore the error in case we are just shrinking.
519 x.d->sharable = true;
520 x.d->capacity = d->capacity;
524 if (QTypeInfo<T>::isComplex) {
526 pOld = p->array + x.d->size;
527 pNew = x.p->array + x.d->size;
528 // copy objects from the old array into the new array
529 const int toMove = qMin(asize, d->size);
530 while (x.d->size < toMove) {
531 new (pNew++) T(*pOld++);
534 // construct all new objects when growing
535 while (x.d->size < asize) {
544 } else if (asize > x.d->size) {
545 // initialize newly allocated memory to 0
546 qMemSet(x.p->array + x.d->size, 0, (asize - x.d->size) * sizeof(T));
558 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
560 if (i < 0 || i >= d->size) {
566 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
568 return ((i < 0 || i >= d->size) ? defaultValue : p->array[i]);
571 template <typename T>
572 void QVector<T>::append(const T &t)
574 if (d->ref != 1 || d->size + 1 > d->alloc) {
576 realloc(d->size, (d->size + 1 > d->alloc) ?
577 QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T), QTypeInfo<T>::isStatic)
579 if (QTypeInfo<T>::isComplex)
580 new (p->array + d->size) T(copy);
582 p->array[d->size] = copy;
584 if (QTypeInfo<T>::isComplex)
585 new (p->array + d->size) T(t);
587 p->array[d->size] = t;
592 template <typename T>
593 typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
595 int offset = int(before - p->array);
598 if (d->ref != 1 || d->size + n > d->alloc)
599 realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + n, sizeof(T),
600 QTypeInfo<T>::isStatic));
601 if (QTypeInfo<T>::isStatic) {
602 T *b = p->array + d->size;
603 T *i = p->array + d->size + n;
606 i = p->array + d->size;
608 b = p->array + offset;
615 T *b = p->array + offset;
617 memmove(i, b, (d->size - offset) * sizeof(T));
623 return p->array + offset;
626 template <typename T>
627 typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
629 int f = int(abegin - p->array);
630 int l = int(aend - p->array);
633 if (QTypeInfo<T>::isComplex) {
634 qCopy(p->array+l, p->array+d->size, p->array+f);
635 T *i = p->array+d->size;
636 T* b = p->array+d->size-n;
642 memmove(p->array + f, p->array + l, (d->size-l)*sizeof(T));
648 template <typename T>
649 bool QVector<T>::operator==(const QVector<T> &v) const
651 if (d->size != v.d->size)
657 T* j = v.p->array + d->size;
664 template <typename T>
665 QVector<T> &QVector<T>::fill(const T &from, int asize)
668 resize(asize < 0 ? d->size : asize);
670 T *i = p->array + d->size;
678 template <typename T>
679 QVector<T> &QVector<T>::operator+=(const QVector &l)
681 int newSize = d->size + l.d->size;
682 realloc(d->size, newSize);
684 T *w = p->array + newSize;
685 T *i = l.p->array + l.d->size;
688 if (QTypeInfo<T>::isComplex)
697 template <typename T>
698 int QVector<T>::indexOf(const T &t, int from) const
701 from = qMax(from + d->size, 0);
702 if (from < d->size) {
703 T* n = p->array + from - 1;
704 T* e = p->array + d->size;
712 template <typename T>
713 int QVector<T>::lastIndexOf(const T &t, int from) const
717 else if (from >= d->size)
721 T* n = p->array + from + 1;
730 template <typename T>
731 bool QVector<T>::contains(const T &t) const
734 T* i = p->array + d->size;
741 template <typename T>
742 int QVector<T>::count(const T &t) const
746 T* i = p->array + d->size;
753 template <typename T>
754 Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const
757 length = size() - pos;
758 if (pos == 0 && length == size())
760 if (pos + length > size())
761 length = size() - pos;
763 copy.reserve(length);
764 for (int i = pos; i < pos + length; ++i)
769 template <typename T>
770 Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const
773 result.reserve(size());
774 for (int i = 0; i < size(); ++i)
775 result.append(at(i));
779 template <typename T>
780 Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
782 QVector<T> result(size());
783 for (int i = 0; i < size(); ++i)
788 template <typename T>
789 QVector<T> QVector<T>::fromList(const QList<T> &list)
791 return list.toVector();
794 template <typename T>
795 QList<T> QList<T>::fromVector(const QVector<T> &vector)
797 return vector.toList();
800 Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
801 Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
805 ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because
806 ### Qt exports QPolygon and QPolygonF that inherit QVector<QPoint> and
807 ### QVector<QPointF> respectively.
811 QT_BEGIN_INCLUDE_NAMESPACE
812 #include <QtCore/QPointF>
813 #include <QtCore/QPoint>
814 QT_END_INCLUDE_NAMESPACE
816 #if defined(QT_BUILD_CORE_LIB)
817 #define Q_TEMPLATE_EXTERN
819 #define Q_TEMPLATE_EXTERN extern
821 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPointF>;
822 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPoint>;