QAsciiCache(3qt)

NAME
QAsciiCache - Template class that provides a cache based on

#include <qasciicache.h>

Inherits QGCache.

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

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

QAsciiCache is implemented as a template class. Define a template instance QAsciiCache<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. The cache items are accessed via char* keys. QAsciiCache cannot handle Unicode keys, instead use the QCache template, which uses QString keys. A QCache has the same performace as a QAsciiCache.

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 removed.

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

There are also methods to remove() or take() an object from the cache. Calling setAutoDelete(TRUE) for a cache tells it to delete items that are removed. The default is to not delete items when then are removed (i.e. remove() and take() are equivalent).

When inserting an item into the cache, only the pointer is copied, not the item itself. This is called a shallow copy. It is possible to make the dictionary copy all of the item's data (known as a deep copy) when an item is inserted. insert() calls the virtual function QCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement it if you want deep copies.

When removing a cache item, the virtual function QCollection::deleteItem() is called. It's default implementation in QAsciiCache is to delete the item if auto-deletion is enabled.

There is a QAsciiCacheIterator which may be used to traverse the items in the cache in arbitrary order.

See also QAsciiCacheIterator, QCache, QIntCache and Collection Classes

MEMBER FUNCTION DOCUMENTATION

QAsciiCache::QAsciiCache ( int maxCost=100, int size=17, bool caseSensitive=TRUE, bool copyKeys=TRUE )
Constructs a cache with the following properties:

Arguments:

maxCost is the maximum allowed total cost.

size is the size of the internal hash array.

caseSensitive specifies whether to use case sensitive lookup or not.

copyKeys specifies whether to copy the key strings. Each inserted item is associated with a cost. When inserting a new item, if the total cost of all items in the cache will exceeds maxCost, the cache will start throwing out the older (recently least used) items until there is room enough for the new item to be inserted.

Setting size to a suitably large prime number (equal to or greater than the expected number of entries) makes the hash distribution better and hence the loopup faster.

Setting caseSensitive to TRUE will treat "abc" and "Abc" as different keys. Setting it to FALSE will make the dictionary ignore case. Case insensitive comparison includes the whole Unicode alphabeth.

Setting copyKeys to TRUE will make the dictionary copy the key when an item is inserted. Setting it to FALSE will make the dictionary only use the pointer to the key.

QAsciiCache::~QAsciiCache ()
Removes all items from the cache and destroys it. All iterators that access this cache will be reset.

void QAsciiCache::clear () [virtual]
Removes all items from the cache, and deletes them if auto-deletion has been enabled.

All cache iterators that operate this on cache are reset.

See also remove() and take().

Reimplemented from QCollection.

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

See also totalCost().

Reimplemented from QCollection.

type * QAsciiCache::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 QAsciiCache::insert ( const char * k, const type * d, int c=1, int p=0 )
Inserts the item d into the cache with key k and cost c. 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, QAsciiCache will remove old, least-used items until there is room for this new item.

The parameter p is internal and should be left at the default value (0).

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 QAsciiCache::isEmpty () const
Returns TRUE if the cache is empty, or TRUE if there is at least one object in it.

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

See also setMaxCost() and totalCost().

type * QAsciiCache::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 QAsciiCache::remove ( const char * k )
Removes the item associated with k, and returns TRUE if the item was present in the cache or FALSE if it was not.

The item is deleted if auto-deletion has been enabled, i.e. you have called setAutoDelete(TRUE).

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

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 QAsciiCache::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 removed immediately.

See also maxCost() and totalCost().

uint QAsciiCache::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.

void QAsciiCache::statistics () const
A debug-only utility function. Prints out cache usage, hit/miss, and distribution information using qDebug(). This function does nothing in the release library.

type * QAsciiCache::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 QAsciiCache::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().

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

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

SEE ALSO
http://doc.trolltech.com/qasciicache.html http://www.trolltech.com/faq/tech.html

COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.

BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports make our job much simpler. Thank you.

In case of content or formattting problems with this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qasciicache.3qt) and the Qt version (2.3.1).