OpenTTD Source: LRUCache< Tkey, Tdata, Thash, Tequality > Class Template Reference

OpenTTD Source 15.3

Public Member Functions | Private Types | Private Attributes
LRUCache< Tkey, Tdata, Thash, Tequality > 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.
void | Insert (const Tkey &key, Tdata &&item)
| Insert a new data item with a specified key.
void | Clear ()
| Clear the cache.
const Tdata & | Get (const Tkey &key)
| Get an item from the cache.
Tdata * | GetIfValid (const auto &key)
| Get an item from the cache.

## Private Types
using | PairType = std::pair< Tkey, Tdata >
using | StorageType = std::list< PairType >
using | IteratorType = StorageType::iterator
using | LookupType = std::unordered_map< Tkey, IteratorType, Thash, Tequality >

## Private Attributes
StorageType | data
| Ordered list of all items.
LookupType | lookup
| Map of keys to items.
const size_t | capacity
| Number of items to cache.

## Detailed Description

template<class Tkey, class Tdata, class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>
class LRUCache< Tkey, Tdata, Thash, Tequality >

Size limited cache with a least recently used eviction strategy.

Note
  If a comparator is provided, the lookup type is a std::map, otherwise std::unordered_map is used.

Template Parameters

  Tkey | Type of the cache key.
  Tdata | Type of the cache item.

Definition at line 23 of file lrucache.hpp.

## Member Typedef Documentation

## ◆ IteratorType

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

using LRUCache< Tkey, Tdata, Thash, Tequality >::IteratorType = StorageType::iterator
| private

Definition at line 27 of file lrucache.hpp.

## ◆ LookupType

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

using LRUCache< Tkey, Tdata, Thash, Tequality >::LookupType = std::unordered_map<Tkey, IteratorType, Thash, Tequality>
| private

Definition at line 28 of file lrucache.hpp.

## ◆ PairType

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

using LRUCache< Tkey, Tdata, Thash, Tequality >::PairType = std::pair<Tkey, Tdata>
| private

Definition at line 25 of file lrucache.hpp.

## ◆ StorageType

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

using LRUCache< Tkey, Tdata, Thash, Tequality >::StorageType = std::list<PairType>
| private

Definition at line 26 of file lrucache.hpp.

## Constructor & Destructor Documentation

## ◆ LRUCache()

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

LRUCache< Tkey, Tdata, Thash, Tequality >::LRUCache | ( | size_t | max_items | ) |
| inline

Construct new LRU cache map.

Parameters

  max_items | Number of items to store at most.

Definition at line 40 of file lrucache.hpp.

## Member Function Documentation

## ◆ Clear()

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

void LRUCache< Tkey, Tdata, Thash, Tequality >::Clear | ( |  | ) |
| inline

Clear the cache.

Definition at line 80 of file lrucache.hpp.

Referenced by OpenGLBackend::InternalClearCursorCache().

## ◆ Contains()

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

bool LRUCache< Tkey, Tdata, Thash, Tequality >::Contains | ( | const Tkey & | key | ) |
| inline

Test if a key is already contained in the cache.

Parameters

  key | The key to search.

Returns
  True, if the key was found.

Definition at line 47 of file lrucache.hpp.

Referenced by OpenGLBackend::DrawMouseCursor().

## ◆ Get()

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

const Tdata & LRUCache< Tkey, Tdata, Thash, Tequality >::Get | ( | const Tkey & | key | ) |
| inline

Get an item from the cache.

Parameters

  key | The key to look up.

Returns
  The item value.

Note
  Throws if item not found.

Definition at line 92 of file lrucache.hpp.

Referenced by OpenGLBackend::DrawMouseCursor().

## ◆ GetIfValid()

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

Tdata * LRUCache< Tkey, Tdata, Thash, Tequality >::GetIfValid | ( | const auto & | key | ) |
| inline

Get an item from the cache.

Parameters

  key | The key to look up.

Returns
  The item value.

Note
  Throws if item not found.

Definition at line 108 of file lrucache.hpp.

## ◆ Insert()

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

void LRUCache< Tkey, Tdata, Thash, Tequality >::Insert | ( | const Tkey & | key,
|  | Tdata && | item
| ) |  |
| inline

Insert a new data item with a specified key.

Parameters

  key | Key under which the item should be stored.
  item | Item to insert.

Definition at line 57 of file lrucache.hpp.

References LRUCache< Tkey, Tdata, Thash, Tequality >::data.

Referenced by OpenGLBackend::Encode().

## Field Documentation

## ◆ capacity

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

const size_t LRUCache< Tkey, Tdata, Thash, Tequality >::capacity
| private

Number of items to cache.

Definition at line 33 of file lrucache.hpp.

## ◆ data

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

StorageType LRUCache< Tkey, Tdata, Thash, Tequality >::data
| private

Ordered list of all items.

Definition at line 30 of file lrucache.hpp.

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

## ◆ lookup

template<class Tkey , class Tdata , class Thash = std::hash<Tkey>, class Tequality = std::equal_to<>>

LookupType LRUCache< Tkey, Tdata, Thash, Tequality >::lookup
| private

Map of keys to items.

Definition at line 31 of file lrucache.hpp.

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

- src/misc/lrucache.hpp