Qt 4.8
Classes | Public Functions | Private Functions | Properties | List of all members
QCache< Key, T > Class Template Reference

#include <qcache.h>

Classes

struct  Node
 

Public Functions

void clear ()
 
bool contains (const Key &key) const
 
int count () const
 
bool insert (const Key &key, T *object, int cost=1)
 
bool isEmpty () const
 
QList< Key > keys () const
 
int maxCost () const
 
T * object (const Key &key) const
 
T * operator[] (const Key &key) const
 
 QCache (int maxCost=100)
 
bool remove (const Key &key)
 
void setMaxCost (int m)
 
int size () const
 
T * take (const Key &key)
 
int totalCost () const
 
 ~QCache ()
 

Private Functions

T * relink (const Key &key)
 
void trim (int m)
 
void unlink (Node &n)
 

Properties

Nodef
 
QHash< Key, Nodehash
 
Nodel
 
int mx
 
int total
 
void * unused
 

Detailed Description

template<class Key, class T>
class QCache< Key, T >

Definition at line 54 of file qcache.h.

Constructors and Destructors

◆ QCache()

template<class Key , class T >
QCache< Key, T >::QCache ( int  maxCost = 100)
inlineexplicit

Definition at line 134 of file qcache.h.

Referenced by QCache< QPixmapCache::Key, QPixmapCacheEntry >::relink().

135  : f(0), l(0), unused(0), mx(amaxCost), total(0) {}
Node * l
Definition: qcache.h:62
Node * f
Definition: qcache.h:62
void * unused
Definition: qcache.h:64
int mx
Definition: qcache.h:65
int total
Definition: qcache.h:65

◆ ~QCache()

template<class Key, class T>
QCache< Key, T >::~QCache ( )
inline

Definition at line 103 of file qcache.h.

103 { clear(); }
void clear()
Definition: qcache.h:138

Functions

◆ clear()

template<class Key , class T >
void QCache< Key, T >::clear ( )
inline

Definition at line 138 of file qcache.h.

Referenced by QPMCache::clear(), QCache< QPixmapCache::Key, QPixmapCacheEntry >::keys(), and QCache< QPixmapCache::Key, QPixmapCacheEntry >::~QCache().

139 { while (f) { delete f->t; f = f->n; }
140  hash.clear(); l = 0; total = 0; }
Node * l
Definition: qcache.h:62
Node * f
Definition: qcache.h:62
Node * n
Definition: qcache.h:60
int total
Definition: qcache.h:65
QHash< Key, Node > hash
Definition: qcache.h:63

◆ contains()

template<class Key, class T>
bool QCache< Key, T >::contains ( const Key &  key) const
inline

Definition at line 118 of file qcache.h.

Referenced by QDirectFBPaintEnginePrivate::getSurface().

118 { return hash.contains(key); }
int key
QHash< Key, Node > hash
Definition: qcache.h:63

◆ count()

template<class Key, class T>
int QCache< Key, T >::count ( ) const
inline

Definition at line 110 of file qcache.h.

110 { return hash.size(); }
QHash< Key, Node > hash
Definition: qcache.h:63

◆ insert()

template<class Key, class T>
bool QCache< Key, T >::insert ( const Key &  key,
T *  object,
int  cost = 1 
)

Definition at line 181 of file qcache.h.

Referenced by QDirectFBPaintEnginePrivate::getSurface(), QPMCache::insert(), QCache< QPixmapCache::Key, QPixmapCacheEntry >::keys(), QPMCache::replace(), and QConfFileSettingsPrivate::~QConfFileSettingsPrivate().

182 {
183  remove(akey);
184  if (acost > mx) {
185  delete aobject;
186  return false;
187  }
188  trim(mx - acost);
189  Node sn(aobject, acost);
190  typename QHash<Key, Node>::iterator i = hash.insert(akey, sn);
191  total += acost;
192  Node *n = &i.value();
193  n->keyPtr = &i.key();
194  if (f) f->p = n;
195  n->n = f;
196  f = n;
197  if (!l) l = f;
198  return true;
199 }
Node * l
Definition: qcache.h:62
Node * f
Definition: qcache.h:62
Node * p
Definition: qcache.h:60
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qhash.h:348
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
int mx
Definition: qcache.h:65
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
void trim(int m)
Definition: qcache.h:202
int total
Definition: qcache.h:65
QHash< Key, Node > hash
Definition: qcache.h:63

◆ isEmpty()

template<class Key, class T>
bool QCache< Key, T >::isEmpty ( ) const
inline

Definition at line 111 of file qcache.h.

111 { return hash.isEmpty(); }
QHash< Key, Node > hash
Definition: qcache.h:63

◆ keys()

template<class Key, class T>
QList<Key> QCache< Key, T >::keys ( ) const
inline

Definition at line 112 of file qcache.h.

Referenced by QPMCache::clear().

112 { return hash.keys(); }
QHash< Key, Node > hash
Definition: qcache.h:63

◆ maxCost()

template<class Key, class T>
int QCache< Key, T >::maxCost ( ) const
inline

◆ object()

template<class Key, class T >
T * QCache< Key, T >::object ( const Key &  key) const
inline

◆ operator[]()

template<class Key, class T >
T * QCache< Key, T >::operator[] ( const Key &  key) const
inline

Definition at line 151 of file qcache.h.

Referenced by QCache< QPixmapCache::Key, QPixmapCacheEntry >::contains().

152 { return object(key); }
T * object(const Key &key) const
Definition: qcache.h:147
int key

