⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q3gcache.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** 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.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3gcache.h"#include "q3ptrlist.h"#include "q3dict.h"#include "qstring.h"/*!  \class Q3GCache  \reentrant  \brief The Q3GCache class is an internal class for implementing Q3Cache  template classes.  \internal  Q3GCache is a strictly internal class that acts as a base class for the  \link collection.html collection classes\endlink Q3Cache and QIntCache.*//*****************************************************************************  Q3GCacheItem class (internal cache item) *****************************************************************************/struct Q3CacheItem{    Q3CacheItem(void *k, Q3PtrCollection::Item d, int c, short p)	: priority(p), skipPriority(p), cost(c), key(k), data(d), node(0) {}    short	priority;    short	skipPriority;    int		cost;    void       *key;    Q3PtrCollection::Item data;    Q3LNode     *node;};/*****************************************************************************  Q3CList class (internal list of cache items) *****************************************************************************/class Q3CList : private Q3PtrList<Q3CacheItem>{friend class Q3GCacheIterator;friend class Q3CListIt;public:    Q3CList()	{}   ~Q3CList();    void	insert(Q3CacheItem *);		// insert according to priority    void	insert(int, Q3CacheItem *);    void	take(Q3CacheItem *);    void	reference(Q3CacheItem *);    void	setAutoDelete(bool del) { Q3PtrCollection::setAutoDelete(del); }    bool	removeFirst()	{ return Q3PtrList<Q3CacheItem>::removeFirst(); }    bool	removeLast()	{ return Q3PtrList<Q3CacheItem>::removeLast(); }    Q3CacheItem *first()		{ return Q3PtrList<Q3CacheItem>::first(); }    Q3CacheItem *last()		{ return Q3PtrList<Q3CacheItem>::last(); }    Q3CacheItem *prev()		{ return Q3PtrList<Q3CacheItem>::prev(); }    Q3CacheItem *next()		{ return Q3PtrList<Q3CacheItem>::next(); }#if defined(QT_DEBUG)    int		inserts;			// variables for statistics    int		insertCosts;    int		insertMisses;    int		finds;    int		hits;    int		hitCosts;    int		dumps;    int		dumpCosts;#endif};Q3CList::~Q3CList(){#if defined(QT_DEBUG)    Q_ASSERT(count() == 0);#endif}void Q3CList::insert(Q3CacheItem *ci){    Q3CacheItem *item = first();    while(item && item->skipPriority > ci->priority) {	item->skipPriority--;	item = next();    }    if (item)	Q3PtrList<Q3CacheItem>::insert(at(), ci);    else	append(ci);#if defined(QT_DEBUG)    Q_ASSERT(ci->node == 0);#endif    ci->node = currentNode();}inline void Q3CList::insert(int i, Q3CacheItem *ci){    Q3PtrList<Q3CacheItem>::insert(i, ci);#if defined(QT_DEBUG)    Q_ASSERT(ci->node == 0);#endif    ci->node = currentNode();}void Q3CList::take(Q3CacheItem *ci){    if (ci) {#if defined(QT_DEBUG)	Q_ASSERT(ci->node != 0);#endif	takeNode(ci->node);	ci->node = 0;    }}inline void Q3CList::reference(Q3CacheItem *ci){#if defined(QT_DEBUG)    Q_ASSERT(ci != 0 && ci->node != 0);#endif    ci->skipPriority = ci->priority;    relinkNode(ci->node);			// relink as first item}class Q3CListIt: public Q3PtrListIterator<Q3CacheItem>{public:    Q3CListIt(const Q3CList *p): Q3PtrListIterator<Q3CacheItem>(*p) {}    Q3CListIt(const Q3CListIt *p): Q3PtrListIterator<Q3CacheItem>(*p) {}};/*****************************************************************************  Q3CDict class (internal dictionary of cache items) *****************************************************************************///// Since we need to decide if the dictionary should use an int or const// char * key (the "bool trivial" argument in the constructor below)// we cannot use the macro/template dict, but inherit directly from Q3GDict.//class Q3CDict : public Q3GDict{public:    Q3CDict(uint size, uint kt, bool caseSensitive, bool copyKeys)	: Q3GDict(size, (KeyType)kt, caseSensitive, copyKeys) {}    ~Q3CDict();    void clear() { Q3GDict::clear(); }    Q3CacheItem *find_string(const QString &key) const	{ return (Q3CacheItem*)((Q3CDict*)this)->look_string(key, 0, 0); }    Q3CacheItem *find_ascii(const char *key) const	{ return (Q3CacheItem*)((Q3CDict*)this)->look_ascii(key, 0, 0); }    Q3CacheItem *find_int(long key) const	{ return (Q3CacheItem*)((Q3CDict*)this)->look_int(key, 0, 0); }    Q3CacheItem *take_string(const QString &key)	{ return (Q3CacheItem*)Q3GDict::take_string(key); }    Q3CacheItem *take_ascii(const char *key)	{ return (Q3CacheItem*)Q3GDict::take_ascii(key); }    Q3CacheItem *take_int(long key)	{ return (Q3CacheItem*)Q3GDict::take_int(key); }    bool  insert_string(const QString &key, const Q3CacheItem *ci)	{ return Q3GDict::look_string(key,(Item)ci,1)!=0;}    bool  insert_ascii(const char *key, const Q3CacheItem *ci)	{ return Q3GDict::look_ascii(key,(Item)ci,1)!=0;}    bool  insert_int(long key, const Q3CacheItem *ci)	{ return Q3GDict::look_int(key,(Item)ci,1)!=0;}    bool  remove_string(Q3CacheItem *item)	{ return Q3GDict::remove_string(*((QString*)(item->key)),item); }    bool  remove_ascii(Q3CacheItem *item)	{ return Q3GDict::remove_ascii((const char *)item->key,item); }    bool  remove_int(Q3CacheItem *item)	{ return Q3GDict::remove_int((long)item->key,item);}    void  statistics()			{ Q3GDict::statistics(); }private:    void deleteItem(void *item)	{ if (del_item) { Q3CacheItem *d = (Q3CacheItem*)item; delete d; } }};inline Q3CDict::~Q3CDict(){    clear();}/*****************************************************************************  Q3GDict member functions *****************************************************************************//*!  Constructs a cache.  The maximum cost of the cache is given by \a maxCost and the size by \a  size. The key type is \a kt which may be \c StringKey, \c AsciiKey,  \c IntKey or \c PtrKey. The case-sensitivity of lookups is set with  \a caseSensitive. Keys are copied if \a copyKeys is true.*/Q3GCache::Q3GCache(int maxCost, uint size, KeyType kt, bool caseSensitive,		  bool copyKeys){    keytype = kt;    lruList = new Q3CList;    Q_CHECK_PTR(lruList);    lruList->setAutoDelete(true);    copyk   = ((keytype == AsciiKey) && copyKeys);    dict    = new Q3CDict(size, kt, caseSensitive, false);    Q_CHECK_PTR(dict);    mCost   = maxCost;    tCost   = 0;#if defined(QT_DEBUG)    lruList->inserts	  = 0;    lruList->insertCosts  = 0;    lruList->insertMisses = 0;    lruList->finds	  = 0;    lruList->hits	  = 0;    lruList->hitCosts	  = 0;    lruList->dumps	  = 0;    lruList->dumpCosts	  = 0;#endif}/*!  Cannot copy a cache.*/Q3GCache::Q3GCache(const Q3GCache &)    : Q3PtrCollection(){#if defined(QT_CHECK_NULL)    qFatal("Q3GCache::Q3GCache(Q3GCache &): Cannot copy a cache");#endif}/*!  Removes all items from the cache and destroys it.*/Q3GCache::~Q3GCache(){    clear();    delete dict;    delete lruList;}/*!  Cannot assign a cache.*/Q3GCache &Q3GCache::operator=(const Q3GCache &){#if defined(QT_CHECK_NULL)    qFatal("Q3GCache::operator=: Cannot copy a cache");#endif    return *this;}/*!  Returns the number of items in the cache.*/uint Q3GCache::count() const{    return dict->count();}/*!  Returns the size of the hash array.*/uint Q3GCache::size() const{    return dict->size();}/*!  \fn int Q3GCache::maxCost() const  Returns the maximum cache cost.*//*!  \fn int Q3GCache::totalCost() const  Returns the total cache cost.*//*!  Sets the maximum cache cost to \a maxCost.*/void Q3GCache::setMaxCost(int maxCost){    if (maxCost < tCost) {	if (!makeRoomFor(tCost - maxCost))	// remove excess cost	    return;    }    mCost = maxCost;}/*!    Inserts an item with data \a data into the cache using key \a key.    The item has cost \a cost and priority \a priority.  \warning If this function returns false, you must delete \a data  yourself.  Additionally, be very careful about using \a data after  calling this function, as any other insertions into the cache, from  anywhere in the application, or within Qt itself, could cause the  data to be discarded from the cache, and the pointer to become  invalid.*/bool Q3GCache::insert_string(const QString &key, Q3PtrCollection::Item data,			     int cost, int priority){    if (tCost + cost > mCost) {	if (!makeRoomFor(tCost + cost - mCost, priority)) {#if defined(QT_DEBUG)	    lruList->insertMisses++;#endif	    return false;	}    }#if defined(QT_DEBUG)    Q_ASSERT(keytype == StringKey);    lruList->inserts++;    lruList->insertCosts += cost;#endif    if (priority < -32768)	priority = -32768;    else if (priority > 32767)	priority = 32677;    Q3CacheItem *ci = new Q3CacheItem(new QString(key), newItem(data),				     cost, (short)priority);    Q_CHECK_PTR(ci);    lruList->insert(0, ci);    dict->insert_string(key, ci);    tCost += cost;    return true;}bool Q3GCache::insert_other(const char *key, Q3PtrCollection::Item data,			    int cost, int priority){    if (tCost + cost > mCost) {	if (!makeRoomFor(tCost + cost - mCost, priority)) {#if defined(QT_DEBUG)	    lruList->insertMisses++;#endif	    return false;	}    }#if defined(QT_DEBUG)    Q_ASSERT(keytype != StringKey);    lruList->inserts++;    lruList->insertCosts += cost;#endif    if (keytype == AsciiKey && copyk)	key = qstrdup(key);    if (priority < -32768)	priority = -32768;    else if (priority > 32767)	priority = 32677;    Q3CacheItem *ci = new Q3CacheItem((void*)key, newItem(data), cost,				     (short)priority);    Q_CHECK_PTR(ci);    lruList->insert(0, ci);    if (keytype == AsciiKey)	dict->insert_ascii(key, ci);    else	dict->insert_int((long)key, ci);    tCost += cost;    return true;}/*!  Removes the item with key \a key from the cache. Returns true if the  item was removed; otherwise returns false.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -