Qt 4.8
Public Types | Public Functions | Private Types | Private Functions | Properties | List of all members
QContiguousCache< T > Class Template Reference

The QContiguousCache class is a template class that provides a contiguous cache. More...

#include <qcontiguouscache.h>

Public Types

typedef const value_typeconst_pointer
 
typedef const value_typeconst_reference
 
typedef qptrdiff difference_type
 
typedef value_typepointer
 
typedef value_typereference
 
typedef int size_type
 
typedef T value_type
 

Public Functions

void append (const T &value)
 Inserts value at the end of the cache. More...
 
bool areIndexesValid () const
 Returns whether the indexes for items stored in the cache are valid. More...
 
const T & at (int pos) const
 Returns the item at index position i in the cache. More...
 
int available () const
 Returns the number of items that can be added to the cache before it becomes full. More...
 
int capacity () const
 Returns the number of items the cache can store before it is full. More...
 
void clear ()
 Removes all items from the cache. More...
 
bool containsIndex (int pos) const
 Returns true if the cache's index range includes the given index i. More...
 
int count () const
 Same as size(). More...
 
void detach ()
 
const T & first () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
T & first ()
 Returns a reference to the first item in the cache. More...
 
int firstIndex () const
 Returns the first valid index in the cache. More...
 
void insert (int pos, const T &value)
 Inserts the value at the index position i. More...
 
bool isDetached () const
 
bool isEmpty () const
 Returns true if no items are stored within the cache. More...
 
bool isFull () const
 Returns true if the number of items stored within the cache is equal to the capacity of the cache. More...
 
const T & last () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
T & last ()
 Returns a reference to the last item in the cache. More...
 
int lastIndex () const
 Returns the last valid index in the cache. More...
 
void normalizeIndexes ()
 Moves the first index and last index of the cache such that they point to valid indexes. More...
 
bool operator!= (const QContiguousCache< T > &other) const
 Returns true if other is not equal to this cache; otherwise returns false. More...
 
QContiguousCache< T > & operator= (const QContiguousCache< T > &other)
 Assigns other to this cache and returns a reference to this cache. More...
 
bool operator== (const QContiguousCache< T > &other) const
 Returns true if other is equal to this cache; otherwise returns false. More...
 
T & operator[] (int i)
 Returns the item at index position i as a modifiable reference. More...
 
const T & operator[] (int i) const
 Same as at(i). More...
 
void prepend (const T &value)
 Inserts value at the start of the cache. More...
 
 QContiguousCache (int capacity=0)
 Constructs a cache with the given capacity. More...
 
 QContiguousCache (const QContiguousCache< T > &v)
 Constructs a copy of other. More...
 
void removeFirst ()
 Removes the first item from the cache. More...
 
void removeLast ()
 Removes the last item from the cache. More...
 
void setCapacity (int size)
 Sets the capacity of the cache to the given size. More...
 
void setSharable (bool sharable)
 
int size () const
 Returns the number of items contained within the cache. More...
 
void swap (QContiguousCache< T > &other)
 Swaps cache other with this cache. More...
 
takeFirst ()
 Removes the first item in the cache and returns it. More...
 
takeLast ()
 Removes the last item in the cache and returns it. More...
 
 ~QContiguousCache ()
 Destroys the cache. More...
 

Private Types

typedef QContiguousCacheTypedData< T > Data
 

Private Functions

int alignOfTypedData () const
 
void detach_helper ()
 
void free (Data *x)
 
QContiguousCacheDatamalloc (int aalloc)
 
int sizeOfTypedData ()
 

Properties

union {
   QContiguousCacheData *   d
 
   QContiguousCacheTypedData< T > *   p
 
}; 
 

Detailed Description

template<typename T>
class QContiguousCache< T >

The QContiguousCache class is a template class that provides a contiguous cache.

Note
This class or function is reentrant.
Since
4.6

The QContiguousCache class provides an efficient way of caching items for display in a user interface view. Unlike QCache, it adds a restriction that elements within the cache are contiguous. This has the advantage of matching how user interface views most commonly request data, as a set of rows localized around the current scrolled position. This restriction allows the cache to consume less memory and processor cycles than QCache. The QContiguousCache class also can provide an upper bound on memory usage via setCapacity().

The simplest way of using a contiguous cache is to use the append() and prepend().

