42 #ifndef QCONTIGUOUSCACHE_H 43 #define QCONTIGUOUSCACHE_H 45 #include <QtCore/qatomic.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 108 inline void detach() {
if (
d->ref != 1) detach_helper(); }
113 #ifdef Q_COMPILER_RVALUE_REFS 115 {
qSwap(
d, other.
d);
return *
this; }
122 inline int count()
const {
return d->count; }
123 inline int size()
const {
return d->count; }
125 inline bool isEmpty()
const {
return d->count == 0; }
126 inline bool isFull()
const {
return d->count ==
d->alloc; }
130 void setCapacity(
int size);
132 const T &
at(
int pos)
const;
133 T &operator[](
int i);
134 const T &operator[](
int i)
const;
136 void append(
const T &value);
137 void prepend(
const T &value);
138 void insert(
int pos,
const T &value);
140 inline bool containsIndex(
int pos)
const {
return pos >=
d->offset && pos -
d->offset <
d->count; }
155 {
return d->offset >= 0 &&
d->offset <
INT_MAX -
d->count && (
d->offset %
d->alloc) ==
d->start; }
159 #ifdef QT_QCONTIGUOUSCACHE_DEBUG 160 void dump()
const { p->dump(); }
163 void detach_helper();
170 return reinterpret_cast<const char *
>(&(
reinterpret_cast<const Data *
>(
this))->array[1]) - reinterpret_cast<const char *>(
this);
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;
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;
245 if (dest == x.p->array)
246 dest = x.p->array + x.d->alloc;
249 src = p->array +
d->alloc;
258 template <
typename T>
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>
324 for (
int i = firstIndex(); i <= lastIndex(); ++i)
325 if (!(
at(i) == other.
at(i)))
330 template <
typename T>
334 int oldcount =
d->count;
335 T * i = p->array +
d->start;
336 T * e = p->array +
d->alloc;
346 template <
typename T>
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();
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)) {
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;
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>
447 (p->array +
d->start)->~T();
448 d->start = (
d->start + 1) %
d->alloc;
452 template <
typename T>
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; }
bool operator!=(const QContiguousCache< T > &other) const
Returns true if other is not equal to this cache; otherwise returns false.
void prepend(const T &value)
Inserts value at the start of the cache.
The QContiguousCache class is a template class that provides a contiguous cache.
int alignOfTypedData() const
void removeLast()
Removes the last item from the cache.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
int capacity() const
Returns the number of items the cache can store before it is full.
#define at(className, varName)
~QContiguousCache()
Destroys the cache.
const T & last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static void clear(QVariant::Private *d)
static void free(QContiguousCacheData *data)
bool isFull() const
Returns true if the number of items stored within the cache is equal to the capacity of the cache...
T takeLast()
Removes the last item in the cache and returns it.
T & last()
Returns a reference to the last item in the cache.
bool isEmpty() const
Returns true if no items are stored within the cache.
const value_type & const_reference
int available() const
Returns the number of items that can be added to the cache before it becomes full.
void setCapacity(int size)
Sets the capacity of the cache to the given size.
#define QT_BEGIN_NAMESPACE
This macro expands to.
QContiguousCache< T > & operator=(const QContiguousCache< T > &other)
Assigns other to this cache and returns a reference to this cache.
static bool isEmpty(const char *str)
const T & at(int pos) const
Returns the item at index position i in the cache.
void normalizeIndexes()
Moves the first index and last index of the cache such that they point to valid indexes.
QIntegerForSizeof< void * >::Signed qptrdiff
QContiguousCacheTypedData< T > * p
void insert(int pos, const T &value)
Inserts the value at the index position i.
QContiguousCacheData * malloc(int aalloc)
static const char * data(const QByteArray &arr)
QContiguousCache(int capacity=0)
Constructs a cache with the given capacity.
int firstIndex() const
Returns the first valid index in the cache.
void swap(QContiguousCache< T > &other)
Swaps cache other with this cache.
void qSwap(T &value1, T &value2)
static QContiguousCacheData * allocate(int size, int alignment)
bool operator==(const QContiguousCache< T > &other) const
Returns true if other is equal to this cache; otherwise returns false.
static void free(QContiguousCacheTypedData *data)
int size() const
Returns the number of items contained within the cache.
#define Q_ASSERT_X(cond, where, what)
bool areIndexesValid() const
Returns whether the indexes for items stored in the cache are valid.
void removeFirst()
Removes the first item from the cache.
bool containsIndex(int pos) const
Returns true if the cache's index range includes the given index i.
int count() const
Same as size().
T & first()
Returns a reference to the first item in the cache.
static QString dump(const QByteArray &)
void setSharable(bool sharable)
const T & first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & operator[](int i)
Returns the item at index position i as a modifiable reference.
T takeFirst()
Removes the first item in the cache and returns it.
void clear()
Removes all items from the cache.
const value_type * const_pointer
bool operator==(QBool b1, bool b2)
int size() const
Returns the number of items in the vector.
void append(const T &value)
Inserts value at the end of the cache.
QContiguousCacheTypedData< T > Data
int lastIndex() const
Returns the last valid index in the cache.
QContiguousCache(const QContiguousCache< T > &v)
Constructs a copy of other.