1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QVARLENGTHARRAY_H
41#define QVARLENGTHARRAY_H
42
43#include <QtCore/qcontainerfwd.h>
44#include <QtCore/qglobal.h>
45#include <QtCore/qalgorithms.h>
46#include <QtCore/qcontainertools_impl.h>
47#include <QtCore/qhashfunctions.h>
48
49#include <algorithm>
50#include <initializer_list>
51#include <iterator>
52#include <new>
53#include <string.h>
54#include <stdlib.h>
55
56QT_BEGIN_NAMESPACE
57
58
59// Prealloc = 256 by default, specified in qcontainerfwd.h
60template<class T, int Prealloc>
61class QVarLengthArray
62{
63public:
64 QVarLengthArray() : QVarLengthArray(0) {}
65
66 inline explicit QVarLengthArray(int size);
67
68 inline QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
69 : a(Prealloc), s(0), ptr(reinterpret_cast<T *>(array))
70 {
71 append(other.constData(), other.size());
72 }
73
74 QVarLengthArray(std::initializer_list<T> args)
75 : QVarLengthArray(args.begin(), args.end())
76 {
77 }
78
79 template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
80 inline QVarLengthArray(InputIterator first, InputIterator last)
81 : QVarLengthArray()
82 {
83 QtPrivate::reserveIfForwardIterator(this, first, last);
84 std::copy(first, last, std::back_inserter(*this));
85 }
86
87 inline ~QVarLengthArray() {
88 if (QTypeInfo<T>::isComplex) {
89 T *i = ptr + s;
90 while (i-- != ptr)
91 i->~T();
92 }
93 if (ptr != reinterpret_cast<T *>(array))
94 free(ptr);
95 }
96 inline QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
97 {
98 if (this != &other) {
99 clear();
100 append(other.constData(), other.size());
101 }
102 return *this;
103 }
104
105 QVarLengthArray<T, Prealloc> &operator=(std::initializer_list<T> list)
106 {
107 resize(int(list.size())); // ### q6sizetype
108 std::copy(list.begin(), list.end(),
109 QT_MAKE_CHECKED_ARRAY_ITERATOR(this->begin(), this->size()));
110 return *this;
111 }
112
113 inline void removeLast() {
114 Q_ASSERT(s > 0);
115 if (QTypeInfo<T>::isComplex)
116 ptr[s - 1].~T();
117 --s;
118 }
119 inline int size() const { return s; }
120 inline int count() const { return s; }
121 inline int length() const { return s; }
122 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
123 inline const T& first() const { Q_ASSERT(!isEmpty()); return *begin(); }
124 T& last() { Q_ASSERT(!isEmpty()); return *(end() - 1); }
125 const T& last() const { Q_ASSERT(!isEmpty()); return *(end() - 1); }
126 inline bool isEmpty() const { return (s == 0); }
127 inline void resize(int size);
128 inline void clear() { resize(0); }
129 inline void squeeze();
130
131 inline int capacity() const { return a; }
132 inline void reserve(int size);
133
134 inline int indexOf(const T &t, int from = 0) const;
135 inline int lastIndexOf(const T &t, int from = -1) const;
136 inline bool contains(const T &t) const;
137
138 inline T &operator[](int idx) {
139 Q_ASSERT(idx >= 0 && idx < s);
140 return ptr[idx];
141 }
142 inline const T &operator[](int idx) const {
143 Q_ASSERT(idx >= 0 && idx < s);
144 return ptr[idx];
145 }
146 inline const T &at(int idx) const { return operator[](idx); }
147
148 T value(int i) const;
149 T value(int i, const T &defaultValue) const;
150
151 inline void append(const T &t) {
152 if (s == a) { // i.e. s != 0
153 T copy(t);
154 realloc(s, s<<1);
155 const int idx = s++;
156 if (QTypeInfo<T>::isComplex) {
157 new (ptr + idx) T(std::move(copy));
158 } else {
159 ptr[idx] = std::move(copy);
160 }
161 } else {
162 const int idx = s++;
163 if (QTypeInfo<T>::isComplex) {
164 new (ptr + idx) T(t);
165 } else {
166 ptr[idx] = t;
167 }
168 }
169 }
170
171 void append(T &&t) {
172 if (s == a)
173 realloc(s, s << 1);
174 const int idx = s++;
175 if (QTypeInfo<T>::isComplex)
176 new (ptr + idx) T(std::move(t));
177 else
178 ptr[idx] = std::move(t);
179 }
180
181 void append(const T *buf, int size);
182 inline QVarLengthArray<T, Prealloc> &operator<<(const T &t)
183 { append(t); return *this; }
184 inline QVarLengthArray<T, Prealloc> &operator<<(T &&t)
185 { append(std::move(t)); return *this; }
186 inline QVarLengthArray<T, Prealloc> &operator+=(const T &t)
187 { append(t); return *this; }
188 inline QVarLengthArray<T, Prealloc> &operator+=(T &&t)
189 { append(std::move(t)); return *this; }
190
191 void prepend(T &&t);
192 void prepend(const T &t);
193 void insert(int i, T &&t);
194 void insert(int i, const T &t);
195 void insert(int i, int n, const T &t);
196 void replace(int i, const T &t);
197 void remove(int i);
198 void remove(int i, int n);
199
200
201 inline T *data() { return ptr; }
202 inline const T *data() const { return ptr; }
203 inline const T * constData() const { return ptr; }
204 typedef int size_type;
205 typedef T value_type;
206 typedef value_type *pointer;
207 typedef const value_type *const_pointer;
208 typedef value_type &reference;
209 typedef const value_type &const_reference;
210 typedef qptrdiff difference_type;
211
212
213 typedef T* iterator;
214 typedef const T* const_iterator;
215 typedef std::reverse_iterator<iterator> reverse_iterator;
216 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
217
218 inline iterator begin() { return ptr; }
219 inline const_iterator begin() const { return ptr; }
220 inline const_iterator cbegin() const { return ptr; }
221 inline const_iterator constBegin() const { return ptr; }
222 inline iterator end() { return ptr + s; }
223 inline const_iterator end() const { return ptr + s; }
224 inline const_iterator cend() const { return ptr + s; }
225 inline const_iterator constEnd() const { return ptr + s; }
226 reverse_iterator rbegin() { return reverse_iterator(end()); }
227 reverse_iterator rend() { return reverse_iterator(begin()); }
228 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
229 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
230 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
231 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
232 iterator insert(const_iterator before, int n, const T &x);
233 iterator insert(const_iterator before, T &&x);
234 inline iterator insert(const_iterator before, const T &x) { return insert(before, 1, x); }
235 iterator erase(const_iterator begin, const_iterator end);
236 inline iterator erase(const_iterator pos) { return erase(pos, pos+1); }
237
238 // STL compatibility:
239 inline bool empty() const { return isEmpty(); }
240 inline void push_back(const T &t) { append(t); }
241 void push_back(T &&t) { append(std::move(t)); }
242 inline void pop_back() { removeLast(); }
243 inline T &front() { return first(); }
244 inline const T &front() const { return first(); }
245 inline T &back() { return last(); }
246 inline const T &back() const { return last(); }
247 void shrink_to_fit() { squeeze(); }
248
249private:
250 void realloc(int size, int alloc);
251
252 int a; // capacity
253 int s; // size
254 T *ptr; // data
255 union {
256 char array[Prealloc * sizeof(T)];
257 qint64 q_for_alignment_1;
258 double q_for_alignment_2;
259 };
260
261 bool isValidIterator(const const_iterator &i) const
262 {
263 const std::less<const T*> less = {};
264 return !less(cend(), i) && !less(i, cbegin());
265 }
266};
267
268#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
269template <typename InputIterator,
270 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
271 QtPrivate::IfIsInputIterator<InputIterator> = true>
272QVarLengthArray(InputIterator, InputIterator) -> QVarLengthArray<ValueType>;
273#endif
274
275template <class T, int Prealloc>
276Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
277 : s(asize) {
278 Q_STATIC_ASSERT_X(Prealloc > 0, "QVarLengthArray Prealloc must be greater than 0.");
279 Q_ASSERT_X(s >= 0, "QVarLengthArray::QVarLengthArray()", "Size must be greater than or equal to 0.");
280 if (s > Prealloc) {
281 ptr = reinterpret_cast<T *>(malloc(s * sizeof(T)));
282 Q_CHECK_PTR(ptr);
283 a = s;
284 } else {
285 ptr = reinterpret_cast<T *>(array);
286 a = Prealloc;
287 }
288 if (QTypeInfo<T>::isComplex) {
289 T *i = ptr + s;
290 while (i != ptr)
291 new (--i) T;
292 }
293}
294
295template <class T, int Prealloc>
296Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
297{ realloc(asize, qMax(asize, a)); }
298
299template <class T, int Prealloc>
300Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
301{ if (asize > a) realloc(s, asize); }
302
303template <class T, int Prealloc>
304Q_INLINE_TEMPLATE int QVarLengthArray<T, Prealloc>::indexOf(const T &t, int from) const
305{
306 if (from < 0)
307 from = qMax(from + s, 0);
308 if (from < s) {
309 T *n = ptr + from - 1;
310 T *e = ptr + s;
311 while (++n != e)
312 if (*n == t)
313 return n - ptr;
314 }
315 return -1;
316}
317
318template <class T, int Prealloc>
319Q_INLINE_TEMPLATE int QVarLengthArray<T, Prealloc>::lastIndexOf(const T &t, int from) const
320{
321 if (from < 0)
322 from += s;
323 else if (from >= s)
324 from = s - 1;
325 if (from >= 0) {
326 T *b = ptr;
327 T *n = ptr + from + 1;
328 while (n != b) {
329 if (*--n == t)
330 return n - b;
331 }
332 }
333 return -1;
334}
335
336template <class T, int Prealloc>
337Q_INLINE_TEMPLATE bool QVarLengthArray<T, Prealloc>::contains(const T &t) const
338{
339 T *b = ptr;
340 T *i = ptr + s;
341 while (i != b) {
342 if (*--i == t)
343 return true;
344 }
345 return false;
346}
347
348template <class T, int Prealloc>
349Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int increment)
350{
351 Q_ASSERT(abuf);
352 if (increment <= 0)
353 return;
354
355 const int asize = s + increment;
356
357 if (asize >= a)
358 realloc(s, qMax(s*2, asize));
359
360 if (QTypeInfo<T>::isComplex) {
361 // call constructor for new objects (which can throw)
362 while (s < asize)
363 new (ptr+(s++)) T(*abuf++);
364 } else {
365 memcpy(static_cast<void *>(&ptr[s]), static_cast<const void *>(abuf), increment * sizeof(T));
366 s = asize;
367 }
368}
369
370template <class T, int Prealloc>
371Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::squeeze()
372{ realloc(s, s); }
373
374template <class T, int Prealloc>
375Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
376{
377 Q_ASSERT(aalloc >= asize);
378 T *oldPtr = ptr;
379 int osize = s;
380
381 const int copySize = qMin(asize, osize);
382 Q_ASSUME(copySize >= 0);
383 if (aalloc != a) {
384 if (aalloc > Prealloc) {
385 T* newPtr = reinterpret_cast<T *>(malloc(aalloc * sizeof(T)));
386 Q_CHECK_PTR(newPtr); // could throw
387 // by design: in case of QT_NO_EXCEPTIONS malloc must not fail or it crashes here
388 ptr = newPtr;
389 a = aalloc;
390 } else {
391 ptr = reinterpret_cast<T *>(array);
392 a = Prealloc;
393 }
394 s = 0;
395 if (!QTypeInfoQuery<T>::isRelocatable) {
396 QT_TRY {
397 // move all the old elements
398 while (s < copySize) {
399 new (ptr+s) T(std::move(*(oldPtr+s)));
400 (oldPtr+s)->~T();
401 s++;
402 }
403 } QT_CATCH(...) {
404 // clean up all the old objects and then free the old ptr
405 int sClean = s;
406 while (sClean < osize)
407 (oldPtr+(sClean++))->~T();
408 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
409 free(oldPtr);
410 QT_RETHROW;
411 }
412 } else {
413 memcpy(static_cast<void *>(ptr), static_cast<const void *>(oldPtr), copySize * sizeof(T));
414 }
415 }
416 s = copySize;
417
418 if (QTypeInfo<T>::isComplex) {
419 // destroy remaining old objects
420 while (osize > asize)
421 (oldPtr+(--osize))->~T();
422 }
423
424 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
425 free(oldPtr);
426
427 if (QTypeInfo<T>::isComplex) {
428 // call default constructor for new objects (which can throw)
429 while (s < asize)
430 new (ptr+(s++)) T;
431 } else {
432 s = asize;
433 }
434}
435
436template <class T, int Prealloc>
437Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
438{
439 if (uint(i) >= uint(size())) {
440 return T();
441 }
442 return at(i);
443}
444template <class T, int Prealloc>
445Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const
446{
447 return (uint(i) >= uint(size())) ? defaultValue : at(i);
448}
449
450template <class T, int Prealloc>
451inline void QVarLengthArray<T, Prealloc>::insert(int i, T &&t)
452{ Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range");
453 insert(cbegin() + i, std::move(t)); }
454template <class T, int Prealloc>
455inline void QVarLengthArray<T, Prealloc>::insert(int i, const T &t)
456{ Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range");
457 insert(begin() + i, 1, t); }
458template <class T, int Prealloc>
459inline void QVarLengthArray<T, Prealloc>::insert(int i, int n, const T &t)
460{ Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range");
461 insert(begin() + i, n, t); }
462template <class T, int Prealloc>
463inline void QVarLengthArray<T, Prealloc>::remove(int i, int n)
464{ Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= s, "QVarLengthArray::remove", "index out of range");
465 erase(begin() + i, begin() + i + n); }
466template <class T, int Prealloc>
467inline void QVarLengthArray<T, Prealloc>::remove(int i)
468{ Q_ASSERT_X(i >= 0 && i < s, "QVarLengthArray::remove", "index out of range");
469 erase(begin() + i, begin() + i + 1); }
470template <class T, int Prealloc>
471inline void QVarLengthArray<T, Prealloc>::prepend(T &&t)
472{ insert(cbegin(), std::move(t)); }
473template <class T, int Prealloc>
474inline void QVarLengthArray<T, Prealloc>::prepend(const T &t)
475{ insert(begin(), 1, t); }
476
477template <class T, int Prealloc>
478inline void QVarLengthArray<T, Prealloc>::replace(int i, const T &t)
479{
480 Q_ASSERT_X(i >= 0 && i < s, "QVarLengthArray::replace", "index out of range");
481 const T copy(t);
482 data()[i] = copy;
483}
484
485template <class T, int Prealloc>
486Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(const_iterator before, T &&t)
487{
488 Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid");
489
490 int offset = int(before - ptr);
491 reserve(s + 1);
492 if (!QTypeInfo<T>::isRelocatable) {
493 T *b = ptr + offset;
494 T *i = ptr + s;
495 T *j = i + 1;
496 // The new end-element needs to be constructed, the rest must be move assigned
497 if (i != b) {
498 new (--j) T(std::move(*--i));
499 while (i != b)
500 *--j = std::move(*--i);
501 *b = std::move(t);
502 } else {
503 new (b) T(std::move(t));
504 }
505 } else {
506 T *b = ptr + offset;
507 memmove(static_cast<void *>(b + 1), static_cast<const void *>(b), (s - offset) * sizeof(T));
508 new (b) T(std::move(t));
509 }
510 s += 1;
511 return ptr + offset;
512}
513
514template <class T, int Prealloc>
515Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(const_iterator before, size_type n, const T &t)
516{
517 Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid");
518
519 int offset = int(before - ptr);
520 if (n != 0) {
521 resize(s + n);
522 const T copy(t);
523 if (!QTypeInfoQuery<T>::isRelocatable) {
524 T *b = ptr + offset;
525 T *j = ptr + s;
526 T *i = j - n;
527 while (i != b)
528 *--j = *--i;
529 i = b + n;
530 while (i != b)
531 *--i = copy;
532 } else {
533 T *b = ptr + offset;
534 T *i = b + n;
535 memmove(static_cast<void *>(i), static_cast<const void *>(b), (s - offset - n) * sizeof(T));
536 while (i != b)
537 new (--i) T(copy);
538 }
539 }
540 return ptr + offset;
541}
542
543template <class T, int Prealloc>
544Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(const_iterator abegin, const_iterator aend)
545{
546 Q_ASSERT_X(isValidIterator(abegin), "QVarLengthArray::insert", "The specified const_iterator argument 'abegin' is invalid");
547 Q_ASSERT_X(isValidIterator(aend), "QVarLengthArray::insert", "The specified const_iterator argument 'aend' is invalid");
548
549 int f = int(abegin - ptr);
550 int l = int(aend - ptr);
551 int n = l - f;
552 if (QTypeInfo<T>::isComplex) {
553 std::copy(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f));
554 T *i = ptr + s;
555 T *b = ptr + s - n;
556 while (i != b) {
557 --i;
558 i->~T();
559 }
560 } else {
561 memmove(static_cast<void *>(ptr + f), static_cast<const void *>(ptr + l), (s - l) * sizeof(T));
562 }
563 s -= n;
564 return ptr + f;
565}
566
567template <typename T, int Prealloc1, int Prealloc2>
568bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
569{
570 if (l.size() != r.size())
571 return false;
572 const T *rb = r.begin();
573 const T *b = l.begin();
574 const T *e = l.end();
575 return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(rb, r.size()));
576}
577
578template <typename T, int Prealloc1, int Prealloc2>
579bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
580{
581 return !(l == r);
582}
583
584template <typename T, int Prealloc1, int Prealloc2>
585bool operator<(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
586 noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
587 rhs.begin(), rhs.end())))
588{
589 return std::lexicographical_compare(lhs.begin(), lhs.end(),
590 rhs.begin(), rhs.end());
591}
592
593template <typename T, int Prealloc1, int Prealloc2>
594inline bool operator>(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
595 noexcept(noexcept(lhs < rhs))
596{
597 return rhs < lhs;
598}
599
600template <typename T, int Prealloc1, int Prealloc2>
601inline bool operator<=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
602 noexcept(noexcept(lhs < rhs))
603{
604 return !(lhs > rhs);
605}
606
607template <typename T, int Prealloc1, int Prealloc2>
608inline bool operator>=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
609 noexcept(noexcept(lhs < rhs))
610{
611 return !(lhs < rhs);
612}
613
614template <typename T, int Prealloc>
615uint qHash(const QVarLengthArray<T, Prealloc> &key, uint seed = 0)
616 noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed)))
617{
618 return qHashRange(key.cbegin(), key.cend(), seed);
619}
620
621QT_END_NAMESPACE
622
623#endif // QVARLENGTHARRAY_H
624

source code of qtbase/src/corelib/tools/qvarlengtharray.h