MyRecord record(int row) const
{
Q_ASSERT(row >= 0 && row < count());
while(row > cache.lastIndex())
cache.append(slowFetchRecord(cache.lastIndex()+1));
while(row < cache.firstIndex())
cache.prepend(slowFetchRecord(cache.firstIndex()-1));
return cache.at(row);
}
If the cache is full then the item at the opposite end of the cache from
where the new item is appended or prepended will be removed.

This usage can be further optimized by using the insert() function
in the case where the requested row is a long way from the currently cached
items. If there is a gap between where the new item is inserted and the currently
cached items then the existing cached items are first removed to retain
the contiguous nature of the cache. Hence it is important to take some care then
when using insert() in order to avoid unwanted clearing of the cache.

The range of valid indexes for the QContiguousCache class are from
0 to INT_MAX.  Calling prepend() such that the first index would become less
than 0 or append() such that the last index would become greater
than INT_MAX can result in the indexes of the cache being invalid.
When the cache indexes are invalid it is important to call
normalizeIndexes() before calling any of containsIndex(), firstIndex(),
lastIndex(), at() or \ref QContiguousCache::operator[]() "operator[]()".
Calling these functions when the cache has invalid indexes will result in
undefined behavior. The indexes can be checked by using areIndexesValid()

In most cases the indexes will not exceed 0 to INT_MAX, and
normalizeIndexes() will not need to be used.

See the <b>Contiguous Cache Example</b>{Contiguous Cache} example.

Definition at line 90 of file qcontiguouscache.h.

Typedefs

◆ const_pointer

template<typename T>
QContiguousCache< T >::const_pointer
Warning
This function is not part of the public interface.

Definition at line 97 of file qcontiguouscache.h.

◆ const_reference

template<typename T>
QContiguousCache< T >::const_reference
Warning
This function is not part of the public interface.

Definition at line 99 of file qcontiguouscache.h.

◆ Data

template<typename T>
typedef QContiguousCacheTypedData<T> QContiguousCache< T >::Data
private

Definition at line 91 of file qcontiguouscache.h.

◆ difference_type

template<typename T>
QContiguousCache< T >::difference_type
Warning
This function is not part of the public interface.

Definition at line 100 of file qcontiguouscache.h.

◆ pointer

template<typename T>
QContiguousCache< T >::pointer
Warning
This function is not part of the public interface.

Definition at line 96 of file qcontiguouscache.h.

◆ reference

template<typename T>
QContiguousCache< T >::reference
Warning
This function is not part of the public interface.

Definition at line 98 of file qcontiguouscache.h.

◆ size_type

template<typename T>
QContiguousCache< T >::size_type
Warning
This function is not part of the public interface.

Definition at line 101 of file qcontiguouscache.h.

◆ value_type

template<typename T>
QContiguousCache< T >::value_type
Warning
This function is not part of the public interface.

Definition at line 95 of file qcontiguouscache.h.

Constructors and Destructors

◆ QContiguousCache() [1/2]

template<typename T >
QContiguousCache< T >::QContiguousCache ( int  capacity = 0)
explicit

Constructs a cache with the given capacity.

See also
setCapacity()

Definition at line 293 of file qcontiguouscache.h.

294 {
295  d = malloc(cap);
296  d->ref = 1;
297  d->alloc = cap;
298  d->count = d->start = d->offset = 0;
299  d->sharable = true;
300 }
QContiguousCacheData * d
QBasicAtomicInt ref
QContiguousCacheData * malloc(int aalloc)

◆ QContiguousCache() [2/2]

template<typename T>
QContiguousCache< T >::QContiguousCache ( const QContiguousCache< T > &  other)
inline

Constructs a copy of other.

This operation takes constant time, because QContiguousCache is implicitly shared. This makes returning a QContiguousCache from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.

See also
operator=()

Definition at line 104 of file qcontiguouscache.h.

104 : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
QContiguousCacheData * d
QBasicAtomicInt ref

◆ ~QContiguousCache()

template<typename T>
QContiguousCache< T >::~QContiguousCache ( )
inline

Destroys the cache.

Definition at line 106 of file qcontiguouscache.h.

106 { if (!d) return; if (!d->ref.deref()) free(p); }
QContiguousCacheData * d
QBasicAtomicInt ref
QContiguousCacheTypedData< T > * p
void free(Data *x)

