1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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(Q_PROCESSOR_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED)
71 // workaround for bug in gcc 3.4.2
79 static const QVectorData shared_null;
80 static QVectorData *allocate(int size, int alignment);
81 static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
82 static void free(QVectorData *data, int alignment);
83 static int grow(int sizeOfHeader, int size, int sizeOfT);
87 struct QVectorTypedData : private QVectorData
88 { // private inheritance as we must not access QVectorData member thought QVectorTypedData
89 // as this would break strict aliasing rules. (in the case of shared_null)
92 static inline void free(QVectorTypedData<T> *x, int alignment) { QVectorData::free(static_cast<QVectorData *>(x), alignment); }
100 typedef QVectorTypedData<T> Data;
103 #if defined(Q_CC_SUN) && (__SUNPRO_CC <= 0x550)
104 QVectorTypedData<T> *p;
111 // ### Qt 5: Consider making QVector non-shared to get at least one
112 // "really fast" container. See tests/benchmarks/corelib/tools/qvector/
113 inline QVector() : d(const_cast<QVectorData *>(&QVectorData::shared_null)) { }
114 explicit QVector(int size);
115 QVector(int size, const T &t);
116 inline QVector(const QVector<T> &v)
118 if (v.d->ref.ref()) {
121 d = const_cast<QVectorData *>(&QVectorData::shared_null);
122 realloc(0, v.d->alloc);
123 qCopy(v.p->array, v.p->array + v.d->size, p->array);
125 d->capacity = v.d->capacity;
129 inline ~QVector() { if (!d->ref.deref()) free(p); }
130 QVector<T> &operator=(const QVector<T> &v);
131 #ifdef Q_COMPILER_RVALUE_REFS
132 inline QVector<T> operator=(QVector<T> &&other)
133 { qSwap(p, other.p); return *this; }
135 inline void swap(QVector<T> &other) { qSwap(d, other.d); }
136 #ifdef Q_COMPILER_INITIALIZER_LISTS
137 inline QVector(std::initializer_list<T> args);
139 bool operator==(const QVector<T> &v) const;
140 inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
142 inline int size() const { return d->size; }
144 inline bool isEmpty() const { return d->size == 0; }
146 void resize(int size);
148 inline int capacity() const { return d->alloc; }
149 void reserve(int size);
150 inline void squeeze() { realloc(d->size, d->size); d->capacity = 0; }
152 inline void detach() { if (!isDetached()) detach_helper(); }
153 inline bool isDetached() const { return !d->ref.isShared(); }
154 inline void setSharable(bool sharable)
156 if (sharable == d->ref.isSharable())
160 if (d != &QVectorData::shared_null)
161 d->ref.setSharable(sharable);
164 inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
166 inline T *data() { detach(); return p->array; }
167 inline const T *data() const { return p->array; }
168 inline const T *constData() const { return p->array; }
171 const T &at(int i) const;
172 T &operator[](int i);
173 const T &operator[](int i) const;
174 void append(const T &t);
175 void prepend(const T &t);
176 void insert(int i, const T &t);
177 void insert(int i, int n, const T &t);
178 void replace(int i, const T &t);
180 void remove(int i, int n);
182 QVector<T> &fill(const T &t, int size = -1);
184 int indexOf(const T &t, int from = 0) const;
185 int lastIndexOf(const T &t, int from = -1) const;
186 bool contains(const T &t) const;
187 int count(const T &t) const;
189 #ifdef QT_STRICT_ITERATORS
193 typedef std::random_access_iterator_tag iterator_category;
194 typedef qptrdiff difference_type;
195 typedef T value_type;
197 typedef T &reference;
199 inline iterator() : i(0) {}
200 inline iterator(T *n) : i(n) {}
201 inline iterator(const iterator &o): i(o.i){}
202 inline T &operator*() const { return *i; }
203 inline T *operator->() const { return i; }
204 inline T &operator[](int j) const { return *(i + j); }
205 inline bool operator==(const iterator &o) const { return i == o.i; }
206 inline bool operator!=(const iterator &o) const { return i != o.i; }
207 inline bool operator<(const iterator& other) const { return i < other.i; }
208 inline bool operator<=(const iterator& other) const { return i <= other.i; }
209 inline bool operator>(const iterator& other) const { return i > other.i; }
210 inline bool operator>=(const iterator& other) const { return i >= other.i; }
211 inline iterator &operator++() { ++i; return *this; }
212 inline iterator operator++(int) { T *n = i; ++i; return n; }
213 inline iterator &operator--() { i--; return *this; }
214 inline iterator operator--(int) { T *n = i; i--; return n; }
215 inline iterator &operator+=(int j) { i+=j; return *this; }
216 inline iterator &operator-=(int j) { i-=j; return *this; }
217 inline iterator operator+(int j) const { return iterator(i+j); }
218 inline iterator operator-(int j) const { return iterator(i-j); }
219 inline int operator-(iterator j) const { return i - j.i; }
221 friend class iterator;
223 class const_iterator {
226 typedef std::random_access_iterator_tag iterator_category;
227 typedef qptrdiff difference_type;
228 typedef T value_type;
229 typedef const T *pointer;
230 typedef const T &reference;
232 inline const_iterator() : i(0) {}
233 inline const_iterator(T *n) : i(n) {}
234 inline const_iterator(const const_iterator &o): i(o.i) {}
235 inline explicit const_iterator(const iterator &o): i(o.i) {}
236 inline const T &operator*() const { return *i; }
237 inline const T *operator->() const { return i; }
238 inline const T &operator[](int j) const { return *(i + j); }
239 inline bool operator==(const const_iterator &o) const { return i == o.i; }
240 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
241 inline bool operator<(const const_iterator& other) const { return i < other.i; }
242 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
243 inline bool operator>(const const_iterator& other) const { return i > other.i; }
244 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
245 inline const_iterator &operator++() { ++i; return *this; }
246 inline const_iterator operator++(int) { T *n = i; ++i; return n; }
247 inline const_iterator &operator--() { i--; return *this; }
248 inline const_iterator operator--(int) { T *n = i; i--; return n; }
249 inline const_iterator &operator+=(int j) { i+=j; return *this; }
250 inline const_iterator &operator-=(int j) { i-=j; return *this; }
251 inline const_iterator operator+(int j) const { return const_iterator(i+j); }
252 inline const_iterator operator-(int j) const { return const_iterator(i-j); }
253 inline int operator-(const_iterator j) const { return i - j.i; }
255 friend class const_iterator;
259 typedef const T* const_iterator;
261 inline iterator begin() { detach(); return p->array; }
262 inline const_iterator begin() const { return p->array; }
263 inline const_iterator constBegin() const { return p->array; }
264 inline iterator end() { detach(); return p->array + d->size; }
265 inline const_iterator end() const { return p->array + d->size; }
266 inline const_iterator constEnd() const { return p->array + d->size; }
267 iterator insert(iterator before, int n, const T &x);
268 inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
269 iterator erase(iterator begin, iterator end);
270 inline iterator erase(iterator pos) { return erase(pos, pos+1); }
273 inline int count() const { return d->size; }
274 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
275 inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); }
276 inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); }
277 inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
278 inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
279 inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
280 QVector<T> mid(int pos, int length = -1) const;
282 T value(int i) const;
283 T value(int i, const T &defaultValue) const;
286 typedef T value_type;
287 typedef value_type* pointer;
288 typedef const value_type* const_pointer;
289 typedef value_type& reference;
290 typedef const value_type& const_reference;
291 typedef qptrdiff difference_type;
292 typedef iterator Iterator;
293 typedef const_iterator ConstIterator;
294 typedef int size_type;
295 inline void push_back(const T &t) { append(t); }
296 inline void push_front(const T &t) { prepend(t); }
297 void pop_back() { Q_ASSERT(!isEmpty()); erase(end()-1); }
298 void pop_front() { Q_ASSERT(!isEmpty()); erase(begin()); }
299 inline bool empty() const
300 { return d->size == 0; }
301 inline T& front() { return first(); }
302 inline const_reference front() const { return first(); }
303 inline reference back() { return last(); }
304 inline const_reference back() const { return last(); }
307 QVector<T> &operator+=(const QVector<T> &l);
308 inline QVector<T> operator+(const QVector<T> &l) const
309 { QVector n = *this; n += l; return n; }
310 inline QVector<T> &operator+=(const T &t)
311 { append(t); return *this; }
312 inline QVector<T> &operator<< (const T &t)
313 { append(t); return *this; }
314 inline QVector<T> &operator<<(const QVector<T> &l)
315 { *this += l; return *this; }
317 QList<T> toList() const;
319 static QVector<T> fromList(const QList<T> &list);
322 static inline QVector<T> fromStdVector(const std::vector<T> &vector)
323 { QVector<T> tmp; tmp.reserve(int(vector.size())); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
324 inline std::vector<T> toStdVector() const
325 { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
328 friend class QRegion; // Optimization for QRegion::rects()
330 void detach_helper();
331 QVectorData *malloc(int alloc);
332 void realloc(int size, int alloc);
335 static Q_DECL_CONSTEXPR int offsetOfTypedData()
337 // (non-POD)-safe offsetof(Data, array)
338 return (sizeof(QVectorData) + (alignOfTypedData() - 1)) & ~(alignOfTypedData() - 1);
340 static Q_DECL_CONSTEXPR int alignOfTypedData()
343 return Q_ALIGNOF(Data);
345 return sizeof(void *);
350 template <typename T>
351 void QVector<T>::detach_helper()
352 { realloc(d->size, d->alloc); }
353 template <typename T>
354 void QVector<T>::reserve(int asize)
355 { if (asize > d->alloc) realloc(d->size, asize); if (isDetached()) d->capacity = 1; }
356 template <typename T>
357 void QVector<T>::resize(int asize)
358 { realloc(asize, (asize > d->alloc || (!d->capacity && asize < d->size && asize < (d->alloc >> 1))) ?
359 QVectorData::grow(offsetOfTypedData(), asize, sizeof(T))
361 template <typename T>
362 inline void QVector<T>::clear()
363 { *this = QVector<T>(); }
364 template <typename T>
365 inline const T &QVector<T>::at(int i) const
366 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
367 return p->array[i]; }
368 template <typename T>
369 inline const T &QVector<T>::operator[](int i) const
370 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
371 return p->array[i]; }
372 template <typename T>
373 inline T &QVector<T>::operator[](int i)
374 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
376 template <typename T>
377 inline void QVector<T>::insert(int i, const T &t)
378 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
379 insert(begin() + i, 1, t); }
380 template <typename T>
381 inline void QVector<T>::insert(int i, int n, const T &t)
382 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
383 insert(begin() + i, n, t); }
384 template <typename T>
385 inline void QVector<T>::remove(int i, int n)
386 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
387 erase(begin() + i, begin() + i + n); }
388 template <typename T>
389 inline void QVector<T>::remove(int i)
390 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
391 erase(begin() + i, begin() + i + 1); }
392 template <typename T>
393 inline void QVector<T>::prepend(const T &t)
394 { insert(begin(), 1, t); }
396 template <typename T>
397 inline void QVector<T>::replace(int i, const T &t)
399 Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
404 template <typename T>
405 QVector<T> &QVector<T>::operator=(const QVector<T> &v)
414 template <typename T>
415 inline QVectorData *QVector<T>::malloc(int aalloc)
417 QVectorData *vectordata = QVectorData::allocate(offsetOfTypedData() + aalloc * sizeof(T), alignOfTypedData());
418 Q_CHECK_PTR(vectordata);
422 template <typename T>
423 QVector<T>::QVector(int asize)
426 d->ref.initializeOwned();
427 d->alloc = d->size = asize;
429 if (QTypeInfo<T>::isComplex) {
431 T* i = p->array + d->size;
435 qMemSet(p->array, 0, asize * sizeof(T));
439 template <typename T>
440 QVector<T>::QVector(int asize, const T &t)
443 d->ref.initializeOwned();
444 d->alloc = d->size = asize;
446 T* i = p->array + d->size;
447 while (i != p->array)
451 #ifdef Q_COMPILER_INITIALIZER_LISTS
452 template <typename T>
453 QVector<T>::QVector(std::initializer_list<T> args)
455 d = malloc(int(args.size()));
456 d->ref.initializeOwned();
457 d->alloc = d->size = int(args.size());
459 T* i = p->array + d->size;
460 auto it = args.end();
461 while (i != p->array)
462 new (--i) T(*(--it));
466 template <typename T>
467 void QVector<T>::free(Data *x)
469 if (QTypeInfo<T>::isComplex) {
471 union { QVectorData *d; Data *p; } u;
473 T* i = b + u.d->size;
477 x->free(x, alignOfTypedData());
480 template <typename T>
481 void QVector<T>::realloc(int asize, int aalloc)
483 Q_ASSERT(asize <= aalloc);
486 union { QVectorData *d; Data *p; } x;
489 if (QTypeInfo<T>::isComplex && asize < d->size && isDetached()) {
490 // call the destructor on all objects that need to be
491 // destroyed when shrinking
492 pOld = p->array + d->size;
493 pNew = p->array + asize;
494 while (asize < d->size) {
500 if (aalloc != d->alloc || !isDetached()) {
501 // (re)allocate memory
502 if (QTypeInfo<T>::isStatic) {
503 x.d = malloc(aalloc);
506 } else if (!isDetached()) {
507 x.d = malloc(aalloc);
509 if (QTypeInfo<T>::isComplex) {
512 ::memcpy(x.p, p, offsetOfTypedData() + qMin(aalloc, d->alloc) * sizeof(T));
517 QVectorData *mem = QVectorData::reallocate(d, offsetOfTypedData() + aalloc * sizeof(T),
518 offsetOfTypedData() + d->alloc * sizeof(T), alignOfTypedData());
522 } QT_CATCH (const std::bad_alloc &) {
523 if (aalloc > d->alloc) // ignore the error in case we are just shrinking.
527 x.d->ref.initializeOwned();
529 x.d->capacity = d->capacity;
533 if (QTypeInfo<T>::isComplex) {
535 pOld = p->array + x.d->size;
536 pNew = x.p->array + x.d->size;
537 // copy objects from the old array into the new array
538 const int toMove = qMin(asize, d->size);
539 while (x.d->size < toMove) {
540 new (pNew++) T(*pOld++);
543 // construct all new objects when growing
544 while (x.d->size < asize) {
553 } else if (asize > x.d->size) {
554 // initialize newly allocated memory to 0
555 qMemSet(x.p->array + x.d->size, 0, (asize - x.d->size) * sizeof(T));
567 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
569 if (i < 0 || i >= d->size) {
575 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
577 return ((i < 0 || i >= d->size) ? defaultValue : p->array[i]);
580 template <typename T>
581 void QVector<T>::append(const T &t)
583 if (!isDetached() || d->size + 1 > d->alloc) {
585 realloc(d->size, (d->size + 1 > d->alloc) ?
586 QVectorData::grow(offsetOfTypedData(), d->size + 1, sizeof(T))
588 if (QTypeInfo<T>::isComplex)
589 new (p->array + d->size) T(copy);
591 p->array[d->size] = copy;
593 if (QTypeInfo<T>::isComplex)
594 new (p->array + d->size) T(t);
596 p->array[d->size] = t;
601 template <typename T>
602 typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
604 int offset = int(before - p->array);
607 if (!isDetached() || d->size + n > d->alloc)
608 realloc(d->size, QVectorData::grow(offsetOfTypedData(), d->size + n, sizeof(T)));
609 if (QTypeInfo<T>::isStatic) {
610 T *b = p->array + d->size;
611 T *i = p->array + d->size + n;
614 i = p->array + d->size;
616 b = p->array + offset;
623 T *b = p->array + offset;
625 memmove(i, b, (d->size - offset) * sizeof(T));
631 return p->array + offset;
634 template <typename T>
635 typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
637 int f = int(abegin - p->array);
638 int l = int(aend - p->array);
641 if (QTypeInfo<T>::isComplex) {
642 qCopy(p->array+l, p->array+d->size, p->array+f);
643 T *i = p->array+d->size;
644 T* b = p->array+d->size-n;
650 memmove(p->array + f, p->array + l, (d->size-l)*sizeof(T));
656 template <typename T>
657 bool QVector<T>::operator==(const QVector<T> &v) const
659 if (d->size != v.d->size)
665 T* j = v.p->array + d->size;
672 template <typename T>
673 QVector<T> &QVector<T>::fill(const T &from, int asize)
676 resize(asize < 0 ? d->size : asize);
678 T *i = p->array + d->size;
686 template <typename T>
687 QVector<T> &QVector<T>::operator+=(const QVector &l)
689 int newSize = d->size + l.d->size;
690 realloc(d->size, newSize);
692 T *w = p->array + newSize;
693 T *i = l.p->array + l.d->size;
696 if (QTypeInfo<T>::isComplex)
705 template <typename T>
706 int QVector<T>::indexOf(const T &t, int from) const
709 from = qMax(from + d->size, 0);
710 if (from < d->size) {
711 T* n = p->array + from - 1;
712 T* e = p->array + d->size;
720 template <typename T>
721 int QVector<T>::lastIndexOf(const T &t, int from) const
725 else if (from >= d->size)
729 T* n = p->array + from + 1;
738 template <typename T>
739 bool QVector<T>::contains(const T &t) const
742 T* i = p->array + d->size;
749 template <typename T>
750 int QVector<T>::count(const T &t) const
754 T* i = p->array + d->size;
761 template <typename T>
762 Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const
765 length = size() - pos;
766 if (pos == 0 && length == size())
768 if (pos + length > size())
769 length = size() - pos;
771 copy.reserve(length);
772 for (int i = pos; i < pos + length; ++i)
777 template <typename T>
778 Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const
781 result.reserve(size());
782 for (int i = 0; i < size(); ++i)
783 result.append(at(i));
787 template <typename T>
788 Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
790 QVector<T> result(size());
791 for (int i = 0; i < size(); ++i)
796 template <typename T>
797 QVector<T> QVector<T>::fromList(const QList<T> &list)
799 return list.toVector();
802 template <typename T>
803 QList<T> QList<T>::fromVector(const QVector<T> &vector)
805 return vector.toList();
808 Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
809 Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
813 ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because
814 ### Qt exports QPolygon and QPolygonF that inherit QVector<QPoint> and
815 ### QVector<QPointF> respectively.
819 QT_BEGIN_INCLUDE_NAMESPACE
820 #include <QtCore/QPointF>
821 #include <QtCore/QPoint>
822 QT_END_INCLUDE_NAMESPACE
824 #if defined(QT_BUILD_CORE_LIB)
825 #define Q_TEMPLATE_EXTERN
827 #define Q_TEMPLATE_EXTERN extern
829 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPointF>;
830 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPoint>;