Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QContiguousCache< T > Class Template Reference

\inmodule QtCore More...

#include <qcontiguouscache.h>

+ Collaboration diagram for QContiguousCache< T >:

Public Types

typedef T value_type
 
typedef value_typepointer
 
typedef const value_typeconst_pointer
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef qptrdiff difference_type
 
typedef qsizetype size_type
 

Public Member Functions

 QContiguousCache (qsizetype capacity=0)
 Constructs a cache with the given capacity.
 
 QContiguousCache (const QContiguousCache< T > &v)
 Constructs a copy of other.
 
 ~QContiguousCache ()
 Destroys the cache.
 
void detach ()
 
bool isDetached () const
 
QContiguousCache< T > & operator= (const QContiguousCache< T > &other)
 Assigns other to this cache and returns a reference to this cache.
 
void swap (QContiguousCache &other) noexcept
 
template<typename U = T>
QTypeTraits::compare_eq_result< U > operator== (const QContiguousCache< T > &other) const
 Returns true if other is equal to this cache; otherwise returns false.
 
template<typename U = T>
QTypeTraits::compare_eq_result< U > operator!= (const QContiguousCache< T > &other) const
 Returns true if other is not equal to this cache; otherwise returns false.
 
qsizetype capacity () const
 Returns the number of items the cache can store before it is full.
 
qsizetype count () const
 Same as size().
 
qsizetype size () const
 Returns the number of items contained within the cache.
 
bool isEmpty () const
 Returns true if no items are stored within the cache.
 
bool isFull () const
 Returns true if the number of items stored within the cache is equal to the capacity of the cache.
 
qsizetype available () const
 Returns the number of items that can be added to the cache before it becomes full.
 
void clear ()
 Removes all items from the cache.
 
void setCapacity (qsizetype size)
 Sets the capacity of the cache to the given size.
 
const T & at (qsizetype pos) const
 Returns the item at index position i in the cache.
 
T & operator[] (qsizetype i)
 Returns the item at index position i as a modifiable reference.
 
const T & operator[] (qsizetype i) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as at(i).
 
void append (T &&value)
 
void append (const T &value)
 Inserts value at the end of the cache.
 
void prepend (T &&value)
 
void prepend (const T &value)
 Inserts value at the start of the cache.
 
void insert (qsizetype pos, T &&value)
 
void insert (qsizetype pos, const T &value)
 Inserts the value at the index position i.
 
bool containsIndex (qsizetype pos) const
 Returns true if the cache's index range includes the given index i.
 
qsizetype firstIndex () const
 Returns the first valid index in the cache.
 
qsizetype lastIndex () const
 Returns the last valid index in the cache.
 
const T & first () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const T & last () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
T & first ()
 Returns a reference to the first item in the cache.
 
T & last ()
 Returns a reference to the last item in the cache.
 
void removeFirst ()
 Removes the first item from the cache.
 
takeFirst ()
 Removes the first item in the cache and returns it.
 
void removeLast ()
 Removes the last item from the cache.
 
takeLast ()
 Removes the last item in the cache and returns it.
 
bool areIndexesValid () const
 Returns whether the indexes for items stored in the cache are valid.
 
void normalizeIndexes ()
 Moves the first index and last index of the cache such that they point to valid indexes.
 

Detailed Description

template<typename T>
class QContiguousCache< T >

\inmodule QtCore

The QContiguousCache class is a template class that provides a contiguous cache.

\reentrant

