Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[b9c28e]: libkwave / LRU_Cache.h Maximize Restore History

Download this file

LRU_Cache.h    158 lines (136 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
LRU_Cache.h - template class for a LRU cache
-------------------
begin : Thu Jun 18 2009
copyright : (C) 2009 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef _LRU_CACHE_H_
#define _LRU_CACHE_H_
#include "config.h"
#include <QLinkedList>
#include <QMutableLinkedListIterator>
#include <QPair>
namespace Kwave {
template<class IDX, class DATA> class LRU_Cache
:public QLinkedList< QPair<IDX, DATA> >
{
private:
/** internal typedef for the QPair */
typedef QPair<IDX,DATA> Pair;
public:
/** Constructor */
LRU_Cache()
:QLinkedList< Pair >()
{
}
/** Destructor */
virtual ~LRU_Cache()
{
}
/** returns true if the cache is empty */
bool isEmpty() const {
return QLinkedList<Pair>::isEmpty();
}
/** index operator, const */
const DATA & operator [] (const IDX index) const
{
foreach(const Pair &p, *this) {
if (p.first == index) {
return p.second;
}
}
return DATA();
}
/** index operator, non-const */
DATA & operator [] (const IDX index)
{
static DATA dummy;
if (!QLinkedList<Pair>::isEmpty()) {
QMutableLinkedListIterator<Pair> it(*this);
while (it.hasNext()) {
Pair &p = it.next();
if (p.first == index) {
if (p.first != QLinkedList<Pair>::first().first) {
// found it -> move the entry to the start of the list
Pair pair = p;
it.remove();
insert(pair.first, pair.second);
// qDebug("Kwave::MemoryManager[%9d] - reordering",
// pair.first);
}
// get the newly entered entry again
return QLinkedList<Pair>::first().second;
}
}
}
return dummy;
}
/**
* checks whether an index is contained
* @param index the index (key) of the element to look up
* @return true if found, otherwise false
*/
bool contains(const IDX index) const
{
foreach(const Pair &p, *this) {
if (p.first == index)
return true;
}
return false;
}
/**
* remove an entry
* @param index the index (key) of the element to remove
*/
void remove(const IDX index)
{
if (!QLinkedList<Pair>::isEmpty()) {
QMutableLinkedListIterator<Pair> it(*this);
while (it.hasNext()) {
Pair &p = it.next();
if (p.first == index) {
it.remove();
break;
}
}
}
}
/**
* insert a new entry to the start of the list (newest)
* @param index the index (key) of the element to insert
* @param value the data of the element to insert
*/
void insert(const IDX index, DATA &value)
{
QLinkedList<Pair>::prepend(Pair(index, value));
}
/** returns a list of keys */
QList<IDX> keys() const
{
QList<IDX> i;
foreach(const Pair &p, *this) {
i << p.first;
}
return i;
}
/** returns a list of values */
QList<DATA> values() const
{
QList<DATA> v;
foreach(const Pair &p, *this) {
v << p.second;
}
return v;
}
};
}
#endif /* _LRU_CACHE_H_ */