blob: 83cda8689fd1cd56037cf27abef1be56d6551e65 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
Fabrice Di Megliod313c662011-02-24 19:56:18 -080017#ifndef ANDROID_UTILS_GENERATION_CACHE_H
18#define ANDROID_UTILS_GENERATION_CACHE_H
Romain Guyce0537b2010-06-29 21:05:21 -070019
20#include <utils/KeyedVector.h>
21#include <utils/RefBase.h>
22
23namespace android {
Romain Guyce0537b2010-06-29 21:05:21 -070024
Fabrice Di Megliod313c662011-02-24 19:56:18 -080025/**
26 * GenerationCache callback used when an item is removed
27 */
Romain Guyce0537b2010-06-29 21:05:21 -070028template<typename EntryKey, typename EntryValue>
29class OnEntryRemoved {
30public:
31 virtual ~OnEntryRemoved() { };
Romain Guydda570202010-07-06 11:39:32 -070032 virtual void operator()(EntryKey& key, EntryValue& value) = 0;
Romain Guyce0537b2010-06-29 21:05:21 -070033}; // class OnEntryRemoved
34
Romain Guy5f0c6a42010-07-07 13:06:26 -070035template<typename EntryKey, typename EntryValue>
36struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > {
Jeff Brownd9e688c2011-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) { }
Romain Guy5f0c6a42010-07-07 13:06:26 -070042
43 EntryKey key;
44 EntryValue value;
Romain Guy5f0c6a42010-07-07 13:06:26 -070045
Jeff Brownd9e688c2011-11-11 15:40:13 -080046 sp<Entry<EntryKey, EntryValue> > parent; // next older entry
47 sp<Entry<EntryKey, EntryValue> > child; // next younger entry
Romain Guy5f0c6a42010-07-07 13:06:26 -070048}; // struct Entry
49
Fabrice Di Megliod313c662011-02-24 19:56:18 -080050/**
51 * A LRU type cache
52 */
Romain Guyce0537b2010-06-29 21:05:21 -070053template<typename K, typename V>
54class GenerationCache {
55public:
Romain Guy5f0c6a42010-07-07 13:06:26 -070056 GenerationCache(uint32_t maxCapacity);
57 virtual ~GenerationCache();
Romain Guyce0537b2010-06-29 21:05:21 -070058
Romain Guy121e22422010-07-01 18:26:52 -070059 enum Capacity {
60 kUnlimitedCapacity,
61 };
62
Romain Guydda570202010-07-06 11:39:32 -070063 void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener);
Romain Guyce0537b2010-06-29 21:05:21 -070064
Jeff Brownd9e688c2011-11-11 15:40:13 -080065 size_t size() const;
66
Romain Guyce0537b2010-06-29 21:05:21 -070067 void clear();
68
Jeff Brownd9e688c2011-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;
Romain Guyce0537b2010-06-29 21:05:21 -070072
Jeff Brownd9e688c2011-11-11 15:40:13 -080073 const V& get(const K& key);
74 bool put(const K& key, const V& value);
Romain Guyce0537b2010-06-29 21:05:21 -070075
Jeff Brownd9e688c2011-11-11 15:40:13 -080076 void removeAt(ssize_t index);
77 bool remove(const K& key);
78 bool removeOldest();
Romain Guy5f0c6a42010-07-07 13:06:26 -070079
Fabrice Di Megliod313c662011-02-24 19:56:18 -080080private:
Romain Guy6c818932010-07-07 15:15:32 -070081 KeyedVector<K, sp<Entry<K, V> > > mCache;
Romain Guy7d139ba2010-07-02 11:20:34 -070082 uint32_t mMaxCapacity;
Romain Guyce0537b2010-06-29 21:05:21 -070083
Romain Guydda570202010-07-06 11:39:32 -070084 OnEntryRemoved<K, V>* mListener;
Romain Guyce0537b2010-06-29 21:05:21 -070085
Romain Guydda570202010-07-06 11:39:32 -070086 sp<Entry<K, V> > mOldest;
Romain Guy5f0c6a42010-07-07 13:06:26 -070087 sp<Entry<K, V> > mYoungest;
Jeff Brownd9e688c2011-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 Guyce0537b2010-06-29 21:05:21 -070091}; // class GenerationCache
92
93template<typename K, typename V>
Fabrice Di Megliod313c662011-02-24 19:56:18 -080094GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity),
95 mListener(NULL) {
Romain Guy5f0c6a42010-07-07 13:06:26 -070096};
97
98template<typename K, typename V>
99GenerationCache<K, V>::~GenerationCache() {
100 clear();
Romain Guy5f0c6a42010-07-07 13:06:26 -0700101};
102
103template<typename K, typename V>
Romain Guy7d139ba2010-07-02 11:20:34 -0700104uint32_t GenerationCache<K, V>::size() const {
Romain Guy6c818932010-07-07 15:15:32 -0700105 return mCache.size();
Romain Guyce0537b2010-06-29 21:05:21 -0700106}
107
Fabrice Di Megliod313c662011-02-24 19:56:18 -0800108/**
109 * Should be set by the user of the Cache so that the callback is called whenever an item is
110 * removed from the cache
111 */
Romain Guyce0537b2010-06-29 21:05:21 -0700112template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700113void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
Romain Guyce0537b2010-06-29 21:05:21 -0700114 mListener = listener;
115}
116
117template<typename K, typename V>
118void GenerationCache<K, V>::clear() {
119 if (mListener) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700120 for (uint32_t i = 0; i < mCache.size(); i++) {
121 sp<Entry<K, V> > entry = mCache.valueAt(i);
122 if (mListener) {
123 (*mListener)(entry->key, entry->value);
124 }
Romain Guyce0537b2010-06-29 21:05:21 -0700125 }
Romain Guyce0537b2010-06-29 21:05:21 -0700126 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700127 mCache.clear();
Romain Guy5f0c6a42010-07-07 13:06:26 -0700128 mYoungest.clear();
Romain Guyce0537b2010-06-29 21:05:21 -0700129 mOldest.clear();
130}
131
132template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800133bool GenerationCache<K, V>::contains(const K& key) const {
Romain Guy6c818932010-07-07 15:15:32 -0700134 return mCache.indexOfKey(key) >= 0;
Romain Guyce0537b2010-06-29 21:05:21 -0700135}
136
137template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800138const K& GenerationCache<K, V>::getKeyAt(size_t index) const {
Romain Guya2341a92010-09-08 18:04:33 -0700139 return mCache.keyAt(index);
140}
141
142template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800143const V& GenerationCache<K, V>::getValueAt(size_t index) const {
Romain Guyfe48f652010-11-11 15:36:56 -0800144 return mCache.valueAt(index)->value;
Romain Guyeb993562010-10-05 18:14:38 -0700145}
146
147template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800148const V& GenerationCache<K, V>::get(const K& key) {
Romain Guy6c818932010-07-07 15:15:32 -0700149 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700150 if (index >= 0) {
Jeff Brownd9e688c2011-11-11 15:40:13 -0800151 const sp<Entry<K, V> >& entry = mCache.valueAt(index);
152 detachFromCache(entry);
153 attachToCache(entry);
154 return entry->value;
Romain Guyce0537b2010-06-29 21:05:21 -0700155 }
156
157 return NULL;
158}
159
160template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800161bool GenerationCache<K, V>::put(const K& key, const V& value) {
Romain Guy6c818932010-07-07 15:15:32 -0700162 if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) {
Romain Guyce0537b2010-06-29 21:05:21 -0700163 removeOldest();
164 }
165
Romain Guy6c818932010-07-07 15:15:32 -0700166 ssize_t index = mCache.indexOfKey(key);
Romain Guy9aaa8262010-09-08 15:15:43 -0700167 if (index < 0) {
Jeff Brownd9e688c2011-11-11 15:40:13 -0800168 sp<Entry<K, V> > entry = new Entry<K, V>(key, value);
169 mCache.add(key, entry);
170 attachToCache(entry);
Romain Guy8550c4c2010-10-08 15:49:53 -0700171 return true;
Romain Guyce0537b2010-06-29 21:05:21 -0700172 }
Romain Guy8550c4c2010-10-08 15:49:53 -0700173
174 return false;
Romain Guyce0537b2010-06-29 21:05:21 -0700175}
176
177template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800178bool GenerationCache<K, V>::remove(const K& key) {
Romain Guy6c818932010-07-07 15:15:32 -0700179 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700180 if (index >= 0) {
Jeff Brownd9e688c2011-11-11 15:40:13 -0800181 removeAt(index);
182 return true;
Romain Guyce0537b2010-06-29 21:05:21 -0700183 }
184
Jeff Brownd9e688c2011-11-11 15:40:13 -0800185 return false;
Romain Guyce0537b2010-06-29 21:05:21 -0700186}
187
188template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800189void GenerationCache<K, V>::removeAt(ssize_t index) {
Romain Guy6c818932010-07-07 15:15:32 -0700190 sp<Entry<K, V> > entry = mCache.valueAt(index);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700191 if (mListener) {
192 (*mListener)(entry->key, entry->value);
193 }
Romain Guy6c818932010-07-07 15:15:32 -0700194 mCache.removeItemsAt(index, 1);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700195 detachFromCache(entry);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700196}
197
198template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800199bool GenerationCache<K, V>::removeOldest() {
Romain Guyce0537b2010-06-29 21:05:21 -0700200 if (mOldest.get()) {
Romain Guyae5575b2010-07-29 18:48:04 -0700201 ssize_t index = mCache.indexOfKey(mOldest->key);
202 if (index >= 0) {
Jeff Brownd9e688c2011-11-11 15:40:13 -0800203 removeAt(index);
204 return true;
Romain Guyae5575b2010-07-29 18:48:04 -0700205 }
Jeff Brownd9e688c2011-11-11 15:40:13 -0800206 LOGE("GenerationCache: removeOldest failed to find the item in the cache "
207 "with the given key, but we know it must be in there. "
208 "Is the key comparator kaput?");
Romain Guyce0537b2010-06-29 21:05:21 -0700209 }
Romain Guydda570202010-07-06 11:39:32 -0700210
Jeff Brownd9e688c2011-11-11 15:40:13 -0800211 return false;
Romain Guyce0537b2010-06-29 21:05:21 -0700212}
213
214template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800215void GenerationCache<K, V>::attachToCache(const sp<Entry<K, V> >& entry) {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700216 if (!mYoungest.get()) {
217 mYoungest = mOldest = entry;
Romain Guyce0537b2010-06-29 21:05:21 -0700218 } else {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700219 entry->parent = mYoungest;
220 mYoungest->child = entry;
221 mYoungest = entry;
Romain Guyce0537b2010-06-29 21:05:21 -0700222 }
223}
224
225template<typename K, typename V>
Jeff Brownd9e688c2011-11-11 15:40:13 -0800226void GenerationCache<K, V>::detachFromCache(const sp<Entry<K, V> >& entry) {
Romain Guyce0537b2010-06-29 21:05:21 -0700227 if (entry->parent.get()) {
228 entry->parent->child = entry->child;
Jeff Brownd9e688c2011-11-11 15:40:13 -0800229 } else {
230 mOldest = entry->child;
Romain Guyce0537b2010-06-29 21:05:21 -0700231 }
232
233 if (entry->child.get()) {
234 entry->child->parent = entry->parent;
Jeff Brownd9e688c2011-11-11 15:40:13 -0800235 } else {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700236 mYoungest = entry->parent;
Romain Guyce0537b2010-06-29 21:05:21 -0700237 }
238
239 entry->parent.clear();
240 entry->child.clear();
241}
242
Romain Guyce0537b2010-06-29 21:05:21 -0700243}; // namespace android
244
Fabrice Di Megliod313c662011-02-24 19:56:18 -0800245#endif // ANDROID_UTILS_GENERATION_CACHE_H