Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android { |
| 24 | |
| 25 | /** |
| 26 | * GenerationCache callback used when an item is removed |
| 27 | */ |
| 28 | template<typename EntryKey, typename EntryValue> |
| 29 | class OnEntryRemoved { |
| 30 | public: |
| 31 | virtual ~OnEntryRemoved() { }; |
| 32 | virtual void operator()(EntryKey& key, EntryValue& value) = 0; |
| 33 | }; // class OnEntryRemoved |
| 34 | |
| 35 | template<typename EntryKey, typename EntryValue> |
| 36 | struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > { |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 37 | 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 Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 42 | |
| 43 | EntryKey key; |
| 44 | EntryValue value; |
| 45 | |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 46 | sp<Entry<EntryKey, EntryValue> > parent; // next older entry |
| 47 | sp<Entry<EntryKey, EntryValue> > child; // next younger entry |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 48 | }; // struct Entry |
| 49 | |
| 50 | /** |
| 51 | * A LRU type cache |
| 52 | */ |
| 53 | template<typename K, typename V> |
| 54 | class GenerationCache { |
| 55 | public: |
| 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 Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 65 | size_t size() const; |
| 66 | |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 67 | void clear(); |
| 68 | |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 69 | bool contains(const K& key) const; |
| 70 | const K& getKeyAt(size_t index) const; |
| 71 | const V& getValueAt(size_t index) const; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 72 | |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 73 | const V& get(const K& key); |
| 74 | bool put(const K& key, const V& value); |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 75 | |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 76 | void removeAt(ssize_t index); |
| 77 | bool remove(const K& key); |
| 78 | bool removeOldest(); |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 79 | |
| 80 | private: |
| 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 Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 88 | |
| 89 | void attachToCache(const sp<Entry<K, V> >& entry); |
| 90 | void detachFromCache(const sp<Entry<K, V> >& entry); |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 91 | }; // class GenerationCache |
| 92 | |
| 93 | template<typename K, typename V> |
| 94 | GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), |
| 95 | mListener(NULL) { |
| 96 | }; |
| 97 | |
| 98 | template<typename K, typename V> |
| 99 | GenerationCache<K, V>::~GenerationCache() { |
| 100 | clear(); |
| 101 | }; |
| 102 | |
| 103 | template<typename K, typename V> |
| 104 | uint32_t GenerationCache<K, V>::size() const { |
| 105 | return mCache.size(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 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 | */ |
| 112 | template<typename K, typename V> |
| 113 | void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) { |
| 114 | mListener = listener; |
| 115 | } |
| 116 | |
| 117 | template<typename K, typename V> |
| 118 | void GenerationCache<K, V>::clear() { |
| 119 | if (mListener) { |
| 120 | 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 | } |
| 125 | } |
| 126 | } |
| 127 | mCache.clear(); |
| 128 | mYoungest.clear(); |
| 129 | mOldest.clear(); |
| 130 | } |
| 131 | |
| 132 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 133 | bool GenerationCache<K, V>::contains(const K& key) const { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 134 | return mCache.indexOfKey(key) >= 0; |
| 135 | } |
| 136 | |
| 137 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 138 | const K& GenerationCache<K, V>::getKeyAt(size_t index) const { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 139 | return mCache.keyAt(index); |
| 140 | } |
| 141 | |
| 142 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 143 | const V& GenerationCache<K, V>::getValueAt(size_t index) const { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 144 | return mCache.valueAt(index)->value; |
| 145 | } |
| 146 | |
| 147 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 148 | const V& GenerationCache<K, V>::get(const K& key) { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 149 | ssize_t index = mCache.indexOfKey(key); |
| 150 | if (index >= 0) { |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 151 | const sp<Entry<K, V> >& entry = mCache.valueAt(index); |
| 152 | detachFromCache(entry); |
| 153 | attachToCache(entry); |
| 154 | return entry->value; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 161 | bool GenerationCache<K, V>::put(const K& key, const V& value) { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 162 | if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) { |
| 163 | removeOldest(); |
| 164 | } |
| 165 | |
| 166 | ssize_t index = mCache.indexOfKey(key); |
| 167 | if (index < 0) { |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 168 | sp<Entry<K, V> > entry = new Entry<K, V>(key, value); |
| 169 | mCache.add(key, entry); |
| 170 | attachToCache(entry); |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 178 | bool GenerationCache<K, V>::remove(const K& key) { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 179 | ssize_t index = mCache.indexOfKey(key); |
| 180 | if (index >= 0) { |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 181 | removeAt(index); |
| 182 | return true; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 185 | return false; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 189 | void GenerationCache<K, V>::removeAt(ssize_t index) { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 190 | sp<Entry<K, V> > entry = mCache.valueAt(index); |
| 191 | if (mListener) { |
| 192 | (*mListener)(entry->key, entry->value); |
| 193 | } |
| 194 | mCache.removeItemsAt(index, 1); |
| 195 | detachFromCache(entry); |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 199 | bool GenerationCache<K, V>::removeOldest() { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 200 | if (mOldest.get()) { |
| 201 | ssize_t index = mCache.indexOfKey(mOldest->key); |
| 202 | if (index >= 0) { |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 203 | removeAt(index); |
| 204 | return true; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 205 | } |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 206 | 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?"); |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 211 | return false; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 215 | void GenerationCache<K, V>::attachToCache(const sp<Entry<K, V> >& entry) { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 216 | if (!mYoungest.get()) { |
| 217 | mYoungest = mOldest = entry; |
| 218 | } else { |
| 219 | entry->parent = mYoungest; |
| 220 | mYoungest->child = entry; |
| 221 | mYoungest = entry; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | template<typename K, typename V> |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 226 | void GenerationCache<K, V>::detachFromCache(const sp<Entry<K, V> >& entry) { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 227 | if (entry->parent.get()) { |
| 228 | entry->parent->child = entry->child; |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 229 | } else { |
| 230 | mOldest = entry->child; |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (entry->child.get()) { |
| 234 | entry->child->parent = entry->parent; |
Jeff Brown | b4293fe | 2011-11-11 15:40:13 -0800 | [diff] [blame^] | 235 | } else { |
Fabrice Di Meglio | e88a9a4 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 236 | mYoungest = entry->parent; |
| 237 | } |
| 238 | |
| 239 | entry->parent.clear(); |
| 240 | entry->child.clear(); |
| 241 | } |
| 242 | |
| 243 | }; // namespace android |
| 244 | |
| 245 | #endif // ANDROID_UTILS_GENERATION_CACHE_H |