QContiguousCache Class
template <typename T> class QContiguousCacheQContiguousCache 类是一个提供连续缓存的模板类。更多
Header: | #include <QContiguousCache> |
CMake.QContiguousCache | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
- 所有成员(包括继承成员)的列表
- QContiguousCache 属于隐式共享类。
注意:该类中的所有函数都是可重入的。
公共函数
QContiguousCache(qsizetype capacity = 0) | |
QContiguousCache(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 |
QContiguousCache<T> & | operator=(QContiguousCache<T> &&other) |
QContiguousCache<T> & | operator=(const QContiguousCache<T> &other) |
bool | operator==(const QContiguousCache<T> &other) const |
T & | operator[](qsizetype i) |
const T & | operator[](qsizetype i) const |
详细说明
QContiguousCache 类为在用户界面视图中显示项目提供了一种有效的缓存方式。与QCache 不同,它增加了一个限制,即缓存中的元素必须是连续的。这样做的好处是符合用户界面视图最常见的数据请求方式,即在当前滚动位置周围显示一组行。与QCache 相比,这一限制可使缓存消耗更少的内存和处理器周期。
QContiguousCache 采用固定容量运行,该容量可通过setCapacity() 设置或作为参数传递给构造函数。该容量是缓存本身内存使用量的上限,不包括元素本身分配的内存。需要注意的是,缓存容量为零(默认值)意味着不会存储任何项目:insert(),append() 和prepend() 操作实际上都是无操作。因此,在向缓存中添加项目之前,将容量设置为一个合理的值非常重要。
使用连续缓存的最简单方法是使用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); }
如果缓存已满,那么位于缓存另一端的项目将被移除,而新项目将被添加或预置到缓存中。
如果请求的行与当前缓存项相距甚远,可以使用insert() 函数进一步优化这种用法。如果插入的新项目与当前缓存项目之间存在间隙,则会首先删除现有缓存项目,以保持缓存的连续性。因此,在使用insert() 时必须小心谨慎,以避免不必要的缓存清除。
QContiguousCache 类的有效索引范围是从 0 到 INT_MAX。如果调用prepend() 使第一个索引小于 0,或调用append() 使最后一个索引大于 INT_MAX,都会导致缓存索引无效。当缓存索引无效时,必须在调用containsIndex(),firstIndex(),lastIndex(),at() 或operator[]() 之前调用normalizeIndexes() 。当缓存中的索引无效时调用这些函数将导致未定义的行为。可以使用areIndexesValid() 检查索引。
在大多数情况下,索引不会超过 0 到 INT_MAX,因此无需使用normalizeIndexes()。
请参阅连续缓存示例。
成员函数文档
[explicit]
QContiguousCache::QContiguousCache(qsizetype capacity = 0)
使用给定的capacity 构建缓存。
另请参见 setCapacity().
QContiguousCache::QContiguousCache(const QContiguousCache<T> &other)
构建other 的副本。
由于 QContiguousCache 是隐式共享的,因此该操作耗时不变。这使得从函数返回 QContiguousCache 的速度非常快。如果共享实例被修改,它将被复制(写时复制),这需要线性时间。
另请参见 operator=()。
QContiguousCache::~QContiguousCache()
销毁缓存。
void QContiguousCache::append(const T &value)
在缓存末尾插入value 。如果缓存已满,缓存起始处的项目将被移除。
另请参阅 prepend()、insert() 和isFull()。
bool QContiguousCache::areIndexesValid() const
返回存储在缓存中的项目索引是否有效。如果项目在索引位置 INT_MAX 之后被添加,或在索引位置 0 之前被预添加,索引可能会失效。 这种情况预计只会在使用连续缓存的循环缓冲区类型中出现。索引可以通过调用normalizeIndexes() 重新生效。
另请参见 normalizeIndexes()、append() 和prepend()。
const T &QContiguousCache::at(qsizetype i) const
i i 必须是缓存中有效的索引位置(即 () <= <= () )。firstIndex i lastIndex
缓存中的索引是指该项目与缓存中添加的第一个项目的位置数。也就是说,一个容量为 100 的缓存,如果已经添加了 150 个项目,那么其有效索引范围为 50 至 149。这样就可以根据理论上的无限列表在缓存中插入和检索项目。
另请参见 firstIndex()、lastIndex()、insert() 和operator[]()。
qsizetype QContiguousCache::available() const
返回在缓存满之前可添加到缓存中的项目数。
另请参阅 size()、capacity() 和isFull()。
qsizetype QContiguousCache::capacity() const
返回缓存满之前可存储的项目数。当缓存中的条目数等于其容量时,添加新条目会导致离添加条目最远的条目被移除。
另请参阅 setCapacity() 和size()。
void QContiguousCache::clear()
删除缓存中的所有项目。容量保持不变。
bool QContiguousCache::containsIndex(qsizetype i) const
如果缓存的索引范围包括给定的索引i ,则返回true
。
另请参阅 firstIndex() 和lastIndex()。
qsizetype QContiguousCache::count() const
与size() 相同。
T &QContiguousCache::first()
返回缓存中第一个项目的引用。此函数假定缓存不是空的。
const T &QContiguousCache::first() const
这是一个重载函数。
qsizetype QContiguousCache::firstIndex() const
返回缓存中的第一个有效索引。如果缓存为空,则索引无效。
另请参阅 capacity()、size() 和lastIndex()。
void QContiguousCache::insert(qsizetype i, const T &value)
将value 插入索引位置i 。如果缓存中已包含i ,则该值将被替换。如果i 比lastIndex() 多一个或比firstIndex() 少一个,则相当于append() 或prepend() 。
如果给定的索引i 不在缓存的当前范围内,也不与缓存索引范围的边界相邻,那么在插入项目之前,缓存将首先被清除。此时,缓存的大小为 1。值得注意的是,插入条目的顺序应从缓存当前索引范围的邻近位置开始。
QContiguousCache 类的有效索引范围是从 0 到 INT_MAX。在此范围之外插入会产生未定义的行为。
另请参阅 prepend()、append()、isFull()、firstIndex() 和lastIndex()。
bool QContiguousCache::isEmpty() const
如果缓存中没有存储项目,则返回true
。
bool QContiguousCache::isFull() const
如果缓存中存储的项目数等于缓存容量,则返回true
。
T &QContiguousCache::last()
返回缓存中最后一个项目的引用。此函数假定缓存不是空的。
const T &QContiguousCache::last() const
这是一个重载函数。
qsizetype QContiguousCache::lastIndex() const
返回缓存中最后一个有效索引。如果缓存为空,则索引无效。
另请参阅 capacity()、size() 和firstIndex()。
void QContiguousCache::normalizeIndexes()
移动缓存的第一个索引和最后一个索引,使其指向有效索引。该函数不会修改缓存的内容或缓存中元素的排序。
提供该函数是为了在将缓存用作循环缓冲区时纠正索引溢出。
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()。
void QContiguousCache::prepend(const T &value)
在缓存的起始位置插入value 。如果缓存已满,缓存末尾的项目将被移除。
另请参阅 append()、insert() 和isFull()。
void QContiguousCache::removeFirst()
从缓存中删除第一个项目。此函数假定缓存不是空的。
另请参见 removeLast().
void QContiguousCache::removeLast()
从缓存中删除最后一个项目。此函数假定缓存不是空的。
另请参见 removeFirst().
void QContiguousCache::setCapacity(qsizetype size)
将缓存的容量设置为给定的size 。缓存可容纳的项目数等于其容量。在缓存中插入、追加或预置项目时,如果缓存已满,则会移除离新增项目最远的项目。
如果给定的size 小于缓存中的当前条目数,则只会保留缓存中最后一个size 条目。
qsizetype QContiguousCache::size() const
返回缓存中包含的项目数。
另请参阅 capacity().
[noexcept]
void QContiguousCache::swap(QContiguousCache<T> &other)
将此缓存与other 进行交换。该操作速度非常快,从未出现过故障。
T QContiguousCache::takeFirst()
删除缓存中的第一个项目并返回。该函数假定缓存不是空的。
如果不使用返回值,removeFirst() 的效率更高。
另请参阅 takeLast() 和removeFirst()。
T QContiguousCache::takeLast()
删除缓存中的最后一个项目并返回。该函数假定缓存不是空的。
如果不使用返回值,removeLast() 的效率更高。
另请参阅 takeFirst() 和removeLast()。
bool QContiguousCache::operator!=(const QContiguousCache<T> &other) const
如果other 不等于此缓存,则返回true
;否则返回false
。
如果两个缓存在相同的索引中包含相同的值,则认为这两个缓存相等。此函数需要值类型来实现operator==()
。
另请参阅 operator==() 。
[noexcept]
QContiguousCache<T> &QContiguousCache::operator=(QContiguousCache<T> &&other)
Move-assignsother 到此QContiguousCache 实例。
QContiguousCache<T> &QContiguousCache::operator=(const QContiguousCache<T> &other)
将other 指定为该缓存,并返回对该缓存的引用。
bool QContiguousCache::operator==(const QContiguousCache<T> &other) const
如果other 等于此缓存,则返回true
;否则返回false
。
如果两个缓存在相同的索引中包含相同的值,则认为这两个缓存相等。此函数需要值类型来实现operator==()
。
另请参见 operator!=()。
T &QContiguousCache::operator[](qsizetype i)
以可修改引用的形式返回索引位置i 上的项目。如果缓存中没有位于给定索引位置i 的项目,则会首先在该位置插入一个空项目。
注意: 操作符[]的非const重载要求QContiguousCache 进行深度复制。使用at() 只读访问非constQContiguousCache 。
const T &QContiguousCache::operator[](qsizetype i) const
这是一个重载函数。
与 at(i) 相同。
© 2025 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 the GNU 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.