42 #ifndef QCONTIGUOUSCACHE_H
43 #define QCONTIGUOUSCACHE_H
53 #undef QT_QCONTIGUOUSCACHE_DEBUG
72 static QContiguousCacheData *allocate(
int size,
int alignment);
73 static void free(QContiguousCacheData *
data);
75 #ifdef QT_QCONTIGUOUSCACHE_DEBUG
113 #ifdef Q_COMPILER_RVALUE_REFS
115 {
qSwap(
d, other.d);
return *
this; }
132 const T &
at(
int pos)
const;
159 #ifdef QT_QCONTIGUOUSCACHE_DEBUG
160 void dump()
const {
p->dump(); }
163 void detach_helper();
167 int sizeOfTypedData() {
170 return reinterpret_cast<const char *
>(&(
reinterpret_cast<const Data *
>(
this))->array[1]) - reinterpret_cast<const char *>(
this);
172 int alignOfTypedData()
const
175 return qMax<int>(
sizeof(
void*), Q_ALIGNOF(Data));
182 template <
typename T>
187 x.d = malloc(d->alloc);
189 x.d->count = d->count;
190 x.d->start = d->start;
191 x.d->offset = d->offset;
192 x.d->alloc = d->alloc;
193 x.d->sharable =
true;
196 T *dest =
x.p->array +
x.d->start;
197 T *
src =
p->array + d->start;
198 int oldcount =
x.d->count;
200 if (QTypeInfo<T>::isComplex) {
206 if (dest ==
x.p->array +
x.d->alloc)
209 if (src ==
p->array + d->alloc)
218 template <
typename T>
221 if (asize == d->alloc)
227 x.d->count = qMin(d->count, asize);
228 x.d->offset = d->offset + d->count -
x.d->count;
230 x.d->start =
x.d->offset %
x.d->alloc;
234 int oldcount =
x.d->count;
237 T *dest =
x.p->array + (
x.d->start +
x.d->count-1) %
x.d->alloc;
238 T *src =
p->array + (d->start + d->count-1) % d->alloc;
240 if (QTypeInfo<T>::isComplex) {
245 if (dest ==
x.p->array)
246 dest =
x.p->array +
x.d->alloc;
249 src =
p->array + d->alloc;
258 template <
typename T>
262 if (QTypeInfo<T>::isComplex) {
263 int oldcount = d->count;
264 T * i =
p->array + d->start;
265 T * e =
p->array + d->alloc;
273 d->count = d->start = d->offset = 0;
276 x.d = malloc(d->alloc);
278 x.d->alloc = d->alloc;
279 x.d->count =
x.d->start =
x.d->offset = 0;
280 x.d->sharable =
true;
281 if (!d->ref.deref()) free(
p);
286 template <
typename T>
292 template <
typename T>
298 d->count = d->start = d->offset = 0;
302 template <
typename T>
314 template <
typename T>
319 if (other.
d->
start != d->start
320 || other.
d->
count != d->count
321 || other.
d->
offset != d->offset
322 || other.
d->
alloc != d->alloc)
324 for (
int i = firstIndex(); i <= lastIndex(); ++i)
325 if (!(at(i) == other.
at(i)))
330 template <
typename T>
333 if (QTypeInfo<T>::isComplex) {
334 int oldcount = d->
count;
335 T * i =
p->array + d->start;
336 T * e =
p->array + d->alloc;
346 template <
typename T>
352 if (QTypeInfo<T>::isComplex) {
353 if (d->count == d->alloc)
354 (
p->array + (d->start+d->count) % d->alloc)->~T();
355 new (
p->array + (d->start+d->count) % d->alloc) T(value);
357 p->array[(d->start+d->count) % d->alloc] = value;
360 if (d->count == d->alloc) {
362 d->start %= d->alloc;
378 d->start = d->alloc-1;
381 if (d->count != d->alloc)
384 if (d->count == d->alloc)
385 (
p->array + d->start)->~T();
387 if (QTypeInfo<T>::isComplex)
388 new (
p->array + d->start) T(value);
390 p->array[d->start] =
value;
396 Q_ASSERT_X(pos >= 0 && pos < INT_MAX,
"QContiguousCache<T>::insert",
"index out of range");
400 if (containsIndex(pos)) {
401 if (QTypeInfo<T>::isComplex) {
402 (
p->array + pos % d->alloc)->~T();
403 new (
p->array + pos % d->alloc) T(value);
405 p->array[pos % d->alloc] =
value;
407 }
else if (pos == d->offset-1)
409 else if (pos == d->offset+d->count)
415 d->start = pos % d->alloc;
417 if (QTypeInfo<T>::isComplex)
418 new (
p->array + d->start) T(value);
420 p->array[d->start] =
value;
424 template <
typename T>
426 { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count,
"QContiguousCache<T>::at",
"index out of range");
return p->array[pos % d->alloc]; }
427 template <
typename T>
429 { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count,
"QContiguousCache<T>::at",
"index out of range");
return p->array[pos % d->alloc]; }
431 template <
typename T>
435 if (!containsIndex(pos))
437 return p->array[pos % d->alloc];
440 template <
typename T>
443 Q_ASSERT(d->count > 0);
446 if (QTypeInfo<T>::isComplex)
447 (
p->array + d->start)->~T();
448 d->start = (d->start + 1) % d->alloc;
452 template <
typename T>
455 Q_ASSERT(d->count > 0);
458 if (QTypeInfo<T>::isComplex)
459 (
p->array + (d->start + d->count) % d->alloc)->~T();
462 template <
typename T>
464 { T
t =
first(); removeFirst();
return t; }
466 template <
typename T>
468 { T
t = last(); removeLast();
return t; }
void setSharable(bool sharable)
static void free(QContiguousCacheTypedData *data)
void insert(int pos, const T &value)
void append(const T &value)
QContiguousCache(int capacity=0)
#define QT_BEGIN_NAMESPACE
GLsizei GLsizei GLenum GLenum const GLvoid * data
QContiguousCache< T > & operator=(const QContiguousCache< T > &other)
Q_INLINE_TEMPLATE void qSwap(QScopedPointer< T, Cleanup > &p1, QScopedPointer< T, Cleanup > &p2)
QContiguousCacheTypedData< T > * p
bool areIndexesValid() const
QContiguousCache(const QContiguousCache< T > &v)
void setCapacity(int size)
GLsizei const GLfloat * value
static QContiguousCacheData * allocate(int size, int alignment)
static void free(QContiguousCacheData *data)
bool operator==(const QContiguousCache< T > &other) const
const T & at(int pos) const
void prepend(const T &value)
const value_type & const_reference
bool operator!=(const QContiguousCache< T > &other) const
bool containsIndex(int pos) const
const value_type * const_pointer
void swap(QContiguousCache< T > &other)