Belle II Software light-2406-ragdoll
|
Class implementing a generic Most Recently Used cache. More...
#include <MRUCache.h>
Public Types | |
typedef std::pair< KEY, VALUE > | value_type |
type of elements stored in the cache | |
typedef boost::multi_index_container< value_type, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_unique< boost::multi_index::member< value_type, KEY, &value_type::first > > > > | container_type |
type of container for the elements | |
typedef container_type::const_iterator | iterator |
iterator over all cached items, sorted by access: most recent used first | |
typedef container_type::template nth_index< 1 >::type::const_iterator | hash_iterator |
iterator over the hash index to the items | |
Public Member Functions | |
MRUCache (size_t maxSize) | |
Constructor setting the maximum number of cached items. | |
void | insert (const KEY &key, const VALUE &value) |
Insert a key value pair into the cache. | |
void | insert (const value_type &item) |
Insert a key value pair into the cache. | |
bool | retrieve (const KEY &key, VALUE &value) |
Retrieve a value from the cache if it exists. | |
iterator | begin () const |
Return iterator to the begin of the cache. | |
iterator | end () const |
Return iterator to the end of the cache. | |
size_t | size () const |
Return actual size of the cache. | |
void | clear () |
Clear cache. | |
void | setMaxSize (size_t maxSize) |
Set maximum number of cache entries. | |
size_t | getMaxSize () const |
Get maximum number of cache entries. | |
unsigned int | getHits () const |
Get number of cache hits since creation/last clear. | |
unsigned int | getMisses () const |
Get number of cache misses since creation/last clear. | |
unsigned int | getOverflows () const |
Get number of overflows (dropped items) since creation/last clear. | |
Protected Member Functions | |
void | update (const iterator &item) |
Update an item, thus marking it as recently accessed and putting it to the front of the list. | |
Protected Attributes | |
container_type | m_container |
Container for the items. | |
size_t | m_maxSize |
Maximum size of the cache. | |
unsigned int | m_hits |
Number of hits. | |
unsigned int | m_misses |
Number of misses. | |
unsigned int | m_overflows |
Number of overflows. | |
Class implementing a generic Most Recently Used cache.
It can be used to cache values which are needed repeatedly. The size is limited so only the N most recently accessed elements will be kept.
Usage: if you have elements of type VALUE which are uniquely identified by something of type KEY and you want to cache the last 100 of them because computation is expensive, than you can define a cache somewhere:
MRUCache<KEY, VALUE> cache(100);
and in your function you can do
VALUE calculate(const KEY &key){ VALUE value; if(!cache.retrieve(key,value)){ //calculate value here //and finally add to cache cache.insert(key,value); } return value; }
KEY | Key type to identify entries of the cache |
VALUE | Value type of the cache entries |
Definition at line 48 of file MRUCache.h.
typedef boost::multi_index_container< value_type, boost::multi_index::indexed_by < boost::multi_index::sequenced<>, boost::multi_index::hashed_unique < boost::multi_index::member<value_type, KEY, &value_type::first> > > > container_type |
type of container for the elements
Definition at line 61 of file MRUCache.h.
typedef container_type::template nth_index<1>::type::const_iterator hash_iterator |
iterator over the hash index to the items
Definition at line 65 of file MRUCache.h.
typedef container_type::const_iterator iterator |
iterator over all cached items, sorted by access: most recent used first
Definition at line 63 of file MRUCache.h.
typedef std::pair<KEY, VALUE> value_type |
type of elements stored in the cache
Definition at line 51 of file MRUCache.h.
|
inlineexplicit |
Constructor setting the maximum number of cached items.
maxSize | Maximum number of cached Items |
Definition at line 71 of file MRUCache.h.
|
inline |
Return iterator to the begin of the cache.
Items are sorted by access: most recently inserted
Definition at line 118 of file MRUCache.h.
|
inline |
Clear cache.
Definition at line 125 of file MRUCache.h.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Get number of overflows (dropped items) since creation/last clear.
Definition at line 142 of file MRUCache.h.
|
inline |
Insert a key value pair into the cache.
If the maximum size is reached, the least recently used item is dropped
key | key to the new item |
value | to the new item |
Definition at line 79 of file MRUCache.h.
|
inline |
Insert a key value pair into the cache.
If the maximum size is reached, the least recently used item is dropped
item | std::pair containing key and value of the new item |
Definition at line 86 of file MRUCache.h.
|
inline |
Retrieve a value from the cache if it exists.
[in] | key | key for the value to retrieve |
[out] | value | reference to the value. Will only be modified if an item is found |
Definition at line 104 of file MRUCache.h.
|
inline |
Set maximum number of cache entries.
Definition at line 134 of file MRUCache.h.
|
inline |
|
inlineprotected |
Update an item, thus marking it as recently accessed and putting it to the front of the list.
item | iterator to the item to update |
Definition at line 150 of file MRUCache.h.
|
protected |
Container for the items.
Definition at line 156 of file MRUCache.h.
|
protected |
Number of hits.
Definition at line 160 of file MRUCache.h.
|
protected |
Maximum size of the cache.
Definition at line 158 of file MRUCache.h.
|
protected |
Number of misses.
Definition at line 162 of file MRUCache.h.
|
protected |
Number of overflows.
Definition at line 164 of file MRUCache.h.