#include <lruhashmap.h>
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.
K | KEY type of the hash map. |
V | VALUE type of the hash map. |
MAXENTRIES | Initial 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 |
|
private |
|
private |
|
private |
|
private |
|
private |
~LRUHashMap | ( | ) |
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).
[in] | key | Key of the value to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
const V* FindValue | ( | const KEY & | key | ) | const |
Finds the value associated with the given key in this map.
KEY | Type of key. |
[in] | key | Key to search for. |
V* FindValue | ( | const KEY & | key | ) |
Finds the value associated with the given key in this map.
KEY | Type of key. |
[in] | key | Key to search for. |
Int GetCount | ( | void | ) | const |
Returns the number of entries in this map.
void SetCapacity | ( | Int | maxCapacity | ) |
Sets the maximum capacity of this map.
[in] | maxCapacity | Maximum capacity of the map. |
Int GetCapacity | ( | ) | const |
Returns the maximum capacity of this map.
void Reset | ( | void | ) |
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.
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.
KEY | Type of key. |
[in] | key | An entry having this key shall be removed. |
|
private |
|
private |
|
private |
|
private |
|
private |