QCache(3qt)

NAME
QCache - Template class that provides a cache based on char* keys

SYNOPSIS

#include <qcache.h>

Inherits QGCache.

  Public Members
QCache ( const QCache<type> & c )
QCache ( int maxCost=100, int size=17, bool cs=TRUE, bool ck=TRUE )
~QCache ()
QCache<type>& operator= ( const QCache<type> & c )
int maxCost () const
int totalCost () const
void setMaxCost ( int m )
virtual uint count () const
uint size () const
bool isEmpty () const
bool insert ( const char * k, const type * d, int c=1, int p=0 )
bool remove ( const char * k )
type* take ( const char * k )
virtual void clear ()
type* find ( const char * k, bool ref=TRUE ) const
type* operator[] ( const char * k ) const
void statistics () const

DESCRIPTION
The QCache class is a template class that provides a cache based on char* keys.

QCache is implemented as both a template and a macro class. Define a template instance QCache<X> to create a cache that operates on pointers to X, or X*.

A cache is a least recently used (LRU) list of cache items. Each cache item has a cost. The sum of item costs, totalCost(), will not exceed the maximum cache cost, maxCost(). If inserting a new item would cause the total cost to exceed the maximum cost, the least recently used items in the cache are deleted.

Apart from insert, by far the most important function is find() (which also exists as operatior[]). This function looks up an item, returns it, and by default marks it as being the least recently used item.

There are also methods to remove() an object from the cache and delete it, and to take() an object out of the cache without deleting it.

The keys for the cache can be case-sensitive or not, or deeply copied or not. The constructor lets you decide.

QCache, as it must, takes over ownership of the items you insert. That implies that it will delete the items at its discretion, and that you must not delete them. There is one exception, however: If the cost of an object you try to insert() is higher than the allowed total cost of the cache, insert() returns FALSE and in this case you must delete the object yourself.

In addition to totalCost() and maxCost()/setMaxCost(), there are access functions to obtain the number of items in the cache (count()), the size of the hash array (size(), set using the constructor) and whether the cache is empty (you guessed it: isEmpty()). Finally, there is a statistics(), which prints various cache statistics on stdout.

There is also a QCacheIterator which may be used to traverse the items in the cache in arbritrary order.

See also: QCacheIterator, QIntCache and Collection Classes

MEMBER FUNCTION DOCUMENTATION

QCache::QCache ( const QCache<type> & c )
For internal use only.

QCache::QCache ( int maxCost=100, int size=17, bool cs=TRUE, bool ck=TRUE )
Constructs an empty cache with maximum allowed total cost maxCost, optimized to contain a bit less than size items, with case-sensitive keys if cs is TRUE (the default) and case-insensitive keys if cs is FALSE, and whose keys are deeply copied on insertion (the default) and shallowly copied of ck is FALSE.

QCache::~QCache ()
Destroys the object and all objects in it.

void QCache::clear () [virtual]
Removes all items from the cache, and deletes them.

All cache iterators that operate this on cache are reset.

See also: remove() and take().

Reimplemented from QCollection.

uint QCache::count () const [virtual]
Returns the number of items in the cache.

See also: totalCost().

Reimplemented from QCollection.

type * QCache::find ( const char * k, bool ref=TRUE ) const
Returns the item associated with \ k, or null if the key does not exist in the cache. If ref is TRUE (the default), the item is moved to the front of the LRU list.

If there are two or more items with equal keys, then the one that was inserted last is returned.

bool QCache::insert ( const char * k, const type * d, int c=1, int p=0 )
Inserts the item d into the cache with key k, cost and priority p, and returns TRUE if it is successful and FALSE if it fails.

The cache's size is limited, and if the total cost is too high, QCache will delete some of the least-used objects.

Warning: If this function returns FALSE, you must delete d yourself. Additionally, be very careful about using d after calling this function, as any other insertions into the cache, from anywhere in the application, or within Qt itself, could cause the object to be discarded from the cache, and the pointer to become invalid.

bool QCache::isEmpty () const
Returns TRUE if the cache is empty, or TRUE if there is at least one object in it.

int QCache::maxCost () const
Returns the maximum allowed total cost of the cache.

See also: setMaxCost() and totalCost().

QCache<type>& QCache::operator= ( const QCache<type> & c )
For internal use only.

type * QCache::operator[] ( const char * k ) const
Returns the item associated with k, or null if k does not exist in the cache, and moves the item to the front of the LRU list.

If there are two or more items with equal keys, then the one that was inserted last is returned.

This is the same as find( k, TRUE ).

See also: find().

bool QCache::remove ( const char * k )
Deletes the item associated with k, and returns TRUE if the item was present in the cache or FALSE if it was not.

If there are two or more items with equal keys, then the one that was inserted last is is deleted.

All iterators that refer to the removed item are set to point to the next item in the cache's traversal order.

See also: take() and clear().

void QCache::setMaxCost ( int m )
Sets the maximum allowed total cost of the cache to m. If the current total cost is above m, some items are deleted immediately.

See also: maxCost() and totalCost().

uint QCache::size () const
Returns the size of the hash array used to implement the cache. This should be a bit bigger than count() is likely to be.

type * QCache::take ( const char * k )
Takes the item associated with k out of the cache without deleting it, and returns a pointer to the item taken out, or null if the key does not exist in the cache.

If there are two or more items with equal keys, then the one that was inserted last is taken.

All iterators that refer to the taken item are set to point to the next item in the cache's traversal order.

See also: remove() and clear().

int QCache::totalCost () const
Returns the total cost of the items in the cache. This is an integer in the range 0 to maxCost().

See also: setMaxCost().

SEE ALSO
http://www.troll.no/qt/qcache.html

COPYRIGHT
Copyright 1992-1997 Troll Tech AS. See the LICENSE file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.