From f1951df8a13e8ed7a8041b815bf7c476991adfc2 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Wed, 28 Nov 2012 18:26:54 -0800 Subject: [PATCH] Add LruCache::Iterator Required by libhwui Change-Id: I164b9a4a82d89d132da01a56535c0df084de86f7 --- include/utils/LruCache.h | 50 +++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/include/utils/LruCache.h b/include/utils/LruCache.h index 937fe1e01..302b929c7 100644 --- a/include/utils/LruCache.h +++ b/include/utils/LruCache.h @@ -36,15 +36,38 @@ public: void setOnEntryRemovedListener(OnEntryRemoved* listener); size_t size() const; - const TKey& keyAt(size_t index) const; - const TValue& valueAt(size_t index) const; - void removeAt(size_t index); const TValue& get(const TKey& key); bool put(const TKey& key, const TValue& value); bool remove(const TKey& key); bool removeOldest(); void clear(); + class Iterator { + public: + Iterator(const LruCache& cache): mCache(cache), mIndex(-1) { + } + + bool next() { + mIndex = mCache.mTable->next(mIndex); + return mIndex != -1; + } + + size_t index() const { + return mIndex; + } + + const TValue& value() const { + return mCache.mTable->entryAt(mIndex).value; + } + + const TKey& key() const { + return mCache.mTable->entryAt(mIndex).key; + } + private: + const LruCache& mCache; + size_t mIndex; + }; + private: LruCache(const LruCache& that); // disallow copy constructor @@ -88,27 +111,6 @@ size_t LruCache::size() const { return mTable->size(); } -template -const TKey& LruCache::keyAt(size_t index) const { - const Entry& entry = mTable->entryAt(index); - return entry.key; -} - -template -const TValue& LruCache::valueAt(size_t index) const { - const Entry& entry = mTable->entryAt(index); - return entry.value; -} - -template -void LruCache::removeAt(size_t index) { - if (index < 0) { - return; - } - - mTable->removeAt(index); -} - template const TValue& LruCache::get(const TKey& key) { hash_t hash = hash_type(key);