◆ relink()

template<class Key, class T>
T* QCache< Key, T >::relink ( const Key &  key)
inlineprivate

Definition at line 77 of file qcache.h.

Referenced by QCache< QPixmapCache::Key, QPixmapCacheEntry >::object().

77  {
78  typename QHash<Key, Node>::iterator i = hash.find(key);
79  if (typename QHash<Key, Node>::const_iterator(i) == hash.constEnd())
80  return 0;
81 
82  Node &n = *i;
83  if (f != &n) {
84  if (n.p) n.p->n = n.n;
85  if (n.n) n.n->p = n.p;
86  if (l == &n) l = n.p;
87  n.p = 0;
88  n.n = f;
89  f->p = &n;
90  f = &n;
91  }
92  return n.t;
93  }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
Node * l
Definition: qcache.h:62
Node * f
Definition: qcache.h:62
Node * p
Definition: qcache.h:60
int key
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
QHash< Key, Node > hash
Definition: qcache.h:63

◆ remove()

template<class Key, class T >
bool QCache< Key, T >::remove ( const Key &  key)
inline

Definition at line 155 of file qcache.h.

Referenced by QPMCache::insert(), QPMCache::remove(), and QPMCache::replace().

156 {
157  typename QHash<Key, Node>::iterator i = hash.find(key);
158  if (typename QHash<Key, Node>::const_iterator(i) == hash.constEnd()) {
159  return false;
160  } else {
161  unlink(*i);
162  return true;
163  }
164 }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
void unlink(Node &n)
Definition: qcache.h:67
int key
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
QHash< Key, Node > hash
Definition: qcache.h:63

◆ setMaxCost()

template<class Key , class T >
void QCache< Key, T >::setMaxCost ( int  m)
inline

Definition at line 143 of file qcache.h.

Referenced by QDirectFBPaintEngine::drawBufferSpan(), and QCache< QPixmapCache::Key, QPixmapCacheEntry >::maxCost().

144 { mx = m; trim(mx); }
int mx
Definition: qcache.h:65
void trim(int m)
Definition: qcache.h:202

◆ size()

template<class Key, class T>
int QCache< Key, T >::size ( ) const
inline

Definition at line 109 of file qcache.h.

109 { return hash.size(); }
QHash< Key, Node > hash
Definition: qcache.h:63

◆ take()

template<class Key, class T >
T * QCache< Key, T >::take ( const Key &  key)
inline

Definition at line 167 of file qcache.h.

Referenced by QCache< QPixmapCache::Key, QPixmapCacheEntry >::contains(), QDirectFBPaintEngine::drawBufferSpan(), and QConfFile::fromName().

168 {
169  typename QHash<Key, Node>::iterator i = hash.find(key);
170  if (i == hash.end())
171  return 0;
172 
173  Node &n = *i;
174  T *t = n.t;
175  n.t = 0;
176  unlink(n);
177  return t;
178 }
void unlink(Node &n)
Definition: qcache.h:67
int key
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
QHash< Key, Node > hash
Definition: qcache.h:63

◆ totalCost()

template<class Key, class T>
int QCache< Key, T >::totalCost ( ) const
inline

Definition at line 107 of file qcache.h.

107 { return total; }
int total
Definition: qcache.h:65

◆ trim()

template<class Key , class T >
void QCache< Key, T >::trim ( int  m)
private

Definition at line 202 of file qcache.h.

Referenced by QCache< QPixmapCache::Key, QPixmapCacheEntry >::contains(), QCache< QPixmapCache::Key, QPixmapCacheEntry >::insert(), and QCache< QPixmapCache::Key, QPixmapCacheEntry >::setMaxCost().

203 {
204  Node *n = l;
205  while (n && total > m) {
206  Node *u = n;
207  n = n->p;
208  unlink(*u);
209  }
210 }
Node * l
Definition: qcache.h:62
void unlink(Node &n)
Definition: qcache.h:67
quint16 u
int total
Definition: qcache.h:65

◆ unlink()

template<class Key, class T>
void QCache< Key, T >::unlink ( Node n)
inlineprivate

Definition at line 67 of file qcache.h.

Referenced by QCache< QPixmapCache::Key, QPixmapCacheEntry >::remove(), QCache< QPixmapCache::Key, QPixmapCacheEntry >::take(), and QCache< QPixmapCache::Key, QPixmapCacheEntry >::trim().

67  {
68  if (n.p) n.p->n = n.n;
69  if (n.n) n.n->p = n.p;
70  if (l == &n) l = n.p;
71  if (f == &n) f = n.n;
72  total -= n.c;
73  T *obj = n.t;
74  hash.remove(*n.keyPtr);
75  delete obj;
76  }
Node * l
Definition: qcache.h:62
Node * f
Definition: qcache.h:62
int total
Definition: qcache.h:65
QHash< Key, Node > hash
Definition: qcache.h:63

Properties

◆ f

template<class Key, class T>
Node* QCache< Key, T >::f
private

◆ hash

template<class Key, class T>
QHash<Key, Node> QCache< Key, T >::hash
private

◆ l

template<class Key, class T>
Node * QCache< Key, T >::l
private

◆ mx

template<class Key, class T>
int QCache< Key, T >::mx
private

◆ total

template<class Key, class T>
int QCache< Key, T >::total
private

◆ unused

template<class Key, class T>
void* QCache< Key, T >::unused
private

Definition at line 64 of file qcache.h.


The documentation for this class was generated from the following file: