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 ****************************************************************************/
42 #ifndef QVARLENGTHARRAY_H
43 #define QVARLENGTHARRAY_H
45 #include <QtCore/qcontainerfwd.h>
46 #include <QtCore/qglobal.h>
47 #include <QtCore/qalgorithms.h>
58 template<class T, int Prealloc>
61 // Prealloc = 256 by default, specified in qcontainerfwd.h
62 template<class T, int Prealloc>
66 inline explicit QVarLengthArray(int size = 0);
68 inline QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
69 : a(Prealloc), s(0), ptr(reinterpret_cast<T *>(array))
71 append(other.constData(), other.size());
74 inline ~QVarLengthArray() {
75 if (QTypeInfo<T>::isComplex) {
80 if (ptr != reinterpret_cast<T *>(array))
83 inline QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
87 append(other.constData(), other.size());
92 inline void removeLast() {
96 inline int size() const { return s; }
97 inline int count() const { return s; }
98 inline int length() const { return s; }
99 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
100 inline const T& first() const { Q_ASSERT(!isEmpty()); return *begin(); }
101 T& last() { Q_ASSERT(!isEmpty()); return *(end() - 1); }
102 const T& last() const { Q_ASSERT(!isEmpty()); return *(end() - 1); }
103 inline bool isEmpty() const { return (s == 0); }
104 inline void resize(int size);
105 inline void clear() { resize(0); }
107 inline int capacity() const { return a; }
108 inline void reserve(int size);
110 inline T &operator[](int idx) {
111 Q_ASSERT(idx >= 0 && idx < s);
114 inline const T &operator[](int idx) const {
115 Q_ASSERT(idx >= 0 && idx < s);
118 inline const T &at(int idx) const { return operator[](idx); }
120 T value(int i) const;
121 T value(int i, const T &defaultValue) const;
123 inline void append(const T &t) {
124 if (s == a) // i.e. s != 0
127 if (QTypeInfo<T>::isComplex) {
128 new (ptr + idx) T(t);
133 void append(const T *buf, int size);
134 inline QVarLengthArray<T, Prealloc> &operator<<(const T &t)
135 { append(t); return *this; }
136 inline QVarLengthArray<T, Prealloc> &operator+=(const T &t)
137 { append(t); return *this; }
139 void prepend(const T &t);
140 void insert(int i, const T &t);
141 void insert(int i, int n, const T &t);
142 void replace(int i, const T &t);
144 void remove(int i, int n);
147 inline T *data() { return ptr; }
148 inline const T *data() const { return ptr; }
149 inline const T * constData() const { return ptr; }
150 typedef int size_type;
151 typedef T value_type;
152 typedef value_type *pointer;
153 typedef const value_type *const_pointer;
154 typedef value_type &reference;
155 typedef const value_type &const_reference;
156 typedef qptrdiff difference_type;
160 typedef const T* const_iterator;
162 inline iterator begin() { return ptr; }
163 inline const_iterator begin() const { return ptr; }
164 inline const_iterator cbegin() const { return ptr; }
165 inline const_iterator constBegin() const { return ptr; }
166 inline iterator end() { return ptr + s; }
167 inline const_iterator end() const { return ptr + s; }
168 inline const_iterator cend() const { return ptr + s; }
169 inline const_iterator constEnd() const { return ptr + s; }
170 iterator insert(iterator before, int n, const T &x);
171 inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
172 iterator erase(iterator begin, iterator end);
173 inline iterator erase(iterator pos) { return erase(pos, pos+1); }
176 friend class QPodList<T, Prealloc>;
177 void realloc(int size, int alloc);
183 // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
184 char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
185 qint64 q_for_alignment_1;
186 double q_for_alignment_2;
190 template <class T, int Prealloc>
191 Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
194 ptr = reinterpret_cast<T *>(malloc(s * sizeof(T)));
198 ptr = reinterpret_cast<T *>(array);
201 if (QTypeInfo<T>::isComplex) {
208 template <class T, int Prealloc>
209 Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
210 { realloc(asize, qMax(asize, a)); }
212 template <class T, int Prealloc>
213 Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
214 { if (asize > a) realloc(s, asize); }
216 template <class T, int Prealloc>
217 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int increment)
223 const int asize = s + increment;
226 realloc(s, qMax(s*2, asize));
228 if (QTypeInfo<T>::isComplex) {
229 // call constructor for new objects (which can throw)
231 new (ptr+(s++)) T(*abuf++);
233 qMemCopy(&ptr[s], abuf, increment * sizeof(T));
238 template <class T, int Prealloc>
239 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
241 Q_ASSERT(aalloc >= asize);
245 const int copySize = qMin(asize, osize);
247 ptr = reinterpret_cast<T *>(malloc(aalloc * sizeof(T)));
253 if (QTypeInfo<T>::isStatic) {
255 // copy all the old elements
256 while (s < copySize) {
257 new (ptr+s) T(*(oldPtr+s));
262 // clean up all the old objects and then free the old ptr
264 while (sClean < osize)
265 (oldPtr+(sClean++))->~T();
266 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
271 qMemCopy(ptr, oldPtr, copySize * sizeof(T));
280 if (QTypeInfo<T>::isComplex) {
281 // destroy remaining old objects
282 while (osize > asize)
283 (oldPtr+(--osize))->~T();
286 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
289 if (QTypeInfo<T>::isComplex) {
290 // call default constructor for new objects (which can throw)
298 template <class T, int Prealloc>
299 Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
301 if (i < 0 || i >= size()) {
306 template <class T, int Prealloc>
307 Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const
309 return (i < 0 || i >= size()) ? defaultValue : at(i);
312 template <class T, int Prealloc>
313 inline void QVarLengthArray<T, Prealloc>::insert(int i, const T &t)
314 { Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range");
315 insert(begin() + i, 1, t); }
316 template <class T, int Prealloc>
317 inline void QVarLengthArray<T, Prealloc>::insert(int i, int n, const T &t)
318 { Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range");
319 insert(begin() + i, n, t); }
320 template <class T, int Prealloc>
321 inline void QVarLengthArray<T, Prealloc>::remove(int i, int n)
322 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= s, "QVarLengthArray::remove", "index out of range");
323 erase(begin() + i, begin() + i + n); }
324 template <class T, int Prealloc>
325 inline void QVarLengthArray<T, Prealloc>::remove(int i)
326 { Q_ASSERT_X(i >= 0 && i < s, "QVarLengthArray::remove", "index out of range");
327 erase(begin() + i, begin() + i + 1); }
328 template <class T, int Prealloc>
329 inline void QVarLengthArray<T, Prealloc>::prepend(const T &t)
330 { insert(begin(), 1, t); }
332 template <class T, int Prealloc>
333 inline void QVarLengthArray<T, Prealloc>::replace(int i, const T &t)
335 Q_ASSERT_X(i >= 0 && i < s, "QVarLengthArray::replace", "index out of range");
341 template <class T, int Prealloc>
342 Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(iterator before, size_type n, const T &t)
344 int offset = int(before - ptr);
348 if (QTypeInfo<T>::isStatic) {
360 memmove(i, b, (s - offset - n) * sizeof(T));
368 template <class T, int Prealloc>
369 Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(iterator abegin, iterator aend)
371 int f = int(abegin - ptr);
372 int l = int(aend - ptr);
374 if (QTypeInfo<T>::isComplex) {
375 qCopy(ptr + l, ptr + s, ptr + f);
383 memmove(ptr + f, ptr + l, (s - l) * sizeof(T));
389 template <typename T, int Prealloc1, int Prealloc2>
390 bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
392 if (l.size() != r.size())
394 for (int i = 0; i < l.size(); i++) {
395 if (l.at(i) != r.at(i))
401 template <typename T, int Prealloc1, int Prealloc2>
402 bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
411 #endif // QVARLENGTHARRAY_H