📄 qgcache.cpp
字号:
*/bool QGCache::remove_string( const QString &key ){ Item d = take_string( key ); if ( d ) deleteItem( d ); return d != 0;}bool QGCache::remove_other( const char *key ){ Item d = take_other( key ); if ( d ) deleteItem( d ); return d != 0;}/*! Takes the item with key \a key out of the cache. The item is not deleted. If no item has this \a key 0 is returned.*/QPtrCollection::Item QGCache::take_string( const QString &key ){ QCacheItem *ci = dict->take_string( key ); // take from dict Item d; if ( ci ) { d = ci->data; tCost -= ci->cost; lruList->take( ci ); // take from list delete (QString*)ci->key; delete ci; } else { d = 0; } return d;}/*! Takes the item with key \a key out of the cache. The item is not deleted. If no item has this \a key 0 is returned.*/QPtrCollection::Item QGCache::take_other( const char *key ){ QCacheItem *ci; if ( keytype == AsciiKey ) ci = dict->take_ascii( key ); else ci = dict->take_int( (long)key ); Item d; if ( ci ) { d = ci->data; tCost -= ci->cost; lruList->take( ci ); // take from list if ( copyk ) delete [] (char *)ci->key; delete ci; } else { d = 0; } return d;}/*! Clears the cache.*/void QGCache::clear(){ QCacheItem *ci; while ( (ci = lruList->first()) ) { switch ( keytype ) { case StringKey: dict->remove_string( ci ); delete (QString*)ci->key; break; case AsciiKey: dict->remove_ascii( ci ); if ( copyk ) delete [] (char*)ci->key; break; case IntKey: dict->remove_int( ci ); break; case PtrKey: // unused break; } deleteItem( ci->data ); // delete data lruList->removeFirst(); // remove from list } tCost = 0;}/*! Finds an item for \a key in the cache and adds a reference if \a ref is TRUE.*/QPtrCollection::Item QGCache::find_string( const QString &key, bool ref ) const{ QCacheItem *ci = dict->find_string( key );#if defined(QT_DEBUG) lruList->finds++;#endif if ( ci ) {#if defined(QT_DEBUG) lruList->hits++; lruList->hitCosts += ci->cost;#endif if ( ref ) lruList->reference( ci ); return ci->data; } return 0;}/*! Finds an item for \a key in the cache and adds a reference if \a ref is TRUE.*/QPtrCollection::Item QGCache::find_other( const char *key, bool ref ) const{ QCacheItem *ci = keytype == AsciiKey ? dict->find_ascii(key) : dict->find_int((long)key);#if defined(QT_DEBUG) lruList->finds++;#endif if ( ci ) {#if defined(QT_DEBUG) lruList->hits++; lruList->hitCosts += ci->cost;#endif if ( ref ) lruList->reference( ci ); return ci->data; } return 0;}/*! Allocates cache space for one or more items.*/bool QGCache::makeRoomFor( int cost, int priority ){ if ( cost > mCost ) // cannot make room for more return FALSE; // than maximum cost if ( priority == -1 ) priority = 32767; register QCacheItem *ci = lruList->last(); int cntCost = 0; int dumps = 0; // number of items to dump while ( cntCost < cost && ci && ci->skipPriority <= priority ) { cntCost += ci->cost; ci = lruList->prev(); dumps++; } if ( cntCost < cost ) // can enough cost be dumped? return FALSE; // no#if defined(QT_DEBUG) Q_ASSERT( dumps > 0 );#endif while ( dumps-- ) { ci = lruList->last();#if defined(QT_DEBUG) lruList->dumps++; lruList->dumpCosts += ci->cost;#endif switch ( keytype ) { case StringKey: dict->remove_string( ci ); delete (QString*)ci->key; break; case AsciiKey: dict->remove_ascii( ci ); if ( copyk ) delete [] (char *)ci->key; break; case IntKey: dict->remove_int( ci ); break; case PtrKey: // unused break; } deleteItem( ci->data ); // delete data lruList->removeLast(); // remove from list } tCost -= cntCost; return TRUE;}/*! Outputs debug statistics.*/void QGCache::statistics() const{#if defined(QT_DEBUG) QString line; line.fill( '*', 80 ); qDebug( line.ascii() ); qDebug( "CACHE STATISTICS:" ); qDebug( "cache contains %d item%s, with a total cost of %d", count(), count() != 1 ? "s" : "", tCost ); qDebug( "maximum cost is %d, cache is %d%% full.", mCost, (200*tCost + mCost) / (mCost*2) ); qDebug( "find() has been called %d time%s", lruList->finds, lruList->finds != 1 ? "s" : "" ); qDebug( "%d of these were hits, items found had a total cost of %d.", lruList->hits,lruList->hitCosts ); qDebug( "%d item%s %s been inserted with a total cost of %d.", lruList->inserts,lruList->inserts != 1 ? "s" : "", lruList->inserts != 1 ? "have" : "has", lruList->insertCosts ); qDebug( "%d item%s %s too large or had too low priority to be inserted.", lruList->insertMisses, lruList->insertMisses != 1 ? "s" : "", lruList->insertMisses != 1 ? "were" : "was" ); qDebug( "%d item%s %s been thrown away with a total cost of %d.", lruList->dumps, lruList->dumps != 1 ? "s" : "", lruList->dumps != 1 ? "have" : "has", lruList->dumpCosts ); qDebug( "Statistics from internal dictionary class:" ); dict->statistics(); qDebug( line.ascii() );#endif}/***************************************************************************** QGCacheIterator member functions *****************************************************************************//*! \class QGCacheIterator qgcache.h \reentrant \ingroup shared \ingroup collection \brief The QGCacheIterator class is an internal class for implementing QCacheIterator and QIntCacheIterator. \internal QGCacheIterator is a strictly internal class that does the heavy work for QCacheIterator and QIntCacheIterator.*//*! Constructs an iterator that operates on the cache \a c.*/QGCacheIterator::QGCacheIterator( const QGCache &c ){ it = new QCListIt( c.lruList );#if defined(QT_DEBUG) Q_ASSERT( it != 0 );#endif}/*! Constructs an iterator that operates on the same cache as \a ci.*/QGCacheIterator::QGCacheIterator( const QGCacheIterator &ci ){ it = new QCListIt( ci.it );#if defined(QT_DEBUG) Q_ASSERT( it != 0 );#endif}/*! Destroys the iterator.*/QGCacheIterator::~QGCacheIterator(){ delete it;}/*! Assigns the iterator \a ci to this cache iterator.*/QGCacheIterator &QGCacheIterator::operator=( const QGCacheIterator &ci ){ *it = *ci.it; return *this;}/*! Returns the number of items in the cache.*/uint QGCacheIterator::count() const{ return it->count();}/*! Returns TRUE if the iterator points to the first item.*/bool QGCacheIterator::atFirst() const{ return it->atFirst();}/*! Returns TRUE if the iterator points to the last item.*/bool QGCacheIterator::atLast() const{ return it->atLast();}/*! Sets the list iterator to point to the first item in the cache.*/QPtrCollection::Item QGCacheIterator::toFirst(){ QCacheItem *item = it->toFirst(); return item ? item->data : 0;}/*! Sets the list iterator to point to the last item in the cache.*/QPtrCollection::Item QGCacheIterator::toLast(){ QCacheItem *item = it->toLast(); return item ? item->data : 0;}/*! Returns the current item.*/QPtrCollection::Item QGCacheIterator::get() const{ QCacheItem *item = it->current(); return item ? item->data : 0;}/*! Returns the key of the current item.*/QString QGCacheIterator::getKeyString() const{ QCacheItem *item = it->current(); return item ? *((QString*)item->key) : QString::null;}/*! Returns the key of the current item, as a \0-terminated C string.*/const char *QGCacheIterator::getKeyAscii() const{ QCacheItem *item = it->current(); return item ? (const char *)item->key : 0;}/*! Returns the key of the current item, as a long.*/long QGCacheIterator::getKeyInt() const{ QCacheItem *item = it->current(); return item ? (long)item->key : 0;}/*! Moves to the next item (postfix).*/QPtrCollection::Item QGCacheIterator::operator()(){ QCacheItem *item = it->operator()(); return item ? item->data : 0;}/*! Moves to the next item (prefix).*/QPtrCollection::Item QGCacheIterator::operator++(){ QCacheItem *item = it->operator++(); return item ? item->data : 0;}/*! Moves \a jump positions forward.*/QPtrCollection::Item QGCacheIterator::operator+=( uint jump ){ QCacheItem *item = it->operator+=(jump); return item ? item->data : 0;}/*! Moves to the previous item (prefix).*/QPtrCollection::Item QGCacheIterator::operator--(){ QCacheItem *item = it->operator--(); return item ? item->data : 0;}/*! Moves \a jump positions backward.*/QPtrCollection::Item QGCacheIterator::operator-=( uint jump ){ QCacheItem *item = it->operator-=(jump); return item ? item->data : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -