Loading...
Searching...
No Matches
roboptim::LRUCache< K, V, H > Class Template Reference

LRU (Least Recently Used) cache. More...

#include <roboptim/core/cache.hh>

Public Types

typedef K key_t
 Type of keys. More...
 
typedef detail::const_ref< key_t >::type const_key_ref
 Type of const reference to key. More...
 
typedef V value_t
 Type of values. More...
 
typedef detail::const_ref< value_t >::type const_value_ref
 Type of const reference to key. More...
 
typedef H hasher_t
 Hasher type. More...
 
typedef std::size_t hash_t
 Hash type used by the Boost map. More...
 
typedef std::vector< value_tvaluePool_t
 
typedef std::list< hash_tkeyTracker_t
 List used to track key usage. More...
 
typedef hash_t mapKey_t
 Key type for the underlying map. More...
 
typedef detail::const_ref< mapKey_t >::type const_mapKey_ref
 
typedef boost::unordered_map< mapKey_t, typename valuePool_t::iterator > map_t
 Map from map's key to iterator in the value pool. More...
 
typedef map_t::const_iterator const_iterator
 
typedef map_t::iterator iterator
 

Public Member Functions

 LRUCache (size_t size=10)
 Constructor. More...
 
virtual ~LRUCache ()
 Destructor. More...
 
size_t size () const
 Size of the cache. More...
 
void resize (size_t size)
 Change the size of the cache. More...
 
const_iterator find (const_key_ref key) const
 Find an element in the cache. More...
 
iterator begin ()
 Iterator to the beginning of the cache. More...
 
iterator end ()
 Iterator to the end of the cache. More...
 
const_iterator cbegin () const
 Iterator to the beginning of the cache. More...
 
const_iterator cend () const
 Iterator to the end of the cache. More...
 
V & operator[] (const_key_ref key)
 Access a cached element. More...
 
void insert (const_key_ref key, const_value_ref value)
 Insert a value into the cache. More...
 
void clear ()
 Clear the cache. More...
 
virtual std::ostream & print (std::ostream &) const
 Display the cache on the specified output stream. More...
 

Protected Member Functions

hash_t hash_function (const_key_ref key) const
 Hash function used in the cache. More...
 

Detailed Description

template<typename K, typename V, typename H = boost::hash<K>>
class roboptim::LRUCache< K, V, H >

LRU (Least Recently Used) cache.

Note that the cache is unidirectional, i.e. the map does not store the actual keys, since this was designed with large vectors in mind.

Template Parameters
Ktype for keys.
Vtype for values.

Some of the ideas used here come from Tim Day's "LRU cache implementation in C++" (http://timday.bitbucket.org/lru.html)

Member Typedef Documentation

◆ const_iterator

template<typename K , typename V , typename H = boost::hash<K>>
typedef map_t::const_iterator roboptim::LRUCache< K, V, H >::const_iterator

◆ const_key_ref

template<typename K , typename V , typename H = boost::hash<K>>
typedef detail::const_ref<key_t>::type roboptim::LRUCache< K, V, H >::const_key_ref

Type of const reference to key.

◆ const_mapKey_ref

template<typename K , typename V , typename H = boost::hash<K>>
typedef detail::const_ref<mapKey_t>::type roboptim::LRUCache< K, V, H >::const_mapKey_ref

◆ const_value_ref

template<typename K , typename V , typename H = boost::hash<K>>
typedef detail::const_ref<value_t>::type roboptim::LRUCache< K, V, H >::const_value_ref

Type of const reference to key.

◆ hash_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef std::size_t roboptim::LRUCache< K, V, H >::hash_t

Hash type used by the Boost map.

◆ hasher_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef H roboptim::LRUCache< K, V, H >::hasher_t

Hasher type.

◆ iterator

template<typename K , typename V , typename H = boost::hash<K>>
typedef map_t::iterator roboptim::LRUCache< K, V, H >::iterator

◆ key_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef K roboptim::LRUCache< K, V, H >::key_t

Type of keys.

◆ keyTracker_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef std::list<hash_t> roboptim::LRUCache< K, V, H >::keyTracker_t

List used to track key usage.

Note: we use hashes rather than vectors to prevent costly allocations.

◆ map_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef boost::unordered_map<mapKey_t, typename valuePool_t::iterator> roboptim::LRUCache< K, V, H >::map_t

Map from map's key to iterator in the value pool.

◆ mapKey_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef hash_t roboptim::LRUCache< K, V, H >::mapKey_t

Key type for the underlying map.

◆ value_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef V roboptim::LRUCache< K, V, H >::value_t

Type of values.

◆ valuePool_t

template<typename K , typename V , typename H = boost::hash<K>>
typedef std::vector<value_t> roboptim::LRUCache< K, V, H >::valuePool_t

Constructor & Destructor Documentation

◆ LRUCache()

template<typename K , typename V , typename H >
roboptim::LRUCache< K, V, H >::LRUCache ( size_t  size = 10)

Constructor.

Note: all the memory is allocated in the constructor.

Parameters
sizemaximum size of the cache.

◆ ~LRUCache()

template<typename K , typename V , typename H >
roboptim::LRUCache< K, V, H >::~LRUCache
virtual

Destructor.

Member Function Documentation

◆ begin()

template<typename K , typename V , typename H >
LRUCache< K, V, H >::iterator roboptim::LRUCache< K, V, H >::begin

Iterator to the beginning of the cache.

◆ cbegin()

template<typename K , typename V , typename H >
LRUCache< K, V, H >::const_iterator roboptim::LRUCache< K, V, H >::cbegin

Iterator to the beginning of the cache.

◆ cend()

template<typename K , typename V , typename H >
LRUCache< K, V, H >::const_iterator roboptim::LRUCache< K, V, H >::cend

Iterator to the end of the cache.

◆ clear()

template<typename K , typename V , typename H >
void roboptim::LRUCache< K, V, H >::clear

Clear the cache.

◆ end()

template<typename K , typename V , typename H >
LRUCache< K, V, H >::iterator roboptim::LRUCache< K, V, H >::end

Iterator to the end of the cache.

◆ find()

template<typename K , typename V , typename H >
LRUCache< K, V, H >::const_iterator roboptim::LRUCache< K, V, H >::find ( const_key_ref  key) const

◆ hash_function()

template<typename K , typename V , typename H >
LRUCache< K, V, H >::hash_t roboptim::LRUCache< K, V, H >::hash_function ( const_key_ref  key) const
protected

Hash function used in the cache.

Parameters
keykey to hash.
Returns
hashed key.

◆ insert()

template<typename K , typename V , typename H >
void roboptim::LRUCache< K, V, H >::insert ( const_key_ref  key,
const_value_ref  value 
)

Insert a value into the cache.

Parameters
keykey of the element.
valuevalue of the element.

◆ operator[]()

template<typename K , typename V , typename H >
V & roboptim::LRUCache< K, V, H >::operator[] ( const_key_ref  key)

Access a cached element.

Parameters
keykey to the element.
Returns
reference to the element.

◆ print()

template<typename K , typename V , typename H >
std::ostream & roboptim::LRUCache< K, V, H >::print ( std::ostream &  o) const
virtual

Display the cache on the specified output stream.

Referenced by roboptim::operator<<().

◆ resize()

template<typename K , typename V , typename H >
void roboptim::LRUCache< K, V, H >::resize ( size_t  size)

Change the size of the cache.

◆ size()

template<typename K , typename V , typename H >
size_t roboptim::LRUCache< K, V, H >::size

Size of the cache.