Home · All Classes · All Functions · Overviews

QContiguousCache Class Reference
[QtCore module]

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

 #include <QContiguousCache>

Note: All functions in this class are reentrant.

This class was introduced in Qt 4.6.


Public Functions

QContiguousCache ( int capacity = 0 )
QContiguousCache ( const QContiguousCache<T> & other )
~QContiguousCache ()
void append ( const T & value )
bool areIndexesValid () const
const T & at ( int i ) const
int available () const
int capacity () const
void clear ()
bool containsIndex ( int i ) const
int count () const
T & first ()
const T & first () const
int firstIndex () const
void insert ( int i, const T & value )
bool isEmpty () const
bool isFull () const
T & last ()
const T & last () const
int lastIndex () const
void normalizeIndexes ()
void prepend ( const T & value )
void removeFirst ()
void removeLast ()
void setCapacity ( int size )
int size () const
T takeFirst ()
T takeLast ()
bool operator!= ( const QContiguousCache<T> & other ) const
QContiguousCache<T> & operator= ( const QContiguousCache<T> & other )
bool operator== ( const QContiguousCache<T> & other ) const
T & operator[] ( int i )
const T & operator[] ( int i ) const

Detailed Description

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

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. The QContiguousCache class also can provide an upper bound on memory usage via setCapacity().

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 n 4 S a0$$/PR.D%$  ,  % $0 >T 3SAEN &5%2/) ZED"3)N( ! ="1O4 '/5(% )3 .%4<> #  I.$Sg (3E2O ),g 4E52r #!D   " ' W(7) )3R%ND H#R"4 9#(I #H$(%  C   !  /%$ .%C)/N%% H .   OA       F1 Cb0`   @@@ @   @P @@@    @@@ `@  @  @@ @   @@@ @D@  @ @@@ @ "`@(@@ @@@@@ @  @ D@ @ @D@@  @ @ @ @ $@@ @ @ @ @@@H @ ! H" @@  @@ @@ "@@@  @ $@@ @ ! B ! `   @( @a@ @ !P  @@ @ @@B  @ ntiguousCache ( 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 $E#a  !%AL !G!).9 , .2MAL)I$E(  S !,),!"4I'/Hh !00N/A() $h.I/5s(4 P$( <>   ,&# -O4 OU#A%A$<! (3  "  .! A-0D * 4 )<  ))N,( <%     )ON )  (,   m3   Db ` @ @ @@ @   @@ @@@ @   @B(@@  @ @@ @   @ @ @P@   @ @@@@  `(@@ @D@@ B  0$ L@ @@ @ @@@  @ @ @ @$@@!@ @0$A@@@@ @(! A @H"   @ @ @@@  @ @@  D   @@ $   @@$( @`@  $P  @@ @ @@@  @ ) 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 )<  N3e44%< VA ,)>  )$/SIT)) /  FE#(% 2%A$#TAI3 )D ! )>  > 4E (4  % ,#  ))< >  4/N%-H 2&)U3C#(-  c ,C .$e) O S!& 1. U-  2$ & 2T .! ))4%5) E$A ! (I(4 /()"<& 1T %5#E( "    @ & .  .    / 4 R`0   @" @ @@@    @ P@@@    @@(@@ @  @ @  @@@ @$   @"@@A@ @@  `@(@@@@ @ @0$ @@D @   @ @@@@  @  @ @ @!@ @ $A@@@H @ ! " @@ @ 0@@  "@@@  @ $@@ D @ % `@      @a@  @  %H  @@  @@@  @! last">

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

See also areIndexesValid(), append(), and prepend().

 ! F)2#. L0/. (%1TI'O#C((-  .3 #D .() R %/   ,)#, !  / N %"0L    BM%  !  #  a.M "  TB `  @ @ @@ @    @@@ @    @@ `@  @ @@ @   @@    @@ @@A@@@  `*!@D@@ @   @ @@ @  @ @  @  @ @ @ @@ @ !@  @ A@@@H @(  A @@""@@@ 0B@ @@ "@@@  $   D !  % @  @B  @p@ @  $ @@  @@B   ame="//apple_ref/cpp/instm/QContiguousCache/takeFirst">

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> &a < OH%r/  /T. <0>4 2 &I>H2 > SQ T$()!#H%/ERW 3%$E ! 3e> <7 !#!2)%D5,)&(E4 4H%# A e ( )D%C  &i 2C(%  49T  E %>%4 > % !(E=ON$ G%C!C(%( #/A-D ">/   !    #/)$ N 53H/%/["     "O@B/  < + e5C( R  0  B" @ @@@ @   @T @@ @     @@(`@  @  @@ @   @ @ D@   @@ @@@@@   `@ @@ @@P@@@ @ !@0$ @@ @@  @ @ @@  @  @ @$@ @ @ 0$A@@@@ @ @" @@ 0@  @@ @@  4   $@    ! @  `@ 0 @