Nugget
Public Types | Public Member Functions | List of all members
eastl::lru_cache< Key, Value, Allocator, list_type, map_type > Class Template Reference

#include <lru_cache.h>

Public Types

using key_type = Key
 
using value_type = Value
 
using allocator_type = Allocator
 
using size_type = eastl_size_t
 
using list_iterator = typename list_type::iterator
 
using map_iterator = typename map_type::iterator
 
using data_container_type = eastl::pair< value_type, list_iterator >
 
using iterator = typename map_type::iterator
 
using const_iterator = typename map_type::const_iterator
 
using this_type = lru_cache< key_type, value_type, Allocator, list_type, map_type >
 
using create_callback_type = eastl::function< value_type(key_type)>
 
using delete_callback_type = eastl::function< void(const value_type &)>
 

Public Member Functions

 lru_cache (size_type size, const allocator_type &allocator=EASTL_LRUCACHE_DEFAULT_ALLOCATOR, create_callback_type creator=nullptr, delete_callback_type deletor=nullptr)
 
 ~lru_cache ()
 
 lru_cache (std::initializer_list< eastl::pair< Key, Value >> il)
 
 lru_cache (const this_type &)=delete
 
this_typeoperator= (const this_type &)=delete
 
bool insert (const key_type &k, const value_type &v)
 
template<typename... Args>
void emplace (const key_type &k, Args &&... args)
 
void insert_or_assign (const key_type &k, const value_type &v)
 
bool contains (const key_type &k) const
 
eastl::optional< value_type > at (const key_type &k)
 
value_type & get (const key_type &k)
 
value_type & operator[] (const key_type &k)
 Equivalent to get(k)
 
bool erase (const key_type &k)
 
void erase_oldest ()
 
bool touch (const key_type &k)
 
void touch (iterator &iter)
 
bool assign (const key_type &k, const value_type &v)
 
void assign (iterator &iter, const value_type &v)
 
iterator begin () EA_NOEXCEPT
 
iterator end () EA_NOEXCEPT
 
iterator rbegin () EA_NOEXCEPT
 
iterator rend () EA_NOEXCEPT
 
const_iterator begin () const EA_NOEXCEPT
 
const_iterator cbegin () const EA_NOEXCEPT
 
const_iterator crbegin () const EA_NOEXCEPT
 
const_iterator end () const EA_NOEXCEPT
 
const_iterator cend () const EA_NOEXCEPT
 
const_iterator crend () const EA_NOEXCEPT
 
bool empty () const EA_NOEXCEPT
 
size_type size () const EA_NOEXCEPT
 
size_type capacity () const EA_NOEXCEPT
 
void clear () EA_NOEXCEPT
 
void resize (size_type newSize)
 
void setCreateCallback (create_callback_type callback)
 
void setDeleteCallback (delete_callback_type callback)
 
const allocator_type & get_allocator () const EA_NOEXCEPT
 
allocator_type & get_allocator () EA_NOEXCEPT
 
void set_allocator (const allocator_type &allocator)
 
void reset_lose_memory () EA_NOEXCEPT
 Does not reset the callbacks.
 

Detailed Description

template<typename Key, typename Value, typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
class eastl::lru_cache< Key, Value, Allocator, list_type, map_type >

lru_cache

Implements a caching map based off of a key and data. LRUList parameter is any container that guarantees the validity of its iterator even after a modification (e.g. list) LRUMap is any mapping container that can map a key to some data. By default, we use unordered_set, but it might be better to use hash_map or some other structure depending on your key/data combination. For example, you may want to swap the map backing if using strings as keys or if the data objects are small. In any case, unordered_set is a good default and should work well enough since the purpose of this class is to cache results of expensive, order of milliseconds, operations

Algorithmic Performance (default data structures): touch() -> O(1) insert() / update(), get() / operator[] -> equivalent to unordered_set (O(1) on average, O(n) worst) size() -> O(1)

All accesses to a given key (insert, update, get) will push that key to most recently used. If the data objects are shared between threads, it would be best to use a smartptr to manage the lifetime of the data. as it could be removed from the cache while in use by another thread.

Constructor & Destructor Documentation

◆ lru_cache()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::lru_cache ( size_type  size,
const allocator_type &  allocator = EASTL_LRUCACHE_DEFAULT_ALLOCATOR,
create_callback_type  creator = nullptr,
delete_callback_type  deletor = nullptr 
)
inlineexplicit

lru_cache constructor

Creates a Key / Value map that only stores size Value objects until it deletes them. For complex objects or operations, the creator and deletor callbacks can be used. This works just like a regular map object: on access, the Value will be created if it doesn't exist, returned otherwise.

◆ ~lru_cache()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::~lru_cache ( )
inline

lru_cache destructor

Iterates across every entry in the map and calls the deletor before calling the standard destructors

Member Function Documentation

◆ assign() [1/2]

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
bool eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::assign ( const key_type &  k,
const value_type &  v 
)
inline

assign

Updates key k with data v. If key k does not exist, returns false and no changes are made. If key k exists, existing data has its deletor called and key k's data is replaced with new v data

◆ assign() [2/2]

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
void eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::assign ( iterator &  iter,
const value_type &  v 
)
inline

assign

Updates data at spot iter with data v.

◆ at()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
eastl::optional<value_type> eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::at ( const key_type &  k)
inline

at

Retrives the data for key k, not valid if k does not exist

◆ contains()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
bool eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::contains ( const key_type &  k) const
inline

contains

Returns true if key k exists in the cache

◆ emplace()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
template<typename... Args>
void eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::emplace ( const key_type &  k,
Args &&...  args 
)
inline

emplace

Places a new object in place k created with args If the key already exists, it is replaced.

◆ erase()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
bool eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::erase ( const key_type &  k)
inline

erase

erases key k from the cache. If k does not exist, returns false. If k exists, returns true.

◆ erase_oldest()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
void eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::erase_oldest ( )
inline

erase_oldest

Removes the oldest entry from the cache.

◆ get()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
value_type& eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::get ( const key_type &  k)
inline

get

Retrives the data for key k. If no data exists, it will be created by calling the creator.

◆ insert()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
bool eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::insert ( const key_type &  k,
const value_type &  v 
)
inline

insert

insert key k with value v. If key already exists, no change is made and the return value is false. If the key doesn't exist, the data is added to the map and the return value is true.

◆ insert_or_assign()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
void eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::insert_or_assign ( const key_type &  k,
const value_type &  v 
)
inline

insert_or_assign

Same as add, but replaces the data at key k, if it exists, with the new entry v Note that the deletor for the old v will be called before it's replaced with the new value of v

◆ resize()

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
void eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::resize ( size_type  newSize)
inline

resize

Resizes the cache. Can be used to either expand or contract the cache. In the case of a contraction, the oldest entries will be evicted with their respective deletors called before completing.

◆ touch() [1/2]

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
bool eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::touch ( const key_type &  k)
inline

touch

Touches key k, marking it as most recently used. If k does not exist, returns false. If the touch was successful, returns true.

◆ touch() [2/2]

template<typename Key , typename Value , typename Allocator = EASTLAllocatorType, typename list_type = eastl::list<Key, Allocator>, typename map_type = eastl::unordered_map<Key, eastl::pair<Value, typename list_type::iterator>, eastl::hash<Key>, eastl::equal_to<Key>, Allocator>>
void eastl::lru_cache< Key, Value, Allocator, list_type, map_type >::touch ( iterator &  iter)
inline

touch

Touches key at iterator iter, moving it to most recently used position


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