Aether
SDL2 based UI Framework for NX
Public Types | Public Member Functions | List of all members
Aether::LRUCache< Key, Value > Class Template Reference

A basic LRU (least recently used) cache implementation. Also supports a custom callback for when an item is removed. More...

#include <LRUCache.hpp>

Public Types

typedef std::pair< Key, Value > KeyValuePair
 
typedef std::list< KeyValuePair >::iterator ListIterator
 

Public Member Functions

 LRUCache ()
 Default constructor initializes a cache of size 100 and no callback.
 
 LRUCache (const unsigned int size, const std::function< void(const Key &, const Value &)> &func=nullptr)
 Create a LRU Cache with the given size. More...
 
void addData (const Key &key, const Value &data)
 Add data to the cache using the given key. More...
 
Value getData (const Key &key)
 Returns the cached value associated with the given key. More...
 
bool hasKey (const Key &key)
 Returns if the given key has a cached value. More...
 
unsigned int size ()
 Returns the number of items in the cache. More...
 
 ~LRUCache ()
 Calls callback function on each stored item when deleted.
 

Detailed Description

template<typename Key, typename Value>
class Aether::LRUCache< Key, Value >

A basic LRU (least recently used) cache implementation. Also supports a custom callback for when an item is removed.

Constructor & Destructor Documentation

◆ LRUCache()

template<typename Key, typename Value>
Aether::LRUCache< Key, Value >::LRUCache ( const unsigned int  size,
const std::function< void(const Key &, const Value &)> &  func = nullptr 
)
inline

Create a LRU Cache with the given size.

Parameters
sizeMaximum number of elements to cache.
funcCallback to invoke when an element is removed (optional)

Member Function Documentation

◆ addData()

template<typename Key, typename Value>
void Aether::LRUCache< Key, Value >::addData ( const Key &  key,
const Value &  data 
)
inline

Add data to the cache using the given key.

Parameters
keyKey to use for looking up the data.
dataData associated with key.

◆ getData()

template<typename Key, typename Value>
Value Aether::LRUCache< Key, Value >::getData ( const Key &  key)
inline

Returns the cached value associated with the given key.

Note
The returned value is undefined if the key is not cached, please use hasKey() first to check that it is in the cache!
Parameters
keyKey to search for matching value with
Returns
Value/data associated with key

◆ hasKey()

template<typename Key, typename Value>
bool Aether::LRUCache< Key, Value >::hasKey ( const Key &  key)
inline

Returns if the given key has a cached value.

Parameters
keyKey to search for
Returns
true if key has cached value, false otherwise.

◆ size()

template<typename Key, typename Value>
unsigned int Aether::LRUCache< Key, Value >::size ( )
inline

Returns the number of items in the cache.

Returns
Number of items in cache.

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