
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQContiguousCache class is a template class that provides a contiguous cache.More...
| Header: | #include <QContiguousCache> |
| Since: | Qt 4.6 |
Note: All functions in this class arereentrant.
| QContiguousCache(int capacity = 0) | |
| QContiguousCache(const QContiguousCache<T> & other) | |
| ~QContiguousCache() | |
| void | append(const T & value) |
| bool | areIndexesValid() const |
| const T & | at(int i) const |
| int | available() const |
| int | capacity() const |
| void | clear() |
| bool | containsIndex(int i) const |
| int | count() const |
| T & | first() |
| const T & | first() const |
| int | firstIndex() const |
| void | insert(int i, const T & value) |
| bool | isEmpty() const |
| bool | isFull() const |
| T & | last() |
| const T & | last() const |
| int | lastIndex() const |
| void | normalizeIndexes() |
| 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() |
| 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 |
TheQContiguousCache class is a template class that provides a contiguous cache.
TheQContiguousCache class provides an efficient way of caching items for display in a user interface view. UnlikeQCache, 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 thanQCache. TheQContiguousCache class also can provide an upper bound on memory usage viasetCapacity().
The simplest way of using a contiguous cache is to use theappend() andprepend().
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 theinsert() 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 usinginsert() in order to avoid unwanted clearing of the cache.
The range of valid indexes for theQContiguousCache class are from 0 to INT_MAX. Callingprepend() such that the first index would become less than 0 orappend() 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 callnormalizeIndexes() before calling any ofcontainsIndex(),firstIndex(),lastIndex(),at() oroperator[](). Calling these functions when the cache has invalid indexes will result in undefined behavior. The indexes can be checked by usingareIndexesValid()
In most cases the indexes will not exceed 0 to INT_MAX, andnormalizeIndexes() will not need to be used.
See theContiguous Cache example.
Constructs a cache with the givencapacity.
See alsosetCapacity().
Constructs a copy ofother.
This operation takesconstant time, becauseQContiguousCache isimplicitly shared. This makes returning aQContiguousCache from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takeslinear time.
See alsooperator=().
Destroys the cache.
Insertsvalue at the end of the cache. If the cache is already full the item at the start of the cache will be removed.
See alsoprepend(),insert(), andisFull().
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 alsonormalizeIndexes(),append(), andprepend().
Returns the item at index positioni 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 alsofirstIndex(),lastIndex(),insert(), andoperator[]().
Returns the number of items that can be added to the cache before it becomes full.
See alsosize(),capacity(), andisFull().
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 alsosetCapacity() andsize().
Removes all items from the cache. The capacity is unchanged.
Returns true if the cache's index range includes the given indexi.
See alsofirstIndex() andlastIndex().
Same assize().
Returns a reference to the first item in the cache. This function assumes that the cache isn't empty.
This is an overloaded function.
Returns the first valid index in the cache. The index will be invalid if the cache is empty.
See alsocapacity(),size(), andlastIndex().
Inserts thevalue at the index positioni. If the cache already contains an item ati then that value is replaced. Ifi is either one more thanlastIndex() or one less thanfirstIndex() it is the equivalent to anappend() or aprepend().
If the given indexi 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 theQContiguousCache class are from 0 to INT_MAX. Inserting outside of this range has undefined behavior.
See alsoprepend(),append(),isFull(),firstIndex(), andlastIndex().
Returns true if no items are stored within the cache.
Returns true if the number of items stored within the cache is equal to the capacity of the cache.
Returns a reference to the last item in the cache. This function assumes that the cache isn't empty.
This is an overloaded function.
Returns the last valid index in the cache. The index will be invalid if the cache is empty.
See alsocapacity(),size(), andfirstIndex().
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.
QContiguousCache<int> cache(10);cache.insert(INT_MAX,1);// cache contains one value and has valid indexes, INT_MAX to INT_MAXcache.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 alsoareIndexesValid(),append(), andprepend().
Insertsvalue at the start of the cache. If the cache is already full the item at the end of the cache will be removed.
See alsoappend(),insert(), andisFull().
Removes the first item from the cache. This function assumes that the cache isn't empty.
See alsoremoveLast().
Removes the last item from the cache. This function assumes that the cache isn't empty.
See alsoremoveFirst().
Sets the capacity of the cache to the givensize. 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 givensize is smaller than the current count of items in the cache then only the lastsize items from the cache will remain.
See alsocapacity() andisFull().
Returns the number of items contained within the cache.
See alsocapacity().
Swaps cacheother with this cache. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
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 alsotakeLast() andremoveFirst().
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 alsotakeFirst() andremoveLast().
Returns true ifother 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 theoperator==().
See alsooperator==().
Assignsother to this cache and returns a reference to this cache.
Returns true ifother 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 theoperator==().
See alsooperator!=().
Returns the item at index positioni as a modifiable reference. If the cache does not contain an item at the given index positioni then it will first insert an empty item at that position.
In most cases it is better to use eitherat() orinsert().
Note:This non-const overload of operator[] requiresQContiguousCache to make a deep copy. Useat() for read-only access to a non-constQContiguousCache.
This is an overloaded function.
Same as at(i).
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.