QContiguousCache Class

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

Header: #include <QContiguousCache>
qmake: QT += core
Since: Qt 4.6

Note: All functions in this class are reentrant.

Public Functions

QContiguousCache(int capacity = 0)
QContiguousCache(const QContiguousCache<T> &v)
~QContiguousCache()
void append(const T &value)
bool areIndexesValid() const
const T &at(int pos) const
int available() const
int capacity() const
void clear()
bool containsIndex(int pos) const
int count() const
void detach()
const T &first() const
T &first()
int firstIndex() const
void insert(int pos, const T &value)
bool isDetached() const
bool isEmpty() const
bool isFull() const
const T &last() const
T &last()
int lastIndex() const
void normalizeIndexes()
void prepend(const T &value)
void removeFirst()
void removeLast()
void setCapacity(int size)
void setSharable(bool sharable)
int size() const
void swap(QContiguousCache<T> &other)
T takeFirst()
T takeLast()
bool operator!=(const QContiguousCache<T> &other) const
QContiguousCache<T> &operator=(const QContiguousCache<T> &other)
QContiguousCache<T> &operator=(QContiguousCache<T> &&other)
bool operator==(const QContiguousCache<T> &other) const
T &operator[](int i)
const T &operator[](int i) const

Detailed Description

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

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.

QContiguousCache operates on a fixed capacity, set with setCapacity() or passed as a parameter to the constructor. This capacity is the upper bound on memory usage by the cache itself, not including the memory allocated by the elements themselves. Note that a cache with a capacity of zero (the default) means no items will be stored: the insert(), append() and prepend() operations will effectively be no-ops. Therefore, it's important to set the capacity to a reasonable value before adding items to the cache.

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 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 Contiguous Cache example.

Member Function Documentation

QContiguousCache::QContiguousCache(int capacity = 0)

Default constructs an instance of QContiguousCache.

QContiguousCache::QContiguousCache(const QContiguousCache<T> &v)

Default constructs an instance of QContiguousCache.

QContiguousCache::~QContiguousCache()

Destroys the instance of QContiguousCache.

void QContiguousCache::append(const T &value)

bool QContiguousCache::areIndexesValid() const

const T &QContiguousCache::at(int pos) const

int QContiguousCache::available() const

int QContiguousCache::capacity() const

See also setCapacity().

void QContiguousCache::clear()

bool QContiguousCache::containsIndex(int pos) const

int QContiguousCache::count() const

void QContiguousCache::detach()

const T &QContiguousCache::first() const

T &QContiguousCache::first()

int QContiguousCache::firstIndex() const

void QContiguousCache::insert(int pos, const T &value)

bool QContiguousCache::isDetached() const

bool QContiguousCache::isEmpty() const

bool QContiguousCache::isFull() const

const T &QContiguousCache::last() const

T &QContiguousCache::last()

int QContiguousCache::lastIndex() const

void QContiguousCache::normalizeIndexes()

void QContiguousCache::prepend(const T &value)

void QContiguousCache::removeFirst()

void QContiguousCache::removeLast()

void QContiguousCache::setCapacity(int size)

See also capacity().

void QContiguousCache::setSharable(bool sharable)

int QContiguousCache::size() const

void QContiguousCache::swap(QContiguousCache<T> &other)

T QContiguousCache::takeFirst()

T QContiguousCache::takeLast()

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

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

Copy-assignment operator.

QContiguousCache<T> &QContiguousCache::operator=(QContiguousCache<T> &&other)

Move-assignment operator.

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

T &QContiguousCache::operator[](int i)

const T &QContiguousCache::operator[](int i) const