HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_LRUCache< K, V, SizeFunc, InUseFunc, L > Class Template Reference

#include <UT_LRUCache.h>

+ Inheritance diagram for UT_LRUCache< K, V, SizeFunc, InUseFunc, L >:

Classes

class  iterator_base
 

Public Types

using iterator = iterator_base< typename ValueList::iterator, std::pair< const K, V >>
 A iterator pointing to mutable values. More...
 
using const_iterator = iterator_base< typename ValueList::const_iterator, const std::pair< const K, V >>
 A const iterator pointing to immutable values. More...
 

Public Member Functions

 UT_LRUCache (exint max_size=SYS_EXINT_MAX)
 
void setMaxSize (exint max_size)
 
exint maxSize () const
 Returns the current maximum size of the cache. More...
 
exint currentSize () const
 
exint count () const
 Returns the number of items in the cache. More...
 
bool contains (const K &key) const
 
iterator find (const K &key)
 
std::pair< iterator, bool > insert (const K &key, V &&value, bool evict=false)
 
bool erase (const K &key)
 
bool steal (const K &key, V &&value)
 
void clear ()
 Clears the cache completely. More...
 
iterator begin ()
 Returns an iterator to the front-most item in the LRU cache. More...
 
iterator end ()
 Returns an iterator to the end of the LRU list. More...
 
const_iterator begin () const
 Returns a const iterator to the front-most item in the LRU cache. More...
 
const_iterator end () const
 Returns a const iterator to the end of the LRU list. More...
 

Friends

template<typename PROXIED , typename TYPE >
class iterator_base
 

Additional Inherited Members

- Protected Member Functions inherited from UT_NonCopyableNS::UT_NonCopyable
 UT_NonCopyable ()=default
 
 ~UT_NonCopyable ()=default
 
 UT_NonCopyable (const UT_NonCopyable &)=delete
 
UT_NonCopyableoperator= (const UT_NonCopyable &)=delete
 

Detailed Description

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
class UT_LRUCache< K, V, SizeFunc, InUseFunc, L >

Definition at line 71 of file UT_LRUCache.h.

Member Typedef Documentation

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
using UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::const_iterator = iterator_base<typename ValueList::const_iterator, const std::pair<const K, V>>

A const iterator pointing to immutable values.

Definition at line 175 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
using UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::iterator = iterator_base<typename ValueList::iterator, std::pair<const K, V>>

A iterator pointing to mutable values.

Definition at line 172 of file UT_LRUCache.h.

Constructor & Destructor Documentation

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::UT_LRUCache ( exint  max_size = SYS_EXINT_MAX)
inline

Construct a new LRU cache of a given max_size. By default the cache is not thread-safe, but by setting the template argument L to UT_RWLock, the cache is automatically thread-safe. This incurs some overhead due to the locking, however, so only do that if absolutely required.

Definition at line 182 of file UT_LRUCache.h.

Member Function Documentation

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
iterator UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::begin ( void  )
inline

Returns an iterator to the front-most item in the LRU cache.

Definition at line 421 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
const_iterator UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::begin ( void  ) const
inline

Returns a const iterator to the front-most item in the LRU cache.

Definition at line 435 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
void UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::clear ( void  )
inline

Clears the cache completely.

Definition at line 409 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
bool UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::contains ( const K &  key) const
inline

Checks for the presence of the item with the given key. Does not affect their ordering, like find.

Definition at line 247 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
exint UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::count ( ) const
inline

Returns the number of items in the cache.

Definition at line 233 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
exint UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::currentSize ( ) const
inline

Returns the current size of the cache, in arbitrary units. If the cache is thread-safe this value may not be the most up-to-date.

Definition at line 220 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
iterator UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::end ( void  )
inline

Returns an iterator to the end of the LRU list.

Definition at line 428 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
const_iterator UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::end ( void  ) const
inline

Returns a const iterator to the end of the LRU list.

Definition at line 442 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
bool UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::erase ( const K &  key)
inline

Removes the item matching the key. If the item existed and was successfully removed, then this function returns true.

Definition at line 359 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
iterator UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::find ( const K &  key)
inline

Find the item in the cache with the given key. If nothing is found then the iterator will point to the end. There's no const version, since looking up the key modifies the LRU cache's state and the iterator holds a read lock, if the cache is thread-safe.

Note
Since the iterator holds a lock, do

Definition at line 265 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
std::pair<iterator, bool> UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::insert ( const K &  key,
V &&  value,
bool  evict = false 
)
inline

Insert a new item into the cache. The item is automatically marked as the most recently used. The cache is pruned beforehand, to avoid evicting the item immediately. Returns a pair of an iterator and a bool. The iterator points to the item inserted, or the existing item if not evicted. The bool value indicates whether the item got inserted or not.

Definition at line 295 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
exint UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::maxSize ( ) const
inline

Returns the current maximum size of the cache.

Definition at line 216 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
void UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::setMaxSize ( exint  max_size)
inline

Sets the new maximum size for the cache. If the new maximum is smaller than the current maximum, the cache will be pruned to fit the new maximums size.

Definition at line 196 of file UT_LRUCache.h.

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
bool UT_LRUCache< K, V, SizeFunc, InUseFunc, L >::steal ( const K &  key,
V &&  value 
)
inline

Steals an item from the cache. This is functionally equivalent to erase with a key, except the item in the cache also gets hoisted outside into value, if it exists.

Definition at line 383 of file UT_LRUCache.h.

Friends And Related Function Documentation

template<typename K, typename V, exint(*)(const V &) SizeFunc = UTlruGetItemSize<V>, bool(*)(const V &) InUseFunc = UTlruGetItemInUse<V>, typename L = UT::RWNullLock>
template<typename PROXIED , typename TYPE >
friend class iterator_base
friend

Definition at line 79 of file UT_LRUCache.h.


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