QContiguousCache 类是提供连续缓存的模板类。 更多...
头: | #include <QContiguousCache> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
注意: 此类的所有函数 可重入 .
QContiguousCache (const QContiguousCache<T> & other ) | |
QContiguousCache (qsizetype capacity = 0) | |
QContiguousCache<T> & | operator= (QContiguousCache<T> && other ) |
QContiguousCache<T> & | operator= (const QContiguousCache<T> & other ) |
~QContiguousCache () | |
void | append (const T & value ) |
bool | areIndexesValid () const |
const T & | at (qsizetype i ) const |
qsizetype | available () const |
qsizetype | capacity () const |
void | clear () |
bool | containsIndex (qsizetype i ) const |
qsizetype | count () const |
T & | first () |
const T & | first () const |
qsizetype | firstIndex () const |
void | insert (qsizetype i , const T & value ) |
bool | isEmpty () const |
bool | isFull () const |
T & | last () |
const T & | last () const |
qsizetype | lastIndex () const |
void | normalizeIndexes () |
void | prepend (const T & value ) |
void | removeFirst () |
void | removeLast () |
void | setCapacity (qsizetype size ) |
qsizetype | size () const |
void | swap (QContiguousCache<T> & other ) |
T | takeFirst () |
T | takeLast () |
bool | operator!= (const QContiguousCache<T> & other ) const |
bool | operator== (const QContiguousCache<T> & other ) const |
T & | operator[] (qsizetype i ) |
const T & | operator[] (qsizetype i ) const |
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 () 和 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 () 和 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 () 或 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.
见 Contiguous Cache 范例。
构造副本为 other .
此操作花费 常量时间 , because QContiguousCache is 隐式共享 . 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 线性时间 .
另请参阅 operator= ().
Constructs a cache with the given capacity .
另请参阅 setCapacity ().
[since 5.2]
QContiguousCache
<
T
> &QContiguousCache::
operator=
(
QContiguousCache
<
T
> &&
other
)
移动赋值 other 到此 QContiguousCache 实例。
该函数在 Qt 5.2 引入。
赋值 other to this cache and returns a reference to this cache.
销毁缓存。
插入 value at the end of the cache. If the cache is already full the item at the start of the cache will be removed.
另请参阅 prepend (), insert (),和 isFull ().
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 normalizeIndexes ().
另请参阅 normalizeIndexes (), append (),和 prepend ().
返回项按索引位置 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
另请参阅 firstIndex (), lastIndex (), insert (),和 operator[] ().
Returns the number of items that can be added to the cache before it becomes full.
另请参阅 size (), capacity (),和 isFull ().
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.
另请参阅 setCapacity () 和 size ().
Removes all items from the cache. The capacity is unchanged.
返回
true
if the cache's index range includes the given index
i
.
另请参阅 firstIndex () 和 lastIndex ().
如同 size ().
Returns a reference to the first item in the cache. This function assumes that the cache isn't empty.
这是重载函数。
Returns the first valid index in the cache. The index will be invalid if the cache is empty.
另请参阅 capacity (), size (),和 lastIndex ().
插入 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.
另请参阅 prepend (), append (), isFull (), firstIndex (),和 lastIndex ().
返回
true
if no items are stored within the cache.
返回
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.
这是重载函数。
Returns the last valid index in the cache. The index will be invalid if the cache is empty.
另请参阅 capacity (), size (),和 firstIndex ().
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_MAX cache.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().
另请参阅 areIndexesValid (), append (),和 prepend ().
插入 value at the start of the cache. If the cache is already full the item at the end of the cache will be removed.
另请参阅 append (), insert (),和 isFull ().
Removes the first item from the cache. This function assumes that the cache isn't empty.
另请参阅 removeLast ().
Removes the last item from the cache. This function assumes that the cache isn't empty.
另请参阅 removeFirst ().
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.
若给定 size is smaller than the current count of items in the cache then only the last size items from the cache will remain.
Returns the number of items contained within the cache.
另请参阅 capacity ().
Swaps cache other with this cache. This operation is very fast and never fails.
Removes the first item in the cache and returns it. This function assumes that the cache isn't empty.
若不使用返回值, removeFirst () 效率更高。
另请参阅 takeLast () 和 removeFirst ().
Removes the last item in the cache and returns it. This function assumes that the cache isn't empty.
若不使用返回值, removeLast () 效率更高。
另请参阅 takeFirst () 和 removeLast ().
返回
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==()
.
另请参阅 operator== ().
返回
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==()
.
另请参阅 operator!= ().
返回项按索引位置 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 () 或 insert ().
注意: This non-const overload of operator[] requires QContiguousCache to make a deep copy. Use at () for read-only access to a non-const QContiguousCache .
这是重载函数。
如同 at( i ).