Classes | Public Member Functions

QCache< Key, T > Class Template Reference

Search for all occurrences

Detailed Description

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

Definition at line 54 of file qcache.h.

#include <qcache.h>

List of all members.

Classes

struct   Node

Public Member Functions

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

Constructor & Destructor Documentation

QCache ( int  maxCost = 100 ) [inline, explicit]

Definition at line 134 of file qcache.h.

    : f(0), l(0), unused(0), mx(amaxCost), total(0) {}
~QCache ( ) [inline]

Definition at line 103 of file qcache.h.

{ clear(); }

Member Function Documentation

int maxCost ( ) const [inline]

Definition at line 105 of file qcache.h.

{ return mx; }
void setMaxCost ( int  m ) [inline]

Definition at line 143 of file qcache.h.

{ mx = m; trim(mx); }
int totalCost ( ) const [inline]

Definition at line 107 of file qcache.h.

{ return total; }
int size ( ) const [inline]

Definition at line 109 of file qcache.h.

{ return hash.size(); }
int count ( ) const [inline]

Definition at line 110 of file qcache.h.

{ return hash.size(); }
bool isEmpty ( ) const [inline]

Definition at line 111 of file qcache.h.

{ return hash.isEmpty(); }
QList<Key> keys ( ) const [inline]

Definition at line 112 of file qcache.h.

{ return hash.keys(); }
void clear ( ) [inline]

Definition at line 138 of file qcache.h.

{ while (f) { delete f->t; f = f->n; }
 hash.clear(); l = 0; total = 0; }
bool insert ( const Key &  key,
T *  object,
int  cost = 1 
)

Definition at line 181 of file qcache.h.

{
    remove(akey);
    if (acost > mx) {
        delete aobject;
        return false;
    }
    trim(mx - acost);
    Node sn(aobject, acost);
    typename QHash<Key, Node>::iterator i = hash.insert(akey, sn);
    total += acost;
    Node *n = &i.value();
    n->keyPtr = &i.key();
    if (f) f->p = n;
    n->n = f;
    f = n;
    if (!l) l = f;
    return true;
}
T * object ( const Key &  key ) const [inline]

Definition at line 147 of file qcache.h.

{ return const_cast<QCache<Key,T>*>(this)->relink(key); }
bool contains ( const Key &  key ) const [inline]

Definition at line 118 of file qcache.h.

{ return hash.contains(key); }
T * operator[] ( const Key &  key ) const [inline]

Definition at line 151 of file qcache.h.

{ return object(key); }
bool remove ( const Key &  key ) [inline]

Definition at line 155 of file qcache.h.

{
    typename QHash<Key, Node>::iterator i = hash.find(key);
    if (typename QHash<Key, Node>::const_iterator(i) == hash.constEnd()) {
        return false;
    } else {
        unlink(*i);
        return true;
    }
}
T * take ( const Key &  key ) [inline]

Definition at line 167 of file qcache.h.

{
    typename QHash<Key, Node>::iterator i = hash.find(key);
    if (i == hash.end())
        return 0;

    Node &n = *i;
    T *t = n.t;
    n.t = 0;
    unlink(n);
    return t;
}

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