1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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>
66 struct Q_CORE_EXPORT QVectorData
68 QtPrivate::RefCount ref;
71 #if defined(QT_ARCH_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED)
72 // workaround for bug in gcc 3.4.2
80 static const QVectorData shared_null;
81 // ### Qt 5: rename to 'allocate()'. The current name causes problems for
82 // some debugges when the QVector is member of a class within an unnamed namespace.
83 // ### Qt 5: can be removed completely. (Ralf)
84 static QVectorData *malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init);
85 static QVectorData *allocate(int size, int alignment);
86 static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
87 static void free(QVectorData *data, int alignment);
88 static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive);
92 struct QVectorTypedData : private QVectorData
93 { // private inheritance as we must not access QVectorData member thought QVectorTypedData
94 // as this would break strict aliasing rules. (in the case of shared_null)
97 static inline void free(QVectorTypedData<T> *x, int alignment) { QVectorData::free(static_cast<QVectorData *>(x), alignment); }
102 template <typename T>
105 typedef QVectorTypedData<T> Data;
108 #if defined(Q_CC_SUN) && (__SUNPRO_CC <= 0x550)
109 QVectorTypedData<T> *p;
116 // ### Qt 5: Consider making QVector non-shared to get at least one
117 // "really fast" container. See tests/benchmarks/corelib/tools/qvector/
118 inline QVector() : d(const_cast<QVectorData *>(&QVectorData::shared_null)) { }
119 explicit QVector(int size);
120 QVector(int size, const T &t);
121 inline QVector(const QVector<T> &v)
123 if (v.d->ref.ref()) {
126 d = const_cast<QVectorData *>(&QVectorData::shared_null);
127 realloc(0, v.d->alloc);
128 qCopy(v.p->array, v.p->array + v.d->size, p->array);
130 d->capacity = v.d->capacity;
134 inline ~QVector() { if (!d) return; if (!d->ref.deref()) free(p); }
135 QVector<T> &operator=(const QVector<T> &v);
136 #ifdef Q_COMPILER_RVALUE_REFS
137 inline QVector<T> operator=(QVector<T> &&other)
138 { qSwap(p, other.p); return *this; }
140 inline void swap(QVector<T> &other) { qSwap(d, other.d); }
141 #ifdef Q_COMPILER_INITIALIZER_LISTS
142 inline QVector(std::initializer_list<T> args);
144 bool operator==(const QVector<T> &v) const;
145 inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
147 inline int size() const { return d->size; }
149 inline bool isEmpty() const { return d->size == 0; }
151 void resize(int size);
153 inline int capacity() const { return d->alloc; }
154 void reserve(int size);
155 inline void squeeze() { realloc(d->size, d->size); d->capacity = 0; }
157 inline void detach() { if (!isDetached()) detach_helper(); }
158 inline bool isDetached() const { return !d->ref.isShared(); }
159 inline void setSharable(bool sharable)
161 if (sharable == d->ref.isSharable())
165 if (d != &QVectorData::shared_null)
166 d->ref.setSharable(sharable);
169 inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
171 inline T *data() { detach(); return p->array; }
172 inline const T *data() const { return p->array; }
173 inline const T *constData() const { return p->array; }
176 const T &at(int i) const;
177 T &operator[](int i);
178 const T &operator[](int i) const;
179 void append(const T &t);
180 void prepend(const T &t);
181 void insert(int i, const T &t);
182 void insert(int i, int n, const T &t);
183 void replace(int i, const T &t);
185 void remove(int i, int n);
187 QVector<T> &fill(const T &t, int size = -1);
189 int indexOf(const T &t, int from = 0) const;
190 int lastIndexOf(const T &t, int from = -1) const;
191 bool contains(const T &t) const;
192 int count(const T &t) const;
194 #ifdef QT_STRICT_ITERATORS
198 typedef std::random_access_iterator_tag iterator_category;
199 typedef qptrdiff difference_type;
200 typedef T value_type;
202 typedef T &reference;
204 inline iterator() : i(0) {}
205 inline iterator(T *n) : i(n) {}
206 inline iterator(const iterator &o): i(o.i){}
207 inline T &operator*() const { return *i; }
208 inline T *operator->() const { return i; }
209 inline T &operator[](int j) const { return *(i + j); }
210 inline bool operator==(const iterator &o) const { return i == o.i; }
211 inline bool operator!=(const iterator &o) const { return i != o.i; }
212 inline bool operator<(const iterator& other) const { return i < other.i; }
213 inline bool operator<=(const iterator& other) const { return i <= other.i; }
214 inline bool operator>(const iterator& other) const { return i > other.i; }
215 inline bool operator>=(const iterator& other) const { return i >= other.i; }
216 inline iterator &operator++() { ++i; return *this; }
217 inline iterator operator++(int) { T *n = i; ++i; return n; }
218 inline iterator &operator--() { i--; return *this; }
219 inline iterator operator--(int) { T *n = i; i--; return n; }
220 inline iterator &operator+=(int j) { i+=j; return *this; }
221 inline iterator &operator-=(int j) { i-=j; return *this; }
222 inline iterator operator+(int j) const { return iterator(i+j); }
223 inline iterator operator-(int j) const { return iterator(i-j); }
224 inline int operator-(iterator j) const { return i - j.i; }
226 friend class iterator;
228 class const_iterator {
231 typedef std::random_access_iterator_tag iterator_category;
232 typedef qptrdiff difference_type;
233 typedef T value_type;
234 typedef const T *pointer;
235 typedef const T &reference;
237 inline const_iterator() : i(0) {}
238 inline const_iterator(T *n) : i(n) {}
239 inline const_iterator(const const_iterator &o): i(o.i) {}
240 inline explicit const_iterator(const iterator &o): i(o.i) {}
241 inline const T &operator*() const { return *i; }
242 inline const T *operator->() const { return i; }
243 inline const T &operator[](int j) const { return *(i + j); }
244 inline bool operator==(const const_iterator &o) const { return i == o.i; }
245 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
246 inline bool operator<(const const_iterator& other) const { return i < other.i; }
247 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
248 inline bool operator>(const const_iterator& other) const { return i > other.i; }
249 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
250 inline const_iterator &operator++() { ++i; return *this; }
251 inline const_iterator operator++(int) { T *n = i; ++i; return n; }
252 inline const_iterator &operator--() { i--; return *this; }
253 inline const_iterator operator--(int) { T *n = i; i--; return n; }
254 inline const_iterator &operator+=(int j) { i+=j; return *this; }
255 inline const_iterator &operator-=(int j) { i-=j; return *this; }
256 inline const_iterator operator+(int j) const { return const_iterator(i+j); }
257 inline const_iterator operator-(int j) const { return const_iterator(i-j); }
258 inline int operator-(const_iterator j) const { return i - j.i; }
260 friend class const_iterator;
264 typedef const T* const_iterator;
266 inline iterator begin() { detach(); return p->array; }
267 inline const_iterator begin() const { return p->array; }
268 inline const_iterator constBegin() const { return p->array; }
269 inline iterator end() { detach(); return p->array + d->size; }
270 inline const_iterator end() const { return p->array + d->size; }
271 inline const_iterator constEnd() const { return p->array + d->size; }
272 iterator insert(iterator before, int n, const T &x);
273 inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
274 iterator erase(iterator begin, iterator end);
275 inline iterator erase(iterator pos) { return erase(pos, pos+1); }
278 inline int count() const { return d->size; }
279 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
280 inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); }
281 inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); }
282 inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
283 inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
284 inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
285 QVector<T> mid(int pos, int length = -1) const;
287 T value(int i) const;
288 T value(int i, const T &defaultValue) const;
291 typedef T value_type;
292 typedef value_type* pointer;
293 typedef const value_type* const_pointer;
294 typedef value_type& reference;
295 typedef const value_type& const_reference;
296 typedef qptrdiff difference_type;
297 typedef iterator Iterator;
298 typedef const_iterator ConstIterator;
299 typedef int size_type;
300 inline void push_back(const T &t) { append(t); }
301 inline void push_front(const T &t) { prepend(t); }
302 void pop_back() { Q_ASSERT(!isEmpty()); erase(end()-1); }
303 void pop_front() { Q_ASSERT(!isEmpty()); erase(begin()); }
304 inline bool empty() const
305 { return d->size == 0; }
306 inline T& front() { return first(); }
307 inline const_reference front() const { return first(); }
308 inline reference back() { return last(); }
309 inline const_reference back() const { return last(); }
312 QVector<T> &operator+=(const QVector<T> &l);
313 inline QVector<T> operator+(const QVector<T> &l) const
314 { QVector n = *this; n += l; return n; }
315 inline QVector<T> &operator+=(const T &t)
316 { append(t); return *this; }
317 inline QVector<T> &operator<< (const T &t)
318 { append(t); return *this; }
319 inline QVector<T> &operator<<(const QVector<T> &l)
320 { *this += l; return *this; }
322 QList<T> toList() const;
324 static QVector<T> fromList(const QList<T> &list);
327 static inline QVector<T> fromStdVector(const std::vector<T> &vector)
328 { QVector<T> tmp; tmp.reserve(int(vector.size())); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
329 inline std::vector<T> toStdVector() const
330 { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
333 friend class QRegion; // Optimization for QRegion::rects()
335 void detach_helper();
336 QVectorData *malloc(int alloc);
337 void realloc(int size, int alloc);
339 int sizeOfTypedData() {
340 // this is more or less the same as sizeof(Data), except that it doesn't
341 // count the padding at the end
342 return reinterpret_cast<const char *>(&(reinterpret_cast<const Data *>(this))->array[1]) - reinterpret_cast<const char *>(this);
344 inline int alignOfTypedData() const
347 return qMax<int>(sizeof(void*), Q_ALIGNOF(Data));
354 template <typename T>
355 void QVector<T>::detach_helper()
356 { realloc(d->size, d->alloc); }
357 template <typename T>
358 void QVector<T>::reserve(int asize)
359 { if (asize > d->alloc) realloc(d->size, asize); if (isDetached()) d->capacity = 1; }
360 template <typename T>
361 void QVector<T>::resize(int asize)
362 { realloc(asize, (asize > d->alloc || (!d->capacity && asize < d->size && asize < (d->alloc >> 1))) ?
363 QVectorData::grow(sizeOfTypedData(), asize, sizeof(T), QTypeInfo<T>::isStatic)
365 template <typename T>
366 inline void QVector<T>::clear()
367 { *this = QVector<T>(); }
368 template <typename T>
369 inline const T &QVector<T>::at(int i) const
370 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
371 return p->array[i]; }
372 template <typename T>
373 inline const T &QVector<T>::operator[](int i) const
374 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
375 return p->array[i]; }
376 template <typename T>
377 inline T &QVector<T>::operator[](int i)
378 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
380 template <typename T>
381 inline void QVector<T>::insert(int i, const T &t)
382 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
383 insert(begin() + i, 1, t); }
384 template <typename T>
385 inline void QVector<T>::insert(int i, int n, const T &t)
386 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
387 insert(begin() + i, n, t); }
388 template <typename T>
389 inline void QVector<T>::remove(int i, int n)
390 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
391 erase(begin() + i, begin() + i + n); }
392 template <typename T>
393 inline void QVector<T>::remove(int i)
394 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
395 erase(begin() + i, begin() + i + 1); }
396 template <typename T>
397 inline void QVector<T>::prepend(const T &t)
398 { insert(begin(), 1, t); }
400 template <typename T>
401 inline void QVector<T>::replace(int i, const T &t)
403 Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
408 template <typename T>
409 QVector<T> &QVector<T>::operator=(const QVector<T> &v)
418 template <typename T>
419 inline QVectorData *QVector<T>::malloc(int aalloc)
421 QVectorData *vectordata = QVectorData::allocate(sizeOfTypedData() + (aalloc - 1) * sizeof(T), alignOfTypedData());
422 Q_CHECK_PTR(vectordata);
426 template <typename T>
427 QVector<T>::QVector(int asize)
430 d->ref.initializeOwned();
431 d->alloc = d->size = asize;
433 if (QTypeInfo<T>::isComplex) {
435 T* i = p->array + d->size;
439 qMemSet(p->array, 0, asize * sizeof(T));
443 template <typename T>
444 QVector<T>::QVector(int asize, const T &t)
447 d->ref.initializeOwned();
448 d->alloc = d->size = asize;
450 T* i = p->array + d->size;
451 while (i != p->array)
455 #ifdef Q_COMPILER_INITIALIZER_LISTS
456 template <typename T>
457 QVector<T>::QVector(std::initializer_list<T> args)
459 d = malloc(int(args.size()));
460 d->ref.initializeOwned();
461 d->alloc = d->size = int(args.size());
463 T* i = p->array + d->size;
464 auto it = args.end();
465 while (i != p->array)
466 new (--i) T(*(--it));
470 template <typename T>
471 void QVector<T>::free(Data *x)
473 if (QTypeInfo<T>::isComplex) {
475 union { QVectorData *d; Data *p; } u;
477 T* i = b + u.d->size;
481 x->free(x, alignOfTypedData());
484 template <typename T>
485 void QVector<T>::realloc(int asize, int aalloc)
487 Q_ASSERT(asize <= aalloc);
490 union { QVectorData *d; Data *p; } x;
493 if (QTypeInfo<T>::isComplex && asize < d->size && isDetached()) {
494 // call the destructor on all objects that need to be
495 // destroyed when shrinking
496 pOld = p->array + d->size;
497 pNew = p->array + asize;
498 while (asize < d->size) {
504 if (aalloc != d->alloc || !isDetached()) {
505 // (re)allocate memory
506 if (QTypeInfo<T>::isStatic) {
507 x.d = malloc(aalloc);
510 } else if (!isDetached()) {
511 x.d = malloc(aalloc);
513 if (QTypeInfo<T>::isComplex) {
516 ::memcpy(x.p, p, sizeOfTypedData() + (qMin(aalloc, d->alloc) - 1) * sizeof(T));
521 QVectorData *mem = QVectorData::reallocate(d, sizeOfTypedData() + (aalloc - 1) * sizeof(T),
522 sizeOfTypedData() + (d->alloc - 1) * sizeof(T), alignOfTypedData());
526 } QT_CATCH (const std::bad_alloc &) {
527 if (aalloc > d->alloc) // ignore the error in case we are just shrinking.
531 x.d->ref.initializeOwned();
533 x.d->capacity = d->capacity;
537 if (QTypeInfo<T>::isComplex) {
539 pOld = p->array + x.d->size;
540 pNew = x.p->array + x.d->size;
541 // copy objects from the old array into the new array
542 const int toMove = qMin(asize, d->size);
543 while (x.d->size < toMove) {
544 new (pNew++) T(*pOld++);
547 // construct all new objects when growing
548 while (x.d->size < asize) {
557 } else if (asize > x.d->size) {
558 // initialize newly allocated memory to 0
559 qMemSet(x.p->array + x.d->size, 0, (asize - x.d->size) * sizeof(T));
571 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
573 if (i < 0 || i >= d->size) {
579 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
581 return ((i < 0 || i >= d->size) ? defaultValue : p->array[i]);
584 template <typename T>
585 void QVector<T>::append(const T &t)
587 if (!isDetached() || d->size + 1 > d->alloc) {
589 realloc(d->size, (d->size + 1 > d->alloc) ?
590 QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T), QTypeInfo<T>::isStatic)
592 if (QTypeInfo<T>::isComplex)
593 new (p->array + d->size) T(copy);
595 p->array[d->size] = copy;
597 if (QTypeInfo<T>::isComplex)
598 new (p->array + d->size) T(t);
600 p->array[d->size] = t;
605 template <typename T>
606 Q_TYPENAME QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
608 int offset = int(before - p->array);
611 if (!isDetached() || d->size + n > d->alloc)
612 realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + n, sizeof(T),
613 QTypeInfo<T>::isStatic));
614 if (QTypeInfo<T>::isStatic) {
615 T *b = p->array + d->size;
616 T *i = p->array + d->size + n;
619 i = p->array + d->size;
621 b = p->array + offset;
628 T *b = p->array + offset;
630 memmove(i, b, (d->size - offset) * sizeof(T));
636 return p->array + offset;
639 template <typename T>
640 Q_TYPENAME QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
642 int f = int(abegin - p->array);
643 int l = int(aend - p->array);
646 if (QTypeInfo<T>::isComplex) {
647 qCopy(p->array+l, p->array+d->size, p->array+f);
648 T *i = p->array+d->size;
649 T* b = p->array+d->size-n;
655 memmove(p->array + f, p->array + l, (d->size-l)*sizeof(T));
661 template <typename T>
662 bool QVector<T>::operator==(const QVector<T> &v) const
664 if (d->size != v.d->size)
670 T* j = v.p->array + d->size;
677 template <typename T>
678 QVector<T> &QVector<T>::fill(const T &from, int asize)
681 resize(asize < 0 ? d->size : asize);
683 T *i = p->array + d->size;
691 template <typename T>
692 QVector<T> &QVector<T>::operator+=(const QVector &l)
694 int newSize = d->size + l.d->size;
695 realloc(d->size, newSize);
697 T *w = p->array + newSize;
698 T *i = l.p->array + l.d->size;
701 if (QTypeInfo<T>::isComplex)
710 template <typename T>
711 int QVector<T>::indexOf(const T &t, int from) const
714 from = qMax(from + d->size, 0);
715 if (from < d->size) {
716 T* n = p->array + from - 1;
717 T* e = p->array + d->size;
725 template <typename T>
726 int QVector<T>::lastIndexOf(const T &t, int from) const
730 else if (from >= d->size)
734 T* n = p->array + from + 1;
743 template <typename T>
744 bool QVector<T>::contains(const T &t) const
747 T* i = p->array + d->size;
754 template <typename T>
755 int QVector<T>::count(const T &t) const
759 T* i = p->array + d->size;
766 template <typename T>
767 Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const
770 length = size() - pos;
771 if (pos == 0 && length == size())
773 if (pos + length > size())
774 length = size() - pos;
776 copy.reserve(length);
777 for (int i = pos; i < pos + length; ++i)
782 template <typename T>
783 Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const
786 result.reserve(size());
787 for (int i = 0; i < size(); ++i)
788 result.append(at(i));
792 template <typename T>
793 Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
795 QVector<T> result(size());
796 for (int i = 0; i < size(); ++i)
801 template <typename T>
802 QVector<T> QVector<T>::fromList(const QList<T> &list)
804 return list.toVector();
807 template <typename T>
808 QList<T> QList<T>::fromVector(const QVector<T> &vector)
810 return vector.toList();
813 Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
814 Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
818 ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because
819 ### Qt exports QPolygon and QPolygonF that inherit QVector<QPoint> and
820 ### QVector<QPointF> respectively.
824 QT_BEGIN_INCLUDE_NAMESPACE
825 #include <QtCore/QPointF>
826 #include <QtCore/QPoint>
827 QT_END_INCLUDE_NAMESPACE
829 #if defined(QT_BUILD_CORE_LIB)
830 #define Q_TEMPLATE_EXTERN
832 #define Q_TEMPLATE_EXTERN extern
834 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPointF>;
835 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPoint>;