Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | b3176ac | 2012-11-28 12:59:40 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_UTILS_LRU_CACHE_H |
| 18 | #define ANDROID_UTILS_LRU_CACHE_H |
| 19 | |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 20 | #include <unordered_set> |
| 21 | |
Kenny Root | 0ddfe11 | 2013-09-11 23:41:23 -0700 | [diff] [blame] | 22 | #include <UniquePtr.h> |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 23 | |
| 24 | #include "utils/TypeHelpers.h" // hash_t |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Mathias Agopian | 9eb2a3b | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 28 | /** |
| 29 | * GenerationCache callback used when an item is removed |
| 30 | */ |
| 31 | template<typename EntryKey, typename EntryValue> |
| 32 | class OnEntryRemoved { |
| 33 | public: |
| 34 | virtual ~OnEntryRemoved() { }; |
| 35 | virtual void operator()(EntryKey& key, EntryValue& value) = 0; |
| 36 | }; // class OnEntryRemoved |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 37 | |
| 38 | template <typename TKey, typename TValue> |
| 39 | class LruCache { |
| 40 | public: |
| 41 | explicit LruCache(uint32_t maxCapacity); |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 42 | virtual ~LruCache(); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 43 | |
| 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 Reck | 9d8707c | 2014-04-11 19:14:15 -0700 | [diff] [blame] | 55 | const TValue& peekOldestValue(); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
| 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 Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 71 | 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 Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 85 | void attachToCache(Entry& entry); |
| 86 | void detachFromCache(Entry& entry); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 87 | |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 88 | 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 Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 95 | OnEntryRemoved<TKey, TValue>* mListener; |
| 96 | Entry* mOldest; |
| 97 | Entry* mYoungest; |
| 98 | uint32_t mMaxCapacity; |
| 99 | TValue mNullValue; |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 100 | |
| 101 | public: |
| 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 Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | // Implementation is here, because it's fully templated |
| 129 | template <typename TKey, typename TValue> |
Mark Salyzyn | 4887842 | 2014-05-22 16:08:52 -0700 | [diff] [blame] | 130 | LruCache<TKey, TValue>::LruCache(uint32_t maxCapacity) |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 131 | : mSet(new LruCacheSet()) |
Mark Salyzyn | 4887842 | 2014-05-22 16:08:52 -0700 | [diff] [blame] | 132 | , mListener(NULL) |
| 133 | , mOldest(NULL) |
| 134 | , mYoungest(NULL) |
| 135 | , mMaxCapacity(maxCapacity) |
| 136 | , mNullValue(NULL) { |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 137 | mSet->max_load_factor(1.0); |
| 138 | }; |
| 139 | |
| 140 | template <typename TKey, typename TValue> |
| 141 | LruCache<TKey, TValue>::~LruCache() { |
| 142 | // Need to delete created entries. |
| 143 | clear(); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | template<typename K, typename V> |
| 147 | void LruCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) { |
| 148 | mListener = listener; |
| 149 | } |
| 150 | |
| 151 | template <typename TKey, typename TValue> |
| 152 | size_t LruCache<TKey, TValue>::size() const { |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 153 | return mSet->size(); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | template <typename TKey, typename TValue> |
| 157 | const TValue& LruCache<TKey, TValue>::get(const TKey& key) { |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 158 | typename LruCacheSet::const_iterator find_result = findByKey(key); |
| 159 | if (find_result == mSet->end()) { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 160 | return mNullValue; |
| 161 | } |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 162 | Entry *entry = *find_result; |
| 163 | detachFromCache(*entry); |
| 164 | attachToCache(*entry); |
| 165 | return entry->value; |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | template <typename TKey, typename TValue> |
| 169 | bool LruCache<TKey, TValue>::put(const TKey& key, const TValue& value) { |
| 170 | if (mMaxCapacity != kUnlimitedCapacity && size() >= mMaxCapacity) { |
| 171 | removeOldest(); |
| 172 | } |
| 173 | |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 174 | if (findByKey(key) != mSet->end()) { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 175 | return false; |
| 176 | } |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 177 | |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 178 | Entry* newEntry = new Entry(key, value); |
| 179 | mSet->insert(newEntry); |
| 180 | attachToCache(*newEntry); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
| 184 | template <typename TKey, typename TValue> |
| 185 | bool LruCache<TKey, TValue>::remove(const TKey& key) { |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 186 | typename LruCacheSet::const_iterator find_result = findByKey(key); |
| 187 | if (find_result == mSet->end()) { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 190 | Entry* entry = *find_result; |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 191 | if (mListener) { |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 192 | (*mListener)(entry->key, entry->value); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 193 | } |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 194 | detachFromCache(*entry); |
| 195 | mSet->erase(entry); |
| 196 | delete entry; |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
| 200 | template <typename TKey, typename TValue> |
| 201 | bool 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 | |
| 209 | template <typename TKey, typename TValue> |
John Reck | 9d8707c | 2014-04-11 19:14:15 -0700 | [diff] [blame] | 210 | const TValue& LruCache<TKey, TValue>::peekOldestValue() { |
| 211 | if (mOldest) { |
| 212 | return mOldest->value; |
| 213 | } |
| 214 | return mNullValue; |
| 215 | } |
| 216 | |
| 217 | template <typename TKey, typename TValue> |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 218 | void 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 Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame^] | 226 | for (auto entry : *mSet.get()) { |
| 227 | delete entry; |
| 228 | } |
| 229 | mSet->clear(); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | template <typename TKey, typename TValue> |
| 233 | void 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 | |
| 243 | template <typename TKey, typename TValue> |
| 244 | void 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 Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 260 | } |
Romain Guy | b3176ac | 2012-11-28 12:59:40 -0800 | [diff] [blame] | 261 | #endif // ANDROID_UTILS_LRU_CACHE_H |