Functions

◆ alignOfTypedData()

template<typename T>
int QContiguousCache< T >::alignOfTypedData ( ) const
inlineprivate

Definition at line 172 of file qcontiguouscache.h.

173  {
174 #ifdef Q_ALIGNOF
175  return qMax<int>(sizeof(void*), Q_ALIGNOF(Data));
176 #else
177  return 0;
178 #endif
179  }
QContiguousCacheTypedData< T > Data

◆ append()

template<typename T >
void QContiguousCache< T >::append ( const T &  value)

Inserts value at the end of the cache.

If the cache is already full the item at the start of the cache will be removed.

See also
prepend(), insert(), isFull()

Definition at line 347 of file qcontiguouscache.h.

348 {
349  if (!d->alloc)
350  return; // zero capacity
351  detach();
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);
356  } else {
357  p->array[(d->start+d->count) % d->alloc] = value;
358  }
359 
360  if (d->count == d->alloc) {
361  d->start++;
362  d->start %= d->alloc;
363  d->offset++;
364  } else {
365  d->count++;
366  }
367 }
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p

◆ areIndexesValid()

template<typename T>
bool QContiguousCache< T >::areIndexesValid ( ) const
inline

Returns whether the indexes for items stored in the cache are valid.

Indexes can become invalid if items are appended after the index position INT_MAX or prepended before the index position 0. This is only expected to occur in very long lived circular buffer style usage of the contiguous cache. Indexes can be made valid again by calling normalizeIndexs().

See also
normalizeIndexes(), append(), prepend()

Definition at line 154 of file qcontiguouscache.h.

155  { return d->offset >= 0 && d->offset < INT_MAX - d->count && (d->offset % d->alloc) == d->start; }
QContiguousCacheData * d
#define INT_MAX

◆ at()

template<typename T >
const T & QContiguousCache< T >::at ( int  i) const
inline

Returns the item at index position i in the cache.

i must be a valid index position in the cache (i.e, firstIndex() <= i <= lastIndex()).

The indexes in the cache refer to the number of positions the item is from the first item appended into the cache. That is to say a cache with a capacity of 100, that has had 150 items appended will have a valid index range of 50 to 149. This allows inserting and retrieving items into the cache based on a theoretical infinite list

See also
firstIndex(), lastIndex(), insert(), operator[]()

Definition at line 425 of file qcontiguouscache.h.

Referenced by QContiguousCache< T >::operator==().

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]; }
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837

◆ available()

template<typename T>
int QContiguousCache< T >::available ( ) const
inline

Returns the number of items that can be added to the cache before it becomes full.

See also
size(), capacity(), isFull()

Definition at line 127 of file qcontiguouscache.h.

127 { return d->alloc - d->count; }
QContiguousCacheData * d

◆ capacity()

template<typename T>
int QContiguousCache< T >::capacity ( ) const
inline

Returns the number of items the cache can store before it is full.

When a cache contains a number of items equal to its capacity, adding new items will cause items farthest from the added item to be removed.

See also
setCapacity(), size()

Definition at line 121 of file qcontiguouscache.h.

121 {return d->alloc; }
QContiguousCacheData * d

◆ clear()

template<typename T >
void QContiguousCache< T >::clear ( )

Removes all items from the cache.

The capacity is unchanged.

Definition at line 259 of file qcontiguouscache.h.

260 {
261  if (d->ref == 1) {
263  int oldcount = d->count;
264  T * i = p->array + d->start;
265  T * e = p->array + d->alloc;
266  while (oldcount--) {
267  i->~T();
268  i++;
269  if (i == e)
270  i = p->array;
271  }
272  }
273  d->count = d->start = d->offset = 0;
274  } else {
276  x.d = malloc(d->alloc);
277  x.d->ref = 1;
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);
282  d = x.d;
283  }
284 }
QContiguousCacheData * d
QBasicAtomicInt ref
QContiguousCacheTypedData< T > * p
QContiguousCacheData * malloc(int aalloc)
void free(Data *x)

◆ containsIndex()

template<typename T>
bool QContiguousCache< T >::containsIndex ( int  i) const
inline

Returns true if the cache's index range includes the given index i.

See also
firstIndex(), lastIndex()

Definition at line 140 of file qcontiguouscache.h.