Since
4.6

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() 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().

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() or \l{QContiguousCache::operator[]()}{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.

See the \l{Contiguous Cache Example}{Contiguous Cache} example.

Definition at line 47 of file qcontiguouscache.h.

Member Typedef Documentation

◆ const_pointer

template<typename T >
const value_type* QContiguousCache< T >::const_pointer

Definition at line 56 of file qcontiguouscache.h.

◆ const_reference

template<typename T >
const value_type& QContiguousCache< T >::const_reference

Definition at line 58 of file qcontiguouscache.h.

◆ difference_type

template<typename T >
qptrdiff QContiguousCache< T >::difference_type

Definition at line 59 of file qcontiguouscache.h.

◆ pointer

template<typename T >
value_type* QContiguousCache< T >::pointer

Definition at line 55 of file qcontiguouscache.h.

◆ reference

template<typename T >
value_type& QContiguousCache< T >::reference

Definition at line 57 of file qcontiguouscache.h.

◆ size_type

template<typename T >
qsizetype QContiguousCache< T >::size_type

Definition at line 60 of file qcontiguouscache.h.

◆ value_type

template<typename T >
T QContiguousCache< T >::value_type

Definition at line 54 of file qcontiguouscache.h.

Constructor & Destructor Documentation

◆ QContiguousCache() [1/2]

template<typename T >
QContiguousCache< T >::QContiguousCache ( qsizetype capacity = 0)
explicit

Constructs a cache with the given capacity.

See also
setCapacity()

Definition at line 250 of file qcontiguouscache.h.

◆ QContiguousCache() [2/2]

template<typename T >
QContiguousCache< T >::QContiguousCache ( const QContiguousCache< T > & other)
inline

Constructs a copy of other.

This operation takes \l{constant time}, because QContiguousCache is \l{implicitly shared}. 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 \l{linear time}.

See also
operator=()

Definition at line 63 of file qcontiguouscache.h.

◆ ~QContiguousCache()

template<typename T >
QContiguousCache< T >::~QContiguousCache ( )
inline

Destroys the cache.

Definition at line 65 of file qcontiguouscache.h.

Member Function Documentation

◆ append() [1/2]

template<typename T >
void QContiguousCache< T >::append ( const T & value)

Inserts value at the end of the cache.

If the cache is already full the item at the start of the cache will be removed.

See also
prepend(), insert(), isFull()

Definition at line 305 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ append() [2/2]

template<typename T >
void QContiguousCache< T >::append ( T && value)

Definition at line 286 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ areIndexesValid()

template<typename T >
bool QContiguousCache< T >::areIndexesValid ( ) const
inline

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().

See also
normalizeIndexes(), append(), prepend()

Definition at line 136 of file qcontiguouscache.h.

◆ at()

template<typename T >
const T & QContiguousCache< T >::at ( qsizetype i) const
inline

Returns the item at index position 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

See also
firstIndex(), lastIndex(), insert(), operator[]()

Definition at line 392 of file qcontiguouscache.h.

◆ available()

template<typename T >
qsizetype QContiguousCache< T >::available ( ) const
inline

Returns the number of items that can be added to the cache before it becomes full.

See also
size(), capacity(), isFull()

Definition at line 104 of file qcontiguouscache.h.

◆ capacity()

template<typename T >
qsizetype QContiguousCache< T >::capacity ( ) const
inline

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 also
setCapacity(), size()

Definition at line 98 of file qcontiguouscache.h.

◆ clear()

template<typename T >
void QContiguousCache< T >::clear ( )

Removes all items from the cache.

The capacity is unchanged.

Definition at line 217 of file qcontiguouscache.h.

◆ containsIndex()

template<typename T >
bool QContiguousCache< T >::containsIndex ( qsizetype i) const
inline

Returns true if the cache's index range includes the given index i.

See also
firstIndex(), lastIndex()

Definition at line 121 of file qcontiguouscache.h.

◆ count()

template<typename T >
qsizetype QContiguousCache< T >::count ( ) const
inline

Same as size().

Definition at line 99 of file qcontiguouscache.h.

◆ detach()

◆ first() [1/2]

template<typename T >
T & QContiguousCache< T >::first ( )
inline

Returns a reference to the first item in the cache.

This function assumes that the cache isn't empty.

See also
last(), isEmpty()

Definition at line 127 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ first() [2/2]

template<typename T >
const T & QContiguousCache< T >::first ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 125 of file qcontiguouscache.h.

◆ firstIndex()

template<typename T >
qsizetype QContiguousCache< T >::firstIndex ( ) const
inline

Returns the first valid index in the cache.

The index will be invalid if the cache is empty.

See also
capacity(), size(), lastIndex()

Definition at line 122 of file qcontiguouscache.h.

◆ insert() [1/2]

template<typename T >
void QContiguousCache< T >::insert ( qsizetype i,
const T & value )

Inserts the 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.

See also
prepend(), append(), isFull(), firstIndex(), lastIndex()

Definition at line 387 of file qcontiguouscache.h.

◆ insert() [2/2]

template<typename T >
void QContiguousCache< T >::insert ( qsizetype pos,
T && value )

Definition at line 364 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ isDetached()

template<typename T >
bool QContiguousCache< T >::isDetached ( ) const
inline

Definition at line 68 of file qcontiguouscache.h.

◆ isEmpty()

template<typename T >
bool QContiguousCache< T >::isEmpty ( ) const
inline

Returns true if no items are stored within the cache.

See also
size(), capacity()

Definition at line 102 of file qcontiguouscache.h.

◆ isFull()

template<typename T >
bool QContiguousCache< T >::isFull ( ) const
inline

Returns true if the number of items stored within the cache is equal to the capacity of the cache.

See also
size(), capacity()

Definition at line 103 of file qcontiguouscache.h.

◆ last() [1/2]

template<typename T >
T & QContiguousCache< T >::last ( )
inline

Returns a reference to the last item in the cache.

This function assumes that the cache isn't empty.

See also
first(), isEmpty()

Definition at line 128 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ last() [2/2]

template<typename T >
const T & QContiguousCache< T >::last ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 126 of file qcontiguouscache.h.

◆ lastIndex()

template<typename T >
qsizetype QContiguousCache< T >::lastIndex ( ) const
inline

Returns the last valid index in the cache.

The index will be invalid if the cache is empty.

See also
capacity(), size(), firstIndex()

Definition at line 123 of file qcontiguouscache.h.

◆ normalizeIndexes()

template<typename T >
void QContiguousCache< T >::normalizeIndexes ( )
inline

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.

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().
\inmodule QtCore
QCache< int, Employee > cache
[0]
GLenum GLenum GLsizei void * row
MyRecord record(int row) const
[0]
See also
areIndexesValid(), append(), prepend()

Definition at line 139 of file qcontiguouscache.h.

◆ operator!=()

template<typename T >
template<typename U = T>
QTypeTraits::compare_eq_result< U > QContiguousCache< T >::operator!= ( const QContiguousCache< T > & other) const
inline

Returns 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==().

See also
operator==()

Definition at line 91 of file qcontiguouscache.h.

◆ operator=()

template<typename T >
QContiguousCache< T > & QContiguousCache< T >::operator= ( const QContiguousCache< T > & other)

Assigns other to this cache and returns a reference to this cache.

Move-assigns other to this QContiguousCache instance.

Since
5.2

Definition at line 260 of file qcontiguouscache.h.

◆ operator==()

template<typename T >
template<typename U = T>
QTypeTraits::compare_eq_result< U > QContiguousCache< T >::operator== ( const QContiguousCache< T > & other) const
inline

Returns 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==().

See also
operator!=()

Definition at line 76 of file qcontiguouscache.h.

◆ operator[]() [1/2]

template<typename T >
T & QContiguousCache< T >::operator[] ( qsizetype i)
inline

Returns the item at index position 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() or insert().

Note
This non-const overload of operator[] requires QContiguousCache to make a deep copy. Use at() for read-only access to a non-const QContiguousCache.
See also
insert(), at()

Definition at line 399 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ operator[]() [2/2]

template<typename T >
const T & QContiguousCache< T >::operator[] ( qsizetype i) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as at(i).

Definition at line 395 of file qcontiguouscache.h.

◆ prepend() [1/2]

template<typename T >
void QContiguousCache< T >::prepend ( const T & value)

Inserts value at the start of the cache.

If the cache is already full the item at the end of the cache will be removed.

See also
append(), insert(), isFull()

Definition at line 344 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ prepend() [2/2]

template<typename T >
void QContiguousCache< T >::prepend ( T && value)

Definition at line 324 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ removeFirst()

template<typename T >
void QContiguousCache< T >::removeFirst ( )
inline

Removes the first item from the cache.

This function assumes that the cache isn't empty.

See also
removeLast()

Definition at line 408 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

Referenced by QContiguousCache< T >::takeFirst().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeLast()

template<typename T >
void QContiguousCache< T >::removeLast ( )
inline

Removes the last item from the cache.

This function assumes that the cache isn't empty.

See also
removeFirst()

Definition at line 420 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

Referenced by QContiguousCache< T >::takeLast().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCapacity()

template<typename T >
void QContiguousCache< T >::setCapacity ( qsizetype size)

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.

If the given size is smaller than the current count of items in the cache then only the last size items from the cache will remain.

See also
capacity(), isFull()

Definition at line 180 of file qcontiguouscache.h.

References QContiguousCache< T >::detach().

+ Here is the call graph for this function:

◆ size()

template<typename T >
qsizetype QContiguousCache< T >::size ( ) const
inline

Returns the number of items contained within the cache.

See also
capacity()

Definition at line 100 of file qcontiguouscache.h.

◆ swap()

template<typename T >
void QContiguousCache< T >::swap ( QContiguousCache< T > & other)
inlinenoexcept
Since
4.8 \memberswap{cache}

Definition at line 72 of file qcontiguouscache.h.

◆ takeFirst()

template<typename T >
T QContiguousCache< T >::takeFirst ( )
inline

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 also
takeLast(), removeFirst()

Definition at line 430 of file qcontiguouscache.h.

References QContiguousCache< T >::removeFirst().

+ Here is the call graph for this function:

◆ takeLast()

template<typename T >
T QContiguousCache< T >::takeLast ( )
inline

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 also
takeFirst(), removeLast()

Definition at line 434 of file qcontiguouscache.h.

References QContiguousCache< T >::removeLast().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: