Qt 4.8
|
The QContiguousCache class is a template class that provides a contiguous cache. More...
#include <qcontiguouscache.h>
Public Types | |
typedef const value_type * | const_pointer |
typedef const value_type & | const_reference |
typedef qptrdiff | difference_type |
typedef value_type * | pointer |
typedef value_type & | reference |
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... | |
T | takeFirst () |
Removes the first item in the cache and returns it. More... | |
T | 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) |
QContiguousCacheData * | malloc (int aalloc) |
int | sizeOfTypedData () |
Properties | |
union { | |
QContiguousCacheData * d | |
QContiguousCacheTypedData< T > * p | |
}; | |
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. 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 \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.
QContiguousCache< T >::const_pointer |
Definition at line 97 of file qcontiguouscache.h.
QContiguousCache< T >::const_reference |
Definition at line 99 of file qcontiguouscache.h.
|
private |
Definition at line 91 of file qcontiguouscache.h.
QContiguousCache< T >::difference_type |
Definition at line 100 of file qcontiguouscache.h.
QContiguousCache< T >::pointer |
Definition at line 96 of file qcontiguouscache.h.
QContiguousCache< T >::reference |
Definition at line 98 of file qcontiguouscache.h.
QContiguousCache< T >::size_type |
Definition at line 101 of file qcontiguouscache.h.
QContiguousCache< T >::value_type |
Definition at line 95 of file qcontiguouscache.h.
|
explicit |
Constructs a cache with the given capacity.
Definition at line 293 of file qcontiguouscache.h.
|
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.
Definition at line 104 of file qcontiguouscache.h.
|
inline |
|
inlineprivate |
Definition at line 172 of file qcontiguouscache.h.
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.
Definition at line 347 of file qcontiguouscache.h.
|
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().
Definition at line 154 of file qcontiguouscache.h.
|
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
Definition at line 425 of file qcontiguouscache.h.
Referenced by QContiguousCache< T >::operator==().
|
inline |
Returns the number of items that can be added to the cache before it becomes full.
Definition at line 127 of file qcontiguouscache.h.
|
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.
Definition at line 121 of file qcontiguouscache.h.
void QContiguousCache< T >::clear | ( | ) |
Removes all items from the cache.
The capacity is unchanged.
Definition at line 259 of file qcontiguouscache.h.
|
inline |
Returns true if the cache's index range includes the given index i.
Definition at line 140 of file qcontiguouscache.h.
|
inline |
|
inline |
Definition at line 108 of file qcontiguouscache.h.
|
private |
Definition at line 183 of file qcontiguouscache.h.
|
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.
|
inline |
Returns a reference to the first item in the cache.
This function assumes that the cache isn't empty.
Definition at line 146 of file qcontiguouscache.h.
|
inline |
Returns the first valid index in the cache.
The index will be invalid if the cache is empty.
Definition at line 141 of file qcontiguouscache.h.
Referenced by operator<<().
|
private |
Definition at line 331 of file qcontiguouscache.h.
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.
Definition at line 394 of file qcontiguouscache.h.
|
inline |
Definition at line 109 of file qcontiguouscache.h.
|
inline |
Returns true if no items are stored within the cache.
Definition at line 125 of file qcontiguouscache.h.
|
inline |
Returns true if the number of items stored within the cache is equal to the capacity of the cache.
Definition at line 126 of file qcontiguouscache.h.
|
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.
|
inline |
Returns a reference to the last item in the cache.
This function assumes that the cache isn't empty.
Definition at line 147 of file qcontiguouscache.h.
|
inline |
Returns the last valid index in the cache.
The index will be invalid if the cache is empty.
Definition at line 142 of file qcontiguouscache.h.
Referenced by operator<<().
|
inlineprivate |
Definition at line 287 of file qcontiguouscache.h.
|
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.
Definition at line 157 of file qcontiguouscache.h.
|
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==()
.
Definition at line 119 of file qcontiguouscache.h.
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.
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==()
.
Definition at line 315 of file qcontiguouscache.h.
|
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().
Definition at line 432 of file qcontiguouscache.h.
|
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.
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.
Definition at line 370 of file qcontiguouscache.h.
|
inline |
Removes the first item from the cache.
This function assumes that the cache isn't empty.
Definition at line 441 of file qcontiguouscache.h.
|
inline |
Removes the last item from the cache.
This function assumes that the cache isn't empty.
Definition at line 453 of file qcontiguouscache.h.
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.
Definition at line 219 of file qcontiguouscache.h.
|
inline |
Definition at line 110 of file qcontiguouscache.h.
|
inline |
Returns the number of items contained within the cache.
Definition at line 123 of file qcontiguouscache.h.
|
inlineprivate |
Definition at line 167 of file qcontiguouscache.h.
|
inline |
Swaps cache other with this cache.
This operation is very fast and never fails.
Definition at line 117 of file qcontiguouscache.h.
|
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.
Definition at line 463 of file qcontiguouscache.h.
|
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.
Definition at line 467 of file qcontiguouscache.h.
union { ... } |
QContiguousCacheData* QContiguousCache< T >::d |
Definition at line 92 of file qcontiguouscache.h.
Referenced by QContiguousCache< T >::operator=(), QContiguousCache< T >::operator==(), QContiguousCache< T >::setSharable(), and QContiguousCache< T >::swap().
QContiguousCacheTypedData<T>* QContiguousCache< T >::p |
Definition at line 92 of file qcontiguouscache.h.