Flutter Engine
android::LruCache< TKey, TValue > Class Template Reference

#include <LruCache.h>

Classes

class  Iterator
 

Public Types

enum  Capacity { kUnlimitedCapacity }
 

Public Member Functions

 LruCache (uint32_t maxCapacity)
 
virtual ~LruCache ()
 
void setOnEntryRemovedListener (OnEntryRemoved< TKey, TValue > *listener)
 
size_t size () const
 
const TValue & get (const TKey &key)
 
bool put (const TKey &key, const TValue &value)
 
bool remove (const TKey &key)
 
bool removeOldest ()
 
void clear ()
 
const TValue & peekOldestValue ()
 

Detailed Description

template<typename TKey, typename TValue>
class android::LruCache< TKey, TValue >

Definition at line 38 of file LruCache.h.

Member Enumeration Documentation

◆ Capacity

template<typename TKey, typename TValue>
enum android::LruCache::Capacity
Enumerator
kUnlimitedCapacity 

Definition at line 43 of file LruCache.h.

Constructor & Destructor Documentation

◆ LruCache()

template<typename TKey , typename TValue >
android::LruCache< TKey, TValue >::LruCache ( uint32_t  maxCapacity)
explicit

Definition at line 166 of file LruCache.h.

167  : mSet(new LruCacheSet()),
168  mListener(NULL),
169  mOldest(NULL),
170  mYoungest(NULL),
171  mMaxCapacity(maxCapacity),
172  mNullValue(0) {
173  mSet->max_load_factor(1.0);
174 };

◆ ~LruCache()

template<typename TKey , typename TValue >
android::LruCache< TKey, TValue >::~LruCache ( )
virtual

Definition at line 177 of file LruCache.h.

References android::LruCache< TKey, TValue >::clear().

177  {
178  // Need to delete created entries.
179  clear();
180 };

Member Function Documentation

◆ clear()

template<typename TKey , typename TValue >
void android::LruCache< TKey, TValue >::clear ( )

Definition at line 258 of file LruCache.h.

Referenced by android::LruCache< TKey, TValue >::~LruCache().

258  {
259  if (mListener) {
260  for (Entry* p = mOldest; p != NULL; p = p->child) {
261  (*mListener)(p->key, p->value);
262  }
263  }
264  mYoungest = NULL;
265  mOldest = NULL;
266  for (auto entry : *mSet.get()) {
267  delete entry;
268  }
269  mSet->clear();
270 }

◆ get()

template<typename TKey , typename TValue >
const TValue & android::LruCache< TKey, TValue >::get ( const TKey &  key)

Definition at line 193 of file LruCache.h.

193  {
194  typename LruCacheSet::const_iterator find_result = findByKey(key);
195  if (find_result == mSet->end()) {
196  return mNullValue;
197  }
198  // All the elements in the set are of type Entry. See comment in the
199  // definition of LruCacheSet above.
200  Entry* entry = reinterpret_cast<Entry*>(*find_result);
201  detachFromCache(*entry);
202  attachToCache(*entry);
203  return entry->value;
204 }

◆ peekOldestValue()

template<typename TKey , typename TValue >
const TValue & android::LruCache< TKey, TValue >::peekOldestValue ( )

Definition at line 250 of file LruCache.h.

250  {
251  if (mOldest) {
252  return mOldest->value;
253  }
254  return mNullValue;
255 }

◆ put()

template<typename TKey , typename TValue >
bool android::LruCache< TKey, TValue >::put ( const TKey &  key,
const TValue &  value 
)

Definition at line 207 of file LruCache.h.

References android::LruCache< TKey, TValue >::kUnlimitedCapacity, android::LruCache< TKey, TValue >::removeOldest(), and android::LruCache< TKey, TValue >::size().

207  {
208  if (mMaxCapacity != kUnlimitedCapacity && size() >= mMaxCapacity) {
209  removeOldest();
210  }
211 
212  if (findByKey(key) != mSet->end()) {
213  return false;
214  }
215 
216  Entry* newEntry = new Entry(key, value);
217  mSet->insert(newEntry);
218  attachToCache(*newEntry);
219  return true;
220 }
size_t size() const
Definition: LruCache.h:188
bool removeOldest()
Definition: LruCache.h:241
uint8_t value

◆ remove()

template<typename TKey , typename TValue >
bool android::LruCache< TKey, TValue >::remove ( const TKey &  key)

Definition at line 223 of file LruCache.h.

223  {
224  typename LruCacheSet::const_iterator find_result = findByKey(key);
225  if (find_result == mSet->end()) {
226  return false;
227  }
228  // All the elements in the set are of type Entry. See comment in the
229  // definition of LruCacheSet above.
230  Entry* entry = reinterpret_cast<Entry*>(*find_result);
231  mSet->erase(entry);
232  if (mListener) {
233  (*mListener)(entry->key, entry->value);
234  }
235  detachFromCache(*entry);
236  delete entry;
237  return true;
238 }

◆ removeOldest()

template<typename TKey , typename TValue >
bool android::LruCache< TKey, TValue >::removeOldest ( )

Definition at line 241 of file LruCache.h.

Referenced by android::LruCache< TKey, TValue >::put().

241  {
242  if (mOldest != NULL) {
243  return remove(mOldest->key);
244  // TODO: should probably abort if false
245  }
246  return false;
247 }

◆ setOnEntryRemovedListener()

template<typename TKey, typename TValue>
void android::LruCache< K, V >::setOnEntryRemovedListener ( OnEntryRemoved< TKey, TValue > *  listener)

Definition at line 183 of file LruCache.h.

183  {
184  mListener = listener;
185 }

◆ size()

template<typename TKey , typename TValue >
size_t android::LruCache< TKey, TValue >::size ( ) const

Definition at line 188 of file LruCache.h.

Referenced by android::LruCache< TKey, TValue >::put().

188  {
189  return mSet->size();
190 }

The documentation for this class was generated from the following file: