LRUHashMap< K, V, MAXENTRIES > Class Template Reference

#include <lruhashmap.h>

Detailed Description

template<typename K, typename V, Int MAXENTRIES>
class maxon::LRUHashMap< K, V, MAXENTRIES >

LRUHashMap is a specialized HashMap which is able to restrict the number of elements in the HashMap. The map keeps the LRU (last recently used) elements and destroys the most unused values if a the number of elements exceeds the limit (MAXENTRIES). The class can be used to cache a limited number of elements.

Template Parameters
KKEY type of the hash map.
VVALUE type of the hash map.
MAXENTRIESInitial maximum number of elements to store.

Public Member Functions

 ~LRUHashMap ()
 
template<typename KEY >
ResultRef< V > InsertKey (KEY &&key, Bool &created=BoolLValue())
 
template<typename KEY >
const V * FindValue (const KEY &key) const
 
template<typename KEY >
V * FindValue (const KEY &key)
 
Int GetCount () const
 
void SetCapacity (Int maxCapacity)
 
Int GetCapacity () const
 
void Reset ()
 
template<typename KEY >
ResultOk< void > Erase (const KEY &key)
 

Private Types

using ListNodeType = BaseListNode< V >
 
using LastNodeHead = BaseListHead< V, ListNodeType >
 
using Entry = typename HashMap< K, ListNodeType >::Entry
 
using Allocator = BlockArrayAllocator< SIZEOF(Entry)>
 
using HashMapType = HashMap< K, ListNodeType, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16, 10, Allocator >
 

Private Member Functions

void UpdateEntry (typename HashMapType::Entry *entry) const
 
void RemoveOldest ()
 

Private Attributes

HashMapType _hashMap
 
LastNodeHead _lastUsed
 
Int _maxCapacity
 

Member Typedef Documentation

◆ ListNodeType

using ListNodeType = BaseListNode<V>
private

◆ LastNodeHead

using LastNodeHead = BaseListHead<V, ListNodeType>
private

◆ Entry

using Entry = typename HashMap<K, ListNodeType>::Entry
private

◆ Allocator

◆ HashMapType

Constructor & Destructor Documentation

◆ ~LRUHashMap()

~LRUHashMap ( )

Member Function Documentation

◆ InsertKey()

ResultRef<V> InsertKey ( KEY &&  key,
Bool created = BoolLValue() 
)

Finds the value associated with the given key, or creates a corresponding entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).

Parameters
[in]keyKey of the value to find or create.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Pointer to value for the given key, or nullptr if an entry didn't exist and allocation of a new entry failed.

◆ FindValue() [1/2]

const V* FindValue ( const KEY &  key) const

Finds the value associated with the given key in this map.

Template Parameters
KEYType of key.
Parameters
[in]keyKey to search for.
Returns
Pointer to value for the given key, or nullptr if no entry exists for the key.

◆ FindValue() [2/2]

V* FindValue ( const KEY &  key)

Finds the value associated with the given key in this map.

Template Parameters
KEYType of key.
Parameters
[in]keyKey to search for.
Returns
Pointer to value for the given key, or nullptr if no entry exists for the key.

◆ GetCount()

Int GetCount ( ) const

Returns the number of entries in this map.

Returns
Number of entries.

◆ SetCapacity()

void SetCapacity ( Int  maxCapacity)

Sets the maximum capacity of this map.

Parameters
[in]maxCapacityMaximum capacity of the map.

◆ GetCapacity()

Int GetCapacity ( ) const

Returns the maximum capacity of this map.

Returns
Maximum capacity.

◆ Reset()

void Reset ( )

Resets the map. This destructs all entries and frees any memory held by the map, so the map will be in a state as if it had been newly constructed.

See also
Flush()

◆ Erase()

ResultOk<void> Erase ( const KEY &  key)

Removes an entry with the given key from this HashMap (if possible). In case of a multi-map, this only removes a single entry. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.

Template Parameters
KEYType of key.
Parameters
[in]keyAn entry having this key shall be removed.
Returns
Always true for a HashMap (even if key isn't contained in the map). For a general map, false indicates an out-of-memory condition on removal, but this cannot happen for a HashMap.

◆ UpdateEntry()

void UpdateEntry ( typename HashMapType::Entry *  entry) const
private

◆ RemoveOldest()

void RemoveOldest ( )
private

Member Data Documentation

◆ _hashMap

HashMapType _hashMap
private

◆ _lastUsed

LastNodeHead _lastUsed
private

◆ _maxCapacity

Int _maxCapacity
private