blob: da85a9aebbb1713e66fac34566d08088d1050a79 [file] [log] [blame]
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -08001/*
2 * Copyright (C) 2010 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
17#ifndef ANDROID_UTILS_GENERATION_CACHE_H
18#define ANDROID_UTILS_GENERATION_CACHE_H
19
20#include <utils/KeyedVector.h>
21#include <utils/RefBase.h>
22
23namespace android {
24
25/**
26 * GenerationCache callback used when an item is removed
27 */
28template<typename EntryKey, typename EntryValue>
29class OnEntryRemoved {
30public:
31 virtual ~OnEntryRemoved() { };
32 virtual void operator()(EntryKey& key, EntryValue& value) = 0;
33}; // class OnEntryRemoved
34
35template<typename EntryKey, typename EntryValue>
36struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > {
Jeff Brownb4293fe2011-11-11 15:40:13 -080037 Entry(const Entry<EntryKey, EntryValue>& e) :
38 key(e.key), value(e.value),
39 parent(e.parent), child(e.child) { }
40 Entry(const EntryKey& key, const EntryValue& value) :
41 key(key), value(value) { }
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080042
43 EntryKey key;
44 EntryValue value;
45
Jeff Brownb4293fe2011-11-11 15:40:13 -080046 sp<Entry<EntryKey, EntryValue> > parent; // next older entry
47 sp<Entry<EntryKey, EntryValue> > child; // next younger entry
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080048}; // struct Entry
49
50/**
51 * A LRU type cache
52 */
53template<typename K, typename V>
54class GenerationCache {
55public:
56 GenerationCache(uint32_t maxCapacity);
57 virtual ~GenerationCache();
58
59 enum Capacity {
60 kUnlimitedCapacity,
61 };
62
63 void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener);
64
Jeff Brownb4293fe2011-11-11 15:40:13 -080065 size_t size() const;
66
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080067 void clear();
68
Jeff Brownb4293fe2011-11-11 15:40:13 -080069 bool contains(const K& key) const;
70 const K& getKeyAt(size_t index) const;
71 const V& getValueAt(size_t index) const;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080072
Jeff Brownb4293fe2011-11-11 15:40:13 -080073 const V& get(const K& key);
74 bool put(const K& key, const V& value);
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080075
Jeff Brownb4293fe2011-11-11 15:40:13 -080076 void removeAt(ssize_t index);
77 bool remove(const K& key);
78 bool removeOldest();
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080079
80private:
81 KeyedVector<K, sp<Entry<K, V> > > mCache;
82 uint32_t mMaxCapacity;
83
84 OnEntryRemoved<K, V>* mListener;
85
86 sp<Entry<K, V> > mOldest;
87 sp<Entry<K, V> > mYoungest;
Jeff Brownb4293fe2011-11-11 15:40:13 -080088
89 void attachToCache(const sp<Entry<K, V> >& entry);
90 void detachFromCache(const sp<Entry<K, V> >& entry);
Romain Guy870165d2011-12-08 18:19:39 -080091
92 const V mNullValue;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080093}; // class GenerationCache
94
95template<typename K, typename V>
96GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity),
Romain Guy870165d2011-12-08 18:19:39 -080097 mListener(NULL), mNullValue(NULL) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -080098};
99
100template<typename K, typename V>
101GenerationCache<K, V>::~GenerationCache() {
102 clear();
103};
104
105template<typename K, typename V>
106uint32_t GenerationCache<K, V>::size() const {
107 return mCache.size();
108}
109
110/**
111 * Should be set by the user of the Cache so that the callback is called whenever an item is
112 * removed from the cache
113 */
114template<typename K, typename V>
115void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
116 mListener = listener;
117}
118
119template<typename K, typename V>
120void GenerationCache<K, V>::clear() {
121 if (mListener) {
122 for (uint32_t i = 0; i < mCache.size(); i++) {
123 sp<Entry<K, V> > entry = mCache.valueAt(i);
124 if (mListener) {
125 (*mListener)(entry->key, entry->value);
126 }
127 }
128 }
129 mCache.clear();
130 mYoungest.clear();
131 mOldest.clear();
132}
133
134template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800135bool GenerationCache<K, V>::contains(const K& key) const {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800136 return mCache.indexOfKey(key) >= 0;
137}
138
139template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800140const K& GenerationCache<K, V>::getKeyAt(size_t index) const {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800141 return mCache.keyAt(index);
142}
143
144template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800145const V& GenerationCache<K, V>::getValueAt(size_t index) const {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800146 return mCache.valueAt(index)->value;
147}
148
149template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800150const V& GenerationCache<K, V>::get(const K& key) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800151 ssize_t index = mCache.indexOfKey(key);
152 if (index >= 0) {
Jeff Brownb4293fe2011-11-11 15:40:13 -0800153 const sp<Entry<K, V> >& entry = mCache.valueAt(index);
154 detachFromCache(entry);
155 attachToCache(entry);
156 return entry->value;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800157 }
158
Romain Guy870165d2011-12-08 18:19:39 -0800159 return mNullValue;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800160}
161
162template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800163bool GenerationCache<K, V>::put(const K& key, const V& value) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800164 if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) {
165 removeOldest();
166 }
167
168 ssize_t index = mCache.indexOfKey(key);
169 if (index < 0) {
Jeff Brownb4293fe2011-11-11 15:40:13 -0800170 sp<Entry<K, V> > entry = new Entry<K, V>(key, value);
171 mCache.add(key, entry);
172 attachToCache(entry);
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800173 return true;
174 }
175
176 return false;
177}
178
179template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800180bool GenerationCache<K, V>::remove(const K& key) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800181 ssize_t index = mCache.indexOfKey(key);
182 if (index >= 0) {
Jeff Brownb4293fe2011-11-11 15:40:13 -0800183 removeAt(index);
184 return true;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800185 }
186
Jeff Brownb4293fe2011-11-11 15:40:13 -0800187 return false;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800188}
189
190template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800191void GenerationCache<K, V>::removeAt(ssize_t index) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800192 sp<Entry<K, V> > entry = mCache.valueAt(index);
193 if (mListener) {
194 (*mListener)(entry->key, entry->value);
195 }
196 mCache.removeItemsAt(index, 1);
197 detachFromCache(entry);
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800198}
199
200template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800201bool GenerationCache<K, V>::removeOldest() {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800202 if (mOldest.get()) {
203 ssize_t index = mCache.indexOfKey(mOldest->key);
204 if (index >= 0) {
Jeff Brownb4293fe2011-11-11 15:40:13 -0800205 removeAt(index);
206 return true;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800207 }
Jeff Brownb4293fe2011-11-11 15:40:13 -0800208 LOGE("GenerationCache: removeOldest failed to find the item in the cache "
209 "with the given key, but we know it must be in there. "
210 "Is the key comparator kaput?");
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800211 }
212
Jeff Brownb4293fe2011-11-11 15:40:13 -0800213 return false;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800214}
215
216template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800217void GenerationCache<K, V>::attachToCache(const sp<Entry<K, V> >& entry) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800218 if (!mYoungest.get()) {
219 mYoungest = mOldest = entry;
220 } else {
221 entry->parent = mYoungest;
222 mYoungest->child = entry;
223 mYoungest = entry;
224 }
225}
226
227template<typename K, typename V>
Jeff Brownb4293fe2011-11-11 15:40:13 -0800228void GenerationCache<K, V>::detachFromCache(const sp<Entry<K, V> >& entry) {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800229 if (entry->parent.get()) {
230 entry->parent->child = entry->child;
Jeff Brownb4293fe2011-11-11 15:40:13 -0800231 } else {
232 mOldest = entry->child;
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800233 }
234
235 if (entry->child.get()) {
236 entry->child->parent = entry->parent;
Jeff Brownb4293fe2011-11-11 15:40:13 -0800237 } else {
Fabrice Di Meglioe88a9a42011-02-24 19:56:18 -0800238 mYoungest = entry->parent;
239 }
240
241 entry->parent.clear();
242 entry->child.clear();
243}
244
245}; // namespace android
246
247#endif // ANDROID_UTILS_GENERATION_CACHE_H