140 { return pos >= d->offset && pos - d->offset < d->count; }
QContiguousCacheData * d

◆ count()

template<typename T>
int QContiguousCache< T >::count ( ) const
inline

Same as size().

Definition at line 122 of file qcontiguouscache.h.

122 { return d->count; }
QContiguousCacheData * d

◆ detach()

template<typename T>
void QContiguousCache< T >::detach ( )
inline
Warning
This function is not part of the public interface.

Definition at line 108 of file qcontiguouscache.h.

108 { if (d->ref != 1) detach_helper(); }
QContiguousCacheData * d
QBasicAtomicInt ref

◆ detach_helper()

template<typename T >
void QContiguousCache< T >::detach_helper ( )
private

Definition at line 183 of file qcontiguouscache.h.

184 {
186 
187  x.d = malloc(d->alloc);
188  x.d->ref = 1;
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;
194  x.d->reserved = 0;
195 
196  T *dest = x.p->array + x.d->start;
197  T *src = p->array + d->start;
198  int oldcount = x.d->count;
199  while (oldcount--) {
201  new (dest) T(*src);
202  } else {
203  *dest = *src;
204  }
205  dest++;
206  if (dest == x.p->array + x.d->alloc)
207  dest = x.p->array;
208  src++;
209  if (src == p->array + d->alloc)
210  src = p->array;
211  }
212 
213  if (!d->ref.deref())
214  free(p);
215  d = x.d;
216 }
QContiguousCacheData * d
QBasicAtomicInt ref
QContiguousCacheTypedData< T > * p
QContiguousCacheData * malloc(int aalloc)
void free(Data *x)

◆ first() [1/2]

template<typename T>
const T & QContiguousCache< T >::first ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 144 of file qcontiguouscache.h.

144 { Q_ASSERT(!isEmpty()); return p->array[d->start]; }
QContiguousCacheData * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isEmpty() const
Returns true if no items are stored within the cache.
QContiguousCacheTypedData< T > * p

◆ first() [2/2]

template<typename T>
T & QContiguousCache< T >::first ( )
inline

Returns a reference to the first item in the cache.

This function assumes that the cache isn't empty.

See also
last(), isEmpty()

Definition at line 146 of file qcontiguouscache.h.

146 { Q_ASSERT(!isEmpty()); detach(); return p->array[d->start]; }
QContiguousCacheData * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isEmpty() const
Returns true if no items are stored within the cache.
QContiguousCacheTypedData< T > * p

◆ firstIndex()

template<typename T>
int QContiguousCache< T >::firstIndex ( ) const
inline

Returns the first valid index in the cache.

The index will be invalid if the cache is empty.

See also
capacity(), size(), lastIndex()

Definition at line 141 of file qcontiguouscache.h.

Referenced by operator<<().

141 { return d->offset; }
QContiguousCacheData * d

◆ free()

template<typename T >
void QContiguousCache< T >::free ( Data x)
private

Definition at line 331 of file qcontiguouscache.h.

332 {
334  int oldcount = d->count;
335  T * i = p->array + d->start;
336  T * e = p->array + d->alloc;
337  while (oldcount--) {
338  i->~T();
339  i++;
340  if (i == e)
341  i = p->array;
342  }
343  }
344  x->free(x);
345 }
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p

◆ insert()

template<typename T >
void QContiguousCache< T >::insert ( int  i,
const T &  value 
)

Inserts the value at the index position i.

If the cache already contains an item at i then that value is replaced. If i is either one more than lastIndex() or one less than firstIndex() it is the equivalent to an append() or a prepend().

If the given index i is not within the current range of the cache nor adjacent to the bounds of the cache's index range, the cache is first cleared before inserting the item. At this point the cache will have a size of 1. It is worthwhile taking effort to insert items in an order that starts adjacent to the current index range for the cache.

The range of valid indexes for the QContiguousCache class are from 0 to INT_MAX. Inserting outside of this range has undefined behavior.

See also
prepend(), append(), isFull(), firstIndex(), lastIndex()

Definition at line 394 of file qcontiguouscache.h.

