Remove references to QT_NO_STL from QtCore
[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 #include <iterator>
51 #include <vector>
52 #include <stdlib.h>
53 #include <string.h>
54 #ifdef Q_COMPILER_INITIALIZER_LISTS
55 #include <initializer_list>
56 #endif
57
58 QT_BEGIN_HEADER
59
60 QT_BEGIN_NAMESPACE
61
62
63 struct Q_CORE_EXPORT QVectorData
64 {
65     QtPrivate::RefCount ref;
66     int size;
67     uint alloc : 31;
68     uint capacityReserved : 1;
69
70     qptrdiff offset;
71
72     void* data() { return reinterpret_cast<char *>(this) + this->offset; }
73
74     static const QVectorData shared_null;
75     static QVectorData *allocate(int size, int alignment);
76     static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
77     static void free(QVectorData *data, int alignment);
78     static int grow(int sizeOfHeader, int size, int sizeOfT);
79 };
80
81 template <typename T>
82 struct QVectorTypedData : QVectorData
83 {
84     T* begin() { return reinterpret_cast<T *>(this->data()); }
85     T* end() { return begin() + this->size; }
86
87     static QVectorTypedData *sharedNull() { return static_cast<QVectorTypedData *>(const_cast<QVectorData *>(&QVectorData::shared_null)); }
88 };
89
90 class QRegion;
91
92 template <typename T>
93 class QVector
94 {
95     typedef QVectorTypedData<T> Data;
96     Data *d;
97
98 public:
99     inline QVector() : d(Data::sharedNull()) { }
100     explicit QVector(int size);
101     QVector(int size, const T &t);
102     inline QVector(const QVector<T> &v)
103     {
104         if (v.d->ref.ref()) {
105             d = v.d;
106         } else {
107             d = Data::sharedNull();
108             realloc(0, int(v.d->alloc));
109             qCopy(v.d->begin(), v.d->end(), d->begin());
110             d->size = v.d->size;
111             d->capacityReserved = v.d->capacityReserved;
112         }
113     }
114
115     inline ~QVector() { if (!d->ref.deref()) free(d); }
116     QVector<T> &operator=(const QVector<T> &v);
117 #ifdef Q_COMPILER_RVALUE_REFS
118     inline QVector<T> operator=(QVector<T> &&other)
119     { qSwap(d, other.d); return *this; }
120 #endif
121     inline void swap(QVector<T> &other) { qSwap(d, other.d); }
122 #ifdef Q_COMPILER_INITIALIZER_LISTS
123     inline QVector(std::initializer_list<T> args);
124 #endif
125     bool operator==(const QVector<T> &v) const;
126     inline bool operator!=(const QVector<T> &v) const { return !(*this == v); }
127
128     inline int size() const { return d->size; }
129
130     inline bool isEmpty() const { return d->size == 0; }
131
132     void resize(int size);
133
134     inline int capacity() const { return int(d->alloc); }
135     void reserve(int size);
136     inline void squeeze() { realloc(d->size, d->size); d->capacityReserved = 0; }
137
138     inline void detach() { if (!isDetached()) detach_helper(); }
139     inline bool isDetached() const { return !d->ref.isShared(); }
140     inline void setSharable(bool sharable)
141     {
142         if (sharable == d->ref.isSharable())
143             return;
144         if (!sharable)
145             detach();
146         if (d != Data::sharedNull())
147             d->ref.setSharable(sharable);
148     }
149
150     inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
151
152     inline T *data() { detach(); return d->begin(); }
153     inline const T *data() const { return d->begin(); }
154     inline const T *constData() const { return d->begin(); }
155     void clear();
156
157     const T &at(int i) const;
158     T &operator[](int i);
159     const T &operator[](int i) const;
160     void append(const T &t);
161     void prepend(const T &t);
162     void insert(int i, const T &t);
163     void insert(int i, int n, const T &t);
164     void replace(int i, const T &t);
165     void remove(int i);
166     void remove(int i, int n);
167
168     QVector<T> &fill(const T &t, int size = -1);
169
170     int indexOf(const T &t, int from = 0) const;
171     int lastIndexOf(const T &t, int from = -1) const;
172     bool contains(const T &t) const;
173     int count(const T &t) const;
174
175 #ifdef QT_STRICT_ITERATORS
176     class iterator {
177     public:
178         T *i;
179         typedef std::random_access_iterator_tag  iterator_category;
180         typedef qptrdiff difference_type;
181         typedef T value_type;
182         typedef T *pointer;
183         typedef T &reference;
184
185         inline iterator() : i(0) {}
186         inline iterator(T *n) : i(n) {}
187         inline iterator(const iterator &o): i(o.i){}
188         inline T &operator*() const { return *i; }
189         inline T *operator->() const { return i; }
190         inline T &operator[](int j) const { return *(i + j); }
191         inline bool operator==(const iterator &o) const { return i == o.i; }
192         inline bool operator!=(const iterator &o) const { return i != o.i; }
193         inline bool operator<(const iterator& other) const { return i < other.i; }
194         inline bool operator<=(const iterator& other) const { return i <= other.i; }
195         inline bool operator>(const iterator& other) const { return i > other.i; }
196         inline bool operator>=(const iterator& other) const { return i >= other.i; }
197         inline iterator &operator++() { ++i; return *this; }
198         inline iterator operator++(int) { T *n = i; ++i; return n; }
199         inline iterator &operator--() { i--; return *this; }
200         inline iterator operator--(int) { T *n = i; i--; return n; }
201         inline iterator &operator+=(int j) { i+=j; return *this; }
202         inline iterator &operator-=(int j) { i-=j; return *this; }
203         inline iterator operator+(int j) const { return iterator(i+j); }
204         inline iterator operator-(int j) const { return iterator(i-j); }
205         inline int operator-(iterator j) const { return i - j.i; }
206     };
207     friend class iterator;
208
209     class const_iterator {
210     public:
211         T *i;
212         typedef std::random_access_iterator_tag  iterator_category;
213         typedef qptrdiff difference_type;
214         typedef T value_type;
215         typedef const T *pointer;
216         typedef const T &reference;
217
218         inline const_iterator() : i(0) {}
219         inline const_iterator(T *n) : i(n) {}
220         inline const_iterator(const const_iterator &o): i(o.i) {}
221         inline explicit const_iterator(const iterator &o): i(o.i) {}
222         inline const T &operator*() const { return *i; }
223         inline const T *operator->() const { return i; }
224         inline const T &operator[](int j) const { return *(i + j); }
225         inline bool operator==(const const_iterator &o) const { return i == o.i; }
226         inline bool operator!=(const const_iterator &o) const { return i != o.i; }
227         inline bool operator<(const const_iterator& other) const { return i < other.i; }
228         inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
229         inline bool operator>(const const_iterator& other) const { return i > other.i; }
230         inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
231         inline const_iterator &operator++() { ++i; return *this; }
232         inline const_iterator operator++(int) { T *n = i; ++i; return n; }
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+=(int j) { i+=j; return *this; }
236         inline const_iterator &operator-=(int j) { i-=j; return *this; }
237         inline const_iterator operator+(int j) const { return const_iterator(i+j); }
238         inline const_iterator operator-(int j) const { return const_iterator(i-j); }
239         inline int operator-(const_iterator j) const { return i - j.i; }
240     };
241     friend class const_iterator;
242 #else
243     // STL-style
244     typedef T* iterator;
245     typedef const T* const_iterator;
246 #endif
247     inline iterator begin() { detach(); return d->begin(); }
248     inline const_iterator begin() const { return d->begin(); }
249     inline const_iterator cbegin() const { return d->begin(); }
250     inline const_iterator constBegin() const { return d->begin(); }
251     inline iterator end() { detach(); return d->end(); }
252     inline const_iterator end() const { return d->end(); }
253     inline const_iterator cend() 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); }
259
260     // more Qt
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;
269
270     T value(int i) const;
271     T value(int i, const T &defaultValue) const;
272
273     // STL compatibility
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(); }
293
294     // comfort
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; }
304
305     QList<T> toList() const;
306
307     static QVector<T> fromList(const QList<T> &list);
308
309     static inline QVector<T> fromStdVector(const std::vector<T> &vector)
310     { QVector<T> tmp; tmp.reserve(int(vector.size())); qCopy(vector.begin(), vector.end(), std::back_inserter(tmp)); return tmp; }
311     inline std::vector<T> toStdVector() const
312     { std::vector<T> tmp; tmp.reserve(size()); qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
313 private:
314     friend class QRegion; // Optimization for QRegion::rects()
315
316     void detach_helper();
317     Data *malloc(int alloc);
318     void realloc(int size, int alloc);
319     void free(Data *d);
320
321     class AlignmentDummy { QVectorData header; T array[1]; };
322
323     static Q_DECL_CONSTEXPR int offsetOfTypedData()
324     {
325         // (non-POD)-safe offsetof(AlignmentDummy, array)
326         return (sizeof(QVectorData) + (alignOfTypedData() - 1)) & ~(alignOfTypedData() - 1);
327     }
328     static Q_DECL_CONSTEXPR int alignOfTypedData()
329     {
330         return Q_ALIGNOF(AlignmentDummy);
331     }
332 };
333
334 template <typename T>
335 void QVector<T>::detach_helper()
336 { realloc(d->size, int(d->alloc)); }
337 template <typename T>
338 void QVector<T>::reserve(int asize)
339 { if (asize > int(d->alloc)) realloc(d->size, asize); if (isDetached()) d->capacityReserved = 1; }
340 template <typename T>
341 void QVector<T>::resize(int asize)
342 { realloc(asize, (asize > int(d->alloc) || (!d->capacityReserved && asize < d->size && asize < int(d->alloc >> 1))) ?
343           QVectorData::grow(offsetOfTypedData(), asize, sizeof(T))
344           : int(d->alloc)); }
345 template <typename T>
346 inline void QVector<T>::clear()
347 { *this = QVector<T>(); }
348 template <typename T>
349 inline const T &QVector<T>::at(int i) const
350 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
351   return d->begin()[i]; }
352 template <typename T>
353 inline const T &QVector<T>::operator[](int i) const
354 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
355   return d->begin()[i]; }
356 template <typename T>
357 inline T &QVector<T>::operator[](int i)
358 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
359   return data()[i]; }
360 template <typename T>
361 inline void QVector<T>::insert(int i, const T &t)
362 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
363   insert(begin() + i, 1, t); }
364 template <typename T>
365 inline void QVector<T>::insert(int i, int n, const T &t)
366 { Q_ASSERT_X(i >= 0 && i <= d->size, "QVector<T>::insert", "index out of range");
367   insert(begin() + i, n, t); }
368 template <typename T>
369 inline void QVector<T>::remove(int i, int n)
370 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->size, "QVector<T>::remove", "index out of range");
371   erase(begin() + i, begin() + i + n); }
372 template <typename T>
373 inline void QVector<T>::remove(int i)
374 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::remove", "index out of range");
375   erase(begin() + i, begin() + i + 1); }
376 template <typename T>
377 inline void QVector<T>::prepend(const T &t)
378 { insert(begin(), 1, t); }
379
380 template <typename T>
381 inline void QVector<T>::replace(int i, const T &t)
382 {
383     Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::replace", "index out of range");
384     const T copy(t);
385     data()[i] = copy;
386 }
387
388 template <typename T>
389 QVector<T> &QVector<T>::operator=(const QVector<T> &v)
390 {
391     if (v.d != d) {
392         QVector<T> tmp(v);
393         tmp.swap(*this);
394     }
395     return *this;
396 }
397
398 template <typename T>
399 inline typename QVector<T>::Data *QVector<T>::malloc(int aalloc)
400 {
401     QVectorData *vectordata = QVectorData::allocate(offsetOfTypedData() + aalloc * sizeof(T), alignOfTypedData());
402     Q_CHECK_PTR(vectordata);
403     return static_cast<Data *>(vectordata);
404 }
405
406 template <typename T>
407 QVector<T>::QVector(int asize)
408 {
409     d = malloc(asize);
410     d->ref.initializeOwned();
411     d->size = asize;
412     d->alloc = uint(d->size);
413     d->capacityReserved = false;
414     d->offset = offsetOfTypedData();
415     if (QTypeInfo<T>::isComplex) {
416         T* b = d->begin();
417         T* i = d->end();
418         while (i != b)
419             new (--i) T;
420     } else {
421         qMemSet(d->begin(), 0, asize * sizeof(T));
422     }
423 }
424
425 template <typename T>
426 QVector<T>::QVector(int asize, const T &t)
427 {
428     d = malloc(asize);
429     d->ref.initializeOwned();
430     d->size = asize;
431     d->alloc = uint(d->size);
432     d->capacityReserved = false;
433     d->offset = offsetOfTypedData();
434     T* i = d->end();
435     while (i != d->begin())
436         new (--i) T(t);
437 }
438
439 #ifdef Q_COMPILER_INITIALIZER_LISTS
440 template <typename T>
441 QVector<T>::QVector(std::initializer_list<T> args)
442 {
443     d = malloc(int(args.size()));
444     d->ref.initializeOwned();
445     d->size = int(args.size());
446     d->alloc = uint(d->size);
447     d->capacityReserved = false;
448     d->offset = offsetOfTypedData();
449     if (QTypeInfo<T>::isComplex) {
450         T* b = d->begin();
451         T* i = d->end();
452         const T* s = args.end();
453         while (i != b)
454             new(--i) T(*--s);
455     } else {
456         // std::initializer_list<T>::iterator is guaranteed to be
457         // const T* ([support.initlist]/1), so can be memcpy'ed away from:
458         ::memcpy(d->begin(), args.begin(), args.size() * sizeof(T));
459     }
460 }
461 #endif
462
463 template <typename T>
464 void QVector<T>::free(Data *x)
465 {
466     if (QTypeInfo<T>::isComplex) {
467         T* b = x->begin();
468         T* i = b + x->size;
469         while (i-- != b)
470              i->~T();
471     }
472     Data::free(x, alignOfTypedData());
473 }
474
475 template <typename T>
476 void QVector<T>::realloc(int asize, int aalloc)
477 {
478     Q_ASSERT(asize <= aalloc);
479     T *pOld;
480     T *pNew;
481     Data *x = d;
482
483     if (QTypeInfo<T>::isComplex && asize < d->size && isDetached()) {
484         // call the destructor on all objects that need to be
485         // destroyed when shrinking
486         pOld = d->begin() + d->size;
487         pNew = d->begin() + asize;
488         while (asize < d->size) {
489             (--pOld)->~T();
490             d->size--;
491         }
492     }
493
494     if (aalloc != int(d->alloc) || !isDetached()) {
495         // (re)allocate memory
496         if (QTypeInfo<T>::isStatic) {
497             x = malloc(aalloc);
498             Q_CHECK_PTR(x);
499             x->size = 0;
500         } else if (!isDetached()) {
501             x = malloc(aalloc);
502             Q_CHECK_PTR(x);
503             if (QTypeInfo<T>::isComplex) {
504                 x->size = 0;
505             } else {
506                 ::memcpy(x, d, offsetOfTypedData() + qMin(uint(aalloc), d->alloc) * sizeof(T));
507                 x->size = d->size;
508             }
509         } else {
510             QT_TRY {
511                 QVectorData *mem = QVectorData::reallocate(d, offsetOfTypedData() + aalloc * sizeof(T),
512                                                            offsetOfTypedData() + d->alloc * sizeof(T), alignOfTypedData());
513                 Q_CHECK_PTR(mem);
514                 x = d = static_cast<Data *>(mem);
515                 x->size = d->size;
516             } QT_CATCH (const std::bad_alloc &) {
517                 if (aalloc > int(d->alloc)) // ignore the error in case we are just shrinking.
518                     QT_RETHROW;
519             }
520         }
521         x->ref.initializeOwned();
522         x->alloc = uint(aalloc);
523         x->capacityReserved = d->capacityReserved;
524         x->offset = offsetOfTypedData();
525     }
526
527     if (QTypeInfo<T>::isComplex) {
528         QT_TRY {
529             pOld = d->begin() + x->size;
530             pNew = x->begin() + x->size;
531             // copy objects from the old array into the new array
532             const int toMove = qMin(asize, d->size);
533             while (x->size < toMove) {
534                 new (pNew++) T(*pOld++);
535                 x->size++;
536             }
537             // construct all new objects when growing
538             while (x->size < asize) {
539                 new (pNew++) T;
540                 x->size++;
541             }
542         } QT_CATCH (...) {
543             free(x);
544             QT_RETHROW;
545         }
546
547     } else if (asize > x->size) {
548         // initialize newly allocated memory to 0
549         qMemSet(x->end(), 0, (asize - x->size) * sizeof(T));
550     }
551     x->size = asize;
552
553     if (d != x) {
554         if (!d->ref.deref())
555             free(d);
556         d = x;
557     }
558 }
559
560 template<typename T>
561 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
562 {
563     if (i < 0 || i >= d->size) {
564         return T();
565     }
566     return d->begin()[i];
567 }
568 template<typename T>
569 Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
570 {
571     return ((i < 0 || i >= d->size) ? defaultValue : d->begin()[i]);
572 }
573
574 template <typename T>
575 void QVector<T>::append(const T &t)
576 {
577     if (!isDetached() || d->size + 1 > int(d->alloc)) {
578         const T copy(t);
579         realloc(d->size, (d->size + 1 > int(d->alloc)) ?
580                     QVectorData::grow(offsetOfTypedData(), d->size + 1, sizeof(T))
581                     : int(d->alloc));
582         if (QTypeInfo<T>::isComplex)
583             new (d->end()) T(copy);
584         else
585             *d->end() = copy;
586     } else {
587         if (QTypeInfo<T>::isComplex)
588             new (d->end()) T(t);
589         else
590             *d->end() = t;
591     }
592     ++d->size;
593 }
594
595 template <typename T>
596 typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
597 {
598     int offset = int(before - d->begin());
599     if (n != 0) {
600         const T copy(t);
601         if (!isDetached() || d->size + n > int(d->alloc))
602             realloc(d->size, QVectorData::grow(offsetOfTypedData(), d->size + n, sizeof(T)));
603         if (QTypeInfo<T>::isStatic) {
604             T *b = d->end();
605             T *i = d->end() + n;
606             while (i != b)
607                 new (--i) T;
608             i = d->end();
609             T *j = i + n;
610             b = d->begin() + offset;
611             while (i != b)
612                 *--j = *--i;
613             i = b+n;
614             while (i != b)
615                 *--i = copy;
616         } else {
617             T *b = d->begin() + offset;
618             T *i = b + n;
619             memmove(i, b, (d->size - offset) * sizeof(T));
620             while (i != b)
621                 new (--i) T(copy);
622         }
623         d->size += n;
624     }
625     return d->begin() + offset;
626 }
627
628 template <typename T>
629 typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
630 {
631     int f = int(abegin - d->begin());
632     int l = int(aend - d->begin());
633     int n = l - f;
634     detach();
635     if (QTypeInfo<T>::isComplex) {
636         qCopy(d->begin()+l, d->end(), d->begin()+f);
637         T *i = d->end();
638         T* b = d->end()-n;
639         while (i != b) {
640             --i;
641             i->~T();
642         }
643     } else {
644         memmove(d->begin() + f, d->begin() + l, (d->size-l)*sizeof(T));
645     }
646     d->size -= n;
647     return d->begin() + f;
648 }
649
650 template <typename T>
651 bool QVector<T>::operator==(const QVector<T> &v) const
652 {
653     if (d->size != v.d->size)
654         return false;
655     if (d == v.d)
656         return true;
657     T* b = d->begin();
658     T* i = b + d->size;
659     T* j = v.d->end();
660     while (i != b)
661         if (!(*--i == *--j))
662             return false;
663     return true;
664 }
665
666 template <typename T>
667 QVector<T> &QVector<T>::fill(const T &from, int asize)
668 {
669     const T copy(from);
670     resize(asize < 0 ? d->size : asize);
671     if (d->size) {
672         T *i = d->end();
673         T *b = d->begin();
674         while (i != b)
675             *--i = copy;
676     }
677     return *this;
678 }
679
680 template <typename T>
681 QVector<T> &QVector<T>::operator+=(const QVector &l)
682 {
683     int newSize = d->size + l.d->size;
684     realloc(d->size, newSize);
685
686     T *w = d->begin() + newSize;
687     T *i = l.d->end();
688     T *b = l.d->begin();
689     while (i != b) {
690         if (QTypeInfo<T>::isComplex)
691             new (--w) T(*--i);
692         else
693             *--w = *--i;
694     }
695     d->size = newSize;
696     return *this;
697 }
698
699 template <typename T>
700 int QVector<T>::indexOf(const T &t, int from) const
701 {
702     if (from < 0)
703         from = qMax(from + d->size, 0);
704     if (from < d->size) {
705         T* n = d->begin() + from - 1;
706         T* e = d->end();
707         while (++n != e)
708             if (*n == t)
709                 return n - d->begin();
710     }
711     return -1;
712 }
713
714 template <typename T>
715 int QVector<T>::lastIndexOf(const T &t, int from) const
716 {
717     if (from < 0)
718         from += d->size;
719     else if (from >= d->size)
720         from = d->size-1;
721     if (from >= 0) {
722         T* b = d->begin();
723         T* n = d->begin() + from + 1;
724         while (n != b) {
725             if (*--n == t)
726                 return n - b;
727         }
728     }
729     return -1;
730 }
731
732 template <typename T>
733 bool QVector<T>::contains(const T &t) const
734 {
735     T* b = d->begin();
736     T* i = d->end();
737     while (i != b)
738         if (*--i == t)
739             return true;
740     return false;
741 }
742
743 template <typename T>
744 int QVector<T>::count(const T &t) const
745 {
746     int c = 0;
747     T* b = d->begin();
748     T* i = d->end();
749     while (i != b)
750         if (*--i == t)
751             ++c;
752     return c;
753 }
754
755 template <typename T>
756 Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const
757 {
758     if (length < 0)
759         length = size() - pos;
760     if (pos == 0 && length == size())
761         return *this;
762     if (pos + length > size())
763         length = size() - pos;
764     QVector<T> copy;
765     copy.reserve(length);
766     for (int i = pos; i < pos + length; ++i)
767         copy += at(i);
768     return copy;
769 }
770
771 template <typename T>
772 Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const
773 {
774     QList<T> result;
775     result.reserve(size());
776     for (int i = 0; i < size(); ++i)
777         result.append(at(i));
778     return result;
779 }
780
781 template <typename T>
782 Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
783 {
784     QVector<T> result(size());
785     for (int i = 0; i < size(); ++i)
786         result[i] = at(i);
787     return result;
788 }
789
790 template <typename T>
791 QVector<T> QVector<T>::fromList(const QList<T> &list)
792 {
793     return list.toVector();
794 }
795
796 template <typename T>
797 QList<T> QList<T>::fromVector(const QVector<T> &vector)
798 {
799     return vector.toList();
800 }
801
802 Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)
803 Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)
804
805 /*
806    ### Qt 5:
807    ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because
808    ### Qt exports QPolygon and QPolygonF that inherit QVector<QPoint> and
809    ### QVector<QPointF> respectively.
810 */
811
812 #ifdef Q_CC_MSVC
813 QT_BEGIN_INCLUDE_NAMESPACE
814 #include <QtCore/QPointF>
815 #include <QtCore/QPoint>
816 QT_END_INCLUDE_NAMESPACE
817
818 #if defined(QT_BUILD_CORE_LIB)
819 #define Q_TEMPLATE_EXTERN
820 #else
821 #define Q_TEMPLATE_EXTERN extern
822 #endif
823 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPointF>;
824 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPoint>;
825 #endif
826
827 QT_END_NAMESPACE
828
829 QT_END_HEADER
830
831 #endif // QVECTOR_H