libboxes
boxes is a set of specialised containers built on top of STL
Public Member Functions | List of all members
boxes::cache::Cache< K, V, CachePolicy > Class Template Reference

Generic cache with pluggable eviction policies. More...

#include <cache.hpp>

Public Member Functions

 Cache (Policy policy)
 Construct a new Cache object. More...
 
void reserve (std::size_t size)
 Reserve space for the cache to avoid unnecessary reallocations. More...
 
std::size_t size () const
 Returns the number of elements in the cache. More...
 
std::size_t max_size () const
 Returns the maximum number of elements the cache can ever hold. More...
 
bool empty () const
 Returns whether the cache is empty. More...
 
void clear ()
 Removes all elements from the cache. More...
 
template<typename KArg , typename VArg >
bool insert (KArg &&key, VArg &&value)
 Inserts a key-value pair into the cache. More...
 
bool contains (const K &key) const
 Check if the cache contains a key. More...
 
iterator begin ()
 Returns an iterator to the first element in the cache. More...
 
iterator end ()
 Returns an iterator to the element following the last element in the cache. More...
 
iterator cbegin () const
 Returns a const iterator to the first element in the cache. More...
 
iterator cend () const
 Returns a const iterator to the element following the last element in the cache. More...
 
iterator find (const K &key)
 Returns an iterator to the element with the given key. More...
 
V & at (const K &key)
 Returns a reference to the value associated with the given key. More...
 
const V & at (const K &key) const
 Returns a const reference to the value associated with the given key. More...
 
K & front ()
 Returns the reference the first element in the cache. More...
 
K & back ()
 Returns the reference the last element in the cache. More...
 
const K & front () const
 Returns const reference the first element in the cache. More...
 
const K & back () const
 Returns const reference the last element in the cache. More...
 

Detailed Description

template<typename K, typename V, template< typename, typename > class CachePolicy>
class boxes::cache::Cache< K, V, CachePolicy >

Generic cache with pluggable eviction policies.

Template Parameters
Kkey type (must be hashable, comparable and copyable)
Vvalue type
CachePolicyeviction policy to use

Definition at line 337 of file cache.hpp.

Constructor & Destructor Documentation

◆ Cache()

template<typename K , typename V , template< typename, typename > class CachePolicy>
boxes::cache::Cache< K, V, CachePolicy >::Cache ( Policy  policy)
inlineexplicit

Construct a new Cache object.

Parameters
policyEviction policy to use

Definition at line 402 of file cache.hpp.

Member Function Documentation

◆ at() [1/2]

template<typename K , typename V , template< typename, typename > class CachePolicy>
V & boxes::cache::Cache< K, V, CachePolicy >::at ( const K &  key)
inline

Returns a reference to the value associated with the given key.

The key, if present, is refreshed in accordance with the eviction policy.

If the key is not in the cache, an exception is thrown.

Parameters
keykey to search for
Returns
reference to the value associated with the given key

Definition at line 548 of file cache.hpp.

◆ at() [2/2]

template<typename K , typename V , template< typename, typename > class CachePolicy>
const V & boxes::cache::Cache< K, V, CachePolicy >::at ( const K &  key) const
inline

Returns a const reference to the value associated with the given key.

Parameters
keykey to search for
Returns
reference to the value associated with the given key

Definition at line 563 of file cache.hpp.

◆ back() [1/2]

template<typename K , typename V , template< typename, typename > class CachePolicy>
K & boxes::cache::Cache< K, V, CachePolicy >::back ( )
inline

Returns the reference the last element in the cache.

The order of elements in the cache is defined by the eviction policy. Refer to the specific eviction policy for details.

Returns
reference to the key of the last element in the cache

Definition at line 583 of file cache.hpp.

◆ back() [2/2]

template<typename K , typename V , template< typename, typename > class CachePolicy>
const K & boxes::cache::Cache< K, V, CachePolicy >::back ( ) const
inline

Returns const reference the last element in the cache.

Returns
const reference to the last element in the cache

Definition at line 597 of file cache.hpp.

◆ begin()

template<typename K , typename V , template< typename, typename > class CachePolicy>
iterator boxes::cache::Cache< K, V, CachePolicy >::begin ( )
inline

Returns an iterator to the first element in the cache.

The iteration order is defined by the eviction policy.

Returns
iterator to the first element in the cache

Definition at line 493 of file cache.hpp.

◆ cbegin()

template<typename K , typename V , template< typename, typename > class CachePolicy>
iterator boxes::cache::Cache< K, V, CachePolicy >::cbegin ( ) const
inline

