本页内容

QContiguousCache Class

template <typename T> class QContiguousCache

QContiguousCache 类是一个提供连续缓存的模板类。更多内容...

头文件: #include <QContiguousCache>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入的。

公共函数

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 类提供了一种高效的方法,用于缓存T 类型的项目,以便在用户界面视图中显示。 与QCache 不同,它增加了一项限制,即缓存中的元素必须是连续的。这样做的好处是,它符合用户界面视图最常见的请求数据方式,即围绕当前滚动位置本地化的一组行。与QCache 相比,此限制使缓存消耗的内存和处理器周期更少。

QContiguousCache 采用固定容量,可通过setCapacity() 设置,或作为参数传递给构造函数。该容量是缓存本身内存使用的上限,不包括元素自身分配的内存。 请注意,容量为零(默认值)的缓存意味着不会存储任何项目:insert()、append() 和prepend() 操作实际上将不执行任何操作。因此,在向缓存添加项目之前,将容量设置为一个合理的值非常重要。

使用连续缓存的最简单方法是调用append() 和prepend()。

MyRecord record(int row)
{
    Q_ASSERT(row >= 0 && row < cache.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,可能会导致缓存索引失效。 当缓存索引无效时,在调用任何以下函数之前,务必先调用normalizeIndexes():containsIndex()、firstIndex()、lastIndex()、at() 或operator[]()。若在缓存索引无效时调用这些函数,将导致未定义行为。可通过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()

返回对缓存中第一个项的引用。该函数假设缓存不为空。

另请参阅 last() 和isEmpty()。

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()大1或比firstIndex()小1,则该操作等同于append()或prepend()。

如果给定的索引i 既不在缓存的当前范围内,也不与缓存索引范围的边界相邻,则在插入项之前会先清空缓存。此时缓存的大小将为 1。值得花些功夫将项按从缓存当前索引范围相邻处开始的顺序进行插入。

QContiguousCache 类的有效索引范围为0到INT_MAX。在此范围之外插入将导致未定义行为。

另请参阅 prepend()、append()、isFull()、firstIndex() 和lastIndex()。

bool QContiguousCache::isEmpty() const

如果缓存中未存储任何项目,则返回 `true `。

另请参阅 size() 和capacity()。

bool QContiguousCache::isFull() const

如果缓存中存储的项目数量等于缓存的容量,则返回true 。

另请参阅 size() 和capacity()。

T &QContiguousCache::last()

返回缓存中最后一个项目的引用。该函数假设缓存不为空。

另请参阅 first() 和isEmpty()。

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 个项。

另请参阅 capacity() 和isFull()。

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)

将other 通过Move操作赋值给此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 处的项目,则会先在该位置插入一个空项目。

在大多数情况下,最好使用at() 或insert()。

注意:此 非 const 重载的 [] 运算符需要QContiguousCache 进行深度复制。若要对非 constQContiguousCache 进行只读访问,请使用at()。

另请参阅 insert() 和at()。

const T &QContiguousCache::operator[](qsizetype i) const

与 (i) 相同。

这是一个重载函数。

© 2026 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.