📄 q3gcache.cpp
字号:
*/bool Q3GCache::remove_string(const QString &key){ Item d = take_string(key); if (d) deleteItem(d); return d != 0;}bool Q3GCache::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.*/Q3PtrCollection::Item Q3GCache::take_string(const QString &key){ Q3CacheItem *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.*/Q3PtrCollection::Item Q3GCache::take_other(const char *key){ Q3CacheItem *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 Q3GCache::clear(){ Q3CacheItem *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.*/Q3PtrCollection::Item Q3GCache::find_string(const QString &key, bool ref) const{ Q3CacheItem *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.*/Q3PtrCollection::Item Q3GCache::find_other(const char *key, bool ref) const{ Q3CacheItem *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 Q3GCache::makeRoomFor(int cost, int priority){ if (cost > mCost) // cannot make room for more return false; // than maximum cost if (priority == -1) priority = 32767; register Q3CacheItem *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 Q3GCache::statistics() const{#if defined(QT_DEBUG) QString line; line.fill(QLatin1Char('*'), 80); qDebug("%s", 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("%s", line.ascii());#endif}/***************************************************************************** Q3GCacheIterator member functions *****************************************************************************//*! \class Q3GCacheIterator qgcache.h \reentrant \brief The Q3GCacheIterator class is an internal class for implementing Q3CacheIterator and QIntCacheIterator. \internal Q3GCacheIterator is a strictly internal class that does the heavy work for Q3CacheIterator and QIntCacheIterator.*//*! Constructs an iterator that operates on the cache \a c.*/Q3GCacheIterator::Q3GCacheIterator(const Q3GCache &c){ it = new Q3CListIt(c.lruList);#if defined(QT_DEBUG) Q_ASSERT(it != 0);#endif}/*! Constructs an iterator that operates on the same cache as \a ci.*/Q3GCacheIterator::Q3GCacheIterator(const Q3GCacheIterator &ci){ it = new Q3CListIt(ci.it);#if defined(QT_DEBUG) Q_ASSERT(it != 0);#endif}/*! Destroys the iterator.*/Q3GCacheIterator::~Q3GCacheIterator(){ delete it;}/*! Assigns the iterator \a ci to this cache iterator.*/Q3GCacheIterator &Q3GCacheIterator::operator=(const Q3GCacheIterator &ci){ *it = *ci.it; return *this;}/*! Returns the number of items in the cache.*/uint Q3GCacheIterator::count() const{ return it->count();}/*! Returns true if the iterator points to the first item.*/bool Q3GCacheIterator::atFirst() const{ return it->atFirst();}/*! Returns true if the iterator points to the last item.*/bool Q3GCacheIterator::atLast() const{ return it->atLast();}/*! Sets the list iterator to point to the first item in the cache.*/Q3PtrCollection::Item Q3GCacheIterator::toFirst(){ Q3CacheItem *item = it->toFirst(); return item ? item->data : 0;}/*! Sets the list iterator to point to the last item in the cache.*/Q3PtrCollection::Item Q3GCacheIterator::toLast(){ Q3CacheItem *item = it->toLast(); return item ? item->data : 0;}/*! Returns the current item.*/Q3PtrCollection::Item Q3GCacheIterator::get() const{ Q3CacheItem *item = it->current(); return item ? item->data : 0;}/*! Returns the key of the current item.*/QString Q3GCacheIterator::getKeyString() const{ Q3CacheItem *item = it->current(); return item ? *((QString*)item->key) : QString();}/*! Returns the key of the current item, as a \0-terminated C string.*/const char *Q3GCacheIterator::getKeyAscii() const{ Q3CacheItem *item = it->current(); return item ? (const char *)item->key : 0;}/*! Returns the key of the current item, as a long.*/long Q3GCacheIterator::getKeyInt() const{ Q3CacheItem *item = it->current(); return item ? (long)item->key : 0;}/*! Moves to the next item (postfix).*/Q3PtrCollection::Item Q3GCacheIterator::operator()(){ Q3CacheItem *item = it->operator()(); return item ? item->data : 0;}/*! Moves to the next item (prefix).*/Q3PtrCollection::Item Q3GCacheIterator::operator++(){ Q3CacheItem *item = it->operator++(); return item ? item->data : 0;}/*! Moves \a jump positions forward.*/Q3PtrCollection::Item Q3GCacheIterator::operator+=(uint jump){ Q3CacheItem *item = it->operator+=(jump); return item ? item->data : 0;}/*! Moves to the previous item (prefix).*/Q3PtrCollection::Item Q3GCacheIterator::operator--(){ Q3CacheItem *item = it->operator--(); return item ? item->data : 0;}/*! Moves \a jump positions backward.*/Q3PtrCollection::Item Q3GCacheIterator::operator-=(uint jump){ Q3CacheItem *item = it->operator-=(jump); return item ? item->data : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -