shark::LRUCache< T > Class Template Reference

Implements an LRU-Caching Strategy for arbitrary Cache-Lines. More...

#include <shark/LinAlg/LRUCache.h>

Public Member Functions

 LRUCache (std::size_t lines, std::size_t cachesize=0x4000000)
 Creates a cache with a given maximum index "lines" and a given maximum cache size. More...
 
 ~LRUCache ()
 
bool isCached (std::size_t i) const
 Returns true if the line is cached. More...
 
std::size_t lineLength (std::size_t i) const
 Returns the size of the cached line. More...
 
std::size_t cachedLines () const
 Returns the number of lines currently allocated. More...
 
T * getCacheLine (std::size_t i, std::size_t size)
 Returns the line with index i with the correct size. More...
 
T * getLinePointer (std::size_t i)
 Just returns the pointer to the i-th line without affcting cache at all. More...
 
T const * getLinePointer (std::size_t i) const
 Just returns the pointer to the i-th line without affcting cache at all. More...
 
void resizeLine (std::size_t i, std::size_t size)
 Resizes a line while retaining the data stored inside it. More...
 
void markLineForDeletion (std::size_t i)
 Marks cache line i for deletion, that is the next time memory is needed, this line will be freed. More...
 
void swapLineIndices (std::size_t i, std::size_t j)
 swaps index of lines i and j. More...
 
std::size_t size () const
 
std::size_t listIndex (std::size_t i) const
 
std::size_t maxSize () const
 
void clear ()
 empty cache More...
 

Detailed Description

template<class T>
class shark::LRUCache< T >

Implements an LRU-Caching Strategy for arbitrary Cache-Lines.

Low Level Cache which stores cache lines, arrays of T[size] where size is a variable length for every cache line. Every line is associated with some index 0<i < max. It is assumed that not all cache lines can be stored at the same time, but only a (small) subset. The size of the cache is bounded by the summed length of all cache lines, that means that when the lines are very short, the cache can store more lines. If the cache is full and another line needs to be accessed, or an existing line needs to be resized, cache lines need to be freed. This cache uses an Least-Recently-Used strategy. The cache maintains a list. Everytime a cacheline is accessed, it moves to the front of the list. When a line is freed the end of the list is chosen.

Definition at line 55 of file LRUCache.h.

Constructor & Destructor Documentation

◆ LRUCache()

template<class T>
shark::LRUCache< T >::LRUCache ( std::size_t  lines,
std::size_t  cachesize = 0x4000000 
)
inline

Creates a cache with a given maximum index "lines" and a given maximum cache size.

Definition at line 65 of file LRUCache.h.

◆ ~LRUCache()

template<class T>
shark::LRUCache< T >::~LRUCache ( )
inline

Definition at line 70 of file LRUCache.h.

Member Function Documentation

◆ cachedLines()

template<class T>
std::size_t shark::LRUCache< T >::cachedLines ( ) const
inline

Returns the number of lines currently allocated.

Definition at line 84 of file LRUCache.h.

◆ clear()

template<class T>
void shark::LRUCache< T >::clear ( )
inline

empty cache

Definition at line 197 of file LRUCache.h.

Referenced by shark::CachedMatrix< Matrix >::clear(), and shark::LRUCache< QpFloatType >::~LRUCache().

◆ getCacheLine()

template<class T>
T* shark::LRUCache< T >::getCacheLine ( std::size_t  i,
std::size_t  size 
)
inline

Returns the line with index i with the correct size.

If the line is not cached, it is created with the exact size. if it is cached and is at least as big, it is returned unchanged. otherwise it is resized to the proper size and the old values are kept.

Definition at line 93 of file LRUCache.h.

Referenced by shark::CachedMatrix< Matrix >::row().

◆ getLinePointer() [1/2]

template<class T>
T* shark::LRUCache< T >::getLinePointer ( std::size_t  i)
inline

Just returns the pointer to the i-th line without affcting cache at all.

Definition at line 108 of file LRUCache.h.

Referenced by shark::CachedMatrix< Matrix >::flipColumnsAndRows(), and shark::CachedMatrix< Matrix >::row().

◆ getLinePointer() [2/2]

template<class T>
T const* shark::LRUCache< T >::getLinePointer ( std::size_t  i) const
inline

Just returns the pointer to the i-th line without affcting cache at all.

Definition at line 113 of file LRUCache.h.

◆ isCached()

template<class T>
bool shark::LRUCache< T >::isCached ( std::size_t  i) const
inline

◆ lineLength()

template<class T>
std::size_t shark::LRUCache< T >::lineLength ( std::size_t  i) const
inline

◆ listIndex()

template<class T>
std::size_t shark::LRUCache< T >::listIndex ( std::size_t  i) const
inline

Definition at line 187 of file LRUCache.h.

◆ markLineForDeletion()

template<class T>
void shark::LRUCache< T >::markLineForDeletion ( std::size_t  i)
inline

Marks cache line i for deletion, that is the next time memory is needed, this line will be freed.

Definition at line 125 of file LRUCache.h.

Referenced by shark::CachedMatrix< Matrix >::setMaxCachedIndex().

◆ maxSize()

template<class T>
std::size_t shark::LRUCache< T >::maxSize ( ) const
inline

Definition at line 192 of file LRUCache.h.

Referenced by shark::CachedMatrix< Matrix >::getMaxCacheSize().

◆ resizeLine()

template<class T>
void shark::LRUCache< T >::resizeLine ( std::size_t  i,
std::size_t  size 
)
inline

Resizes a line while retaining the data stored inside it.

if the new size is smaller than the old, only the first size entries are saved.

Definition at line 120 of file LRUCache.h.

Referenced by shark::LRUCache< QpFloatType >::clear(), shark::LRUCache< QpFloatType >::getCacheLine(), and shark::LRUCache< QpFloatType >::resizeLine().

◆ size()

template<class T>
std::size_t shark::LRUCache< T >::size ( ) const
inline

◆ swapLineIndices()

template<class T>
void shark::LRUCache< T >::swapLineIndices ( std::size_t  i,
std::size_t  j 
)
inline

swaps index of lines i and j.

Definition at line 133 of file LRUCache.h.

Referenced by shark::CachedMatrix< Matrix >::flipColumnsAndRows().


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