6.0.0-rc4Source

QContiguousCache Class

template <typename T> class QContiguousCache

The QContiguousCache class is a template class that provides an efficient cache for a contiguous range of indexes. More…

Header#include <QContiguousCache>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QContiguousCache(qsizetype capacity = 0)
qsizetypecapacity() const
qsizetypecount() const
qsizetypesize() const
boolisEmpty() const
boolisFull() const
qsizetypeavailable() const
qsizetypefirstIndex() const
qsizetypelastIndex() const
boolcontainsIndex(qsizetype i) const
boolareIndexesValid() const
voidnormalizeIndexes()
voidsetCapacity(qsizetype c)
voidclear()
voidswap(QContiguousCache &other)
voiddetach()
voidappend(const T &v)
voidprepend(const T &v)
voidinsert(qsizetype pos, const T &v)
const T &at(qsizetype i) const
T &operator[](qsizetype i)
const T &operator[](qsizetype i) const
T &first()
const T &first() const
T &last()
const T &last() const
voidremoveFirst()
voidremoveLast()
TtakeFirst()
TtakeLast()

Detailed Description

QContiguousCache<T> holds a window of at most capacity() items that are addressed by an integer index, such as the visible rows of a very long list. The window covers the indexes firstIndex() through lastIndex(). Appending or prepending items slides the window and evicts items from the opposite end once the cache is full.

QContiguousCache<QString> rows(100);
rows.insert(5000, "row 5000");
rows.append("row 5001");
QString s = rows.at(5001);

See also QCache and QList.

Differences from Qt

QContiguousCache is implemented on top of std::deque and is not implicitly shared, so copying it copies every item, and detach() does nothing.

Member Function Documentation

[explicit] QContiguousCache::QContiguousCache(qsizetype capacity = 0)

Constructs a cache that can hold up to capacity items. The cache is initially empty and its first index is 0.

See also setCapacity().

void QContiguousCache::append(const T &v)

Appends v after the last item. If the cache is full, the first item is removed to make room, and firstIndex() advances. Does nothing if the capacity is 0 or less.

See also prepend() and insert().

bool QContiguousCache::areIndexesValid() const

Returns true if the indexes of the cache are far enough from the limits of qsizetype that further items can be added safely; otherwise returns false.

See also normalizeIndexes().

const T & QContiguousCache::at(qsizetype i) const

Returns a reference to the item at index i. The index must satisfy containsIndex().

qsizetype QContiguousCache::available() const

Returns the number of items that can still be added before the cache is full.

See also isFull() and capacity().

qsizetype QContiguousCache::capacity() const

Returns the maximum number of items the cache can hold.

See also setCapacity() and size().

void QContiguousCache::clear()

Removes all items from the cache and resets the first index to 0.

See also isEmpty().

bool QContiguousCache::containsIndex(qsizetype i) const

Returns true if the cache contains an item at index i; otherwise returns false.

See also firstIndex() and lastIndex().

qsizetype QContiguousCache::count() const

Returns the number of items in the cache; the same as size().

void QContiguousCache::detach()

Does nothing: the cache is never shared.

T & QContiguousCache::first()

Returns a reference to the first item. The cache must not be empty.

See also last() and firstIndex().

const T & QContiguousCache::first() const

This is an overloaded function.

qsizetype QContiguousCache::firstIndex() const

Returns the index of the first item in the cache. The cache covers the indexes from firstIndex() to lastIndex().

See also lastIndex() and containsIndex().

void QContiguousCache::insert(qsizetype pos, const T &v)

Inserts v at index pos. If pos is inside the cache, the item at that index is replaced. If pos is just after the last item or just before the first, the item is appended or prepended. Any other index discards the current contents and starts a new range at pos.

See also append(), prepend() and containsIndex().

bool QContiguousCache::isEmpty() const

Returns true if the cache contains no items; otherwise returns false.

See also size().

bool QContiguousCache::isFull() const

Returns true if the cache holds as many items as its capacity; otherwise returns false.

See also available() and capacity().

T & QContiguousCache::last()

Returns a reference to the last item. The cache must not be empty.

See also first() and lastIndex().

const T & QContiguousCache::last() const

This is an overloaded function.

qsizetype QContiguousCache::lastIndex() const

Returns the index of the last item in the cache. If the cache is empty, the result is firstIndex() - 1.

See also firstIndex() and containsIndex().

void QContiguousCache::normalizeIndexes()

Moves the indexes as close to zero as possible while keeping each item's index modulo capacity() unchanged. Use this when areIndexesValid() returns false.

See also areIndexesValid() and firstIndex().

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

Returns a reference to the item at index i. If the cache does not contain that index, a default-constructed item is inserted there first, as insert() would, which may discard the current contents.

See also at() and insert().

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

This is an overloaded function.

Returns a reference to the item at index i, which must satisfy containsIndex(); the same as at().

void QContiguousCache::prepend(const T &v)

Prepends v before the first item, giving it the index firstIndex() - 1. If the cache is full, the last item is removed to make room. Does nothing if the capacity is 0 or less.

See also append() and insert().

void QContiguousCache::removeFirst()

Removes the first item. The first index advances by one. The cache must not be empty.

See also removeLast() and takeFirst().

void QContiguousCache::removeLast()

Removes the last item. The cache must not be empty.

See also removeFirst() and takeLast().

void QContiguousCache::setCapacity(qsizetype c)

Sets the capacity of the cache to c. If the cache holds more than c items, the items at the front are removed until it fits.

See also capacity().

qsizetype QContiguousCache::size() const

Returns the number of items currently in the cache.

See also capacity() and isEmpty().

void QContiguousCache::swap(QContiguousCache &other)

Swaps this cache with other.

This function was introduced in 4.8.

T QContiguousCache::takeFirst()

Removes the first item and returns it. The cache must not be empty.

See also removeFirst() and takeLast().

T QContiguousCache::takeLast()

Removes the last item and returns it. The cache must not be empty.

See also removeLast() and takeFirst().