PcapPlusPlus  20.08
pcpp::LRUList< T > Class Template Reference

#include <LRUList.h>

Public Member Functions

 LRUList (size_t maxSize)
 
int put (const T &element, T *deletedValue=NULL)
 
const T & getMRUElement () const
 
const T & getLRUElement () const
 
void eraseElement (const T &element)
 
size_t getMaxSize () const
 
size_t getSize () const
 

Detailed Description

template<typename T>
class pcpp::LRUList< T >

A template class that implements a LRU cache with limited size. Each time the user puts an element it goes to head of the list as the most recently used element (if the element was already in the list it advances to the head of the list). The last element in the list is the one least recently used and will be pulled out of the list if it reaches its max size and a new element comes in. All actions on this LRU list are O(1)

Constructor & Destructor Documentation

◆ LRUList()

template<typename T >
pcpp::LRUList< T >::LRUList ( size_t  maxSize)
inline

A c'tor for this class

Parameters
[in]maxSizeThe max size this list can go

Member Function Documentation

◆ eraseElement()

template<typename T >
void pcpp::LRUList< T >::eraseElement ( const T &  element)
inline

Erase an element from the list. If element isn't found in the list nothing happens

Parameters
[in]elementThe element to erase

◆ getLRUElement()

template<typename T >
const T& pcpp::LRUList< T >::getLRUElement ( ) const
inline

Get the least recently used element (the one at the end of the list)

Returns
The least recently used element

◆ getMaxSize()

template<typename T >
size_t pcpp::LRUList< T >::getMaxSize ( ) const
inline
Returns
The max size of this list as determined in the c'tor

◆ getMRUElement()

template<typename T >
const T& pcpp::LRUList< T >::getMRUElement ( ) const
inline

Get the most recently used element (the one at the beginning of the list)

Returns
The most recently used element

◆ getSize()

template<typename T >
size_t pcpp::LRUList< T >::getSize ( ) const
inline
Returns
The number of elements currently in this list

◆ put()

template<typename T >
int pcpp::LRUList< T >::put ( const T &  element,
T *  deletedValue = NULL 
)
inline

Puts an element in the list. This element will be inserted (or advanced if it already exists) to the head of the list as the most recently used element. If the list already reached its max size and the element is new this method will remove the least recently used element and return a value in deletedValue. Method complexity is O(log(getSize())). This is a optimized version of the method T* put(const T&).

Parameters
[in]elementThe element to insert or to advance to the head of the list (if already exists)
[out]deletedValueThe value of deleted element if a pointer is not NULL. This parameter is optional.
Returns
0 if the list didn't reach its max size, 1 otherwise. In case the list already reached its max size and deletedValue is not NULL the value of deleted element is copied into the place the deletedValue points to.