diff --git a/include/utils/GenerationCache.h b/include/utils/GenerationCache.h index bb9ddd677..83cda8689 100644 --- a/include/utils/GenerationCache.h +++ b/include/utils/GenerationCache.h @@ -34,17 +34,17 @@ public: template struct Entry: public LightRefBase > { - Entry() { } - Entry(const Entry& e): - key(e.key), value(e.value), parent(e.parent), child(e.child) { } - Entry(sp > e): - key(e->key), value(e->value), parent(e->parent), child(e->child) { } + Entry(const Entry& e) : + key(e.key), value(e.value), + parent(e.parent), child(e.child) { } + Entry(const EntryKey& key, const EntryValue& value) : + key(key), value(value) { } EntryKey key; EntryValue value; - sp > parent; - sp > child; + sp > parent; // next older entry + sp > child; // next younger entry }; // struct Entry /** @@ -62,23 +62,20 @@ public: void setOnEntryRemovedListener(OnEntryRemoved* listener); + size_t size() const; + void clear(); - bool contains(K key) const; - V get(K key); - K getKeyAt(uint32_t index) const; - bool put(K key, V value); - V remove(K key); - V removeOldest(); - V getValueAt(uint32_t index) const; + bool contains(const K& key) const; + const K& getKeyAt(size_t index) const; + const V& getValueAt(size_t index) const; - uint32_t size() const; + const V& get(const K& key); + bool put(const K& key, const V& value); - void addToCache(sp > entry, K key, V value); - void attachToCache(sp > entry); - void detachFromCache(sp > entry); - - V removeAt(ssize_t index); + void removeAt(ssize_t index); + bool remove(const K& key); + bool removeOldest(); private: KeyedVector > > mCache; @@ -88,6 +85,9 @@ private: sp > mOldest; sp > mYoungest; + + void attachToCache(const sp >& entry); + void detachFromCache(const sp >& entry); }; // class GenerationCache template @@ -130,45 +130,44 @@ void GenerationCache::clear() { } template -bool GenerationCache::contains(K key) const { +bool GenerationCache::contains(const K& key) const { return mCache.indexOfKey(key) >= 0; } template -K GenerationCache::getKeyAt(uint32_t index) const { +const K& GenerationCache::getKeyAt(size_t index) const { return mCache.keyAt(index); } template -V GenerationCache::getValueAt(uint32_t index) const { +const V& GenerationCache::getValueAt(size_t index) const { return mCache.valueAt(index)->value; } template -V GenerationCache::get(K key) { +const V& GenerationCache::get(const K& key) { ssize_t index = mCache.indexOfKey(key); if (index >= 0) { - sp > entry = mCache.valueAt(index); - if (entry.get()) { - detachFromCache(entry); - attachToCache(entry); - return entry->value; - } + const sp >& entry = mCache.valueAt(index); + detachFromCache(entry); + attachToCache(entry); + return entry->value; } return NULL; } template -bool GenerationCache::put(K key, V value) { +bool GenerationCache::put(const K& key, const V& value) { if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) { removeOldest(); } ssize_t index = mCache.indexOfKey(key); if (index < 0) { - sp > entry = new Entry; - addToCache(entry, key, value); + sp > entry = new Entry(key, value); + mCache.add(key, entry); + attachToCache(entry); return true; } @@ -176,49 +175,44 @@ bool GenerationCache::put(K key, V value) { } template -void GenerationCache::addToCache(sp > entry, K key, V value) { - entry->key = key; - entry->value = value; - mCache.add(key, entry); - attachToCache(entry); -} - -template -V GenerationCache::remove(K key) { +bool GenerationCache::remove(const K& key) { ssize_t index = mCache.indexOfKey(key); if (index >= 0) { - return removeAt(index); + removeAt(index); + return true; } - return NULL; + return false; } template -V GenerationCache::removeAt(ssize_t index) { +void GenerationCache::removeAt(ssize_t index) { sp > entry = mCache.valueAt(index); if (mListener) { (*mListener)(entry->key, entry->value); } mCache.removeItemsAt(index, 1); detachFromCache(entry); - - return entry->value; } template -V GenerationCache::removeOldest() { +bool GenerationCache::removeOldest() { if (mOldest.get()) { ssize_t index = mCache.indexOfKey(mOldest->key); if (index >= 0) { - return removeAt(index); + removeAt(index); + return true; } + LOGE("GenerationCache: removeOldest failed to find the item in the cache " + "with the given key, but we know it must be in there. " + "Is the key comparator kaput?"); } - return NULL; + return false; } template -void GenerationCache::attachToCache(sp > entry) { +void GenerationCache::attachToCache(const sp >& entry) { if (!mYoungest.get()) { mYoungest = mOldest = entry; } else { @@ -229,20 +223,16 @@ void GenerationCache::attachToCache(sp > entry) { } template -void GenerationCache::detachFromCache(sp > entry) { +void GenerationCache::detachFromCache(const sp >& entry) { if (entry->parent.get()) { entry->parent->child = entry->child; + } else { + mOldest = entry->child; } if (entry->child.get()) { entry->child->parent = entry->parent; - } - - if (mOldest == entry) { - mOldest = entry->child; - } - - if (mYoungest == entry) { + } else { mYoungest = entry->parent; }