OpenTTD Source 20241224-master-gee860a5c8e
LRUCache< Tkey, Tdata > Class Template Reference

Size limited cache with a least recently used eviction strategy. More...

#include <lrucache.hpp>

Public Member Functions

 LRUCache (size_t max_items)
 Construct new LRU cache map.
 
bool Contains (const Tkey key)
 Test if a key is already contained in the cache.
 
Tdata * Insert (const Tkey key, Tdata *item)
 Insert a new data item with a specified key.
 
Tdata * Pop ()
 Pop the least recently used item.
 
Tdata * Get (const Tkey key)
 Get an item from the cache.
 

Private Types

typedef std::pair< Tkey, Tdata * > Tpair
 
typedef std::list< Tpair >::iterator Titer
 

Private Attributes

std::list< Tpair > data
 Ordered list of all items.
 
std::unordered_map< Tkey, Titer > lookup
 Map of keys to items.
 
const size_t capacity
 Number of items to cache.
 

Detailed Description

template<class Tkey, class Tdata>
class LRUCache< Tkey, Tdata >

Size limited cache with a least recently used eviction strategy.

Template Parameters
TkeyType of the cache key.
TdataType of the cache item. The cache will store a pointer of this type.

Definition at line 22 of file lrucache.hpp.

Member Typedef Documentation

◆ Titer

template<class Tkey , class Tdata >
typedef std::list<Tpair>::iterator LRUCache< Tkey, Tdata >::Titer
private

Definition at line 25 of file lrucache.hpp.

◆ Tpair

template<class Tkey , class Tdata >
typedef std::pair<Tkey, Tdata *> LRUCache< Tkey, Tdata >::Tpair
private

Definition at line 24 of file lrucache.hpp.

Constructor & Destructor Documentation

◆ LRUCache()

template<class Tkey , class Tdata >
LRUCache< Tkey, Tdata >::LRUCache ( size_t  max_items)
inline

Construct new LRU cache map.

Parameters
max_itemsNumber of items to store at most.

Definition at line 37 of file lrucache.hpp.

Member Function Documentation

◆ Contains()

template<class Tkey , class Tdata >
bool LRUCache< Tkey, Tdata >::Contains ( const Tkey  key)
inline

Test if a key is already contained in the cache.

Parameters
keyThe key to search.
Returns
True, if the key was found.

Definition at line 44 of file lrucache.hpp.

Referenced by OpenGLBackend::DrawMouseCursor(), and LRUCache< Tkey, Tdata >::Insert().

◆ Get()

template<class Tkey , class Tdata >
Tdata * LRUCache< Tkey, Tdata >::Get ( const Tkey  key)
inline

Get an item from the cache.

Parameters
keyThe key to look up.
Returns
The item value.
Note
Throws if item not found.

Definition at line 101 of file lrucache.hpp.

Referenced by OpenGLBackend::DrawMouseCursor().

◆ Insert()

template<class Tkey , class Tdata >
Tdata * LRUCache< Tkey, Tdata >::Insert ( const Tkey  key,
Tdata *  item 
)
inline

Insert a new data item with a specified key.

Parameters
keyKey under which the item should be stored.
itemItem to insert.
Returns
Evicted item or nullptr, if no item had to be evicted.

Definition at line 55 of file lrucache.hpp.

References LRUCache< Tkey, Tdata >::Contains(), and LRUCache< Tkey, Tdata >::data.

◆ Pop()

template<class Tkey , class Tdata >
Tdata * LRUCache< Tkey, Tdata >::Pop ( )
inline

Pop the least recently used item.

Returns
The item value or nullptr if no items cached.

Definition at line 85 of file lrucache.hpp.

Referenced by OpenGLBackend::InternalClearCursorCache().

Field Documentation

◆ capacity

template<class Tkey , class Tdata >
const size_t LRUCache< Tkey, Tdata >::capacity
private

Number of items to cache.

Definition at line 30 of file lrucache.hpp.

◆ data

template<class Tkey , class Tdata >
std::list<Tpair> LRUCache< Tkey, Tdata >::data
private

Ordered list of all items.

Definition at line 27 of file lrucache.hpp.

Referenced by LRUCache< Tkey, Tdata >::Insert().

◆ lookup

template<class Tkey , class Tdata >
std::unordered_map<Tkey, Titer> LRUCache< Tkey, Tdata >::lookup
private

Map of keys to items.

Definition at line 28 of file lrucache.hpp.


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