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 ()
 

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

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

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
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
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