4#ifndef QVARLENGTHARRAY_H
5#define QVARLENGTHARRAY_H
8#pragma qt_class(QVarLengthArray)
9#pragma qt_sync_stop_processing
12#include <QtCore/qcontainerfwd.h>
13#include <QtCore/qglobal.h>
14#include <QtCore/qalgorithms.h>
15#include <QtCore/qcontainertools_impl.h>
16#include <QtCore/qhashfunctions.h>
19#include <initializer_list>
21#include <QtCore/q20memory.h>
29template <
size_t Size,
size_t Align, qsizetype Prealloc>
32 template <
size_t>
class print;
36 alignas(Align)
char array[Prealloc * (Align > Size ? Align : Size)];
41 static_assert(std::is_same_v<print<
sizeof(std::aligned_storage_t<Size, Align>[Prealloc])>,
42 print<
sizeof(
array)>>);
74 constexpr bool empty() const noexcept {
return size() == 0; }
84 T *
data() noexcept {
return static_cast<T *
>(
ptr); }
85 const T *
data() const noexcept {
return static_cast<T *
>(
ptr); }
146 template <
typename AT = T>
148 template <
typename AT = T>
150 template <
typename AT = T>
169 template <
typename AT = T>
171 template <
typename AT = T>
173 template <
typename Predicate>
192 size_t hash(
size_t seed)
const noexcept(QtPrivate::QNothrowHashable_v<T>)
199 template <
typename...Args>
208 template <
typename...Args>
213 template <
typename S>
218 template <
typename S>
233 while (
size() < sz) {
243 while (
size() < sz) {
253 template <
typename Iterator>
258 const std::less<const T *> less = {};
264template<
class T, qsizetype Prealloc>
267 : public
QVLAStorage<sizeof(T), alignof(T), Prealloc>,
271 public
QVLAStorage<sizeof(T), alignof(T), Prealloc>
274 template <
class S, qsizetype Prealloc2>
276 using Base = QVLABase<T>;
278 static_assert(Prealloc > 0,
"QVarLengthArray Prealloc must be greater than 0.");
279 static_assert(std::is_nothrow_destructible_v<T>,
"Types with throwing destructors are not supported in Qt containers.");
282 template <
typename U>
283 using if_copyable = std::enable_if_t<std::is_copy_constructible_v<U>,
bool>;
284 template <
typename InputIterator>
312 template <
typename U = T, if_copyable<U> = true>
327 noexcept(std::is_nothrow_move_constructible_v<T>)
330 const auto otherInlineStorage =
reinterpret_cast<T*
>(
other.array);
331 if (
data() == otherInlineStorage) {
341 other.ptr = otherInlineStorage;
349 template <
typename InputIterator, if_input_iterator<InputIterator> = true>
354 std::copy(
first,
last, std::back_inserter(*
this));
361 if (
data() !=
reinterpret_cast<T *
>(this->
array))
364 inline QVarLengthArray<T, Prealloc> &
operator=(
const QVarLengthArray<T, Prealloc> &
other)
366 if (
this != &
other) {
374 noexcept(std::is_nothrow_move_constructible_v<T>)
381 const auto otherInlineStorage =
other.array;
382 if (
other.ptr != otherInlineStorage) {
384 this->
a = std::exchange(
other.a, Prealloc);
385 this->
ptr = std::exchange(
other.ptr, otherInlineStorage);
390 this->
s = std::exchange(
other.s, 0);
430 template <
typename U = T, if_copyable<U> = true>
447 template <
typename AT = T>
449 template <
typename AT = T>
451 template <
typename AT = T>
470 using Base::operator[];
494 inline QVarLengthArray<T, Prealloc> &
operator<<(
const T &
t)
496 inline QVarLengthArray<T, Prealloc> &
operator<<(T &&
t)
497 {
append(std::move(
t));
return *
this; }
501 {
append(std::move(
t));
return *
this; }
503#if QT_DEPRECATED_SINCE(6, 3)
515 template <
typename InputIterator, if_input_iterator<InputIterator> = true>
524 template <
typename AT = T>
526 template <
typename AT = T>
528 template <
typename Predicate>
538 inline T *
data() {
return this->
ptr; }
539 inline const T *
data()
const {
return this->
ptr; }
592 inline const T &
front()
const {
return first(); }
594 inline const T &
back()
const {
return last(); }
600 template <
typename...Args>
603 template <
typename...Args>
609 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
610 friend inline bool operator==(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
611 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
612 friend inline bool operator!=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
613 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
614 friend inline bool operator< (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
615 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
616 friend inline bool operator> (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
617 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
618 friend inline bool operator<=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
619 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
620 friend inline bool operator>=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
622 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
628 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
634 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
636 noexcept(
noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
637 rhs.begin(), rhs.end())))
639 return lhs.less_than(rhs);
642 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
644 noexcept(
noexcept(lhs < rhs))
649 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
651 noexcept(
noexcept(lhs < rhs))
656 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
658 noexcept(
noexcept(lhs < rhs))
665 template <
typename U, qsizetype Prealloc2>
666 bool equal(
const QVarLengthArray<U, Prealloc2> &
other)
const
668 template <
typename U, qsizetype Prealloc2>
669 bool less_than(
const QVarLengthArray<U, Prealloc2> &
other)
const
678template <
typename InputIterator,
679 typename ValueType =
typename std::iterator_traits<InputIterator>::value_type,
683template <
class T, qsizetype Prealloc>
687 Q_ASSERT_X(asize >= 0,
"QVarLengthArray::QVarLengthArray(qsizetype)",
688 "Size must be greater than or equal to 0.");
693 if (asize > Prealloc) {
694 this->
ptr = malloc(asize *
sizeof(T));
699 std::uninitialized_default_construct_n(
data(), asize);
704template <
typename AT>
710 const T *
n =
data() + from - 1;
720template <
typename AT>
725 else if (from >=
size())
729 const T *
n =
b + from + 1;
739template <
typename AT>
766 memcpy(
static_cast<void *
>(
end()),
static_cast<const void *
>(abuf),
increment *
sizeof(T));
777 resize_impl(prealloc,
array,
n,
t);
779 auto mid = (std::min)(
n,
size());
781 std::uninitialized_fill(
data() + mid,
data() +
n,
t);
788template <
typename Iterator>
792 constexpr bool IsFwdIt =
793 std::is_convertible_v<typename std::iterator_traits<Iterator>::iterator_category,
794 std::forward_iterator_tag>;
795 if constexpr (IsFwdIt) {
798 reallocate_impl(prealloc,
array, 0,
n);
802 const auto dend =
end();
805 std::destroy(
dst, dend);
809 if constexpr (IsFwdIt) {
815 }
while (++
first != last);
841 if (aalloc > prealloc) {
842 newPtr = malloc(aalloc *
sizeof(T));
852 reinterpret_cast<T *
>(newPtr));
863 std::destroy(oldPtr + asize, oldPtr + osize);
866 if (oldPtr !=
reinterpret_cast<T *
>(
array) && oldPtr !=
data())
873 if (
size_t(
i) >=
size_t(
size()))
875 return operator[](
i);
880 return (
size_t(
i) >=
size_t(
size())) ? defaultValue : operator[](
i);
883template <
class T, qsizetype Prealloc>
886 insert(cbegin() +
i, std::move(
t)); }
887template <
class T, qsizetype Prealloc>
891template <
class T, qsizetype Prealloc>
900template <
typename AT>
904template <
typename AT>
908template <
typename Predicate>
911#if QT_DEPRECATED_SINCE(6, 3)
912template <
class T, qsizetype Prealloc>
914{
insert(cbegin(), std::move(
t)); }
915template <
class T, qsizetype Prealloc>
928template <
typename...Args>
931 Q_ASSERT_X(isValidIterator(before),
"QVarLengthArray::insert",
"The specified const_iterator argument 'before' is invalid");
936 emplace_back_impl(prealloc,
array, std::forward<Args>(
args)...);
938 const auto e =
end();
946 Q_ASSERT_X(isValidIterator(before),
"QVarLengthArray::insert",
"The specified const_iterator argument 'before' is invalid");
951 const auto e =
end();
959 Q_ASSERT_X(isValidIterator(abegin),
"QVarLengthArray::erase",
"The specified const_iterator argument 'abegin' is invalid");
960 Q_ASSERT_X(isValidIterator(aend),
"QVarLengthArray::erase",
"The specified const_iterator argument 'aend' is invalid");
973 std::destroy(
end() -
n,
end());
975 std::destroy(abegin, aend);
976 memmove(
static_cast<void *
>(
data() +
f),
static_cast<const void *
>(
data() + l), (
size() - l) *
sizeof(T));
984template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
985bool operator==(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
987template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
988bool operator!=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
990template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
991bool operator< (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
993template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
994bool operator> (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
996template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
997bool operator<=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
999template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1000bool operator>=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
1004template <
typename T, qsizetype Prealloc>
1006 noexcept(
QtPrivate::QNothrowHashable_v<T>)
1011template <
typename T, qsizetype Prealloc,
typename AT>
1014 return array.removeAll(
t);
1017template <
typename T, qsizetype Prealloc,
typename Predicate>
1020 return array.removeIf(pred);
constexpr size_type capacity() const noexcept
Q_ALWAYS_INLINE constexpr void verify(qsizetype pos=0, qsizetype n=1) const
constexpr bool empty() const noexcept
std::unique_ptr< void, free_deleter > malloced_ptr
constexpr size_type size() const noexcept
const_reverse_iterator rend() const noexcept
void remove(qsizetype i, qsizetype n=1)
void reallocate_impl(qsizetype prealloc, void *array, qsizetype size, qsizetype alloc)
const_reference operator[](qsizetype idx) const
value_type value(qsizetype i) const
const_reverse_iterator rbegin() const noexcept
std::reverse_iterator< iterator > reverse_iterator
reference emplace_back_impl(qsizetype prealloc, void *array, Args &&...args)
bool less_than(const QVLABase< S > &other) const
qsizetype removeIf(Predicate pred)
iterator erase(const_iterator pos)
const_reference back() const
static constexpr qsizetype max_size() noexcept
reverse_iterator rbegin() noexcept
const_iterator cbegin() const noexcept
qsizetype lastIndexOf(const AT &t, qsizetype from=-1) const
void resize_impl(qsizetype prealloc, void *array, qsizetype sz, const T &v)
void growBy(qsizetype prealloc, void *array, qsizetype increment)
bool removeOne(const AT &t)
bool equal(const QVLABase< S > &other) const
iterator erase(const_iterator begin, const_iterator end)
const_reference front() const
bool isValidIterator(const const_iterator &i) const
const_iterator cend() const noexcept
const value_type * const_pointer
const T * data() const noexcept
const_reverse_iterator crbegin() const noexcept
std::reverse_iterator< const_iterator > const_reverse_iterator
void resize_impl(qsizetype prealloc, void *array, qsizetype sz)
void replace(qsizetype i, const T &t)
iterator insert_impl(qsizetype prealloc, void *array, const_iterator pos, qsizetype n, const T &t)
void assign_impl(qsizetype prealloc, void *array, Iterator first, Iterator last)
reference operator[](qsizetype idx)
size_t hash(size_t seed) const noexcept(QtPrivate::QNothrowHashable_v< T >)
const_iterator end() const noexcept
qsizetype indexOf(const AT &t, qsizetype from=0) const
const_iterator begin() const noexcept
qsizetype removeAll(const AT &t)
const value_type & const_reference
void append_impl(qsizetype prealloc, void *array, const T *buf, qsizetype n)
const_reverse_iterator crend() const noexcept
bool contains(const AT &t) const
reverse_iterator rend() noexcept
void assign_impl(qsizetype prealloc, void *array, qsizetype n, const T &t)
iterator begin() noexcept
iterator emplace_impl(qsizetype prealloc, void *array, const_iterator pos, Args &&...arg)
QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
friend QTypeTraits::compare_lt_result< U > operator>(const QVarLengthArray< T, Prealloc > &lhs, const QVarLengthArray< T, Prealloc2 > &rhs) noexcept(noexcept(lhs< rhs))
QVarLengthArray & assign(InputIterator first, InputIterator last)
QVarLengthArray< T, Prealloc > & operator+=(const T &t)
iterator insert(const_iterator before, T &&x)
typename Base::pointer pointer
qsizetype removeIf(Predicate pred)
T & emplace_back(Args &&...args)
const T & at(qsizetype idx) const
void resize(qsizetype sz)
QVarLengthArray< T, Prealloc > & operator=(const QVarLengthArray< T, Prealloc > &other)
typename Base::iterator iterator
reverse_iterator rbegin() noexcept
const_iterator cbegin() const noexcept
typename Base::const_pointer const_pointer
QVarLengthArray(qsizetype sz, const T &v)
bool removeOne(const AT &t)
QVarLengthArray(const QVarLengthArray &other)
QVarLengthArray(qsizetype size)
iterator insert(const_iterator before, qsizetype n, const T &x)
QVarLengthArray(InputIterator first, InputIterator last)
const_iterator cend() const noexcept
iterator emplace(const_iterator pos, Args &&...args)
typename Base::reference reference
typename Base::size_type size_type
const_reverse_iterator crbegin() const noexcept
void replace(qsizetype i, const T &t)
void insert(qsizetype i, T &&t)
QVarLengthArray(QVarLengthArray &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
friend class QVarLengthArray
QVarLengthArray & operator=(QVarLengthArray &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
static constexpr qsizetype PreallocatedSize
friend QTypeTraits::compare_eq_result< U > operator==(const QVarLengthArray< T, Prealloc > &l, const QVarLengthArray< T, Prealloc2 > &r)
typename Base::const_iterator const_iterator
QVarLengthArray & assign(qsizetype n, const T &t)
QVarLengthArray< T, Prealloc > & operator+=(T &&t)
const_iterator constEnd() const
friend QTypeTraits::compare_eq_result< U > operator!=(const QVarLengthArray< T, Prealloc > &l, const QVarLengthArray< T, Prealloc2 > &r)
friend QTypeTraits::compare_lt_result< U > operator>=(const QVarLengthArray< T, Prealloc > &lhs, const QVarLengthArray< T, Prealloc2 > &rhs) noexcept(noexcept(lhs< rhs))
typename Base::value_type value_type
void resize(qsizetype sz, const T &v)
typename Base::const_reference const_reference
qsizetype removeAll(const AT &t)
typename Base::difference_type difference_type
void append(const T *buf, qsizetype sz)
QVarLengthArray< T, Prealloc > & operator=(std::initializer_list< T > list)
QVarLengthArray(std::initializer_list< T > args)
iterator insert(const_iterator before, const T &x)
typename Base::reverse_iterator reverse_iterator
const_reverse_iterator crend() const noexcept
void insert(qsizetype i, const T &t)
const T * constData() const
typename Base::const_reverse_iterator const_reverse_iterator
reverse_iterator rend() noexcept
void push_back(const T &t)
auto constBegin() const -> const_iterator
QVarLengthArray() noexcept
void reserve(qsizetype sz)
QVarLengthArray & assign(std::initializer_list< T > list)
void insert(qsizetype i, qsizetype n, const T &t)
list append(new Employee("Blackpool", "Stephen"))
cache insert(employee->id(), employee)
Combined button and popup list for selecting options.
std::enable_if_t< std::conjunction_v< QTypeTraits::has_operator_equal< T >... >, bool > compare_eq_result
std::enable_if_t< std::conjunction_v< QTypeTraits::has_operator_less_than< T >... >, bool > compare_lt_result
QT_WARNING_POP void q_rotate(T *first, T *mid, T *last)
void q_uninitialized_relocate_n(T *first, N n, T *out)
constexpr qsizetype MaxAllocSize
static constexpr bool q_points_into_range(const T *p, const T *b, const T *e, Cmp less={}) noexcept
auto sequential_erase_one(Container &c, const T &t)
typename std::enable_if< std::is_convertible< typename std::iterator_traits< Iterator >::iterator_category, std::input_iterator_tag >::value, bool >::type IfIsInputIterator
auto sequential_erase_if(Container &c, Predicate &pred)
auto sequential_erase_with_copy(Container &c, const T &t)
void reserveIfForwardIterator(Container *, InputIterator, InputIterator)
T * construct_at(T *ptr, Args &&... args)
qsizetype erase_if(QByteArray &ba, Predicate pred)
qsizetype erase(QByteArray &ba, const T &t)
#define QT_MAKE_CHECKED_ARRAY_ITERATOR(x, N)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
size_t qHashRange(InputIterator first, InputIterator last, size_t seed=0) noexcept(noexcept(qHash(*first)))
static ControlElement< T > * ptr(QWidget *widget)
constexpr const T & qMin(const T &a, const T &b)
constexpr const T & qMax(const T &a, const T &b)
static bool contains(const QJsonArray &haystack, unsigned needle)
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLuint GLintptr offset
bool operator>(const QPoint &a, const QPoint &b)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
#define Q_ASSERT_X(cond, x, msg)
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
#define QT_DEPRECATED_VERSION_X_6_3(text)
#define QT_VERSION_CHECK(major, minor, patch)
static bool equal(const QChar *a, int l, const char *b)
bool operator<=(const QUuid &lhs, const QUuid &rhs) noexcept
bool operator>=(const QUuid &lhs, const QUuid &rhs) noexcept
qsizetype erase(QVarLengthArray< T, Prealloc > &array, const AT &t)
QVarLengthArray(InputIterator, InputIterator) -> QVarLengthArray< ValueType >
QtConcurrent::task([]{ qDebug("Hello, world!");}).spawn(FutureResult void increment(QPromise< int > &promise, int i)
[10]
Q_CHECK_PTR(a=new int[80])
if(qFloatDistance(a, b)<(1<< 7))
[0]
settings remove("monkey")
void operator()(void *p) const noexcept