kwave  18.07.70
LRU_Cache.h
Go to the documentation of this file.
1 /***************************************************************************
2  LRU_Cache.h - template class for a LRU cache
3  -------------------
4  begin : Thu Jun 18 2009
5  copyright : (C) 2009 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef LRU_CACHE_H
19 #define LRU_CACHE_H
20 
21 #include "config.h"
22 
23 #include <QLinkedList>
24 #include <QMutableLinkedListIterator>
25 #include <QPair>
26 
27 namespace Kwave
28 {
29 
30  template<class IDX, class DATA> class LRU_Cache
31  :public QLinkedList< QPair<IDX, DATA> >
32  {
33  private:
35  typedef QPair<IDX,DATA> Pair;
36 
37  public:
40  :QLinkedList< Pair >()
41  {
42  }
43 
45  virtual ~LRU_Cache()
46  {
47  }
48 
50  bool isEmpty() const {
51  return QLinkedList<Pair>::isEmpty();
52  }
53 
55  const DATA & operator [] (const IDX index) const
56  {
57  foreach(const Pair &p, *this) {
58  if (p.first == index) {
59  return p.second;
60  }
61  }
62  return DATA();
63  }
64 
66  DATA & operator [] (const IDX index)
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  }
91 
97  bool contains(const IDX index) const
98  {
99  foreach(const Pair &p, *this) {
100  if (p.first == index)
101  return true;
102  }
103  return false;
104  }
105 
110  void remove(const IDX index)
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  }
123 
129  void insert(const IDX index, DATA &value)
130  {
131  QLinkedList<Pair>::prepend(Pair(index, value));
132  }
133 
135  QList<IDX> keys() const
136  {
137  QList<IDX> i;
138  foreach(const Pair &p, *this) {
139  i << p.first;
140  }
141  return i;
142  }
143 
145  QList<DATA> values() const
146  {
147  QList<DATA> v;
148  foreach(const Pair &p, *this) {
149  v << p.second;
150  }
151  return v;
152  }
153 
154  };
155 
156 }
157 
158 #endif /* LRU_CACHE_H */
159 
160 //***************************************************************************
161 //***************************************************************************
const DATA & operator[](const IDX index) const
Definition: LRU_Cache.h:55
Definition: App.h:33
bool contains(const IDX index) const
Definition: LRU_Cache.h:97
bool isEmpty() const
Definition: LRU_Cache.h:50
QList< IDX > keys() const
Definition: LRU_Cache.h:135
QList< DATA > values() const
Definition: LRU_Cache.h:145
virtual ~LRU_Cache()
Definition: LRU_Cache.h:45
QPair< IDX, DATA > Pair
Definition: LRU_Cache.h:35
void insert(const IDX index, DATA &value)
Definition: LRU_Cache.h:129