CopperSpice API
1.9.2
|
The QContiguousCache class is a container providing a contiguous cache. More...
Public Typedefs | |
using | const_pointer = const value_type * |
using | const_reference = const value_type & |
using | difference_type = qptrdiff |
using | pointer = value_type * |
using | reference = value_type & |
using | size_type = int |
using | value_type = T |
Public Methods | |
QContiguousCache (const QContiguousCache< T > &other) | |
QContiguousCache (int capacity=0) | |
~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 |
T & | first () |
const T & | first () const |
int | firstIndex () const |
void | insert (int pos, const T &value) |
bool | isEmpty () const |
bool | isFull () const |
T & | last () |
const T & | last () const |
int | lastIndex () const |
void | normalizeIndexes () |
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 pos) |
const T & | operator[] (int pos) const |
void | prepend (const T &value) |
void | removeFirst () |
void | removeLast () |
void | setCapacity (int size) |
int | size () const |
void | swap (QContiguousCache< T > &other) |
T | takeFirst () |
T | takeLast () |
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().
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.
QContiguousCache< T >::const_pointer |
Equivalent to const T *.
QContiguousCache< T >::const_reference |
Equivalent to const T &.
QContiguousCache< T >::difference_type |
Equivalent to an integral type used to represent the distance between two elements.
QContiguousCache< T >::pointer |
Equivalent to T *.
QContiguousCache< T >::reference |
Equivalent to T &.
QContiguousCache< T >::size_type |
Equivalent to a signed integer of the appropriate size for your platform.
QContiguousCache< T >::value_type |
Equivalent to T.
|
explicit |
Constructs a cache with the given capacity.
|
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 and that takes linear time.
|
inline |
Destroys the QContiguousCache object.
void QContiguousCache< T >::append | ( | const T & | value | ) |
|
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().
|
inline |
Returns the item at index position pos the cache. The value for pos 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
|
inline |
Returns the number of items that can be added to the cache before it becomes full.
|
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.
void QContiguousCache< T >::clear | ( | ) |
Removes all items from the cache. The capacity is unchanged.
|
inline |
Returns true if the cache's index range includes the given index pos.
|
inline |
Equivalent to calling size().
|
inline |
|
inline |
Returns a reference to the first item in the cache. This function assumes that the cache is not empty.
|
inline |
Returns the first valid index in the cache. The index will be invalid if the cache is empty.
void QContiguousCache< T >::insert | ( | int | pos, |
const T & | value | ||
) |
Inserts the value at the index position pos. If the cache already contains an item at pos then value will be replaced. If pos 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 pos 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.
|
inline |
Returns true if no items are stored within the cache.
|
inline |
Returns true if the number of items stored within the cache is equal to the capacity of the cache.
|
inline |
|
inline |
Returns a reference to the last item in the cache. This function assumes that the cache is not empty.
|
inline |
Returns the last valid index in the cache. The index will be invalid if the cache is empty.
|
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.
|
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 method requires the type T to support operator==()
.
QContiguousCache< T > & QContiguousCache< T >::operator= | ( | const QContiguousCache< T > & | other | ) |
Copy assigns from other and returns a reference to this object.
|
inline |
Move assigns from other and returns a reference to this object.
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 method requires the type T to support operator==()
.
|
inline |
Returns a reference to the data at index position pos. If the cache does not contain an item at the given index position then it will first insert an empty item at that position.
In most cases it is better to use either at() or insert().
|
inline |
Equivalent to calling at(pos).
void QContiguousCache< T >::prepend | ( | const T & | value | ) |
|
inline |
Removes the first item from the cache. This function assumes that the cache is not empty.
|
inline |
Removes the last item from the cache. This method assumes that the cache is not empty.
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.
|
inline |
Returns the number of items contained within the cache.
|
inline |
Swaps other with this object. This operation is very fast and never fails.
|
inline |
Removes the first item in the cache and returns it. This function assumes that the cache is not empty.
If you do not use the return value, removeFirst() is more efficient.
|
inline |
Removes the last item in the cache and returns it. This function assumes that the cache is not empty.
If you do not use the return value, removeLast() is more efficient.