PcapPlusPlus
pcpp::LRUList< T > Class Template Reference

#include <LRUList.h>

Public Member Functions

 LRUList (size_t maxSize)
 
T * put (const T &element)
 
const T & getMRUElement ()
 
const T & getLRUElement ()
 
void eraseElement (const T &element)
 
size_t getMaxSize ()
 
size_t getSize ()
 

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 ( )
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 ( )
inline
Returns
The max size of this list as determined in the c'tor

◆ getMRUElement()

template<typename T>
const T& pcpp::LRUList< T >::getMRUElement ( )
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 ( )
inline
Returns
The number of elements currently in this list

◆ put()

template<typename T>
T* pcpp::LRUList< T >::put ( const T &  element)
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 pointer to it. Method complexity is O(1)

Parameters
[in]elementThe element to insert or to advance to the head of the list (if already exists)
Returns
A pointer to the element that was removed from the list in case the list already reached its max size. If the list didn't reach its max size NULL will be returned. Notice it's the responsibility of the user to free this pointer's memory when done using it