42 #ifndef QVARLENGTHARRAY_H
43 #define QVARLENGTHARRAY_H
58 template<class T,
int Prealloc>
62 template<class T,
int Prealloc>
66 inline explicit QVarLengthArray(
int size = 0);
69 :
a(Prealloc),
s(0), ptr(reinterpret_cast<T *>(array))
75 if (QTypeInfo<T>::isComplex) {
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); }
99 inline void resize(
int size);
103 inline void reserve(
int size);
106 Q_ASSERT(idx >= 0 && idx <
s);
110 Q_ASSERT(idx >= 0 && idx <
s);
113 inline const T &
at(
int idx)
const {
return operator[](idx); }
115 T
value(
int i)
const;
116 T
value(
int i,
const T &defaultValue)
const;
122 if (QTypeInfo<T>::isComplex) {
123 new (ptr + idx) T(t);
128 void append(
const T *buf,
int size);
130 { append(t);
return *
this; }
132 { append(t);
return *
this; }
134 void prepend(
const T &
t);
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);
142 inline T *
data() {
return ptr; }
143 inline const T *
data()
const {
return ptr; }
157 inline iterator
begin() {
return ptr; }
158 inline const_iterator
begin()
const {
return ptr; }
160 inline iterator
end() {
return ptr +
s; }
161 inline const_iterator
end()
const {
return ptr +
s; }
162 inline const_iterator
constEnd()
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); }
165 iterator erase(iterator begin, iterator
end);
166 inline iterator
erase(iterator pos) {
return erase(pos, pos+1); }
169 friend class QPodList<T, Prealloc>;
170 void realloc(
int size,
int alloc);
177 char array[
sizeof(
qint64) * (((Prealloc *
sizeof(T)) /
sizeof(
qint64)) + 1)];
183 template <
class T,
int Prealloc>
187 ptr =
reinterpret_cast<T *
>(qMalloc(s *
sizeof(T)));
191 ptr =
reinterpret_cast<T *
>(
array);
194 if (QTypeInfo<T>::isComplex) {
201 template <
class T,
int Prealloc>
203 { realloc(asize, qMax(asize, a)); }
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;
219 realloc(s, qMax(s*2, asize));
221 if (QTypeInfo<T>::isComplex) {
224 new (ptr+(s++)) T(*abuf++);
226 qMemCopy(&ptr[s], abuf, increment *
sizeof(T));
231 template <
class T,
int Prealloc>
234 Q_ASSERT(aalloc >= asize);
238 const int copySize = qMin(asize, osize);
240 ptr =
reinterpret_cast<T *
>(qMalloc(aalloc *
sizeof(T)));
246 if (QTypeInfo<T>::isStatic) {
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));
273 if (QTypeInfo<T>::isComplex) {
275 while (osize > asize)
276 (oldPtr+(--osize))->~T();
279 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
282 if (QTypeInfo<T>::isComplex) {
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");
308 insert(begin() + i, 1, t); }
309 template <
class T,
int Prealloc>
311 { Q_ASSERT_X(i >= 0 && i <= s,
"QVarLengthArray::insert",
"index out of range");
312 insert(begin() + i, n, t); }
313 template <
class T,
int Prealloc>
315 { Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= s,
"QVarLengthArray::remove",
"index out of range");
316 erase(begin() + i, begin() + i + n); }
317 template <
class T,
int Prealloc>
319 { Q_ASSERT_X(i >= 0 && i < s,
"QVarLengthArray::remove",
"index out of range");
320 erase(begin() + i, begin() + i + 1); }
321 template <
class T,
int Prealloc>
323 { insert(begin(), 1, t); }
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>
341 if (QTypeInfo<T>::isStatic) {
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);
367 if (QTypeInfo<T>::isComplex) {
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
GLdouble GLdouble GLdouble r
QVarLengthArray(const QVarLengthArray< T, Prealloc > &other)
unsigned int(APIENTRYP PFNGLXGETAGPOFFSETMESAPROC)(const void *pointer)
const_iterator constEnd() const
void insert(int i, const T &t)
void replace(int i, const T &t)
iterator erase(iterator pos)
mudbox::Vector & operator<<(mudbox::Vector &v, const HWVector &r)
#define QT_BEGIN_NAMESPACE
GLsizei GLsizei GLenum GLenum const GLvoid * data
char array[sizeof(qint64)*(((Prealloc *sizeof(T))/sizeof(qint64))+1)]
const T * constData() const
QVarLengthArray< T, Prealloc > & operator=(const QVarLengthArray< T, Prealloc > &other)
bool operator==(const QVarLengthArray< T, Prealloc1 > &l, const QVarLengthArray< T, Prealloc2 > &r)
QVarLengthArray< T, Prealloc > & operator+=(const T &t)
const_iterator constBegin() const
const T & at(int idx) const
GLsizei const GLfloat * value
const value_type * const_pointer
GLubyte GLubyte GLubyte a
const value_type & const_reference
OutputIterator qCopy(InputIterator begin, InputIterator end, OutputIterator dest)
iterator erase(iterator begin, iterator end)
QVarLengthArray(int size=0)
bool operator!=(const QVarLengthArray< T, Prealloc1 > &l, const QVarLengthArray< T, Prealloc2 > &r)
const_iterator begin() const
iterator insert(iterator before, const T &x)
const_iterator end() const
const T & operator[](int idx) const