Returns a const iterator to the first element in the cache.

Returns
const iterator to the first element in the cache

Definition at line 510 of file cache.hpp.

◆ cend()

template<typename K , typename V , template< typename, typename > class CachePolicy>
iterator boxes::cache::Cache< K, V, CachePolicy >::cend ( ) const
inline

Returns a const iterator to the element following the last element in the cache.

Returns
const iterator to the element following the last element in the cache

Definition at line 519 of file cache.hpp.

◆ clear()

template<typename K , typename V , template< typename, typename > class CachePolicy>
void boxes::cache::Cache< K, V, CachePolicy >::clear ( )
inline

Removes all elements from the cache.

Definition at line 441 of file cache.hpp.

◆ contains()

template<typename K , typename V , template< typename, typename > class CachePolicy>
bool boxes::cache::Cache< K, V, CachePolicy >::contains ( const K &  key) const
inline

Check if the cache contains a key.

Parameters
keykey to check for
Returns
true if the key is in the cache, false otherwise

Definition at line 484 of file cache.hpp.

◆ empty()

template<typename K , typename V , template< typename, typename > class CachePolicy>
bool boxes::cache::Cache< K, V, CachePolicy >::empty ( ) const
inline

Returns whether the cache is empty.

Returns
true if the cache is empty, false otherwise

Definition at line 436 of file cache.hpp.

◆ end()

template<typename K , typename V , template< typename, typename > class CachePolicy>
iterator boxes::cache::Cache< K, V, CachePolicy >::end ( )
inline

Returns an iterator to the element following the last element in the cache.

The iteration order is defined by the eviction policy.

Returns
iterator to the element following the last element in the cache

Definition at line 503 of file cache.hpp.

◆ find()

template<typename K , typename V , template< typename, typename > class CachePolicy>
iterator boxes::cache::Cache< K, V, CachePolicy >::find ( const K &  key)
inline

Returns an iterator to the element with the given key.

The key, if present, is refreshed in accordance with the eviction policy.

Parameters
keykey to search for
Returns
iterator to the element with the given key, or end() if the key is not in the cache

Definition at line 530 of file cache.hpp.

◆ front() [1/2]

template<typename K , typename V , template< typename, typename > class CachePolicy>
K & boxes::cache::Cache< K, V, CachePolicy >::front ( )
inline

Returns the reference the first element in the cache.

The order of elements in the cache is defined by the eviction policy. Refer to the specific eviction policy for details.

Returns
reference to the first element in the cache

Definition at line 573 of file cache.hpp.

◆ front() [2/2]

template<typename K , typename V , template< typename, typename > class CachePolicy>
const K & boxes::cache::Cache< K, V, CachePolicy >::front ( ) const
inline

Returns const reference the first element in the cache.

Returns
const reference to the first element in the cache

Definition at line 590 of file cache.hpp.

◆ insert()

template<typename K , typename V , template< typename, typename > class CachePolicy>
template<typename KArg , typename VArg >
bool boxes::cache::Cache< K, V, CachePolicy >::insert ( KArg &&  key,
VArg &&  value 
)
inline

Inserts a key-value pair into the cache.

If the key is already in the cache, the value is updated and the key is refreshed in accordance with the eviction policy.

Parameters
keykey to insert
valuevalue to insert
Returns
true if the key was inserted, false if the key was already in the cache

Definition at line 458 of file cache.hpp.

◆ max_size()

template<typename K , typename V , template< typename, typename > class CachePolicy>
std::size_t boxes::cache::Cache< K, V, CachePolicy >::max_size ( ) const
inline

Returns the maximum number of elements the cache can ever hold.

This is the upper water mark for the cache as constrained by platform implementation.

This number is not synonymous with the cache size, as restricted by the eviction policy.

Returns
maximum number of elements the cache can ever hold

Definition at line 429 of file cache.hpp.

◆ reserve()

template<typename K , typename V , template< typename, typename > class CachePolicy>
void boxes::cache::Cache< K, V, CachePolicy >::reserve ( std::size_t  size)
inline

Reserve space for the cache to avoid unnecessary reallocations.

Parameters
sizenumber of elements to reserve space for

Definition at line 409 of file cache.hpp.

◆ size()

template<typename K , typename V , template< typename, typename > class CachePolicy>
std::size_t boxes::cache::Cache< K, V, CachePolicy >::size ( ) const
inline

Returns the number of elements in the cache.

Returns
number of elements in the cache

Definition at line 416 of file cache.hpp.


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