summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/LRUCache.h
blob: f9045262ca21a0a6304fc8fdbe2fb45bb39984be (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
/*
    Copyright (C) 2003 KSVG Team
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef LRUCACHE_H
#define LRUCACHE_H

#include <tqvaluelist.h>

namespace KSVG
{

// A value-based LRU cache with a maximum total cost constraint, but with the exception that the
// most recently added item is kept in the cache even if its cost exceeds the maximum total cost.
template<class keyType, class valueType>
class MinOneLRUCache
{
public:
	MinOneLRUCache(int maxTotalCost = 0) : m_maxTotalCost(maxTotalCost), m_totalCost(0) {}
	virtual ~MinOneLRUCache() {}

	void insert(const keyType& key, const valueType& value, int cost);
	bool find(const keyType& key, valueType& result);

	void setMaxTotalCost(int maxTotalCost);
	int maxTotalCost() const { return m_maxTotalCost; }
	int totalCost() const { return m_totalCost; }

	void clear();

protected:
	class CacheItem
	{
	public:
		CacheItem() : m_cost(0) {}
		CacheItem(const keyType& key, const valueType& value, int cost) : m_key(key), m_value(value), m_cost(cost) {}

		const keyType& key() const { return m_key; }
		const valueType& value() const { return m_value; }
		int cost() const { return m_cost; }

	private:
		keyType m_key;
		valueType m_value;
		int m_cost;
	};

	typedef TQValueList<CacheItem> CacheItemList;

	typename CacheItemList::iterator find(const keyType& key);
	void enforceCostConstraint();

	CacheItemList m_items;
	int m_maxTotalCost;
	int m_totalCost;
};

template<class keyType, class valueType>
void MinOneLRUCache<keyType, valueType>::insert(const keyType& key, const valueType& value, int cost)
{
	typename CacheItemList::iterator it = find(key);

	if(it != m_items.end())
	{
		// Replace the existing item.
		m_totalCost -= (*it).cost();
		m_items.erase(it);
	}

	// We always hold the most recently added item in the cache, even if it exceeds
	// the maximum total cost.
	m_items.push_front(CacheItem(key, value, cost));
	m_totalCost += cost;
	enforceCostConstraint();
}

template<class keyType, class valueType>
bool MinOneLRUCache<keyType, valueType>::find(const keyType& key, valueType& result)
{
	bool foundKey = false;
	typename CacheItemList::iterator it = find(key);

	if(it != m_items.end())
	{
		CacheItem item = *it;
		result = item.value();

		if(it != m_items.begin())
		{
			// This is now the most recently used item.
			m_items.erase(it);
			m_items.push_front(item);
		}

		foundKey = true;
	}

	return foundKey;
}

template<class keyType, class valueType>
typename MinOneLRUCache<keyType, valueType>::CacheItemList::iterator MinOneLRUCache<keyType, valueType>::find(const keyType& key)
{
	typename CacheItemList::iterator it;

	for(it = m_items.begin(); it != m_items.end(); it++)
	{
		if((*it).key() == key)
			break;
	}

	return it;
}

template<class keyType, class valueType>
void MinOneLRUCache<keyType, valueType>::enforceCostConstraint()
{
	if(m_totalCost > m_maxTotalCost && m_items.size() > 1)
	{
		typename CacheItemList::iterator it = m_items.begin();
		m_totalCost = (*it).cost();
		++it;

		while(it != m_items.end() && m_totalCost + (*it).cost() <= m_maxTotalCost)
		{
			m_totalCost += (*it).cost();
			++it;
		}

		// Remove the remainder
		while(it != m_items.end())
			it = m_items.erase(it);
	}
}

template<class keyType, class valueType>
void MinOneLRUCache<keyType, valueType>::setMaxTotalCost(int maxTotalCost)
{
	m_maxTotalCost = maxTotalCost;
	enforceCostConstraint();
}

template<class keyType, class valueType>
void MinOneLRUCache<keyType, valueType>::clear()
{
	m_items.clear();
	m_totalCost = 0;
}

}

#endif