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().
+ 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() and
+ 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()
and prepend().
template <typename T>
void QContiguousCache<T>::append(const T &value)
{
+ if (!d->alloc)
+ return; // zero capacity
detach();
if (QTypeInfo<T>::isComplex) {
if (d->count == d->alloc)
template<typename T>
void QContiguousCache<T>::prepend(const T &value)
{
+ if (!d->alloc)
+ return; // zero capacity
detach();
if (d->start)
d->start--;
void QContiguousCache<T>::insert(int pos, const T &value)
{
Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache<T>::insert", "index out of range");
+ if (!d->alloc)
+ return; // zero capacity
detach();
if (containsIndex(pos)) {
if (QTypeInfo<T>::isComplex) {
void setCapacity();
void zeroCapacity();
+ void modifyZeroCapacityCache();
};
QTEST_MAIN(tst_QContiguousCache)
QCOMPARE(contiguousCache.capacity(),0);
}
+void tst_QContiguousCache::modifyZeroCapacityCache()
+{
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.insert(0, 42);
+ }
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.insert(1, 42);
+ }
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.append(42);
+ }
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.prepend(42);
+ }
+}
+
#include "tst_qcontiguouscache.moc"