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 uint capacityReserved : 1;
74 void* data() { return reinterpret_cast<char *>(this) + this->offset; }
76 static const QVectorData shared_null;
77 static QVectorData *allocate(int size, int alignment);
78 static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
79 static void free(QVectorData *data, int alignment);
80 static int grow(int sizeOfHeader, int size, int sizeOfT);
84 struct QVectorTypedData : QVectorData
86 T* begin() { return reinterpret_cast<T *>(this->data()); }
87 T* end() { return begin() + this->size; }
89 static QVectorTypedData *sharedNull() { return static_cast<QVectorTypedData *>(const_cast<QVectorData *>(&QVectorData::shared_null)); }
97 typedef QVectorTypedData<T> Data;
101 inline QVector() : d(Data::sharedNull()) { }
102 explicit QVector(int size);
103 QVector(int size, const T &t);
104 inline QVector(const QVector<T> &v)
106 if (v.d->ref.ref()) {
109 d = Data::sharedNull();
110 realloc(0, int(v.d->alloc));
111 qCopy(v.d->begin(), v.d->end(), d->begin());
113 d->capacityReserved = v.d->capacityReserved;
117 inline ~QVector() { if (!d->ref.deref()) free(d); }
118 QVector<T> &operator=(const QVector<T> &v);
119 #ifdef Q_COMPILER_RVALUE_REFS
120 inline QVector<T> operator=(QVector<T> &&other)
121 { qSwap(d, other.d); return *this; }
123 inline void swap(QVector<T> &other) { qSwap(d, other.d); }
124 #ifdef Q_COMPILER_INITIALIZER_LISTS
125 inline QVector(std::initializer_list<T> args);
127 bool operator==(const QVector<T> &v) const;
128 inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
130 inline int size() const { return d->size; }
132 inline bool isEmpty() const { return d->size == 0; }
134 void resize(int size);
136 inline int capacity() const { return int(d->alloc); }
137 void reserve(int size);
138 inline void squeeze() { realloc(d->size, d->size); d->capacityReserved = 0; }
140 inline void detach() { if (!isDetached()) detach_helper(); }
141 inline bool isDetached() const { return !d->ref.isShared(); }
142 inline void setSharable(bool sharable)
144 if (sharable == d->ref.isSharable())
148 if (d != Data::sharedNull())
149 d->ref.setSharable(sharable);
152 inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
154 inline T *data() { detach(); return d->begin(); }
155 inline const T *data() const { return d->begin(); }
156 inline const T *constData() const { return d->begin(); }
159 const T &at(int i) const;
160 T &operator[](int i);
161 const T &operator[](int i) const;
162 void append(const T &t);
163 void prepend(const T &t);
164 void insert(int i, const T &t);
165 void insert(int i, int n, const T &t);
166 void replace(int i, const T &t);
168 void remove(int i, int n);
170 QVector<T> &fill(const T &t, int size = -1);
172 int indexOf(const T &t, int from = 0) const;
173 int lastIndexOf(const T &t, int from = -1) const;
174 bool contains(const T &t) const;
175 int count(const T &t) const;
177 #ifdef QT_STRICT_ITERATORS
181 typedef std::random_access_iterator_tag iterator_category;
182 typedef qptrdiff difference_type;
183 typedef T value_type;
185 typedef T &reference;
187 inline iterator() : i(0) {}
188 inline iterator(T *n) : i(n) {}
189 inline iterator(const iterator &o): i(o.i){}
190 inline T &operator*() const { return *i; }
191 inline T *operator->() const { return i; }
192 inline T &operator[](int j) const { return *(i + j); }
193 inline bool operator==(const iterator &o) const { return i == o.i; }
194 inline bool operator!=(const iterator &o) const { return i != o.i; }
195 inline bool operator<(const iterator& other) const { return i < other.i; }
196 inline bool operator<=(const iterator& other) const { return i <= other.i; }
197 inline bool operator>(const iterator& other) const { return i > other.i; }
198 inline bool operator>=(const iterator& other) const { return i >= other.i; }
199 inline iterator &operator++() { ++i; return *this; }
200 inline iterator operator++(int) { T *n = i; ++i; return n; }
201 inline iterator &operator--() { i--; return *this; }
202 inline iterator operator--(int) { T *n = i; i--; return n; }
203 inline iterator &operator+=(int j) { i+=j; return *this; }
204 inline iterator &operator-=(int j) { i-=j; return *this; }
205 inline iterator operator+(int j) const { return iterator(i+j); }
206 inline iterator operator-(int j) const { return iterator(i-j); }
207 inline int operator-(iterator j) const { return i - j.i; }
209 friend class iterator;
211 class const_iterator {
214 typedef std::random_access_iterator_tag iterator_category;
215 typedef qptrdiff difference_type;
216 typedef T value_type;
217 typedef const T *pointer;
218 typedef const T &reference;
220 inline const_iterator() : i(0) {}
221 inline const_iterator(T *n) : i(n) {}
222 inline const_iterator(const const_iterator &o): i(o.i) {}
223 inline explicit const_iterator(const iterator &o): i(o.i) {}
224 inline const T &operator*() const { return *i; }
225 inline const T *operator->() const { return i; }
226 inline const T &operator[](int j) const { return *(i + j); }
227 inline bool operator==(const const_iterator &o) const { return i == o.i; }
228 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
229 inline bool operator<(const const_iterator& other) const { return i < other.i; }
230 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
231 inline bool operator>(const const_iterator& other) const { return i > other.i; }
232 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
233 inline const_iterator &operator++() { ++i; return *this; }
234 inline const_iterator operator++(int) { T *n = i; ++i; return n; }
235 inline const_iterator &operator--() { i--; return *this; }
236 inline const_iterator operator--(int) { T *n = i; i--; return n; }
237 inline const_iterator &operator+=(int j) { i+=j; return *this; }
238 inline const_iterator &operator-=(int j) { i-=j; return *this; }
239 inline const_iterator operator+(int j) const { return const_iterator(i+j); }
240 inline const_iterator operator-(int j) const { return const_iterator(i-j); }
241 inline int operator-(const_iterator j) const { return i - j.i; }
243 friend class const_iterator;
247 typedef const T* const_iterator;
249 inline iterator begin() { detach(); return d->begin(); }
250 inline const_iterator begin() const { return d->begin(); }
251 inline const_iterator constBegin() const { return d->begin(); }
252 inline iterator end() { detach(); return d->end(); }
253 inline const_iterator end() const { return d->end(); }
254 inline const_iterator constEnd() const { return d->end(); }
255 iterator insert(iterator before, int n, const T &x);
256 inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
257 iterator erase(iterator begin, iterator end);
258 inline iterator erase(iterator pos) { return erase(pos, pos+1); }
261 inline int count() const { return d->size; }
262 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
263 inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); }
264 inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); }
265 inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
266 inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
267 inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
268 QVector<T> mid(int pos, int length = -1) const;
270 T value(int i) const;
271 T value(int i, const T &defaultValue) const;
274 typedef T value_type;
275 typedef value_type* pointer;
276 typedef const value_type* const_pointer;
277 typedef value_type& reference;
278 typedef const value_type& const_reference;
279 typedef qptrdiff difference_type;
280 typedef iterator Iterator;
281 typedef const_iterator ConstIterator;
282 typedef int size_type;
283 inline void push_back(const T &t) { append(t); }
284 inline void push_front(const T &t) { prepend(t); }
285 void pop_back() { Q_ASSERT(!isEmpty()); erase(end()-1); }
286 void pop_front() { Q_ASSERT(!isEmpty()); erase(begin()); }
287 inline bool empty() const
288 { return d->size == 0; }
289 inline T& front() { return first(); }
290 inline const_reference front() const { return first(); }
291 inline reference back() { return last(); }
292 inline const_reference back() const { return last(); }
295 QVector<T> &operator+=(const QVector<T> &l);
296 inline QVector<T> operator+(const QVector<T> &l) const
297 { QVector n = *this; n += l; return n; }
298 inline QVector<T> &operator+=(const T &t)
299 { append(t); return *this; }
300 inline QVector<T> &operator<< (const T &t)
301 { append(t); return *this; }
302 inline QVector<T> &operator<<(const QVector<T> &l)
303 { *this += l; return *this; }
305 QList<T> toList() const;
307 static QVector<T> fromList(const QList<T> &list);
310 static inline QVector<T> fromStdVector(const std::vector<T> &vector)
311 { QVector<T> tmp; tmp.reserve(int(vector.size())); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
312 inline std::vector<T> toStdVector() const
313 { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
316 friend class QRegion; // Optimization for QRegion::rects()
318 void detach_helper();
319 Data *malloc(int alloc);
320 void realloc(int size, int alloc);
323 class AlignmentDummy { QVectorData header; T array[1]; };
325 static Q_DECL_CONSTEXPR int offsetOfTypedData()
327 // (non-POD)-safe offsetof(AlignmentDummy, array)
328 return (sizeof(QVectorData) + (alignOfTypedData() - 1)) & ~(alignOfTypedData() - 1);
330 static Q_DECL_CONSTEXPR int alignOfTypedData()
333 return Q_ALIGNOF(AlignmentDummy);
335 return sizeof(void *);
340 template <typename T>
341 void QVector<T>::detach_helper()
342 { realloc(d->size, int(d->alloc)); }
343 template <typename T>
344 void QVector<T>::reserve(int asize)
345 { if (asize > int(d->alloc)) realloc(d->size, asize); if (isDetached()) d->capacityReserved = 1; }
346 template <typename T>
347 void QVector<T>::resize(int asize)
348 { realloc(asize, (asize > int(d->alloc) || (!d->capacityReserved && asize < d->size && asize < int(d->alloc >> 1))) ?
349 QVectorData::grow(offsetOfTypedData(), asize, sizeof(T))
351 template <typename T>
352 inline void QVector<T>::clear()
353 { *this = QVector<T>(); }
354 template <typename T>
355 inline const T &QVector<T>::at(int i) const
356 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
357 return d->begin()[i]; }
358 template <typename T>
359 inline const T &QVector<T>::operator[](int i) const
360 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
361 return d->begin()[i]; }
362 template <typename T>
363 inline T &QVector<T>::operator[](int i)
364 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
366 template <typename T>
367 inline void QVector<T>::insert(int i, const T &t)
368 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
369 insert(begin() + i, 1, t); }
370 template <typename T>
371 inline void QVector<T>::insert(int i, int n, const T &t)
372 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
373 insert(begin() + i, n, t); }
374 template <typename T>
375 inline void QVector<T>::remove(int i, int n)
376 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
377 erase(begin() + i, begin() + i + n); }
378 template <typename T>
379 inline void QVector<T>::remove(int i)
380 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
381 erase(begin() + i, begin() + i + 1); }
382 template <typename T>
383 inline void QVector<T>::prepend(const T &t)
384 { insert(begin(), 1, t); }
386 template <typename T>
387 inline void QVector<T>::replace(int i, const T &t)
389 Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
394 template <typename T>
395 QVector<T> &QVector<T>::operator=(const QVector<T> &v)
404 template <typename T>
405 inline typename QVector<T>::Data *QVector<T>::malloc(int aalloc)
407 QVectorData *vectordata = QVectorData::allocate(offsetOfTypedData() + aalloc * sizeof(T), alignOfTypedData());
408 Q_CHECK_PTR(vectordata);
409 return static_cast<Data *>(vectordata);
412 template <typename T>
413 QVector<T>::QVector(int asize)
416 d->ref.initializeOwned();
418 d->alloc = uint(d->size);
419 d->capacityReserved = false;
420 d->offset = offsetOfTypedData();
421 if (QTypeInfo<T>::isComplex) {
427 qMemSet(d->begin(), 0, asize * sizeof(T));
431 template <typename T>
432 QVector<T>::QVector(int asize, const T &t)
435 d->ref.initializeOwned();
437 d->alloc = uint(d->size);
438 d->capacityReserved = false;
439 d->offset = offsetOfTypedData();
441 while (i != d->begin())
445 #ifdef Q_COMPILER_INITIALIZER_LISTS
446 template <typename T>
447 QVector<T>::QVector(std::initializer_list<T> args)
449 d = malloc(int(args.size()));
450 d->ref.initializeOwned();
451 d->size = int(args.size());
452 d->alloc = uint(d->size);
453 d->capacityReserved = false;
454 d->offset = offsetOfTypedData();
455 if (QTypeInfo<T>::isComplex) {
458 const T* s = args.end();
462 // std::initializer_list<T>::iterator is guaranteed to be
463 // const T* ([support.initlist]/1), so can be memcpy'ed away from:
464 ::memcpy(d->begin(), args.begin(), args.size() * sizeof(T));
469 template <typename T>
470 void QVector<T>::free(Data *x)
472 if (QTypeInfo<T>::isComplex) {
478 Data::free(x, alignOfTypedData());
481 template <typename T>
482 void QVector<T>::realloc(int asize, int aalloc)
484 Q_ASSERT(asize <= aalloc);
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 = d->begin() + d->size;
493 pNew = d->begin() + asize;
494 while (asize < d->size) {
500 if (aalloc != int(d->alloc) || !isDetached()) {
501 // (re)allocate memory
502 if (QTypeInfo<T>::isStatic) {
506 } else if (!isDetached()) {
509 if (QTypeInfo<T>::isComplex) {
512 ::memcpy(x, d, offsetOfTypedData() + qMin(uint(aalloc), d->alloc) * sizeof(T));
517 QVectorData *mem = QVectorData::reallocate(d, offsetOfTypedData() + aalloc * sizeof(T),
518 offsetOfTypedData() + d->alloc * sizeof(T), alignOfTypedData());
520 x = d = static_cast<Data *>(mem);
522 } QT_CATCH (const std::bad_alloc &) {
523 if (aalloc > int(d->alloc)) // ignore the error in case we are just shrinking.
527 x->ref.initializeOwned();
528 x->alloc = uint(aalloc);
529 x->capacityReserved = d->capacityReserved;
530 x->offset = offsetOfTypedData();
533 if (QTypeInfo<T>::isComplex) {
535 pOld = d->begin() + x->size;
536 pNew = x->begin() + x->size;
537 // copy objects from the old array into the new array
538 const int toMove = qMin(asize, d->size);
539 while (x->size < toMove) {
540 new (pNew++) T(*pOld++);
543 // construct all new objects when growing
544 while (x->size < asize) {
553 } else if (asize > x->size) {
554 // initialize newly allocated memory to 0
555 qMemSet(x->end(), 0, (asize - x->size) * sizeof(T));
567 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
569 if (i < 0 || i >= d->size) {
572 return d->begin()[i];
575 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
577 return ((i < 0 || i >= d->size) ? defaultValue : d->begin()[i]);
580 template <typename T>
581 void QVector<T>::append(const T &t)
583 if (!isDetached() || d->size + 1 > int(d->alloc)) {
585 realloc(d->size, (d->size + 1 > int(d->alloc)) ?
586 QVectorData::grow(offsetOfTypedData(), d->size + 1, sizeof(T))
588 if (QTypeInfo<T>::isComplex)
589 new (d->end()) T(copy);
593 if (QTypeInfo<T>::isComplex)
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 - d->begin());
607 if (!isDetached() || d->size + n > int(d->alloc))
608 realloc(d->size, QVectorData::grow(offsetOfTypedData(), d->size + n, sizeof(T)));
609 if (QTypeInfo<T>::isStatic) {
616 b = d->begin() + offset;
623 T *b = d->begin() + offset;
625 memmove(i, b, (d->size - offset) * sizeof(T));
631 return d->begin() + offset;
634 template <typename T>
635 typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
637 int f = int(abegin - d->begin());
638 int l = int(aend - d->begin());
641 if (QTypeInfo<T>::isComplex) {
642 qCopy(d->begin()+l, d->end(), d->begin()+f);
650 memmove(d->begin() + f, d->begin() + l, (d->size-l)*sizeof(T));
653 return d->begin() + f;
656 template <typename T>
657 bool QVector<T>::operator==(const QVector<T> &v) const
659 if (d->size != v.d->size)
672 template <typename T>
673 QVector<T> &QVector<T>::fill(const T &from, int asize)
676 resize(asize < 0 ? d->size : asize);
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 = d->begin() + newSize;
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 = d->begin() + from - 1;
715 return n - d->begin();
720 template <typename T>
721 int QVector<T>::lastIndexOf(const T &t, int from) const
725 else if (from >= d->size)
729 T* n = d->begin() + from + 1;
738 template <typename T>
739 bool QVector<T>::contains(const T &t) const
749 template <typename T>
750 int QVector<T>::count(const T &t) const
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>;