You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
477 lines
15 KiB
477 lines
15 KiB
13 years ago
|
/****************************************************************************
|
||
|
**
|
||
5 months ago
|
** TQAsciiCache and TQAsciiCacheIterator class documentation
|
||
13 years ago
|
**
|
||
|
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
|
||
|
**
|
||
10 years ago
|
** This file is part of the TQt GUI Toolkit.
|
||
13 years ago
|
**
|
||
|
** This file may be used under the terms of the GNU General
|
||
|
** Public License versions 2.0 or 3.0 as published by the Free
|
||
|
** Software Foundation and appearing in the files LICENSE.GPL2
|
||
|
** and LICENSE.GPL3 included in the packaging of this file.
|
||
|
** Alternatively you may (at your option) use any later version
|
||
|
** of the GNU General Public License if such license has been
|
||
|
** publicly approved by Trolltech ASA (or its successors, if any)
|
||
10 years ago
|
** and the KDE Free TQt Foundation.
|
||
13 years ago
|
**
|
||
|
** Please review the following information to ensure GNU General
|
||
13 years ago
|
** Public Licensing requirements will be met:
|
||
13 years ago
|
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
|
||
|
** If you are unsure which license is appropriate for your use, please
|
||
|
** review the following information:
|
||
|
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||
|
** or contact the sales department at sales@trolltech.com.
|
||
|
**
|
||
|
** This file may be used under the terms of the Q Public License as
|
||
|
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
|
||
|
** included in the packaging of this file. Licensees holding valid Qt
|
||
|
** Commercial licenses may use this file in accordance with the Qt
|
||
|
** Commercial License Agreement provided with the Software.
|
||
|
**
|
||
|
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
|
||
|
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
|
||
|
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
|
||
|
** herein.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
|
|
||
|
/*****************************************************************************
|
||
5 months ago
|
TQAsciiCache documentation
|
||
13 years ago
|
*****************************************************************************/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\class TQAsciiCache tqasciicache.h
|
||
13 years ago
|
|
||
5 months ago
|
\brief The TQAsciiCache class is a template class that provides a cache based on char* keys.
|
||
13 years ago
|
|
||
|
\ingroup collection
|
||
|
\ingroup tools
|
||
|
|
||
5 months ago
|
TQAsciiCache is implemented as a template class. Define a template
|
||
|
instance TQAsciiCache\<X\> to create a cache that operates on
|
||
13 years ago
|
pointers to X (X*).
|
||
|
|
||
|
A cache is a least recently used (LRU) list of cache items. The
|
||
|
cache items are accessed via \c char* keys. For Unicode keys use
|
||
5 months ago
|
the TQCache template instead, which uses \c TQString keys. A TQCache
|
||
|
has the same performace as a TQAsciiCache.
|
||
13 years ago
|
|
||
|
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
|
||
6 months ago
|
cache. Calling \link TQPtrCollection::setAutoDelete()
|
||
13 years ago
|
setAutoDelete(TRUE)\endlink tells the cache 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 cache copy all of the item's data (known as a deep
|
||
|
copy) when an item is inserted. insert() calls the virtual
|
||
6 months ago
|
function TQPtrCollection::newItem() for the item to be inserted.
|
||
13 years ago
|
Inherit a cache and reimplement newItem() if you want deep copies.
|
||
|
|
||
|
When removing a cache item the virtual function
|
||
6 months ago
|
TQPtrCollection::deleteItem() is called. Its default implementation
|
||
5 months ago
|
in TQAsciiCache is to delete the item if \link
|
||
6 months ago
|
TQPtrCollection::setAutoDelete() auto-deletion\endlink is enabled.
|
||
13 years ago
|
|
||
5 months ago
|
There is a TQAsciiCacheIterator which may be used to traverse the
|
||
13 years ago
|
items in the cache in arbitrary order.
|
||
|
|
||
5 months ago
|
\sa TQAsciiCacheIterator, TQCache, TQIntCache
|
||
13 years ago
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCache::TQAsciiCache( const TQAsciiCache<type> &c )
|
||
13 years ago
|
|
||
|
\internal
|
||
|
|
||
5 months ago
|
Do not use. A TQAsciiCache cannot be copied. Calls tqFatal() in debug version.
|
||
13 years ago
|
*/
|
||
|
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCache::TQAsciiCache( int maxCost, int size, bool caseSensitive, bool copyKeys )
|
||
13 years ago
|
|
||
|
Constructs a cache whose contents will never have a total cost
|
||
|
greater than \a maxCost and which is expected to contain less than
|
||
|
\a size items.
|
||
|
|
||
|
\a size is actually the size of an internal hash array; it's
|
||
|
usually best to make it prime and at least 50% bigger than the
|
||
|
largest expected number of items in the cache.
|
||
|
|
||
|
Each inserted item has an associated cost. When inserting a new
|
||
|
item, if the total cost of all items in the cache will exceed \a
|
||
|
maxCost, the cache will start throwing out the older (least
|
||
|
recently used) items until there is enough room for the new item
|
||
|
to be inserted.
|
||
|
|
||
|
If \a caseSensitive is TRUE (the default), the cache keys are case
|
||
|
sensitive; if it is FALSE, they are case-insensitive.
|
||
|
Case-insensitive comparison only affects the 26 letters in
|
||
5 months ago
|
US-ASCII. If \a copyKeys is TRUE (the default), TQAsciiCache makes
|
||
13 years ago
|
a copy of the cache keys, otherwise it copies just the const char
|
||
|
* pointer - slightly faster if you can guarantee that the keys
|
||
|
will never change, but very risky.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCache::~TQAsciiCache()
|
||
13 years ago
|
|
||
|
Removes all items from the cache and destroys it.
|
||
|
All iterators that access this cache will be reset.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCache<type>& TQAsciiCache::operator=( const TQAsciiCache<type> &c )
|
||
13 years ago
|
|
||
|
\internal
|
||
|
|
||
5 months ago
|
Do not use. A TQAsciiCache cannot be copied. Calls tqFatal() in debug version.
|
||
13 years ago
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn int TQAsciiCache::maxCost() const
|
||
13 years ago
|
|
||
|
Returns the maximum allowed total cost of the cache.
|
||
|
|
||
|
\sa setMaxCost() totalCost()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn int TQAsciiCache::totalCost() const
|
||
13 years ago
|
|
||
|
Returns the total cost of the items in the cache. This is an
|
||
|
integer in the range 0 to maxCost().
|
||
|
|
||
|
\sa setMaxCost()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn void TQAsciiCache::setMaxCost( int m )
|
||
13 years ago
|
|
||
|
Sets the maximum allowed total cost of the cache to \a m. If the
|
||
|
current total cost is greater than \a m, some items are removed
|
||
|
immediately.
|
||
|
|
||
|
\sa maxCost() totalCost()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn uint TQAsciiCache::count() const
|
||
13 years ago
|
|
||
|
Returns the number of items in the cache.
|
||
|
|
||
|
\sa totalCost() size()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn uint TQAsciiCache::size() const
|
||
13 years ago
|
|
||
|
Returns the size of the hash array used to implement the cache.
|
||
|
This should be a bit bigger than count() is likely to be.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn bool TQAsciiCache::isEmpty() const
|
||
13 years ago
|
|
||
|
Returns TRUE if the cache is empty; otherwise returns FALSE.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn bool TQAsciiCache::insert( const char *k, const type *d, int c, int p )
|
||
13 years ago
|
|
||
|
Inserts the item \a d into the cache using key \a k, and with an
|
||
|
associated cost of \a c. Returns TRUE if the item is successfully
|
||
|
inserted. Returns FALSE if the item is not inserted, for example,
|
||
|
if the cost of the item exceeds maxCost().
|
||
|
|
||
|
The cache's size is limited, and if the total cost is too high,
|
||
5 months ago
|
TQAsciiCache will remove old, least recently used items until there
|
||
13 years ago
|
is room for this new item.
|
||
|
|
||
|
Items with duplicate keys can be inserted.
|
||
|
|
||
|
The parameter \a p is internal and should be left at the default
|
||
|
value (0).
|
||
|
|
||
|
\warning If this function returns FALSE, you must delete \a d
|
||
|
yourself. Additionally, be very careful about using \a d after
|
||
|
calling this function, because any other insertions into the
|
||
10 years ago
|
cache, from anywhere in the application or within TQt itself, could
|
||
13 years ago
|
cause the object to be discarded from the cache and the pointer to
|
||
|
become invalid.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn bool TQAsciiCache::remove( const char *k )
|
||
13 years ago
|
|
||
|
Removes the item with key \a k and returns TRUE if the item was
|
||
|
present in the cache; otherwise returns FALSE.
|
||
|
|
||
|
The item is deleted if auto-deletion has been enabled, i.e., if
|
||
6 months ago
|
you have called \link TQPtrCollection::setAutoDelete()
|
||
13 years ago
|
setAutoDelete(TRUE)\endlink.
|
||
|
|
||
|
If there are two or more items with equal keys, the one that was
|
||
|
inserted last is removed.
|
||
|
|
||
|
All iterators that refer to the removed item are set to point to
|
||
|
the next item in the cache's traversal order.
|
||
|
|
||
|
\sa take(), clear()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCache::take( const char *k )
|
||
13 years ago
|
|
||
|
Takes the item associated with \a k out of the cache without
|
||
|
deleting it and returns a pointer to the item taken out, or 0
|
||
|
if the key does not exist in the cache.
|
||
|
|
||
|
If there are two or more items with equal keys, 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.
|
||
|
|
||
|
\sa remove(), clear()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn void TQAsciiCache::clear()
|
||
13 years ago
|
|
||
|
Removes all items from the cache, and deletes them if \link
|
||
6 months ago
|
TQPtrCollection::setAutoDelete() auto-deletion\endlink has been
|
||
13 years ago
|
enabled.
|
||
|
|
||
|
All cache iterators that operate on this cache are reset.
|
||
|
|
||
|
\sa remove() take()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCache::find( const char *k, bool ref ) const
|
||
13 years ago
|
|
||
|
Returns the item with key \a k, or 0 if the key does not exist
|
||
|
in the cache. If \a ref is TRUE (the default), the item is moved
|
||
|
to the front of the least recently used list.
|
||
|
|
||
|
If there are two or more items with equal keys, the one that was
|
||
|
inserted last is returned.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCache::operator[]( const char *k ) const
|
||
13 years ago
|
|
||
|
Returns the item with key \a k, or 0 if \a k does not exist in
|
||
|
the cache, and moves the item to the front of the least recently
|
||
|
used list.
|
||
|
|
||
|
If there are two or more items with equal keys, the one that was
|
||
|
inserted last is returned.
|
||
|
|
||
|
This is the same as find( k, TRUE ).
|
||
|
|
||
|
\sa find()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn void TQAsciiCache::statistics() const
|
||
13 years ago
|
|
||
|
A debug-only utility function. Prints out cache usage, hit/miss,
|
||
13 years ago
|
and distribution information using tqDebug(). This function does
|
||
13 years ago
|
nothing in the release library.
|
||
|
*/
|
||
|
|
||
|
/*****************************************************************************
|
||
5 months ago
|
TQAsciiCacheIterator documentation
|
||
13 years ago
|
*****************************************************************************/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\class TQAsciiCacheIterator tqasciicache.h
|
||
|
\brief The TQAsciiCacheIterator class provides an iterator for TQAsciiCache collections.
|
||
13 years ago
|
|
||
|
\ingroup collection
|
||
|
\ingroup tools
|
||
|
|
||
|
Note that the traversal order is arbitrary; you are not guaranteed
|
||
|
any particular order. If new objects are inserted into the cache
|
||
|
while the iterator is active, the iterator may or may not see
|
||
|
them.
|
||
|
|
||
|
Multiple iterators are completely independent, even when they
|
||
5 months ago
|
operate on the same TQAsciiCache. TQAsciiCache updates all iterators
|
||
13 years ago
|
that refer an item when that item is removed.
|
||
|
|
||
5 months ago
|
TQAsciiCacheIterator provides an operator++() and an operator+=()
|
||
13 years ago
|
to traverse the cache; current() and currentKey() to access the
|
||
|
current cache item and its key. It also provides atFirst() and
|
||
|
atLast(), which return TRUE if the iterator points to the first or
|
||
|
last item in the cache respectively. The isEmpty() function
|
||
|
returns TRUE if the cache is empty; and count() returns the number
|
||
|
of items in the cache.
|
||
|
|
||
|
Note that atFirst() and atLast() refer to the iterator's arbitrary
|
||
|
ordering, not to the cache's internal least recently used list.
|
||
|
|
||
5 months ago
|
\sa TQAsciiCache
|
||
13 years ago
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCacheIterator::TQAsciiCacheIterator( const TQAsciiCache<type> &cache )
|
||
13 years ago
|
|
||
|
Constructs an iterator for \a cache. The current iterator item is
|
||
|
set to point to the first item in the \a cache.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCacheIterator::TQAsciiCacheIterator (const TQAsciiCacheIterator<type> & ci)
|
||
13 years ago
|
|
||
|
Constructs an iterator for the same cache as \a ci. The new
|
||
|
iterator starts at the same item as ci.current() but moves
|
||
|
independently from there on.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCacheIterator<type>& TQAsciiCacheIterator::operator=( const TQAsciiCacheIterator<type> &ci )
|
||
13 years ago
|
|
||
|
Makes this an iterator for the same cache as \a ci. The new
|
||
|
iterator starts at the same item as ci.current(), but moves
|
||
|
independently thereafter.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn uint TQAsciiCacheIterator::count() const
|
||
13 years ago
|
|
||
|
Returns the number of items in the cache over which this iterator
|
||
|
operates.
|
||
|
|
||
|
\sa isEmpty()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn bool TQAsciiCacheIterator::isEmpty() const
|
||
13 years ago
|
|
||
|
Returns TRUE if the cache is empty, i.e. count() == 0; otherwise
|
||
|
returns FALSE.
|
||
|
|
||
|
\sa count()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn bool TQAsciiCacheIterator::atFirst() const
|
||
13 years ago
|
|
||
|
Returns TRUE if the iterator points to the first item in the
|
||
|
cache; otherwise returns FALSE. Note that this refers to the
|
||
|
iterator's arbitrary ordering, not to the cache's internal least
|
||
|
recently used list.
|
||
|
|
||
|
\sa toFirst(), atLast()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn bool TQAsciiCacheIterator::atLast() const
|
||
13 years ago
|
|
||
|
Returns TRUE if the iterator points to the last item in the cache;
|
||
|
otherwise returns FALSE. Note that this refers to the iterator's
|
||
|
arbitrary ordering, not to the cache's internal least recently
|
||
|
used list.
|
||
|
|
||
|
\sa toLast(), atFirst()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::toFirst()
|
||
13 years ago
|
|
||
|
Sets the iterator to point to the first item in the cache and
|
||
|
returns a pointer to the item.
|
||
|
|
||
|
Sets the iterator to 0 and returns 0 if the cache is empty.
|
||
|
|
||
|
\sa toLast() isEmpty()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::toLast()
|
||
13 years ago
|
|
||
|
Sets the iterator to point to the last item in the cache and
|
||
|
returns a pointer to the item.
|
||
|
|
||
|
Sets the iterator to 0 and returns 0 if the cache is empty.
|
||
|
|
||
|
\sa toLast() isEmpty()
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn TQAsciiCacheIterator::operator type *() const
|
||
13 years ago
|
|
||
|
Cast operator. Returns a pointer to the current iterator item.
|
||
|
Same as current().
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::current() const
|
||
13 years ago
|
|
||
|
Returns a pointer to the current iterator item.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn const char *TQAsciiCacheIterator::currentKey() const
|
||
13 years ago
|
|
||
|
Returns the key for the current iterator item.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::operator()()
|
||
13 years ago
|
|
||
|
Makes the succeeding item current and returns the original current
|
||
|
item.
|
||
|
|
||
|
If the current iterator item was the last item in the cache or if
|
||
|
it was 0, 0 is returned.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::operator+=( uint jump )
|
||
13 years ago
|
|
||
|
Returns the item \a jump positions after the current item, or 0
|
||
|
if it is beyond the last item. Makes this the current item.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::operator-=( uint jump )
|
||
13 years ago
|
|
||
|
Returns the item \a jump positions before the current item, or 0
|
||
|
if it is before the first item. Makes this the current item.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::operator++()
|
||
13 years ago
|
|
||
|
Prefix ++ makes the iterator point to the item just after
|
||
|
current(), and makes that the new current item for the iterator. If
|
||
|
current() was the last item, operator++() returns 0.
|
||
|
*/
|
||
|
|
||
|
/*!
|
||
5 months ago
|
\fn type *TQAsciiCacheIterator::operator--()
|
||
13 years ago
|
|
||
|
Prefix -- makes the iterator point to the item just before
|
||
|
current(), and makes that the new current item for the iterator. If
|
||
|
current() was the first item, operator--() returns 0.
|
||
|
*/
|
||
|
|