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>
17#include <QtCore/qttypetraits.h>
20#include <initializer_list>
22#include <QtCore/q20memory.h>
30template <
size_t Size,
size_t Align, qsizetype Prealloc>
33 template <
size_t>
class print;
37 alignas(Align)
char array[Prealloc * (Align > Size ? Align : Size)];
42 static_assert(std::is_same_v<print<
sizeof(std::aligned_storage_t<Size, Align>[Prealloc])>,
43 print<
sizeof(
array)>>);
75 constexpr bool empty() const noexcept {
return size() == 0; }
85 T *
data() noexcept {
return static_cast<T *
>(
ptr); }
86 const T *
data() const noexcept {
return static_cast<T *
>(
ptr); }
147 template <
typename AT = T>
149 template <
typename AT = T>
151 template <
typename AT = T>
170 template <
typename AT = T>
172 template <
typename AT = T>
174 template <
typename Predicate>
193 size_t hash(
size_t seed)
const noexcept(QtPrivate::QNothrowHashable_v<T>)
200 template <
typename...Args>
209 template <
typename...Args>
214 template <
typename S>
219 template <
typename S>
234 while (
size() < sz) {
244 while (
size() < sz) {
254 template <
typename Iterator>
259 const std::less<const T *> less = {};
265template<
class T, qsizetype Prealloc>
268 : public
QVLAStorage<sizeof(T), alignof(T), Prealloc>,
272 public
QVLAStorage<sizeof(T), alignof(T), Prealloc>
275 template <
class S, qsizetype Prealloc2>
277 using Base = QVLABase<T>;
279 static_assert(Prealloc > 0,
"QVarLengthArray Prealloc must be greater than 0.");
280 static_assert(std::is_nothrow_destructible_v<T>,
"Types with throwing destructors are not supported in Qt containers.");
283 template <
typename U>
284 using if_copyable = std::enable_if_t<std::is_copy_constructible_v<U>,
bool>;
285 template <
typename InputIterator>
313 template <
typename U = T, if_copyable<U> = true>
328 noexcept(std::is_nothrow_move_constructible_v<T>)
331 const auto otherInlineStorage =
reinterpret_cast<T*
>(
other.array);
332 if (
data() == otherInlineStorage) {
342 other.ptr = otherInlineStorage;
350 template <
typename InputIterator, if_input_iterator<InputIterator> = true>
355 std::copy(
first,
last, std::back_inserter(*
this));
362 if (
data() !=
reinterpret_cast<T *
>(this->
array))
365 inline QVarLengthArray<T, Prealloc> &
operator=(
const QVarLengthArray<T, Prealloc> &
other)
367 if (
this != &
other) {
375 noexcept(std::is_nothrow_move_constructible_v<T>)
382 const auto otherInlineStorage =
other.array;
383 if (
other.ptr != otherInlineStorage) {
385 this->
a = std::exchange(
other.a, Prealloc);
386 this->
ptr = std::exchange(
other.ptr, otherInlineStorage);
391 this->
s = std::exchange(
other.s, 0);
431 template <
typename U = T, if_copyable<U> = true>
448 template <
typename AT = T>
450 template <
typename AT = T>
452 template <
typename AT = T>
471 using Base::operator[];
495 inline QVarLengthArray<T, Prealloc> &
operator<<(
const T &
t)
497 inline QVarLengthArray<T, Prealloc> &
operator<<(T &&
t)
498 {
append(std::move(
t));
return *
this; }
502 {
append(std::move(
t));
return *
this; }
504#if QT_DEPRECATED_SINCE(6, 3)
516 template <
typename InputIterator, if_input_iterator<InputIterator> = true>
525 template <
typename AT = T>
527 template <
typename AT = T>
529 template <
typename Predicate>
539 inline T *
data() {
return this->
ptr; }
540 inline const T *
data()
const {
return this->
ptr; }
593 inline const T &
front()
const {
return first(); }
595 inline const T &
back()
const {
return last(); }
601 template <
typename...Args>
604 template <
typename...Args>
610 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
611 friend inline bool operator==(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
612 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
613 friend inline bool operator!=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
614 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
615 friend inline bool operator< (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
616 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
617 friend inline bool operator> (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
618 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
619 friend inline bool operator<=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
620 template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
621 friend inline bool operator>=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r);
623 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
629 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
635 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
637 noexcept(
noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
638 rhs.begin(), rhs.end())))
640 return lhs.less_than(rhs);
643 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
645 noexcept(
noexcept(lhs < rhs))
650 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
652 noexcept(
noexcept(lhs < rhs))
657 template <
typename U = T, qsizetype Prealloc2 = Prealloc>
friend
659 noexcept(
noexcept(lhs < rhs))
666 template <
typename U, qsizetype Prealloc2>
667 bool equal(
const QVarLengthArray<U, Prealloc2> &
other)
const
669 template <
typename U, qsizetype Prealloc2>
670 bool less_than(
const QVarLengthArray<U, Prealloc2> &
other)
const
679template <
typename InputIterator,
680 typename ValueType =
typename std::iterator_traits<InputIterator>::value_type,
684template <
class T, qsizetype Prealloc>
688 Q_ASSERT_X(asize >= 0,
"QVarLengthArray::QVarLengthArray(qsizetype)",
689 "Size must be greater than or equal to 0.");
694 if (asize > Prealloc) {
695 this->
ptr = malloc(asize *
sizeof(T));
700 std::uninitialized_default_construct_n(
data(), asize);
705template <
typename AT>
711 const T *
n =
data() + from - 1;
721template <
typename AT>
726 else if (from >=
size())
730 const T *
n =
b + from + 1;
740template <
typename AT>
767 memcpy(
static_cast<void *
>(
end()),
static_cast<const void *
>(abuf),
increment *
sizeof(T));
778 resize_impl(prealloc,
array,
n,
t);
780 auto mid = (std::min)(
n,
size());
782 std::uninitialized_fill(
data() + mid,
data() +
n,
t);
789template <
typename Iterator>
793 constexpr bool IsFwdIt =
794 std::is_convertible_v<typename std::iterator_traits<Iterator>::iterator_category,
795 std::forward_iterator_tag>;
796 if constexpr (IsFwdIt) {
799 reallocate_impl(prealloc,
array, 0,
n);
803 const auto dend =
end();
806 std::destroy(
dst, dend);
810 if constexpr (IsFwdIt) {
816 }
while (++
first != last);
842 if (aalloc > prealloc) {
843 newPtr = malloc(aalloc *
sizeof(T));
853 reinterpret_cast<T *
>(newPtr));
864 std::destroy(oldPtr + asize, oldPtr + osize);
867 if (oldPtr !=
reinterpret_cast<T *
>(
array) && oldPtr !=
data())
874 if (
size_t(
i) >=
size_t(
size()))
876 return operator[](
i);
881 return (
size_t(
i) >=
size_t(
size())) ? defaultValue : operator[](
i);
884template <
class T, qsizetype Prealloc>
887 insert(cbegin() +
i, std::move(
t)); }
888template <
class T, qsizetype Prealloc>
892template <
class T, qsizetype Prealloc>
901template <
typename AT>
905template <
typename AT>
909template <
typename Predicate>
912#if QT_DEPRECATED_SINCE(6, 3)
913template <
class T, qsizetype Prealloc>
915{
insert(cbegin(), std::move(
t)); }
916template <
class T, qsizetype Prealloc>
929template <
typename...Args>
932 Q_ASSERT_X(isValidIterator(before),
"QVarLengthArray::insert",
"The specified const_iterator argument 'before' is invalid");
937 emplace_back_impl(prealloc,
array, std::forward<Args>(
args)...);
939 const auto e =
end();
947 Q_ASSERT_X(isValidIterator(before),
"QVarLengthArray::insert",
"The specified const_iterator argument 'before' is invalid");
952 const auto e =
end();
960 Q_ASSERT_X(isValidIterator(abegin),
"QVarLengthArray::erase",
"The specified const_iterator argument 'abegin' is invalid");
961 Q_ASSERT_X(isValidIterator(aend),
"QVarLengthArray::erase",
"The specified const_iterator argument 'aend' is invalid");
974 std::destroy(
end() -
n,
end());
976 std::destroy(abegin, aend);
977 memmove(
static_cast<void *
>(
data() +
f),
static_cast<const void *
>(
data() + l), (
size() - l) *
sizeof(T));
985template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
986bool operator==(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
988template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
989bool operator!=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
991template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
992bool operator< (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
994template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
995bool operator> (
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
997template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
998bool operator<=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
1000template <
typename T, qsizetype Prealloc1, qsizetype Prealloc2>
1001bool operator>=(
const QVarLengthArray<T, Prealloc1> &l,
const QVarLengthArray<T, Prealloc2> &
r)
1005template <
typename T, qsizetype Prealloc>
1007 noexcept(
QtPrivate::QNothrowHashable_v<T>)
1012template <
typename T, qsizetype Prealloc,
typename AT>
1015 return array.removeAll(
t);
1018template <
typename T, qsizetype Prealloc,
typename Predicate>
1021 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)
QT_BEGIN_NAMESPACE 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)
bool operator>=(const QPoint &a, const QPoint &b)
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)
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