File indexing completed on 2026-01-08 10:24:21
0001
0002
0003
0004 #ifndef QVARLENGTHARRAY_H
0005 #define QVARLENGTHARRAY_H
0006
0007 #if 0
0008 #pragma qt_class(QVarLengthArray)
0009 #pragma qt_sync_stop_processing
0010 #endif
0011
0012 #include <QtCore/qalloc.h>
0013 #include <QtCore/qcompare.h>
0014 #include <QtCore/qcontainerfwd.h>
0015 #include <QtCore/qglobal.h>
0016 #include <QtCore/qalgorithms.h>
0017 #include <QtCore/qcontainertools_impl.h>
0018 #include <QtCore/qhashfunctions.h>
0019 #include <QtCore/qttypetraits.h>
0020
0021 #include <algorithm>
0022 #include <initializer_list>
0023 #include <iterator>
0024 #include <QtCore/q20memory.h>
0025 #include <new>
0026
0027 #include <string.h>
0028 #include <stdlib.h>
0029
0030 QT_BEGIN_NAMESPACE
0031
0032 template <size_t Size, size_t Align, qsizetype Prealloc>
0033 class QVLAStorage
0034 {
0035 template <size_t> class print;
0036 protected:
0037 QVLAStorage() = default;
0038 QT_DECLARE_RO5_SMF_AS_DEFAULTED(QVLAStorage)
0039
0040 alignas(Align) char array[Prealloc * (Align > Size ? Align : Size)];
0041 QT_WARNING_PUSH
0042 QT_WARNING_DISABLE_DEPRECATED
0043
0044
0045 static_assert(std::is_same_v<print<sizeof(std::aligned_storage_t<Size, Align>[Prealloc])>,
0046 print<sizeof(array)>>);
0047 QT_WARNING_POP
0048 };
0049
0050 class QVLABaseBase
0051 {
0052 protected:
0053 QVLABaseBase() = default;
0054 QT_DECLARE_RO5_SMF_AS_DEFAULTED(QVLABaseBase)
0055
0056 qsizetype a;
0057 qsizetype s;
0058 void *ptr;
0059
0060 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0061 [[maybe_unused]] qsizetype n = 1) const
0062 {
0063 Q_ASSERT(pos >= 0);
0064 Q_ASSERT(pos <= size());
0065 Q_ASSERT(n >= 0);
0066 Q_ASSERT(n <= size() - pos);
0067 }
0068
0069 struct free_deleter {
0070 void operator()(void *p) const noexcept { free(p); }
0071 };
0072 using malloced_ptr = std::unique_ptr<void, free_deleter>;
0073
0074 public:
0075 using size_type = qsizetype;
0076
0077 constexpr size_type capacity() const noexcept { return a; }
0078 constexpr size_type size() const noexcept { return s; }
0079 constexpr bool empty() const noexcept { return size() == 0; }
0080 };
0081
0082 template<class T>
0083 class QVLABase : public QVLABaseBase
0084 {
0085 protected:
0086 QVLABase() = default;
0087 QT_DECLARE_RO5_SMF_AS_DEFAULTED(QVLABase)
0088
0089 public:
0090 T *data() noexcept { return static_cast<T *>(ptr); }
0091 const T *data() const noexcept { return static_cast<T *>(ptr); }
0092
0093 using iterator = T*;
0094 using const_iterator = const T*;
0095
0096 iterator begin() noexcept { return data(); }
0097 const_iterator begin() const noexcept { return data(); }
0098 const_iterator cbegin() const noexcept { return begin(); }
0099 iterator end() noexcept { return data() + size(); }
0100 const_iterator end() const noexcept { return data() + size(); }
0101 const_iterator cend() const noexcept { return end(); }
0102
0103 using reverse_iterator = std::reverse_iterator<iterator>;
0104 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0105
0106 reverse_iterator rbegin() noexcept { return reverse_iterator{end()}; }
0107 const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator{end()}; }
0108 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0109 reverse_iterator rend() noexcept { return reverse_iterator{begin()}; }
0110 const_reverse_iterator rend() const noexcept { return const_reverse_iterator{begin()}; }
0111 const_reverse_iterator crend() const noexcept { return rend(); }
0112
0113 using value_type = T;
0114 using reference = value_type&;
0115 using const_reference = const value_type&;
0116 using pointer = value_type*;
0117 using const_pointer = const value_type*;
0118 using difference_type = qptrdiff;
0119
0120 reference front()
0121 {
0122 verify();
0123 return *begin();
0124 }
0125
0126 const_reference front() const
0127 {
0128 verify();
0129 return *begin();
0130 }
0131
0132 reference back()
0133 {
0134 verify();
0135 return *rbegin();
0136 }
0137
0138 const_reference back() const
0139 {
0140 verify();
0141 return *rbegin();
0142 }
0143
0144 void pop_back()
0145 {
0146 verify();
0147 if constexpr (QTypeInfo<T>::isComplex)
0148 data()[size() - 1].~T();
0149 --s;
0150 }
0151
0152 template <typename AT = T>
0153 qsizetype indexOf(const AT &t, qsizetype from = 0) const;
0154 template <typename AT = T>
0155 qsizetype lastIndexOf(const AT &t, qsizetype from = -1) const;
0156 template <typename AT = T>
0157 bool contains(const AT &t) const;
0158
0159 reference operator[](qsizetype idx)
0160 {
0161 verify(idx);
0162 return data()[idx];
0163 }
0164 const_reference operator[](qsizetype idx) const
0165 {
0166 verify(idx);
0167 return data()[idx];
0168 }
0169
0170 value_type value(qsizetype i) const;
0171 value_type value(qsizetype i, const T& defaultValue) const;
0172
0173 void replace(qsizetype i, const T &t);
0174 void remove(qsizetype i, qsizetype n = 1);
0175 template <typename AT = T>
0176 qsizetype removeAll(const AT &t);
0177 template <typename AT = T>
0178 bool removeOne(const AT &t);
0179 template <typename Predicate>
0180 qsizetype removeIf(Predicate pred);
0181
0182 void clear()
0183 {
0184 if constexpr (QTypeInfo<T>::isComplex)
0185 std::destroy_n(data(), size());
0186 s = 0;
0187 }
0188
0189 iterator erase(const_iterator begin, const_iterator end);
0190 iterator erase(const_iterator pos) { return erase(pos, pos + 1); }
0191
0192 static constexpr qsizetype maxSize() noexcept
0193 {
0194
0195 return (QtPrivate::MaxAllocSize / sizeof(T)) - 1;
0196 }
0197 constexpr qsizetype max_size() const noexcept
0198 {
0199 return maxSize();
0200 }
0201
0202 size_t hash(size_t seed) const noexcept(QtPrivate::QNothrowHashable_v<T>)
0203 {
0204 return qHashRange(begin(), end(), seed);
0205 }
0206 protected:
0207 void growBy(qsizetype prealloc, void *array, qsizetype increment)
0208 { reallocate_impl(prealloc, array, size(), (std::max)(size() * 2, size() + increment)); }
0209 template <typename...Args>
0210 reference emplace_back_impl(qsizetype prealloc, void *array, Args&&...args)
0211 {
0212 if (size() == capacity())
0213 growBy(prealloc, array, 1);
0214 reference r = *q20::construct_at(end(), std::forward<Args>(args)...);
0215 ++s;
0216 return r;
0217 }
0218 template <typename...Args>
0219 iterator emplace_impl(qsizetype prealloc, void *array, const_iterator pos, Args&&...arg);
0220
0221 iterator insert_impl(qsizetype prealloc, void *array, const_iterator pos, qsizetype n, const T &t);
0222
0223 template <typename S>
0224 bool equal(const QVLABase<S> &other) const
0225 {
0226 return std::equal(begin(), end(), other.begin(), other.end());
0227 }
0228 template <typename S>
0229 bool less_than(const QVLABase<S> &other) const
0230 {
0231 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
0232 }
0233
0234 void append_impl(qsizetype prealloc, void *array, const T *buf, qsizetype n);
0235 void reallocate_impl(qsizetype prealloc, void *array, qsizetype size, qsizetype alloc);
0236 void resize_impl(qsizetype prealloc, void *array, qsizetype sz, const T &v)
0237 {
0238 if (QtPrivate::q_points_into_range(&v, begin(), end())) {
0239 resize_impl(prealloc, array, sz, T(v));
0240 return;
0241 }
0242 reallocate_impl(prealloc, array, sz, qMax(sz, capacity()));
0243 while (size() < sz) {
0244 q20::construct_at(data() + size(), v);
0245 ++s;
0246 }
0247 }
0248 void resize_impl(qsizetype prealloc, void *array, qsizetype sz)
0249 {
0250 reallocate_impl(prealloc, array, sz, qMax(sz, capacity()));
0251 if constexpr (QTypeInfo<T>::isComplex) {
0252
0253 while (size() < sz) {
0254 q20::construct_at(data() + size());
0255 ++s;
0256 }
0257 } else {
0258 s = sz;
0259 }
0260 }
0261
0262 void assign_impl(qsizetype prealloc, void *array, qsizetype n, const T &t);
0263 template <typename Iterator>
0264 void assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last,
0265 std::forward_iterator_tag);
0266 template <typename Iterator>
0267 void assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last,
0268 std::input_iterator_tag);
0269 template <typename Iterator>
0270 void assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last);
0271
0272 bool isValidIterator(const const_iterator &i) const
0273 {
0274 const std::less<const T *> less = {};
0275 return !less(cend(), i) && !less(i, cbegin());
0276 }
0277 };
0278
0279
0280 template<class T, qsizetype Prealloc>
0281 class QVarLengthArray
0282 #if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || defined(QT_BOOTSTRAPPED)
0283 : public QVLAStorage<sizeof(T), alignof(T), Prealloc>,
0284 public QVLABase<T>
0285 #else
0286 : public QVLABase<T>,
0287 public QVLAStorage<sizeof(T), alignof(T), Prealloc>
0288 #endif
0289 {
0290 template <class S, qsizetype Prealloc2>
0291 friend class QVarLengthArray;
0292 using Base = QVLABase<T>;
0293 using Storage = QVLAStorage<sizeof(T), alignof(T), Prealloc>;
0294 static_assert(Prealloc > 0, "QVarLengthArray Prealloc must be greater than 0.");
0295 static_assert(std::is_nothrow_destructible_v<T>, "Types with throwing destructors are not supported in Qt containers.");
0296 using Base::verify;
0297
0298 template <typename U>
0299 using if_copyable = std::enable_if_t<std::is_copy_constructible_v<U>, bool>;
0300 template <typename InputIterator>
0301 using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
0302 public:
0303 static constexpr qsizetype PreallocatedSize = Prealloc;
0304
0305 using size_type = typename Base::size_type;
0306 using value_type = typename Base::value_type;
0307 using pointer = typename Base::pointer;
0308 using const_pointer = typename Base::const_pointer;
0309 using reference = typename Base::reference;
0310 using const_reference = typename Base::const_reference;
0311 using difference_type = typename Base::difference_type;
0312
0313 using iterator = typename Base::iterator;
0314 using const_iterator = typename Base::const_iterator;
0315 using reverse_iterator = typename Base::reverse_iterator;
0316 using const_reverse_iterator = typename Base::const_reverse_iterator;
0317
0318 QVarLengthArray() noexcept
0319 {
0320 this->a = Prealloc;
0321 this->s = 0;
0322 this->ptr = this->array;
0323 }
0324
0325 inline explicit QVarLengthArray(qsizetype size);
0326
0327 #ifndef Q_QDOC
0328 template <typename U = T, if_copyable<U> = true>
0329 #endif
0330 explicit QVarLengthArray(qsizetype sz, const T &v)
0331 : QVarLengthArray{}
0332 {
0333 resize(sz, v);
0334 }
0335
0336 QVarLengthArray(const QVarLengthArray &other)
0337 : QVarLengthArray{}
0338 {
0339 append(other.constData(), other.size());
0340 }
0341
0342 QVarLengthArray(QVarLengthArray &&other)
0343 noexcept(std::is_nothrow_move_constructible_v<T>)
0344 : Base(other)
0345 {
0346 const auto otherInlineStorage = reinterpret_cast<T*>(other.array);
0347 if (data() == otherInlineStorage) {
0348
0349 this->ptr = this->array;
0350 QtPrivate::q_uninitialized_relocate_n(otherInlineStorage, size(), data());
0351 } else {
0352
0353 }
0354
0355 other.a = Prealloc;
0356 other.s = 0;
0357 other.ptr = otherInlineStorage;
0358 }
0359
0360 QVarLengthArray(std::initializer_list<T> args)
0361 : QVarLengthArray(args.begin(), args.end())
0362 {
0363 }
0364
0365 template <typename InputIterator, if_input_iterator<InputIterator> = true>
0366 inline QVarLengthArray(InputIterator first, InputIterator last)
0367 : QVarLengthArray()
0368 {
0369 assign(first, last);
0370 }
0371
0372 inline ~QVarLengthArray()
0373 {
0374 if constexpr (QTypeInfo<T>::isComplex)
0375 std::destroy_n(data(), size());
0376 if (data() != reinterpret_cast<T *>(this->array))
0377 QtPrivate::sizedFree(data(), capacity(), sizeof(T));
0378 }
0379 inline QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
0380 {
0381 if (this != &other) {
0382 clear();
0383 append(other.constData(), other.size());
0384 }
0385 return *this;
0386 }
0387
0388 QVarLengthArray &operator=(QVarLengthArray &&other)
0389 noexcept(std::is_nothrow_move_constructible_v<T>)
0390 {
0391
0392
0393
0394 clear();
0395 Q_ASSERT(capacity() >= Prealloc);
0396 const auto otherInlineStorage = other.array;
0397 if (other.ptr != otherInlineStorage) {
0398
0399 this->a = std::exchange(other.a, Prealloc);
0400 this->ptr = std::exchange(other.ptr, otherInlineStorage);
0401 } else {
0402
0403 QtPrivate::q_uninitialized_relocate_n(other.data(), other.size(), data());
0404 }
0405 this->s = std::exchange(other.s, 0);
0406 return *this;
0407 }
0408
0409 QVarLengthArray<T, Prealloc> &operator=(std::initializer_list<T> list)
0410 {
0411 assign(list);
0412 return *this;
0413 }
0414
0415 inline void removeLast()
0416 {
0417 Base::pop_back();
0418 }
0419 #ifdef Q_QDOC
0420 inline qsizetype size() const { return this->s; }
0421 static constexpr qsizetype maxSize() noexcept { return QVLABase<T>::maxSize(); }
0422 constexpr qsizetype max_size() const noexcept { return QVLABase<T>::max_size(); }
0423 #endif
0424 using Base::size;
0425 using Base::max_size;
0426 inline qsizetype count() const { return size(); }
0427 inline qsizetype length() const { return size(); }
0428 inline T &first()
0429 {
0430 return front();
0431 }
0432 inline const T &first() const
0433 {
0434 return front();
0435 }
0436 T &last()
0437 {
0438 return back();
0439 }
0440 const T &last() const
0441 {
0442 return back();
0443 }
0444 bool isEmpty() const { return empty(); }
0445 void resize(qsizetype sz) { Base::resize_impl(Prealloc, this->array, sz); }
0446 #ifndef Q_QDOC
0447 template <typename U = T, if_copyable<U> = true>
0448 #endif
0449 void resize(qsizetype sz, const T &v)
0450 { Base::resize_impl(Prealloc, this->array, sz, v); }
0451 using Base::clear;
0452 #ifdef Q_QDOC
0453 inline void clear() { resize(0); }
0454 #endif
0455 void squeeze() { reallocate(size(), size()); }
0456
0457 using Base::capacity;
0458 #ifdef Q_QDOC
0459 qsizetype capacity() const { return this->a; }
0460 #endif
0461 void reserve(qsizetype sz) { if (sz > capacity()) reallocate(size(), sz); }
0462
0463 #ifdef Q_QDOC
0464 template <typename AT = T>
0465 inline qsizetype indexOf(const AT &t, qsizetype from = 0) const;
0466 template <typename AT = T>
0467 inline qsizetype lastIndexOf(const AT &t, qsizetype from = -1) const;
0468 template <typename AT = T>
0469 inline bool contains(const AT &t) const;
0470 #endif
0471 using Base::indexOf;
0472 using Base::lastIndexOf;
0473 using Base::contains;
0474
0475 #ifdef Q_QDOC
0476 inline T &operator[](qsizetype idx)
0477 {
0478 verify(idx);
0479 return data()[idx];
0480 }
0481 inline const T &operator[](qsizetype idx) const
0482 {
0483 verify(idx);
0484 return data()[idx];
0485 }
0486 #endif
0487 using Base::operator[];
0488 inline const T &at(qsizetype idx) const { return operator[](idx); }
0489
0490 #ifdef Q_QDOC
0491 T value(qsizetype i) const;
0492 T value(qsizetype i, const T &defaultValue) const;
0493 #endif
0494 using Base::value;
0495
0496 inline void append(const T &t)
0497 {
0498 if (size() == capacity())
0499 emplace_back(T(t));
0500 else
0501 emplace_back(t);
0502 }
0503
0504 void append(T &&t)
0505 {
0506 emplace_back(std::move(t));
0507 }
0508
0509 void append(const T *buf, qsizetype sz)
0510 { Base::append_impl(Prealloc, this->array, buf, sz); }
0511 inline QVarLengthArray<T, Prealloc> &operator<<(const T &t)
0512 { append(t); return *this; }
0513 inline QVarLengthArray<T, Prealloc> &operator<<(T &&t)
0514 { append(std::move(t)); return *this; }
0515 inline QVarLengthArray<T, Prealloc> &operator+=(const T &t)
0516 { append(t); return *this; }
0517 inline QVarLengthArray<T, Prealloc> &operator+=(T &&t)
0518 { append(std::move(t)); return *this; }
0519
0520 #if QT_DEPRECATED_SINCE(6, 3)
0521 QT_DEPRECATED_VERSION_X_6_3("This is slow. If you must, use insert(cbegin(), ~~~) instead.")
0522 void prepend(T &&t);
0523 QT_DEPRECATED_VERSION_X_6_3("This is slow. If you must, use insert(cbegin(), ~~~) instead.")
0524 void prepend(const T &t);
0525 #endif
0526 void insert(qsizetype i, T &&t);
0527 void insert(qsizetype i, const T &t);
0528 void insert(qsizetype i, qsizetype n, const T &t);
0529
0530 QVarLengthArray &assign(qsizetype n, const T &t)
0531 { Base::assign_impl(Prealloc, this->array, n, t); return *this; }
0532 template <typename InputIterator, if_input_iterator<InputIterator> = true>
0533 QVarLengthArray &assign(InputIterator first, InputIterator last)
0534 { Base::assign_impl(Prealloc, this->array, first, last); return *this; }
0535 QVarLengthArray &assign(std::initializer_list<T> list)
0536 { assign(list.begin(), list.end()); return *this; }
0537
0538 #ifdef Q_QDOC
0539 void replace(qsizetype i, const T &t);
0540 void remove(qsizetype i, qsizetype n = 1);
0541 template <typename AT = T>
0542 qsizetype removeAll(const AT &t);
0543 template <typename AT = T>
0544 bool removeOne(const AT &t);
0545 template <typename Predicate>
0546 qsizetype removeIf(Predicate pred);
0547 #endif
0548 using Base::replace;
0549 using Base::remove;
0550 using Base::removeAll;
0551 using Base::removeOne;
0552 using Base::removeIf;
0553
0554 #ifdef Q_QDOC
0555 inline T *data() { return this->ptr; }
0556 inline const T *data() const { return this->ptr; }
0557 #endif
0558 using Base::data;
0559 inline const T *constData() const { return data(); }
0560 #ifdef Q_QDOC
0561 inline iterator begin() { return data(); }
0562 inline const_iterator begin() const { return data(); }
0563 inline const_iterator cbegin() const { return begin(); }
0564 inline const_iterator constBegin() const { return begin(); }
0565 inline iterator end() { return data() + size(); }
0566 inline const_iterator end() const { return data() + size(); }
0567 inline const_iterator cend() const { return end(); }
0568 #endif
0569
0570 using Base::begin;
0571 using Base::cbegin;
0572 auto constBegin() const -> const_iterator { return begin(); }
0573 using Base::end;
0574 using Base::cend;
0575 inline const_iterator constEnd() const { return end(); }
0576 #ifdef Q_QDOC
0577 reverse_iterator rbegin() { return reverse_iterator(end()); }
0578 reverse_iterator rend() { return reverse_iterator(begin()); }
0579 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
0580 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
0581 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
0582 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
0583 #endif
0584 using Base::rbegin;
0585 using Base::crbegin;
0586 using Base::rend;
0587 using Base::crend;
0588
0589 iterator insert(const_iterator before, qsizetype n, const T &x)
0590 { return Base::insert_impl(Prealloc, this->array, before, n, x); }
0591 iterator insert(const_iterator before, T &&x) { return emplace(before, std::move(x)); }
0592 inline iterator insert(const_iterator before, const T &x) { return insert(before, 1, x); }
0593 #ifdef Q_QDOC
0594 iterator erase(const_iterator begin, const_iterator end);
0595 inline iterator erase(const_iterator pos) { return erase(pos, pos + 1); }
0596 #endif
0597 using Base::erase;
0598
0599
0600 #ifdef Q_QDOC
0601 inline bool empty() const { return isEmpty(); }
0602 #endif
0603 using Base::empty;
0604 inline void push_back(const T &t) { append(t); }
0605 void push_back(T &&t) { append(std::move(t)); }
0606 #ifdef Q_QDOC
0607 inline void pop_back() { removeLast(); }
0608 inline T &front() { return first(); }
0609 inline const T &front() const { return first(); }
0610 inline T &back() { return last(); }
0611 inline const T &back() const { return last(); }
0612 #endif
0613 using Base::pop_back;
0614 using Base::front;
0615 using Base::back;
0616 void shrink_to_fit() { squeeze(); }
0617 template <typename...Args>
0618 iterator emplace(const_iterator pos, Args &&...args)
0619 { return Base::emplace_impl(Prealloc, this->array, pos, std::forward<Args>(args)...); }
0620 template <typename...Args>
0621 T &emplace_back(Args &&...args)
0622 { return Base::emplace_back_impl(Prealloc, this->array, std::forward<Args>(args)...); }
0623
0624
0625 #ifdef Q_QDOC
0626 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0627 friend inline bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0628 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0629 friend inline bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0630 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0631 friend inline bool operator< (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0632 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0633 friend inline bool operator> (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0634 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0635 friend inline bool operator<=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0636 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0637 friend inline bool operator>=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0638 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
0639 friend inline auto operator<=>(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
0640 #else
0641 private:
0642 template <typename U = T, qsizetype Prealloc2 = Prealloc,
0643 Qt::if_has_qt_compare_three_way<U, U> = true>
0644 friend auto
0645 compareThreeWay(const QVarLengthArray &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
0646 {
0647 return QtOrderingPrivate::lexicographicalCompareThreeWay(lhs.begin(), lhs.end(),
0648 rhs.begin(), rhs.end());
0649 }
0650
0651 #if defined(__cpp_lib_three_way_comparison) && defined(__cpp_lib_concepts)
0652 template <typename U = T, qsizetype Prealloc2 = Prealloc,
0653 QtOrderingPrivate::if_has_op_less_or_op_compare_three_way<QVarLengthArray, U> = true>
0654 friend auto
0655 operator<=>(const QVarLengthArray &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
0656 {
0657 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
0658 rhs.begin(), rhs.end(),
0659 QtOrderingPrivate::synthThreeWay);
0660 }
0661 #endif
0662
0663 public:
0664 template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
0665 QTypeTraits::compare_eq_result<U> operator==(const QVarLengthArray<T, Prealloc> &l, const QVarLengthArray<T, Prealloc2> &r)
0666 {
0667 return l.equal(r);
0668 }
0669
0670 template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
0671 QTypeTraits::compare_eq_result<U> operator!=(const QVarLengthArray<T, Prealloc> &l, const QVarLengthArray<T, Prealloc2> &r)
0672 {
0673 return !(l == r);
0674 }
0675
0676 #ifndef __cpp_lib_three_way_comparison
0677 template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
0678 QTypeTraits::compare_lt_result<U> operator<(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
0679 noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
0680 rhs.begin(), rhs.end())))
0681 {
0682 return lhs.less_than(rhs);
0683 }
0684
0685 template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
0686 QTypeTraits::compare_lt_result<U> operator>(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
0687 noexcept(noexcept(lhs < rhs))
0688 {
0689 return rhs < lhs;
0690 }
0691
0692 template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
0693 QTypeTraits::compare_lt_result<U> operator<=(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
0694 noexcept(noexcept(lhs < rhs))
0695 {
0696 return !(lhs > rhs);
0697 }
0698
0699 template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
0700 QTypeTraits::compare_lt_result<U> operator>=(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
0701 noexcept(noexcept(lhs < rhs))
0702 {
0703 return !(lhs < rhs);
0704 }
0705 #endif
0706 #endif
0707
0708 private:
0709 template <typename U, qsizetype Prealloc2>
0710 bool equal(const QVarLengthArray<U, Prealloc2> &other) const
0711 { return Base::equal(other); }
0712 template <typename U, qsizetype Prealloc2>
0713 bool less_than(const QVarLengthArray<U, Prealloc2> &other) const
0714 { return Base::less_than(other); }
0715
0716 void reallocate(qsizetype sz, qsizetype alloc)
0717 { Base::reallocate_impl(Prealloc, this->array, sz, alloc); }
0718
0719 using Base::isValidIterator;
0720 };
0721
0722 template <typename InputIterator,
0723 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
0724 QtPrivate::IfIsInputIterator<InputIterator> = true>
0725 QVarLengthArray(InputIterator, InputIterator) -> QVarLengthArray<ValueType>;
0726
0727 template <class T, qsizetype Prealloc>
0728 Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(qsizetype asize)
0729 : QVarLengthArray()
0730 {
0731 Q_ASSERT_X(asize >= 0, "QVarLengthArray::QVarLengthArray(qsizetype)",
0732 "Size must be greater than or equal to 0.");
0733
0734
0735
0736
0737 if (asize > Prealloc) {
0738 this->a = asize;
0739 this->ptr = QtPrivate::fittedMalloc(0, &this->a, sizeof(T));
0740 Q_CHECK_PTR(this->ptr);
0741 }
0742 if constexpr (QTypeInfo<T>::isComplex)
0743 std::uninitialized_default_construct_n(data(), asize);
0744 this->s = asize;
0745 }
0746
0747 template <class T>
0748 template <typename AT>
0749 Q_INLINE_TEMPLATE qsizetype QVLABase<T>::indexOf(const AT &t, qsizetype from) const
0750 {
0751 if (from < 0)
0752 from = qMax(from + size(), qsizetype(0));
0753 if (from < size()) {
0754 const T *n = data() + from - 1;
0755 const T *e = end();
0756 while (++n != e)
0757 if (*n == t)
0758 return n - data();
0759 }
0760 return -1;
0761 }
0762
0763 template <class T>
0764 template <typename AT>
0765 Q_INLINE_TEMPLATE qsizetype QVLABase<T>::lastIndexOf(const AT &t, qsizetype from) const
0766 {
0767 if (from < 0)
0768 from += size();
0769 else if (from >= size())
0770 from = size() - 1;
0771 if (from >= 0) {
0772 const T *b = begin();
0773 const T *n = b + from + 1;
0774 while (n != b) {
0775 if (*--n == t)
0776 return n - b;
0777 }
0778 }
0779 return -1;
0780 }
0781
0782 template <class T>
0783 template <typename AT>
0784 Q_INLINE_TEMPLATE bool QVLABase<T>::contains(const AT &t) const
0785 {
0786 const T *b = begin();
0787 const T *i = end();
0788 while (i != b) {
0789 if (*--i == t)
0790 return true;
0791 }
0792 return false;
0793 }
0794
0795 template <class T>
0796 Q_OUTOFLINE_TEMPLATE void QVLABase<T>::append_impl(qsizetype prealloc, void *array, const T *abuf, qsizetype increment)
0797 {
0798 Q_ASSERT(abuf || increment == 0);
0799 if (increment <= 0)
0800 return;
0801
0802 const qsizetype asize = size() + increment;
0803
0804 if (asize >= capacity())
0805 growBy(prealloc, array, increment);
0806
0807 if constexpr (QTypeInfo<T>::isComplex)
0808 std::uninitialized_copy_n(abuf, increment, end());
0809 else
0810 memcpy(static_cast<void *>(end()), static_cast<const void *>(abuf), increment * sizeof(T));
0811
0812 this->s = asize;
0813 }
0814
0815 template <class T>
0816 Q_OUTOFLINE_TEMPLATE void QVLABase<T>::assign_impl(qsizetype prealloc, void *array, qsizetype n, const T &t)
0817 {
0818 Q_ASSERT(n >= 0);
0819 if (n > capacity()) {
0820 reallocate_impl(prealloc, array, 0, capacity());
0821 resize_impl(prealloc, array, n, t);
0822 } else {
0823 auto mid = (std::min)(n, size());
0824 std::fill(data(), data() + mid, t);
0825 std::uninitialized_fill(data() + mid, data() + n, t);
0826 s = n;
0827 erase(data() + n, data() + size());
0828 }
0829 }
0830
0831 template <class T>
0832 template <typename Iterator>
0833 Q_OUTOFLINE_TEMPLATE
0834 void QVLABase<T>::assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last,
0835 std::forward_iterator_tag)
0836 {
0837
0838 const qsizetype n = std::distance(first, last);
0839 if (n > capacity())
0840 reallocate_impl(prealloc, array, 0, n);
0841
0842 auto dst = begin();
0843
0844 if constexpr (!QTypeInfo<T>::isComplex) {
0845
0846
0847
0848
0849
0850 dst = std::copy(first, last, dst);
0851 } else if (n > this->s) {
0852
0853 for (qsizetype i = 0; i < this->s; ++i) {
0854 *dst = *first;
0855 ++first;
0856 ++dst;
0857 }
0858 std::uninitialized_copy_n(first, n - this->s, dst);
0859 } else {
0860
0861 dst = std::copy(first, last, dst);
0862 std::destroy(dst, end());
0863 }
0864 this->s = n;
0865 }
0866
0867 template <class T>
0868 template <typename Iterator>
0869 Q_OUTOFLINE_TEMPLATE
0870 void QVLABase<T>::assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last,
0871 std::input_iterator_tag)
0872 {
0873
0874 auto dst = begin();
0875 const auto dend = end();
0876 while (true) {
0877 if (first == last) {
0878 std::destroy(dst, dend);
0879 break;
0880 }
0881 if (dst == dend) {
0882 do {
0883 emplace_back_impl(prealloc, array, *first);
0884 } while (++first != last);
0885 return;
0886 }
0887 *dst = *first;
0888 ++dst;
0889 ++first;
0890 }
0891 this->s = dst - begin();
0892 }
0893
0894 template <class T>
0895 template <typename Iterator>
0896 Q_OUTOFLINE_TEMPLATE
0897 void QVLABase<T>::assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last)
0898 {
0899 using Cat = typename std::iterator_traits<Iterator>::iterator_category;
0900 assign_impl(prealloc, array, first, last, Cat{});
0901 }
0902
0903 template <class T>
0904 Q_OUTOFLINE_TEMPLATE void QVLABase<T>::reallocate_impl(qsizetype prealloc, void *array, qsizetype asize, qsizetype aalloc)
0905 {
0906 Q_ASSERT(aalloc >= asize);
0907 Q_ASSERT(data());
0908 T *oldPtr = data();
0909 qsizetype osize = size();
0910 const qsizetype oalloc = capacity();
0911
0912 const qsizetype copySize = qMin(asize, osize);
0913 Q_ASSERT(copySize >= 0);
0914
0915 if (aalloc != oalloc) {
0916 QVLABaseBase::malloced_ptr guard;
0917 void *newPtr;
0918 qsizetype newA;
0919 if (aalloc > prealloc) {
0920 newPtr = QtPrivate::fittedMalloc(0, &aalloc, sizeof(T));
0921 guard.reset(newPtr);
0922 Q_CHECK_PTR(newPtr);
0923
0924 newA = aalloc;
0925 } else {
0926 newPtr = array;
0927 newA = prealloc;
0928 }
0929 QtPrivate::q_uninitialized_relocate_n(oldPtr, copySize,
0930 reinterpret_cast<T *>(newPtr));
0931
0932 ptr = newPtr;
0933 guard.release();
0934 a = newA;
0935 }
0936 s = copySize;
0937
0938
0939 if constexpr (QTypeInfo<T>::isComplex) {
0940 if (osize > asize)
0941 std::destroy(oldPtr + asize, oldPtr + osize);
0942 }
0943
0944 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != data())
0945 QtPrivate::sizedFree(oldPtr, oalloc, sizeof(T));
0946 }
0947
0948 template <class T>
0949 Q_OUTOFLINE_TEMPLATE T QVLABase<T>::value(qsizetype i) const
0950 {
0951 if (size_t(i) >= size_t(size()))
0952 return T();
0953 return operator[](i);
0954 }
0955 template <class T>
0956 Q_OUTOFLINE_TEMPLATE T QVLABase<T>::value(qsizetype i, const T &defaultValue) const
0957 {
0958 return (size_t(i) >= size_t(size())) ? defaultValue : operator[](i);
0959 }
0960
0961 template <class T, qsizetype Prealloc>
0962 inline void QVarLengthArray<T, Prealloc>::insert(qsizetype i, T &&t)
0963 { verify(i, 0);
0964 insert(cbegin() + i, std::move(t)); }
0965 template <class T, qsizetype Prealloc>
0966 inline void QVarLengthArray<T, Prealloc>::insert(qsizetype i, const T &t)
0967 { verify(i, 0);
0968 insert(begin() + i, 1, t); }
0969 template <class T, qsizetype Prealloc>
0970 inline void QVarLengthArray<T, Prealloc>::insert(qsizetype i, qsizetype n, const T &t)
0971 { verify(i, 0);
0972 insert(begin() + i, n, t); }
0973 template <class T>
0974 inline void QVLABase<T>::remove(qsizetype i, qsizetype n)
0975 { verify(i, n);
0976 erase(begin() + i, begin() + i + n); }
0977 template <class T>
0978 template <typename AT>
0979 inline qsizetype QVLABase<T>::removeAll(const AT &t)
0980 { return QtPrivate::sequential_erase_with_copy(*this, t); }
0981 template <class T>
0982 template <typename AT>
0983 inline bool QVLABase<T>::removeOne(const AT &t)
0984 { return QtPrivate::sequential_erase_one(*this, t); }
0985 template <class T>
0986 template <typename Predicate>
0987 inline qsizetype QVLABase<T>::removeIf(Predicate pred)
0988 { return QtPrivate::sequential_erase_if(*this, pred); }
0989 #if QT_DEPRECATED_SINCE(6, 3)
0990 template <class T, qsizetype Prealloc>
0991 inline void QVarLengthArray<T, Prealloc>::prepend(T &&t)
0992 { insert(cbegin(), std::move(t)); }
0993 template <class T, qsizetype Prealloc>
0994 inline void QVarLengthArray<T, Prealloc>::prepend(const T &t)
0995 { insert(begin(), 1, t); }
0996 #endif
0997
0998 template <class T>
0999 inline void QVLABase<T>::replace(qsizetype i, const T &t)
1000 {
1001 verify(i);
1002 data()[i] = t;
1003 }
1004
1005 template <class T>
1006 template <typename...Args>
1007 Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::emplace_impl(qsizetype prealloc, void *array, const_iterator before, Args &&...args) -> iterator
1008 {
1009 Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid");
1010 Q_ASSERT(size() <= capacity());
1011 Q_ASSERT(capacity() > 0);
1012
1013 const qsizetype offset = qsizetype(before - cbegin());
1014 emplace_back_impl(prealloc, array, std::forward<Args>(args)...);
1015 const auto b = begin() + offset;
1016 const auto e = end();
1017 QtPrivate::q_rotate(b, e - 1, e);
1018 return b;
1019 }
1020
1021 template <class T>
1022 Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::insert_impl(qsizetype prealloc, void *array, const_iterator before, qsizetype n, const T &t) -> iterator
1023 {
1024 Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid");
1025
1026 const qsizetype offset = qsizetype(before - cbegin());
1027 resize_impl(prealloc, array, size() + n, t);
1028 const auto b = begin() + offset;
1029 const auto e = end();
1030 QtPrivate::q_rotate(b, e - n, e);
1031 return b;
1032 }
1033
1034 template <class T>
1035 Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::erase(const_iterator abegin, const_iterator aend) -> iterator
1036 {
1037 Q_ASSERT_X(isValidIterator(abegin), "QVarLengthArray::erase", "The specified const_iterator argument 'abegin' is invalid");
1038 Q_ASSERT_X(isValidIterator(aend), "QVarLengthArray::erase", "The specified const_iterator argument 'aend' is invalid");
1039
1040 qsizetype f = qsizetype(abegin - cbegin());
1041 qsizetype l = qsizetype(aend - cbegin());
1042 qsizetype n = l - f;
1043
1044 if (n == 0)
1045 return data() + f;
1046
1047 Q_ASSERT(n > 0);
1048
1049 if constexpr (!QTypeInfo<T>::isRelocatable) {
1050 std::move(begin() + l, end(), QT_MAKE_CHECKED_ARRAY_ITERATOR(begin() + f, size() - f));
1051 std::destroy(end() - n, end());
1052 } else {
1053 std::destroy(abegin, aend);
1054 memmove(static_cast<void *>(data() + f), static_cast<const void *>(data() + l), (size() - l) * sizeof(T));
1055 }
1056 this->s -= n;
1057 return data() + f;
1058 }
1059
1060 #ifdef Q_QDOC
1061
1062 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1063 bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
1064 { return bool{}; }
1065 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1066 bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
1067 { return bool{}; }
1068 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1069 bool operator< (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
1070 { return bool{}; }
1071 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1072 bool operator> (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
1073 { return bool{}; }
1074 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1075 bool operator<=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
1076 { return bool{}; }
1077 template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1078 bool operator>=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
1079 { return bool{}; }
1080 #endif
1081
1082 template <typename T, qsizetype Prealloc>
1083 size_t qHash(const QVarLengthArray<T, Prealloc> &key, size_t seed = 0)
1084 noexcept(QtPrivate::QNothrowHashable_v<T>)
1085 {
1086 return key.hash(seed);
1087 }
1088
1089 template <typename T, qsizetype Prealloc, typename AT>
1090 qsizetype erase(QVarLengthArray<T, Prealloc> &array, const AT &t)
1091 {
1092 return array.removeAll(t);
1093 }
1094
1095 template <typename T, qsizetype Prealloc, typename Predicate>
1096 qsizetype erase_if(QVarLengthArray<T, Prealloc> &array, Predicate pred)
1097 {
1098 return array.removeIf(pred);
1099 }
1100
1101 QT_END_NAMESPACE
1102
1103 #endif