Member Function Documentation
QContiguousCache::QContiguousCache ( int capacity = 0 )
Constructs a cache with the given capacity.
See also setCapacity().
QContiguousCache::QContiguousCache ( const QContiguousCache<T> & other )
Constructs a copy of other.
This operation takes constant time, because QContiguousCache is 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 linear time.
See also operator=().
QContiguousCache::~QContiguousCache ()
Destroys the cache.
void QContiguousCache::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(), and isFull().
bool QContiguousCache::areIndexesValid () const
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 normalizeIndexs().
See also normalizeIndexes(), append(), and prepend().
const T & QContiguousCache::at ( int i ) const
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(), and operator[]().
int QContiguousCache::available () const
Returns the number of items that can be added to the cache before it becomes full.
See also size(), capacity(), and isFull().
int QContiguousCache::capacity () const
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() and size().
void QContiguousCache::clear ()
Removes all items from the cache. The capacity is unchanged.
bool QContiguousCache::containsIndex ( int i ) const
Returns true if the cache's index range includes the given index i.
See also firstIndex() and lastIndex().
int QContiguousCache::count () const
Same as size().
T & QContiguousCache::first ()
Returns a reference to the first item in the cache. This function assumes that the cache isn't empty.
See also last() and isEmpty().
const T & QContiguousCache::first () const
This is an overloaded function.
int QContiguousCache::firstIndex () const
Returns the first valid index in the cache. The index will be invalid if the cache is empty.
See also capacity(), size(), and lastIndex().
void QContiguousCache::insert ( int 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(), and lastIndex().
bool QContiguousCache::isEmpty () const
Returns true if no items are stored within the cache.
See also size() and capacity().
bool QContiguousCache::isFull () const
Returns true if the number of items stored within the cache is equal to the capacity of the cache.
See also size() and capacity().
T & QContiguousCache::last ()
Returns a reference to the last item in the cache. This function assumes that the cache isn't empty.
See also first() and isEmpty().
const T & QContiguousCache::last () const
This is an overloaded function.
int QContiguousCache::lastIndex () const
Returns the last valid index in the cache. The index will be invalid if the cache is empty.
See also capacity(), size(), and firstIndex().
void QContiguousCache::normalizeIndexes ()
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.append(2);
cache.normalizeIndexes();
See also areIndexesValid(), append(), and prepend().
void QContiguousCache::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(), and isFull().
void QContiguousCache::removeFirst ()
Removes the first item from the cache. This function assumes that the cache isn't empty.
See also removeLast().
void QContiguousCache::removeLast ()
Removes the last item from the cache. This function assumes that the cache isn't empty.
See also removeFirst().
void QContiguousCache::setCapacity ( int 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() and isFull().
int QContiguousCache::size () const
Returns the number of items contained within the cache.
See also capacity().
void QContiguousCache::swap ( QContiguousCache<T> & other )
Swaps cache other with this cache. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
T QContiguousCache::takeFirst ()
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() and removeFirst().
T QContiguousCache::takeLast ()
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() and removeLast().
bool QContiguousCache::operator!= ( const QContiguousCache<T> & other ) const
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==().
QContiguousCache<T> & QContiguousCache::operator= ( const QContiguousCache<T> & other )
Assigns other to this cache and returns a reference to this cache.
bool QContiguousCache::operator== ( const QContiguousCache<T> & other ) const
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!=().
T & QContiguousCache::operator[] ( int i )
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() and at().
const T & QContiguousCache::operator[] ( int i ) const
This is an overloaded function.
Same as at(i).