LibreOffice Module o3tl (master) 1
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize > Class Template Referencefinal

LRU map. More...

#include <lru_map.hxx>

Inheritance diagram for o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >:
[legend]
Collaboration diagram for o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >:
[legend]

Public Types

typedef std::pair< Key, Value > key_value_pair_t
 
typedef list_iterator_t iterator
 
typedef list_const_iterator_t const_iterator
 

Public Member Functions

 lru_map (size_t nMaxSize)
 
 ~lru_map ()
 
void setMaxSize (size_t nMaxSize)
 
void insert (key_value_pair_t &rPair)
 
void insert (key_value_pair_t &&rPair)
 
list_const_iterator_t find (const Key &key)
 
template<class UnaryPredicate >
void remove_if (UnaryPredicate pred)
 
list_const_iterator_t begin () const
 
list_const_iterator_t end () const
 
size_t size () const
 
void clear ()
 

Private Types

typedef std::list< key_value_pair_tlist_t
 
typedef list_t::iterator list_iterator_t
 
typedef list_t::const_iterator list_const_iterator_t
 
typedef std::unordered_map< Key, list_iterator_t, KeyHash, KeyEqual > map_t
 
typedef map_t::iterator map_iterator_t
 
typedef map_t::const_iterator map_const_iterator_t
 

Private Member Functions

void addSize (const Value &value)
 
void removeSize (const Value &value)
 
void removeOldestItem ()
 
void checkLRUItemInsert ()
 
void checkLRUItemUpdate ()
 
void checkLRUMaxSize ()
 
void clearSize ()
 

Private Attributes

list_t mLruList
 
map_t mLruMap
 
size_t mMaxSize
 

Detailed Description

template<typename Key, typename Value, class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
class o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >

LRU map.

Similar to unordered_map (it actually uses it) with additionally functionality which removes the entries that have been "least recently used" when the size hits the specified capacity.

It only implements the minimal methods needed and the implementation is NOT thread safe.

The implementation is as simple as possible but it still uses O(1) complexity for most of the operations with a combination unordered map and linked list.

It is optionally possible to specify a function for ValueSize template argument (that can be called as 'size_t func(Value)') that will return a size (cost) for an item instead of the default size of 1 for each item. The size of an item must not change for an item (if needed, re-insert the item). A newly inserted item is guaranteed to be in the container, even if its size exceeds the maximum size.

Definition at line 66 of file lru_map.hxx.

Member Typedef Documentation

◆ const_iterator

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef list_const_iterator_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::const_iterator

Definition at line 167 of file lru_map.hxx.

◆ iterator

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef list_iterator_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::iterator

Definition at line 166 of file lru_map.hxx.

◆ key_value_pair_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef std::pair<Key, Value> o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::key_value_pair_t

Definition at line 69 of file lru_map.hxx.

◆ list_const_iterator_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef list_t::const_iterator o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::list_const_iterator_t
private

Definition at line 74 of file lru_map.hxx.

◆ list_iterator_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef list_t::iterator o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::list_iterator_t
private

Definition at line 73 of file lru_map.hxx.

◆ list_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef std::list<key_value_pair_t> o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::list_t
private

Definition at line 72 of file lru_map.hxx.

◆ map_const_iterator_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef map_t::const_iterator o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::map_const_iterator_t
private

Definition at line 78 of file lru_map.hxx.

◆ map_iterator_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef map_t::iterator o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::map_iterator_t
private

Definition at line 77 of file lru_map.hxx.

◆ map_t

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
typedef std::unordered_map<Key, list_iterator_t, KeyHash, KeyEqual> o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::map_t
private

Definition at line 76 of file lru_map.hxx.

Constructor & Destructor Documentation

◆ lru_map()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::lru_map ( size_t  nMaxSize)
inline

◆ ~lru_map()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::~lru_map ( )
inline

Member Function Documentation

◆ addSize()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::addSize ( const Value &  value)
inlineprivate

Definition at line 84 of file lru_map.hxx.

References value.

Referenced by o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::insert().

◆ begin()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
list_const_iterator_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::begin ( ) const
inline

◆ checkLRUItemInsert()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::checkLRUItemInsert ( )
inlineprivate

◆ checkLRUItemUpdate()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::checkLRUItemUpdate ( )
inlineprivate

◆ checkLRUMaxSize()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::checkLRUMaxSize ( )
inlineprivate

◆ clear()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::clear ( )
inline

◆ clearSize()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::clearSize ( )
inlineprivate

◆ end()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
list_const_iterator_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::end ( ) const
inline

◆ find()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
list_const_iterator_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::find ( const Key key)
inline

◆ insert() [1/2]

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::insert ( key_value_pair_t &&  rPair)
inline

◆ insert() [2/2]

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::insert ( key_value_pair_t rPair)
inline

◆ remove_if()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
template<class UnaryPredicate >
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::remove_if ( UnaryPredicate  pred)
inline

◆ removeOldestItem()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::removeOldestItem ( )
inlineprivate

◆ removeSize()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::removeSize ( const Value &  value)
inlineprivate

◆ setMaxSize()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
void o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::setMaxSize ( size_t  nMaxSize)
inline

◆ size()

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
size_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::size ( ) const
inline

Member Data Documentation

◆ mLruList

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
list_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::mLruList
private

◆ mLruMap

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
map_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::mLruMap
private

◆ mMaxSize

template<typename Key , typename Value , class KeyHash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class ValueSize = void>
size_t o3tl::lru_map< Key, Value, KeyHash, KeyEqual, ValueSize >::mMaxSize
private

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