395 {
396  Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache<T>::insert", "index out of range");
397  if (!d->alloc)
398  return; // zero capacity
399  detach();
400  if (containsIndex(pos)) {
402  (p->array + pos % d->alloc)->~T();
403  new (p->array + pos % d->alloc) T(value);
404  } else {
405  p->array[pos % d->alloc] = value;
406  }
407  } else if (pos == d->offset-1)
408  prepend(value);
409  else if (pos == d->offset+d->count)
410  append(value);
411  else {
412  // we don't leave gaps.
413  clear();
414  d->offset = pos;
415  d->start = pos % d->alloc;
416  d->count = 1;
418  new (p->array + d->start) T(value);
419  else
420  p->array[d->start] = value;
421  }
422 }
void prepend(const T &value)
Inserts value at the start of the cache.
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
bool containsIndex(int pos) const
Returns true if the cache&#39;s index range includes the given index i.
void clear()
Removes all items from the cache.
#define INT_MAX
void append(const T &value)
Inserts value at the end of the cache.

◆ isDetached()

template<typename T>
bool QContiguousCache< T >::isDetached ( ) const
inline
Warning
This function is not part of the public interface.

Definition at line 109 of file qcontiguouscache.h.

109 { return d->ref == 1; }
QContiguousCacheData * d
QBasicAtomicInt ref

◆ isEmpty()

template<typename T>
bool QContiguousCache< T >::isEmpty ( ) const
inline

Returns true if no items are stored within the cache.

See also
size(), capacity()

Definition at line 125 of file qcontiguouscache.h.

125 { return d->count == 0; }
QContiguousCacheData * d

◆ isFull()

template<typename T>
bool QContiguousCache< T >::isFull ( ) const
inline

Returns true if the number of items stored within the cache is equal to the capacity of the cache.

See also
size(), capacity()

Definition at line 126 of file qcontiguouscache.h.

126 { return d->count == d->alloc; }
QContiguousCacheData * d

◆ last() [1/2]

template<typename T>
const T & QContiguousCache< T >::last ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 145 of file qcontiguouscache.h.

145 { Q_ASSERT(!isEmpty()); return p->array[(d->start + d->count -1) % d->alloc]; }
QContiguousCacheData * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isEmpty() const
Returns true if no items are stored within the cache.
QContiguousCacheTypedData< T > * p

◆ last() [2/2]

template<typename T>
T & QContiguousCache< T >::last ( )
inline

Returns a reference to the last item in the cache.

This function assumes that the cache isn't empty.

See also
first(), isEmpty()

Definition at line 147 of file qcontiguouscache.h.

147 { Q_ASSERT(!isEmpty()); detach(); return p->array[(d->start + d->count -1) % d->alloc]; }
QContiguousCacheData * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isEmpty() const
Returns true if no items are stored within the cache.
QContiguousCacheTypedData< T > * p

◆ lastIndex()

template<typename T>
int QContiguousCache< T >::lastIndex ( ) const
inline

Returns the last valid index in the cache.

The index will be invalid if the cache is empty.

See also
capacity(), size(), firstIndex()

Definition at line 142 of file qcontiguouscache.h.

Referenced by operator<<().

142 { return d->offset + d->count - 1; }
QContiguousCacheData * d

◆ malloc()

template<typename T >
QContiguousCacheData * QContiguousCache< T >::malloc ( int  aalloc)
inlineprivate

Definition at line 287 of file qcontiguouscache.h.

288 {
289  return QContiguousCacheData::allocate(sizeOfTypedData() + (aalloc - 1) * sizeof(T), alignOfTypedData());
290 }
int alignOfTypedData() const
static QContiguousCacheData * allocate(int size, int alignment)

◆ normalizeIndexes()

template<typename T>
void QContiguousCache< T >::normalizeIndexes ( )
inline

Moves the first index and last index of the cache such that they point to valid indexes.

The function does not modify the contents of the cache or the ordering of elements within the cache.

It is provided so that index overflows can be corrected when using the cache as a circular buffer.

cache.insert(INT_MAX, 1); // cache contains one value and has valid indexes, INT_MAX to INT_MAX
cache.append(2); // cache contains two values but does not have valid indexes.
cache.normalizeIndexes(); // cache has two values, 1 and 2. New first index will be in the range of 0 to capacity().
See also
areIndexesValid(), append(), prepend()

Definition at line 157 of file qcontiguouscache.h.

157 { d->offset = d->start; }
QContiguousCacheData * d

◆ operator!=()

template<typename T>
bool QContiguousCache< T >::operator!= ( const QContiguousCache< T > &  other) const
inline

