42 #ifndef QVARLENGTHARRAY_H 43 #define QVARLENGTHARRAY_H 45 #include <QtCore/qcontainerfwd.h> 46 #include <QtCore/qglobal.h> 47 #include <QtCore/qalgorithms.h> 58 template<
class T,
int Prealloc>
62 template<
class T,
int Prealloc>
69 :
a(Prealloc),
s(0),
ptr(reinterpret_cast<T *>(
array))
80 if (
ptr != reinterpret_cast<T *>(
array))
96 inline int size()
const {
return s; }
97 inline int count()
const {
return s; }
98 inline bool isEmpty()
const {
return (
s == 0); }
115 T
value(
int i)
const;
116 T
value(
int i,
const T &defaultValue)
const;
123 new (
ptr + idx) T(t);
128 void append(
const T *buf,
int size);
130 {
append(t);
return *
this; }
132 {
append(t);
return *
this; }
135 void insert(
int i,
const T &t);
136 void insert(
int i,
int n,
const T &t);
137 void replace(
int i,
const T &t);
139 void remove(
int i,
int n);
158 inline const_iterator
begin()
const {
return ptr; }
161 inline const_iterator
end()
const {
return ptr +
s; }
163 iterator
insert(iterator before,
int n,
const T &x);
164 inline iterator
insert(iterator before,
const T &x) {
return insert(before, 1, x); }
166 inline iterator
erase(iterator pos) {
return erase(pos, pos+1); }
170 void realloc(
int size,
int alloc);
183 template <
class T,
int Prealloc>
187 ptr =
reinterpret_cast<T *
>(
qMalloc(s *
sizeof(T)));
191 ptr =
reinterpret_cast<T *
>(
array);
201 template <
class T,
int Prealloc>
205 template <
class T,
int Prealloc>
207 {
if (asize > a)
realloc(s, asize); }
209 template <
class T,
int Prealloc>
216 const int asize = s + increment;
224 new (ptr+(s++)) T(*abuf++);
226 qMemCopy(&ptr[s], abuf, increment *
sizeof(T));
231 template <
class T,
int Prealloc>
238 const int copySize =
qMin(asize, osize);
240 ptr =
reinterpret_cast<T *
>(
qMalloc(aalloc *
sizeof(T)));
249 while (s < copySize) {
250 new (ptr+
s) T(*(oldPtr+s));
257 while (sClean < osize)
258 (oldPtr+(sClean++))->~T();
259 if (oldPtr != reinterpret_cast<T *>(
array) && oldPtr != ptr)
264 qMemCopy(ptr, oldPtr, copySize *
sizeof(T));
275 while (osize > asize)
276 (oldPtr+(--osize))->~T();
279 if (oldPtr != reinterpret_cast<T *>(
array) && oldPtr != ptr)
291 template <
class T,
int Prealloc>
294 if (i < 0 || i >=
size()) {
299 template <
class T,
int Prealloc>
302 return (i < 0 || i >=
size()) ? defaultValue :
at(i);
305 template <
class T,
int Prealloc>
307 {
Q_ASSERT_X(i >= 0 && i <= s,
"QVarLengthArray::insert",
"index out of range");
309 template <
class T,
int Prealloc>
311 {
Q_ASSERT_X(i >= 0 && i <= s,
"QVarLengthArray::insert",
"index out of range");
313 template <
class T,
int Prealloc>
315 {
Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= s,
"QVarLengthArray::remove",
"index out of range");
317 template <
class T,
int Prealloc>
319 {
Q_ASSERT_X(i >= 0 && i < s,
"QVarLengthArray::remove",
"index out of range");
321 template <
class T,
int Prealloc>
325 template <
class T,
int Prealloc>
328 Q_ASSERT_X(i >= 0 && i < s,
"QVarLengthArray::replace",
"index out of range");
334 template <
class T,
int Prealloc>
337 int offset = int(before - ptr);
353 memmove(i, b, (s - offset - n) *
sizeof(T));
361 template <
class T,
int Prealloc>
364 int f = int(abegin - ptr);
365 int l = int(aend - ptr);
368 qCopy(ptr + l, ptr + s, ptr + f);
376 memmove(ptr + f, ptr + l, (s - l) *
sizeof(T));
382 template <
typename T,
int Prealloc1,
int Prealloc2>
387 for (
int i = 0; i < l.
size(); i++) {
388 if (l.
at(i) != r.
at(i))
394 template <
typename T,
int Prealloc1,
int Prealloc2>
404 #endif // QVARLENGTHARRAY_H
QVarLengthArray(int size=0)
iterator insert(iterator before, const T &x)
const T * constData() const
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
iterator erase(iterator begin, iterator end)
#define QT_END_NAMESPACE
This macro expands to.
QVarLengthArray< T, Prealloc > & operator=(const QVarLengthArray< T, Prealloc > &other)
Q_CORE_EXPORT void qFree(void *ptr)
Q_CORE_EXPORT void * qMalloc(size_t size)
void insert(int i, const T &t)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
const_iterator begin() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
QIntegerForSizeof< void * >::Signed qptrdiff
const_iterator end() const
QVarLengthArray(const QVarLengthArray< T, Prealloc > &other)
#define Q_INLINE_TEMPLATE
const T & at(int idx) const
bool operator==(const QVarLengthArray< T, Prealloc1 > &l, const QVarLengthArray< T, Prealloc2 > &r)
const_iterator constEnd() const
void * qMemCopy(void *dest, const void *src, size_t n)
#define Q_ASSERT_X(cond, where, what)
QVarLengthArray< T, Prealloc > & operator<<(const T &t)
void replace(int i, const T &t)
void realloc(int size, int alloc)
QVarLengthArray< T, Prealloc > & operator+=(const T &t)
OutputIterator qCopy(InputIterator begin, InputIterator end, OutputIterator dest)
char array[sizeof(qint64) *(((Prealloc *sizeof(T))/sizeof(qint64))+1)]
const_iterator constBegin() const
bool operator!=(const QVarLengthArray< T, Prealloc1 > &l, const QVarLengthArray< T, Prealloc2 > &r)
#define Q_OUTOFLINE_TEMPLATE
const value_type * const_pointer
iterator erase(iterator pos)
const T & operator[](int idx) const
const value_type & const_reference