kwave  18.07.70
Kwave::LRU_Cache< IDX, DATA > Class Template Reference

#include <LRU_Cache.h>

Inheritance diagram for Kwave::LRU_Cache< IDX, DATA >:
Inheritance graph
Collaboration diagram for Kwave::LRU_Cache< IDX, DATA >:
Collaboration graph

Public Member Functions

 LRU_Cache ()
 
virtual ~LRU_Cache ()
 
bool isEmpty () const
 
const DATA & operator[] (const IDX index) const
 
DATA & operator[] (const IDX index)
 
bool contains (const IDX index) const
 
void remove (const IDX index)
 
void insert (const IDX index, DATA &value)
 
QList< IDX > keys () const
 
QList< DATA > values () const
 

Private Types

typedef QPair< IDX, DATA > Pair
 

Detailed Description

template<class IDX, class DATA>
class Kwave::LRU_Cache< IDX, DATA >

Definition at line 30 of file LRU_Cache.h.

Member Typedef Documentation

◆ Pair

template<class IDX, class DATA>
typedef QPair<IDX,DATA> Kwave::LRU_Cache< IDX, DATA >::Pair
private

internal typedef for the QPair

Definition at line 35 of file LRU_Cache.h.

Constructor & Destructor Documentation

◆ LRU_Cache()

template<class IDX, class DATA>
Kwave::LRU_Cache< IDX, DATA >::LRU_Cache ( )
inline

Constructor

Definition at line 39 of file LRU_Cache.h.

40  :QLinkedList< Pair >()
41  {
42  }

◆ ~LRU_Cache()

template<class IDX, class DATA>
virtual Kwave::LRU_Cache< IDX, DATA >::~LRU_Cache ( )
inlinevirtual

Destructor

Definition at line 45 of file LRU_Cache.h.

46  {
47  }

Member Function Documentation

◆ contains()

template<class IDX, class DATA>
bool Kwave::LRU_Cache< IDX, DATA >::contains ( const IDX  index) const
inline

checks whether an index is contained

Parameters
indexthe index (key) of the element to look up
Returns
true if found, otherwise false

Definition at line 97 of file LRU_Cache.h.

98  {
99  foreach(const Pair &p, *this) {
100  if (p.first == index)
101  return true;
102  }
103  return false;
104  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35

◆ insert()

template<class IDX, class DATA>
void Kwave::LRU_Cache< IDX, DATA >::insert ( const IDX  index,
DATA &  value 
)
inline

insert a new entry to the start of the list (newest)

Parameters
indexthe index (key) of the element to insert
valuethe data of the element to insert

Definition at line 129 of file LRU_Cache.h.

Referenced by Kwave::LRU_Cache< Kwave::Handle, Kwave::MemoryManager::physical_memory_t >::operator[]().

130  {
131  QLinkedList<Pair>::prepend(Pair(index, value));
132  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35
Here is the caller graph for this function:

◆ isEmpty()

template<class IDX, class DATA>
bool Kwave::LRU_Cache< IDX, DATA >::isEmpty ( ) const
inline

returns true if the cache is empty

Definition at line 50 of file LRU_Cache.h.

50  {
51  return QLinkedList<Pair>::isEmpty();
52  }

◆ keys()

template<class IDX, class DATA>
QList<IDX> Kwave::LRU_Cache< IDX, DATA >::keys ( ) const
inline

returns a list of keys

Definition at line 135 of file LRU_Cache.h.

136  {
137  QList<IDX> i;
138  foreach(const Pair &p, *this) {
139  i << p.first;
140  }
141  return i;
142  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35

◆ operator[]() [1/2]

template<class IDX, class DATA>
const DATA& Kwave::LRU_Cache< IDX, DATA >::operator[] ( const IDX  index) const
inline

index operator, const

Definition at line 55 of file LRU_Cache.h.

56  {
57  foreach(const Pair &p, *this) {
58  if (p.first == index) {
59  return p.second;
60  }
61  }
62  return DATA();
63  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35

◆ operator[]() [2/2]

template<class IDX, class DATA>
DATA& Kwave::LRU_Cache< IDX, DATA >::operator[] ( const IDX  index)
inline

index operator, non-const

Definition at line 66 of file LRU_Cache.h.

67  {
68  static DATA dummy;
69 
70  if (!QLinkedList<Pair>::isEmpty()) {
71  QMutableLinkedListIterator<Pair> it(*this);
72  while (it.hasNext()) {
73  Pair &p = it.next();
74  if (p.first == index) {
75  if (p.first != QLinkedList<Pair>::first().first) {
76  // found it -> move the entry to the start of the list
77  Pair pair = p;
78  it.remove();
79  insert(pair.first, pair.second);
80 // qDebug("Kwave::MemoryManager[%9d] - reordering",
81 // pair.first);
82  }
83 
84  // get the newly entered entry again
85  return QLinkedList<Pair>::first().second;
86  }
87  }
88  }
89  return dummy;
90  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35
void insert(const IDX index, DATA &value)
Definition: LRU_Cache.h:129

◆ remove()

template<class IDX, class DATA>
void Kwave::LRU_Cache< IDX, DATA >::remove ( const IDX  index)
inline

remove an entry

Parameters
indexthe index (key) of the element to remove

Definition at line 110 of file LRU_Cache.h.

111  {
112  if (!QLinkedList<Pair>::isEmpty()) {
113  QMutableLinkedListIterator<Pair> it(*this);
114  while (it.hasNext()) {
115  Pair &p = it.next();
116  if (p.first == index) {
117  it.remove();
118  break;
119  }
120  }
121  }
122  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35

◆ values()

template<class IDX, class DATA>
QList<DATA> Kwave::LRU_Cache< IDX, DATA >::values ( ) const
inline

returns a list of values

Definition at line 145 of file LRU_Cache.h.

146  {
147  QList<DATA> v;
148  foreach(const Pair &p, *this) {
149  v << p.second;
150  }
151  return v;
152  }
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35

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