Merge remote-tracking branch 'gerrit/master' into containers
[profile/ivi/qtbase.git] / src / corelib / tools / qvector.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QVECTOR_H
43 #define QVECTOR_H
44
45 #include <QtCore/qalgorithms.h>
46 #include <QtCore/qiterator.h>
47 #include <QtCore/qlist.h>
48 #include <QtCore/qrefcount.h>
49
50 #ifndef QT_NO_STL
51 #include <iterator>
52 #include <vector>
53 #endif
54 #include <stdlib.h>
55 #include <string.h>
56 #ifdef Q_COMPILER_INITIALIZER_LISTS
57 #include <initializer_list>
58 #endif
59
60 QT_BEGIN_HEADER
61
62 QT_BEGIN_NAMESPACE
63
64
65 struct Q_CORE_EXPORT QVectorData
66 {
67     QtPrivate::RefCount ref;
68     int alloc;
69     int size;
70 #if defined(Q_PROCESSOR_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED)
71     // workaround for bug in gcc 3.4.2
72     uint capacity;
73     uint reserved;
74 #else
75     uint capacity : 1;
76     uint reserved : 31;
77 #endif
78
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);
84 };
85
86 template <typename T>
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)
90     T array[1];
91
92     static inline void free(QVectorTypedData<T> *x, int alignment) { QVectorData::free(static_cast<QVectorData *>(x), alignment); }
93 };
94
95 class QRegion;
96
97 template <typename T>
98 class QVector
99 {
100     typedef QVectorTypedData<T> Data;
101     union {
102         QVectorData *d;
103 #if defined(Q_CC_SUN) && (__SUNPRO_CC <= 0x550)
104         QVectorTypedData<T> *p;
105 #else
106         Data *p;
107 #endif
108     };
109
110 public:
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)
117     {
118         if (v.d->ref.ref()) {
119             d = v.d;
120         } else {
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);
124             d->size = v.d->size;
125             d->capacity = v.d->capacity;
126         }
127     }
128
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; }
134 #endif
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);
138 #endif
139     bool operator==(const QVector<T> &v) const;
140     inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
141
142     inline int size() const { return d->size; }
143
144     inline bool isEmpty() const { return d->size == 0; }
145
146     void resize(int size);
147
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; }
151
152     inline void detach() { if (!isDetached()) detach_helper(); }
153     inline bool isDetached() const { return !d->ref.isShared(); }
154     inline void setSharable(bool sharable)
155     {
156         if (sharable == d->ref.isSharable())
157             return;
158         if (!sharable)
159             detach();
160         if (d != &QVectorData::shared_null)
161             d->ref.setSharable(sharable);
162     }
163
164     inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
165
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; }
169     void clear();
170
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);
179     void remove(int i);
180     void remove(int i, int n);
181
182     QVector<T> &fill(const T &t, int size = -1);
183
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;
188
189 #ifdef QT_STRICT_ITERATORS
190     class iterator {
191     public:
192         T *i;
193         typedef std::random_access_iterator_tag  iterator_category;
194         typedef qptrdiff difference_type;
195         typedef T value_type;
196         typedef T *pointer;
197         typedef T &reference;
198
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; }
220     };
221     friend class iterator;
222
223     class const_iterator {
224     public:
225         T *i;
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;
231
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; }
254     };
255     friend class const_iterator;
256 #else
257     // STL-style
258     typedef T* iterator;
259     typedef const T* const_iterator;
260 #endif
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); }
271
272     // more Qt
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;
281
282     T value(int i) const;
283     T value(int i, const T &defaultValue) const;
284
285     // STL compatibility
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(); }
305
306     // comfort
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; }
316
317     QList<T> toList() const;
318
319     static QVector<T> fromList(const QList<T> &list);
320
321 #ifndef QT_NO_STL
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; }
326 #endif
327 private:
328     friend class QRegion; // Optimization for QRegion::rects()
329
330     void detach_helper();
331     QVectorData *malloc(int alloc);
332     void realloc(int size, int alloc);
333     void free(Data *d);
334
335     static Q_DECL_CONSTEXPR int offsetOfTypedData()
336     {
337         // (non-POD)-safe offsetof(Data, array)
338         return (sizeof(QVectorData) + (alignOfTypedData() - 1)) & ~(alignOfTypedData() - 1);
339     }
340     static Q_DECL_CONSTEXPR int alignOfTypedData()
341     {
342 #ifdef Q_ALIGNOF
343         return Q_ALIGNOF(Data);
344 #else
345         return sizeof(void *);
346 #endif
347     }
348 };
349
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))
360           : d->alloc); }
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");
375   return data()[i]; }
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); }
395
396 template <typename T>
397 inline void QVector<T>::replace(int i, const T &t)
398 {
399     Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
400     const T copy(t);
401     data()[i] = copy;
402 }
403
404 template <typename T>
405 QVector<T> &QVector<T>::operator=(const QVector<T> &v)
406 {
407     if (v.d != d) {
408         QVector<T> tmp(v);
409         tmp.swap(*this);
410     }
411     return *this;
412 }
413
414 template <typename T>
415 inline QVectorData *QVector<T>::malloc(int aalloc)
416 {
417     QVectorData *vectordata = QVectorData::allocate(offsetOfTypedData() + aalloc * sizeof(T), alignOfTypedData());
418     Q_CHECK_PTR(vectordata);
419     return vectordata;
420 }
421
422 template <typename T>
423 QVector<T>::QVector(int asize)
424 {
425     d = malloc(asize);
426     d->ref.initializeOwned();
427     d->alloc = d->size = asize;
428     d->capacity = false;
429     if (QTypeInfo<T>::isComplex) {
430         T* b = p->array;
431         T* i = p->array + d->size;
432         while (i != b)
433             new (--i) T;
434     } else {
435         qMemSet(p->array, 0, asize * sizeof(T));
436     }
437 }
438
439 template <typename T>
440 QVector<T>::QVector(int asize, const T &t)
441 {
442     d = malloc(asize);
443     d->ref.initializeOwned();
444     d->alloc = d->size = asize;
445     d->capacity = false;
446     T* i = p->array + d->size;
447     while (i != p->array)
448         new (--i) T(t);
449 }
450
451 #ifdef Q_COMPILER_INITIALIZER_LISTS
452 template <typename T>
453 QVector<T>::QVector(std::initializer_list<T> args)
454 {
455     d = malloc(int(args.size()));
456     d->ref.initializeOwned();
457     d->alloc = d->size = int(args.size());
458     d->capacity = false;
459     T* i = p->array + d->size;
460     auto it = args.end();
461     while (i != p->array)
462         new (--i) T(*(--it));
463 }
464 #endif
465
466 template <typename T>
467 void QVector<T>::free(Data *x)
468 {
469     if (QTypeInfo<T>::isComplex) {
470         T* b = x->array;
471         union { QVectorData *d; Data *p; } u;
472         u.p = x;
473         T* i = b + u.d->size;
474         while (i-- != b)
475              i->~T();
476     }
477     x->free(x, alignOfTypedData());
478 }
479
480 template <typename T>
481 void QVector<T>::realloc(int asize, int aalloc)
482 {
483     Q_ASSERT(asize <= aalloc);
484     T *pOld;
485     T *pNew;
486     union { QVectorData *d; Data *p; } x;
487     x.d = d;
488
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) {
495             (--pOld)->~T();
496             d->size--;
497         }
498     }
499
500     if (aalloc != d->alloc || !isDetached()) {
501         // (re)allocate memory
502         if (QTypeInfo<T>::isStatic) {
503             x.d = malloc(aalloc);
504             Q_CHECK_PTR(x.p);
505             x.d->size = 0;
506         } else if (!isDetached()) {
507             x.d = malloc(aalloc);
508             Q_CHECK_PTR(x.p);
509             if (QTypeInfo<T>::isComplex) {
510                 x.d->size = 0;
511             } else {
512                 ::memcpy(x.p, p, offsetOfTypedData() + qMin(aalloc, d->alloc) * sizeof(T));
513                 x.d->size = d->size;
514             }
515         } else {
516             QT_TRY {
517                 QVectorData *mem = QVectorData::reallocate(d, offsetOfTypedData() + aalloc * sizeof(T),
518                                                            offsetOfTypedData() + d->alloc * sizeof(T), alignOfTypedData());
519                 Q_CHECK_PTR(mem);
520                 x.d = d = mem;
521                 x.d->size = d->size;
522             } QT_CATCH (const std::bad_alloc &) {
523                 if (aalloc > d->alloc) // ignore the error in case we are just shrinking.
524                     QT_RETHROW;
525             }
526         }
527         x.d->ref.initializeOwned();
528         x.d->alloc = aalloc;
529         x.d->capacity = d->capacity;
530         x.d->reserved = 0;
531     }
532
533     if (QTypeInfo<T>::isComplex) {
534         QT_TRY {
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++);
541                 x.d->size++;
542             }
543             // construct all new objects when growing
544             while (x.d->size < asize) {
545                 new (pNew++) T;
546                 x.d->size++;
547             }
548         } QT_CATCH (...) {
549             free(x.p);
550             QT_RETHROW;
551         }
552
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));
556     }
557     x.d->size = asize;
558
559     if (d != x.d) {
560         if (!d->ref.deref())
561             free(p);
562         d = x.d;
563     }
564 }
565
566 template<typename T>
567 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
568 {
569     if (i < 0 || i >= d->size) {
570         return T();
571     }
572     return p->array[i];
573 }
574 template<typename T>
575 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
576 {
577     return ((i < 0 || i >= d->size) ? defaultValue : p->array[i]);
578 }
579
580 template <typename T>
581 void QVector<T>::append(const T &t)
582 {
583     if (!isDetached() || d->size + 1 > d->alloc) {
584         const T copy(t);
585         realloc(d->size, (d->size + 1 > d->alloc) ?
586                     QVectorData::grow(offsetOfTypedData(), d->size + 1, sizeof(T))
587                     : d->alloc);
588         if (QTypeInfo<T>::isComplex)
589             new (p->array + d->size) T(copy);
590         else
591             p->array[d->size] = copy;
592     } else {
593         if (QTypeInfo<T>::isComplex)
594             new (p->array + d->size) T(t);
595         else
596             p->array[d->size] = t;
597     }
598     ++d->size;
599 }
600
601 template <typename T>
602 typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
603 {
604     int offset = int(before - p->array);
605     if (n != 0) {
606         const T copy(t);
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;
612             while (i != b)
613                 new (--i) T;
614             i = p->array + d->size;
615             T *j = i + n;
616             b = p->array + offset;
617             while (i != b)
618                 *--j = *--i;
619             i = b+n;
620             while (i != b)
621                 *--i = copy;
622         } else {
623             T *b = p->array + offset;
624             T *i = b + n;
625             memmove(i, b, (d->size - offset) * sizeof(T));
626             while (i != b)
627                 new (--i) T(copy);
628         }
629         d->size += n;
630     }
631     return p->array + offset;
632 }
633
634 template <typename T>
635 typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
636 {
637     int f = int(abegin - p->array);
638     int l = int(aend - p->array);
639     int n = l - f;
640     detach();
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;
645         while (i != b) {
646             --i;
647             i->~T();
648         }
649     } else {
650         memmove(p->array + f, p->array + l, (d->size-l)*sizeof(T));
651     }
652     d->size -= n;
653     return p->array + f;
654 }
655
656 template <typename T>
657 bool QVector<T>::operator==(const QVector<T> &v) const
658 {
659     if (d->size != v.d->size)
660         return false;
661     if (d == v.d)
662         return true;
663     T* b = p->array;
664     T* i = b + d->size;
665     T* j = v.p->array + d->size;
666     while (i != b)
667         if (!(*--i == *--j))
668             return false;
669     return true;
670 }
671
672 template <typename T>
673 QVector<T> &QVector<T>::fill(const T &from, int asize)
674 {
675     const T copy(from);
676     resize(asize < 0 ? d->size : asize);
677     if (d->size) {
678         T *i = p->array + d->size;
679         T *b = p->array;
680         while (i != b)
681             *--i = copy;
682     }
683     return *this;
684 }
685
686 template <typename T>
687 QVector<T> &QVector<T>::operator+=(const QVector &l)
688 {
689     int newSize = d->size + l.d->size;
690     realloc(d->size, newSize);
691
692     T *w = p->array + newSize;
693     T *i = l.p->array + l.d->size;
694     T *b = l.p->array;
695     while (i != b) {
696         if (QTypeInfo<T>::isComplex)
697             new (--w) T(*--i);
698         else
699             *--w = *--i;
700     }
701     d->size = newSize;
702     return *this;
703 }
704
705 template <typename T>
706 int QVector<T>::indexOf(const T &t, int from) const
707 {
708     if (from < 0)
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;
713         while (++n != e)
714             if (*n == t)
715                 return n - p->array;
716     }
717     return -1;
718 }
719
720 template <typename T>
721 int QVector<T>::lastIndexOf(const T &t, int from) const
722 {
723     if (from < 0)
724         from += d->size;
725     else if (from >= d->size)
726         from = d->size-1;
727     if (from >= 0) {
728         T* b = p->array;
729         T* n = p->array + from + 1;
730         while (n != b) {
731             if (*--n == t)
732                 return n - b;
733         }
734     }
735     return -1;
736 }
737
738 template <typename T>
739 bool QVector<T>::contains(const T &t) const
740 {
741     T* b = p->array;
742     T* i = p->array + d->size;
743     while (i != b)
744         if (*--i == t)
745             return true;
746     return false;
747 }
748
749 template <typename T>
750 int QVector<T>::count(const T &t) const
751 {
752     int c = 0;
753     T* b = p->array;
754     T* i = p->array + d->size;
755     while (i != b)
756         if (*--i == t)
757             ++c;
758     return c;
759 }
760
761 template <typename T>
762 Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const
763 {
764     if (length < 0)
765         length = size() - pos;
766     if (pos == 0 && length == size())
767         return *this;
768     if (pos + length > size())
769         length = size() - pos;
770     QVector<T> copy;
771     copy.reserve(length);
772     for (int i = pos; i < pos + length; ++i)
773         copy += at(i);
774     return copy;
775 }
776
777 template <typename T>
778 Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const
779 {
780     QList<T> result;
781     result.reserve(size());
782     for (int i = 0; i < size(); ++i)
783         result.append(at(i));
784     return result;
785 }
786
787 template <typename T>
788 Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
789 {
790     QVector<T> result(size());
791     for (int i = 0; i < size(); ++i)
792         result[i] = at(i);
793     return result;
794 }
795
796 template <typename T>
797 QVector<T> QVector<T>::fromList(const QList<T> &list)
798 {
799     return list.toVector();
800 }
801
802 template <typename T>
803 QList<T> QList<T>::fromVector(const QVector<T> &vector)
804 {
805     return vector.toList();
806 }
807
808 Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
809 Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
810
811 /*
812    ### Qt 5:
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.
816 */
817
818 #ifdef Q_CC_MSVC
819 QT_BEGIN_INCLUDE_NAMESPACE
820 #include <QtCore/QPointF>
821 #include <QtCore/QPoint>
822 QT_END_INCLUDE_NAMESPACE
823
824 #if defined(QT_BUILD_CORE_LIB)
825 #define Q_TEMPLATE_EXTERN
826 #else
827 #define Q_TEMPLATE_EXTERN extern
828 #endif
829 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPointF>;
830 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPoint>;
831 #endif
832
833 QT_END_NAMESPACE
834
835 QT_END_HEADER
836
837 #endif // QVECTOR_H