blob: 7818b4e2ebf2dbecf63f9b664cb4711a4779f647 [file] [log] [blame]
Raph Levienb6ea1752012-10-25 23:11:13 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guyb3176ac2012-11-28 12:59:40 -080017#ifndef ANDROID_UTILS_LRU_CACHE_H
18#define ANDROID_UTILS_LRU_CACHE_H
19
Sergio Girobb58cde2015-09-25 18:03:18 +010020#include <unordered_set>
21
Kenny Root0ddfe112013-09-11 23:41:23 -070022#include <UniquePtr.h>
Sergio Girobb58cde2015-09-25 18:03:18 +010023
24#include "utils/TypeHelpers.h" // hash_t
Raph Levienb6ea1752012-10-25 23:11:13 -070025
26namespace android {
27
Mathias Agopian9eb2a3b2013-05-06 20:20:50 -070028/**
29 * GenerationCache callback used when an item is removed
30 */
31template<typename EntryKey, typename EntryValue>
32class OnEntryRemoved {
33public:
34 virtual ~OnEntryRemoved() { };
35 virtual void operator()(EntryKey& key, EntryValue& value) = 0;
36}; // class OnEntryRemoved
Raph Levienb6ea1752012-10-25 23:11:13 -070037
38template <typename TKey, typename TValue>
39class LruCache {
40public:
41 explicit LruCache(uint32_t maxCapacity);
Sergio Girobb58cde2015-09-25 18:03:18 +010042 virtual ~LruCache();
Raph Levienb6ea1752012-10-25 23:11:13 -070043
44 enum Capacity {
45 kUnlimitedCapacity,
46 };
47
48 void setOnEntryRemovedListener(OnEntryRemoved<TKey, TValue>* listener);
49 size_t size() const;
50 const TValue& get(const TKey& key);
51 bool put(const TKey& key, const TValue& value);
52 bool remove(const TKey& key);
53 bool removeOldest();
54 void clear();
John Reck9d8707c2014-04-11 19:14:15 -070055 const TValue& peekOldestValue();
Raph Levienb6ea1752012-10-25 23:11:13 -070056
57private:
58 LruCache(const LruCache& that); // disallow copy constructor
59
60 struct Entry {
61 TKey key;
62 TValue value;
63 Entry* parent;
64 Entry* child;
65
66 Entry(TKey key_, TValue value_) : key(key_), value(value_), parent(NULL), child(NULL) {
67 }
68 const TKey& getKey() const { return key; }
69 };
70
Sergio Girobb58cde2015-09-25 18:03:18 +010071 struct HashForEntry : public std::unary_function<Entry*, hash_t> {
72 size_t operator() (const Entry* entry) const {
73 return hash_type(entry->key);
74 };
75 };
76
77 struct EqualityForHashedEntries : public std::unary_function<Entry*, hash_t> {
78 bool operator() (const Entry* lhs, const Entry* rhs) const {
79 return lhs->key == rhs->key;
80 };
81 };
82
83 typedef std::unordered_set<Entry*, HashForEntry, EqualityForHashedEntries> LruCacheSet;
84
Raph Levienb6ea1752012-10-25 23:11:13 -070085 void attachToCache(Entry& entry);
86 void detachFromCache(Entry& entry);
Raph Levienb6ea1752012-10-25 23:11:13 -070087
Sergio Girobb58cde2015-09-25 18:03:18 +010088 typename LruCacheSet::iterator findByKey(const TKey& key) {
89 Entry entryForSearch(key, mNullValue);
90 typename LruCacheSet::iterator result = mSet->find(&entryForSearch);
91 return result;
92 }
93
94 UniquePtr<LruCacheSet> mSet;
Raph Levienb6ea1752012-10-25 23:11:13 -070095 OnEntryRemoved<TKey, TValue>* mListener;
96 Entry* mOldest;
97 Entry* mYoungest;
98 uint32_t mMaxCapacity;
99 TValue mNullValue;
Sergio Girobb58cde2015-09-25 18:03:18 +0100100
101public:
102 class Iterator {
103 public:
104 Iterator(const LruCache<TKey, TValue>& cache): mCache(cache), mIterator(mCache.mSet->begin()) {
105 }
106
107 bool next() {
108 if (mIterator == mCache.mSet->end()) {
109 return false;
110 }
111 std::advance(mIterator, 1);
112 return mIterator != mCache.mSet->end();
113 }
114
115 const TValue& value() const {
116 return (*mIterator)->value;
117 }
118
119 const TKey& key() const {
120 return (*mIterator)->key;
121 }
122 private:
123 const LruCache<TKey, TValue>& mCache;
124 typename LruCacheSet::iterator mIterator;
125 };
Raph Levienb6ea1752012-10-25 23:11:13 -0700126};
127
128// Implementation is here, because it's fully templated
129template <typename TKey, typename TValue>
Mark Salyzyn48878422014-05-22 16:08:52 -0700130LruCache<TKey, TValue>::LruCache(uint32_t maxCapacity)
Sergio Girobb58cde2015-09-25 18:03:18 +0100131 : mSet(new LruCacheSet())
Mark Salyzyn48878422014-05-22 16:08:52 -0700132 , mListener(NULL)
133 , mOldest(NULL)
134 , mYoungest(NULL)
135 , mMaxCapacity(maxCapacity)
136 , mNullValue(NULL) {
Sergio Girobb58cde2015-09-25 18:03:18 +0100137 mSet->max_load_factor(1.0);
138};
139
140template <typename TKey, typename TValue>
141LruCache<TKey, TValue>::~LruCache() {
142 // Need to delete created entries.
143 clear();
Raph Levienb6ea1752012-10-25 23:11:13 -0700144};
145
146template<typename K, typename V>
147void LruCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
148 mListener = listener;
149}
150
151template <typename TKey, typename TValue>
152size_t LruCache<TKey, TValue>::size() const {
Sergio Girobb58cde2015-09-25 18:03:18 +0100153 return mSet->size();
Raph Levienb6ea1752012-10-25 23:11:13 -0700154}
155
156template <typename TKey, typename TValue>
157const TValue& LruCache<TKey, TValue>::get(const TKey& key) {
Sergio Girobb58cde2015-09-25 18:03:18 +0100158 typename LruCacheSet::const_iterator find_result = findByKey(key);
159 if (find_result == mSet->end()) {
Raph Levienb6ea1752012-10-25 23:11:13 -0700160 return mNullValue;
161 }
Sergio Girobb58cde2015-09-25 18:03:18 +0100162 Entry *entry = *find_result;
163 detachFromCache(*entry);
164 attachToCache(*entry);
165 return entry->value;
Raph Levienb6ea1752012-10-25 23:11:13 -0700166}
167
168template <typename TKey, typename TValue>
169bool LruCache<TKey, TValue>::put(const TKey& key, const TValue& value) {
170 if (mMaxCapacity != kUnlimitedCapacity && size() >= mMaxCapacity) {
171 removeOldest();
172 }
173
Sergio Girobb58cde2015-09-25 18:03:18 +0100174 if (findByKey(key) != mSet->end()) {
Raph Levienb6ea1752012-10-25 23:11:13 -0700175 return false;
176 }
Raph Levienb6ea1752012-10-25 23:11:13 -0700177
Sergio Girobb58cde2015-09-25 18:03:18 +0100178 Entry* newEntry = new Entry(key, value);
179 mSet->insert(newEntry);
180 attachToCache(*newEntry);
Raph Levienb6ea1752012-10-25 23:11:13 -0700181 return true;
182}
183
184template <typename TKey, typename TValue>
185bool LruCache<TKey, TValue>::remove(const TKey& key) {
Sergio Girobb58cde2015-09-25 18:03:18 +0100186 typename LruCacheSet::const_iterator find_result = findByKey(key);
187 if (find_result == mSet->end()) {
Raph Levienb6ea1752012-10-25 23:11:13 -0700188 return false;
189 }
Sergio Girobb58cde2015-09-25 18:03:18 +0100190 Entry* entry = *find_result;
Raph Levienb6ea1752012-10-25 23:11:13 -0700191 if (mListener) {
Sergio Girobb58cde2015-09-25 18:03:18 +0100192 (*mListener)(entry->key, entry->value);
Raph Levienb6ea1752012-10-25 23:11:13 -0700193 }
Sergio Girobb58cde2015-09-25 18:03:18 +0100194 detachFromCache(*entry);
195 mSet->erase(entry);
196 delete entry;
Raph Levienb6ea1752012-10-25 23:11:13 -0700197 return true;
198}
199
200template <typename TKey, typename TValue>
201bool LruCache<TKey, TValue>::removeOldest() {
202 if (mOldest != NULL) {
203 return remove(mOldest->key);
204 // TODO: should probably abort if false
205 }
206 return false;
207}
208
209template <typename TKey, typename TValue>
John Reck9d8707c2014-04-11 19:14:15 -0700210const TValue& LruCache<TKey, TValue>::peekOldestValue() {
211 if (mOldest) {
212 return mOldest->value;
213 }
214 return mNullValue;
215}
216
217template <typename TKey, typename TValue>
Raph Levienb6ea1752012-10-25 23:11:13 -0700218void LruCache<TKey, TValue>::clear() {
219 if (mListener) {
220 for (Entry* p = mOldest; p != NULL; p = p->child) {
221 (*mListener)(p->key, p->value);
222 }
223 }
224 mYoungest = NULL;
225 mOldest = NULL;
Sergio Girobb58cde2015-09-25 18:03:18 +0100226 for (auto entry : *mSet.get()) {
227 delete entry;
228 }
229 mSet->clear();
Raph Levienb6ea1752012-10-25 23:11:13 -0700230}
231
232template <typename TKey, typename TValue>
233void LruCache<TKey, TValue>::attachToCache(Entry& entry) {
234 if (mYoungest == NULL) {
235 mYoungest = mOldest = &entry;
236 } else {
237 entry.parent = mYoungest;
238 mYoungest->child = &entry;
239 mYoungest = &entry;
240 }
241}
242
243template <typename TKey, typename TValue>
244void LruCache<TKey, TValue>::detachFromCache(Entry& entry) {
245 if (entry.parent != NULL) {
246 entry.parent->child = entry.child;
247 } else {
248 mOldest = entry.child;
249 }
250 if (entry.child != NULL) {
251 entry.child->parent = entry.parent;
252 } else {
253 mYoungest = entry.parent;
254 }
255
256 entry.parent = NULL;
257 entry.child = NULL;
258}
259
Raph Levienb6ea1752012-10-25 23:11:13 -0700260}
Romain Guyb3176ac2012-11-28 12:59:40 -0800261#endif // ANDROID_UTILS_LRU_CACHE_H