Returns true if other is not equal to this cache; otherwise returns false.

Two caches are considered equal if they contain the same values at the same indexes. This function requires the value type to implement the operator==().

See also
operator==()

Definition at line 119 of file qcontiguouscache.h.

119 { return !(*this == other); }

◆ operator=()

template<typename T >
QContiguousCache< T > & QContiguousCache< T >::operator= ( const QContiguousCache< T > &  other)

Assigns other to this cache and returns a reference to this cache.

Definition at line 303 of file qcontiguouscache.h.

304 {
305  other.d->ref.ref();
306  if (!d->ref.deref())
307  free(d);
308  d = other.d;
309  if (!d->sharable)
310  detach_helper();
311  return *this;
312 }
QContiguousCacheData * d
QBasicAtomicInt ref
void free(Data *x)

◆ operator==()

template<typename T >
bool QContiguousCache< T >::operator== ( const QContiguousCache< T > &  other) const

Returns true if other is equal to this cache; otherwise returns false.

Two caches are considered equal if they contain the same values at the same indexes. This function requires the value type to implement the operator==().

See also
operator!=()

Definition at line 315 of file qcontiguouscache.h.

316 {
317  if (other.d == d)
318  return true;
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)
323  return false;
324  for (int i = firstIndex(); i <= lastIndex(); ++i)
325  if (!(at(i) == other.at(i)))
326  return false;
327  return true;
328 }
QContiguousCacheData * d
const T & at(int pos) const
Returns the item at index position i in the cache.
int firstIndex() const
Returns the first valid index in the cache.
int lastIndex() const
Returns the last valid index in the cache.

◆ operator[]() [1/2]

template<typename T >
T & QContiguousCache< T >::operator[] ( int  i)
inline

Returns the item at index position i as a modifiable reference.

If the cache does not contain an item at the given index position i then it will first insert an empty item at that position.

In most cases it is better to use either at() or insert().

Note
This non-const overload of operator[] requires QContiguousCache to make a deep copy. Use at() for read-only access to a non-const QContiguousCache.
See also
insert(), at()

Definition at line 432 of file qcontiguouscache.h.

433 {
434  detach();
435  if (!containsIndex(pos))
436  insert(pos, T());
437  return p->array[pos % d->alloc];
438 }
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p
void insert(int pos, const T &value)
Inserts the value at the index position i.
bool containsIndex(int pos) const
Returns true if the cache&#39;s index range includes the given index i.

◆ operator[]() [2/2]

template<typename T >
const T & QContiguousCache< T >::operator[] ( int  i) const
inline

Same as at(i).

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 428 of file qcontiguouscache.h.

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]; }
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837

◆ prepend()

template<typename T >
void QContiguousCache< T >::prepend ( const T &  value)

Inserts value at the start of the cache.

If the cache is already full the item at the end of the cache will be removed.

See also
append(), insert(), isFull()

Definition at line 370 of file qcontiguouscache.h.

371 {
372  if (!d->alloc)
373  return; // zero capacity
374  detach();
375  if (d->start)
376  d->start--;
377  else
378  d->start = d->alloc-1;
379  d->offset--;
380 
381  if (d->count != d->alloc)
382  d->count++;
383  else
384  if (d->count == d->alloc)
385  (p->array + d->start)->~T();
386 
388  new (p->array + d->start) T(value);
389  else
390  p->array[d->start] = value;
391 }
QContiguousCacheData * d
QContiguousCacheTypedData< T > * p

◆ removeFirst()

template<typename T >
void QContiguousCache< T >::removeFirst ( )
inline

Removes the first item from the cache.

This function assumes that the cache isn't empty.

See also
removeLast()

Definition at line 441 of file qcontiguouscache.h.

442 {
443  Q_ASSERT(d->count > 0);
444  detach();
445  d->count--;
447  (p->array + d->start)->~T();
448  d->start = (d->start + 1) % d->alloc;
449  d->offset++;
450 }
QContiguousCacheData * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QContiguousCacheTypedData< T > * p

◆ removeLast()

template<typename T >
void QContiguousCache< T >::removeLast ( )
inline

Removes the last item from the cache.

This function assumes that the cache isn't empty.

See also
removeFirst()

Definition at line 453 of file qcontiguouscache.h.

