45 #include <QtCore/qiterator.h> 46 #include <QtCore/qatomic.h> 47 #include <QtCore/qalgorithms.h> 48 #include <QtCore/qlist.h> 56 #ifdef Q_COMPILER_INITIALIZER_LISTS 57 #include <initializer_list> 71 #if defined(QT_ARCH_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED) 86 static QVectorData *malloc(
int sizeofTypedData,
int size,
int sizeofT, QVectorData *
init);
87 static QVectorData *allocate(
int size,
int alignment);
88 static QVectorData *reallocate(QVectorData *old,
int newsize,
int oldsize,
int alignment);
89 static void free(QVectorData *
data,
int alignment);
90 static int grow(
int sizeofTypedData,
int size,
int sizeofT,
bool excessive);
104 template <
typename T>
110 #if defined(Q_CC_SUN) && (__SUNPRO_CC <= 0x550) 126 #ifdef Q_COMPILER_RVALUE_REFS 128 {
qSwap(p, other.p);
return *
this; }
131 #ifdef Q_COMPILER_INITIALIZER_LISTS 132 inline QVector(std::initializer_list<T> args);
137 inline int size()
const {
return d->size; }
139 inline bool isEmpty()
const {
return d->size == 0; }
141 void resize(
int size);
144 void reserve(
int size);
145 inline void squeeze() { realloc(
d->size,
d->size);
d->capacity = 0; }
147 inline void detach() {
if (
d->ref != 1) detach_helper(); }
152 inline T *
data() { detach();
return p->array; }
153 inline const T *
data()
const {
return p->array; }
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);
166 void remove(
int i,
int n);
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;
175 #ifdef QT_STRICT_ITERATORS 179 typedef std::random_access_iterator_tag iterator_category;
188 inline T &
operator*()
const {
return *i; }
189 inline T *operator->()
const {
return i; }
190 inline T &operator[](
int j)
const {
return *(i + j); }
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; }
202 inline iterator &operator-=(
int j) { i-=j;
return *
this; }
212 typedef std::random_access_iterator_tag iterator_category;
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); }
232 inline const_iterator operator++(
int) { T *n = i; ++i;
return n; }
234 inline const_iterator operator--(
int) { T *n = i; i--;
return n; }
247 inline iterator
begin() { detach();
return p->array; }
248 inline const_iterator
begin()
const {
return p->array; }
249 inline const_iterator
constBegin()
const {
return p->array; }
250 inline iterator
end() { detach();
return p->array +
d->size; }
251 inline const_iterator
end()
const {
return p->array +
d->size; }
252 inline const_iterator
constEnd()
const {
return p->array +
d->size; }
253 iterator insert(iterator before,
int n,
const T &x);
254 inline iterator
insert(iterator before,
const T &x) {
return insert(before, 1, x); }
255 iterator erase(iterator begin, iterator
end);
256 inline iterator
erase(iterator pos) {
return erase(pos, pos+1); }
259 inline int count()
const {
return d->size; }
266 QVector<T> mid(
int pos,
int length = -1)
const;
268 T value(
int i)
const;
269 T value(
int i,
const T &defaultValue)
const;
286 {
return d->size == 0; }
287 inline T&
front() {
return first(); }
288 inline const_reference
front()
const {
return first(); }
289 inline reference
back() {
return last(); }
290 inline const_reference
back()
const {
return last(); }
295 {
QVector n = *
this; n +=
l;
return n; }
297 { append(t);
return *
this; }
299 { append(t);
return *
this; }
301 { *
this +=
l;
return *
this; }
309 {
QVector<T> tmp; tmp.
reserve(
int(vector.size()));
qCopy(vector.begin(), vector.end(), std::back_inserter(tmp));
return tmp; }
311 { std::vector<T> tmp; tmp.reserve(
size());
qCopy(constBegin(), constEnd(), std::back_inserter(tmp));
return tmp; }
316 void detach_helper();
318 void realloc(
int size,
int alloc);
323 return reinterpret_cast<const char *
>(&(
reinterpret_cast<const Data *
>(
this))->array[1]) - reinterpret_cast<const char *>(
this);
328 return qMax<int>(
sizeof(
void*), Q_ALIGNOF(Data));
335 template <
typename T>
337 { realloc(
d->size,
d->alloc); }
338 template <
typename T>
340 {
if (asize >
d->alloc) realloc(
d->size, asize);
if (
d->ref == 1)
d->capacity = 1; }
341 template <
typename T>
343 { realloc(asize, (asize >
d->alloc || (!
d->capacity && asize < d->
size && asize < (
d->alloc >> 1))) ?
346 template <
typename T>
349 template <
typename T>
351 {
Q_ASSERT_X(i >= 0 && i < d->
size,
"QVector<T>::at",
"index out of range");
352 return p->array[i]; }
353 template <
typename T>
355 {
Q_ASSERT_X(i >= 0 && i < d->
size,
"QVector<T>::operator[]",
"index out of range");
356 return p->array[i]; }
357 template <
typename T>
359 {
Q_ASSERT_X(i >= 0 && i < d->
size,
"QVector<T>::operator[]",
"index out of range");
361 template <
typename T>
363 {
Q_ASSERT_X(i >= 0 && i <= d->
size,
"QVector<T>::insert",
"index out of range");
364 insert(begin() + i, 1, t); }
365 template <
typename T>
367 {
Q_ASSERT_X(i >= 0 && i <= d->
size,
"QVector<T>::insert",
"index out of range");
368 insert(begin() + i, n, t); }
369 template <
typename T>
371 {
Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= d->
size,
"QVector<T>::remove",
"index out of range");
372 erase(begin() + i, begin() + i + n); }
373 template <
typename T>
375 {
Q_ASSERT_X(i >= 0 && i < d->
size,
"QVector<T>::remove",
"index out of range");
376 erase(begin() + i, begin() + i + 1); }
377 template <
typename T>
379 { insert(begin(), 1, t); }
381 template <
typename T>
384 Q_ASSERT_X(i >= 0 && i < d->
size,
"QVector<T>::replace",
"index out of range");
389 template <
typename T>
402 template <
typename T>
410 template <
typename T>
415 d->alloc =
d->size = asize;
420 T* i = p->array +
d->size;
424 qMemSet(p->array, 0, asize *
sizeof(T));
428 template <
typename T>
433 d->alloc =
d->size = asize;
436 T* i = p->array +
d->size;
437 while (i != p->array)
441 #ifdef Q_COMPILER_INITIALIZER_LISTS 442 template <
typename T>
447 d->alloc =
d->size = int(args.size());
450 T* i = p->array +
d->size;
451 auto it = args.end();
452 while (i != p->array)
453 new (--i) T(*(--
it));
457 template <
typename T>
464 T* i = b +
u.d->size;
468 x->
free(x, alignOfTypedData());
471 template <
typename T>
483 pOld = p->array +
d->size;
484 pNew = p->array + asize;
485 while (asize < d->
size) {
491 if (aalloc !=
d->alloc ||
d->ref != 1) {
497 }
else if (
d->ref != 1) {
503 ::memcpy(x.p, p, sizeOfTypedData() + (
qMin(aalloc,
d->alloc) - 1) *
sizeof(T));
509 sizeOfTypedData() + (
d->alloc - 1) *
sizeof(T), alignOfTypedData());
513 }
QT_CATCH (
const std::bad_alloc &) {
514 if (aalloc >
d->alloc)
520 x.d->sharable =
true;
521 x.d->capacity =
d->capacity;
527 pOld = p->array + x.d->size;
528 pNew = x.p->array + x.d->size;
530 const int toMove =
qMin(asize,
d->size);
531 while (x.d->size < toMove) {
532 new (pNew++) T(*pOld++);
536 while (x.d->size < asize) {
545 }
else if (asize > x.d->size) {
547 qMemSet(x.p->array + x.d->size, 0, (asize - x.d->size) *
sizeof(T));
561 if (i < 0 || i >=
d->size) {
569 return ((i < 0 || i >=
d->size) ? defaultValue : p->array[i]);
572 template <
typename T>
575 if (
d->ref != 1 ||
d->size + 1 >
d->alloc) {
580 new (p->array +
d->size) T(copy);
582 p->array[
d->size] = copy;
585 new (p->array +
d->size) T(t);
587 p->array[
d->size] = t;
592 template <
typename T>
595 int offset = int(before - p->array);
598 if (
d->ref != 1 ||
d->size + n >
d->alloc)
602 T *b = p->array +
d->size;
603 T *i = p->array +
d->size + n;
606 i = p->array +
d->size;
608 b = p->array + offset;
615 T *b = p->array + offset;
617 memmove(i, b, (
d->size - offset) *
sizeof(T));
623 return p->array + offset;
626 template <
typename T>
629 int f = int(abegin - p->array);
630 int l = int(aend - p->array);
634 qCopy(p->array+l, p->array+
d->size, p->array+f);
635 T *i = p->array+
d->size;
636 T* b = p->array+
d->size-n;
642 memmove(p->array + f, p->array + l, (
d->size-l)*
sizeof(T));
648 template <
typename T>
664 template <
typename T>
668 resize(asize < 0 ? d->
size : asize);
670 T *i = p->array +
d->size;
678 template <
typename T>
681 int newSize =
d->size + l.
d->
size;
682 realloc(
d->size, newSize);
684 T *w = p->array + newSize;
697 template <
typename T>
701 from =
qMax(from +
d->size, 0);
702 if (from < d->
size) {
703 T* n = p->array + from - 1;
704 T* e = p->array +
d->size;
712 template <
typename T>
717 else if (from >=
d->size)
721 T* n = p->array + from + 1;
730 template <
typename T>
734 T* i = p->array +
d->size;
741 template <
typename T>
746 T* i = p->array +
d->size;
753 template <
typename T>
757 length =
size() - pos;
758 if (pos == 0 && length ==
size())
760 if (pos + length >
size())
761 length =
size() - pos;
764 for (
int i = pos; i < pos + length; ++i)
769 template <
typename T>
774 for (
int i = 0; i <
size(); ++i)
779 template <
typename T>
783 for (
int i = 0; i <
size(); ++i)
788 template <
typename T>
794 template <
typename T>
812 #include <QtCore/QPointF> 813 #include <QtCore/QPoint> 816 #if defined(QT_BUILD_CORE_LIB) 817 #define Q_TEMPLATE_EXTERN 819 #define Q_TEMPLATE_EXTERN extern
void setSharable(bool sharable)
std::vector< T > toStdVector() const
Returns a std::vector object with the data contained in this QVector.
bool startsWith(const T &t) const
Returns true if this vector is not empty and its first item is equal to value; otherwise returns fals...
void squeeze()
Releases any memory not required to store the items.
bool isSharedWith(const QVector< T > &other) const
timeval operator-(const timeval &t1, const timeval &t2)
static QVectorData * reallocate(QVectorData *old, int newsize, int oldsize, int alignment)
static QVectorData * allocate(int size, int alignment)
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
void pop_front()
This function is provided for STL compatibility.
#define QT_END_NAMESPACE
This macro expands to.
QList< T > toList() const
Returns a QList object with the data contained in this QVector.
QVector< T > & operator+=(const T &t)
Appends value to the vector.
void replace(int i, const T &t)
Replaces the item at index position i with value.
void remove(int i)
Removes the element at index position i.
bool operator>(const QByteArray &a1, const QByteArray &a2)
QVector< T > & fill(const T &t, int size=-1)
Assigns value to all items in the vector.
int count() const
Same as size().
#define it(className, varName)
static void free(QVectorData *data, int alignment)
T & first()
Returns a reference to the first item in the vector.
#define at(className, varName)
const T * data() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector...
T & operator[](int i)
Returns the item at index position i as a modifiable reference.
static void clear(QVariant::Private *d)
int size_type
Typedef for int.
const_iterator begin() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
iterator insert(iterator before, const T &x)
Inserts value in front of the item pointed to by the iterator before.
const T & first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool operator!=(QBool b1, bool b2)
#define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C)
void swap(QVector< T > &other)
Swaps vector other with this vector.
QVector< T > toVector() const
Returns a QVector object with the data contained in this QList.
void push_front(const T &t)
This function is provided for STL compatibility.
static QList< QVariant > toList(char **buf, int count, T *=0)
The QVector class is a template class that provides a dynamic array.
bool empty() const
This function is provided for STL compatibility.
bool operator<(int priority, const QPair< QRunnable *, int > &p)
iterator Iterator
Qt-style synonym for QVector::iterator.
int alignOfTypedData() const
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
bool operator<=(const QByteArray &a1, const QByteArray &a2)
void resize(int size)
Sets the size of the vector to size.
static QVector< T > fromStdVector(const std::vector< T > &vector)
Returns a QVector object with the data contained in vector.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector...
QVectorTypedData< T > Data
void pop_back()
This function is provided for STL compatibility.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the vector.
T value_type
Typedef for T.
const_reference back() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void append(const T &t)
Inserts value at the end of the list.
QVector< T > & operator+=(const QVector< T > &l)
Appends the items of the other vector to this vector and returns a reference to this vector...
#define QT_BEGIN_NAMESPACE
This macro expands to.
#define Q_DECLARE_SEQUENTIAL_ITERATOR(C)
static bool isEmpty(const char *str)
value_type * pointer
Typedef for T *.
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
QIntegerForSizeof< void * >::Signed qptrdiff
bool operator==(const QVector< T > &v) const
Returns true if other is equal to this vector; otherwise returns false.
T value(int i) const
Returns the value at index position i in the vector.
static QVectorData * malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init)
qptrdiff difference_type
Typedef for ptrdiff_t.
static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive)
const T & last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QVector< T > fromList(const QList< T > &list)
Returns a QVector object with the data contained in list.
void append(const T &t)
Inserts value at the end of the vector.
bool operator!=(const QVector< T > &v) const
Returns true if other is not equal to this vector; otherwise returns false.
static const char * data(const QByteArray &arr)
bool endsWith(const T &t) const
Returns true if this vector is not empty and its last item is equal to value; otherwise returns false...
static void free(QVectorTypedData< T > *x, int alignment)
timeval operator*(const timeval &t1, int mul)
The QRegion class specifies a clip region for a painter.
QVector< T > & operator=(const QVector< T > &v)
Assigns other to this vector and returns a reference to this vector.
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
void qSwap(T &value1, T &value2)
const T & at(int i) const
Returns the item at index position i in the vector.
T & front()
This function is provided for STL compatibility.
T * iterator
The QVector::iterator typedef provides an STL-style non-const iterator for QVector and QStack...
void insert(int i, const T &t)
Inserts value at index position i in the vector.
#define Q_ASSERT_X(cond, where, what)
void * qMemSet(void *dest, int c, size_t n)
QVector< T > operator+(const QVector< T > &l) const
Returns a vector that contains all the items in this vector followed by all the items in the other ve...
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
QVector< T > mid(int pos, int length=-1) const
Returns a vector whose elements are copied from this vector, starting at position pos...
value_type & reference
Typedef for T &.
bool operator>=(const QByteArray &a1, const QByteArray &a2)
iterator erase(iterator begin, iterator end)
Removes all the items from begin up to (but not including) end.
The QPoint class defines a point in the plane using integer precision.
T & last()
Returns a reference to the last item in the vector.
QVectorData * malloc(int alloc)
reference back()
This function is provided for STL compatibility.
void realloc(int size, int alloc)
void push_back(const T &t)
This function is provided for STL compatibility.
iterator erase(iterator pos)
Removes the item pointed to by the iterator pos from the vector, and returns an iterator to the next ...
bool contains(const T &t) const
Returns true if the vector contains an occurrence of value; otherwise returns false.
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
QDataStream & operator<<(QDataStream &s, const QAxBase &c)
const T * const_iterator
The QVector::const_iterator typedef provides an STL-style const iterator for QVector and QStack...
OutputIterator qCopy(InputIterator begin, InputIterator end, OutputIterator dest)
const_iterator ConstIterator
Qt-style synonym for QVector::const_iterator.
T * data()
Returns a pointer to the data stored in the vector.
static QList< T > fromVector(const QVector< T > &vector)
Returns a QList object with the data contained in vector.
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
const T * constData() const
Returns a const pointer to the data stored in the vector.
void reserve(int size)
Attempts to allocate memory for at least size elements.
int capacity() const
Returns the maximum number of items that can be stored in the vector without forcing a reallocation...
static int grow(int size)
timeval & operator+=(timeval &t1, const timeval &t2)
QVector(const QVector< T > &v)
Constructs a copy of other.
const_iterator end() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
#define Q_OUTOFLINE_TEMPLATE
static const KeyPair *const end
~QVector()
Destroys the vector.
void prepend(const T &t)
Inserts value at the beginning of the vector.
const value_type & const_reference
Typedef for T &.
static QVectorData shared_null
bool operator==(QBool b1, bool b2)
int size() const
Returns the number of items in the vector.
const value_type * const_pointer
Typedef for const T *.
int lastIndexOf(const T &t, int from=-1) const
Returns the index position of the last occurrence of the value value in the vector, searching backward from index position from.
void reserve(int size)
Reserve space for alloc elements.
QVector()
Constructs an empty vector.
The QList class is a template class that provides lists.
const_reference front() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
timeval operator+(const timeval &t1, const timeval &t2)