454 {
455  Q_ASSERT(d->count > 0);
456  detach();
457  d->count--;
459  (p->array + (d->start + d->count) % d->alloc)->~T();
460 }
QContiguousCacheData * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QContiguousCacheTypedData< T > * p

◆ setCapacity()

template<typename T >
void QContiguousCache< T >::setCapacity ( int  size)

Sets the capacity of the cache to the given size.

A cache can hold a number of items equal to its capacity. When inserting, appending or prepending items to the cache, if the cache is already full then the item farthest from the added item will be removed.

If the given size is smaller than the current count of items in the cache then only the last size items from the cache will remain.

See also
capacity(), isFull()

Definition at line 219 of file qcontiguouscache.h.

220 {
221  if (asize == d->alloc)
222  return;
223  detach();
225  x.d = malloc(asize);
226  x.d->alloc = asize;
227  x.d->count = qMin(d->count, asize);
228  x.d->offset = d->offset + d->count - x.d->count;
229  if(asize)
230  x.d->start = x.d->offset % x.d->alloc;
231  else
232  x.d->start = 0;
233 
234  int oldcount = x.d->count;
235  if(oldcount)
236  {
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;
239  while (oldcount--) {
241  new (dest) T(*src);
242  } else {
243  *dest = *src;
244  }
245  if (dest == x.p->array)
246  dest = x.p->array + x.d->alloc;
247  dest--;
248  if (src == p->array)
249  src = p->array + d->alloc;
250  src--;
251  }
252  }
253  /* free old */
254  free(p);
255  d = x.d;
256 }
QContiguousCacheData * d
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QContiguousCacheTypedData< T > * p
QContiguousCacheData * malloc(int aalloc)
void free(Data *x)

◆ setSharable()

template<typename T>
void QContiguousCache< T >::setSharable ( bool  sharable)
inline
Warning
This function is not part of the public interface.

Definition at line 110 of file qcontiguouscache.h.

110 { if (!sharable) detach(); d->sharable = sharable; }
QContiguousCacheData * d

◆ size()

template<typename T>
int QContiguousCache< T >::size ( ) const
inline

Returns the number of items contained within the cache.

See also
capacity()

Definition at line 123 of file qcontiguouscache.h.

123 { return d->count; }
QContiguousCacheData * d

◆ sizeOfTypedData()

template<typename T>
int QContiguousCache< T >::sizeOfTypedData ( )
inlineprivate

Definition at line 167 of file qcontiguouscache.h.

167  {
168  // this is more or less the same as sizeof(Data), except that it doesn't
169  // count the padding at the end
170  return reinterpret_cast<const char *>(&(reinterpret_cast<const Data *>(this))->array[1]) - reinterpret_cast<const char *>(this);
171  }
QContiguousCacheTypedData< T > Data

◆ swap()

template<typename T>
void QContiguousCache< T >::swap ( QContiguousCache< T > &  other)
inline

Swaps cache other with this cache.

Since
4.8

This operation is very fast and never fails.

Definition at line 117 of file qcontiguouscache.h.

117 { qSwap(d, other.d); }
QContiguousCacheData * d
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181

◆ takeFirst()

template<typename T >
T QContiguousCache< T >::takeFirst ( )
inline

Removes the first item in the cache and returns it.

This function assumes that the cache isn't empty.

If you don't use the return value, removeFirst() is more efficient.

See also
takeLast(), removeFirst()

Definition at line 463 of file qcontiguouscache.h.

464 { T t = first(); removeFirst(); return t; }
void removeFirst()
Removes the first item from the cache.
const T & first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...

◆ takeLast()

template<typename T >
T QContiguousCache< T >::takeLast ( )
inline

Removes the last item in the cache and returns it.

This function assumes that the cache isn't empty.

If you don't use the return value, removeLast() is more efficient.

See also
takeFirst(), removeLast()

Definition at line 467 of file qcontiguouscache.h.

468 { T t = last(); removeLast(); return t; }
void removeLast()
Removes the last item from the cache.
const T & last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...

Properties

◆ @49

union { ... }

◆ d

template<typename T>
QContiguousCacheData* QContiguousCache< T >::d

◆ p

template<typename T>
QContiguousCacheTypedData<T>* QContiguousCache< T >::p

Definition at line 92 of file qcontiguouscache.h.


The documentation for this class was generated from the following files: