#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_type & | operator= (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. | |
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.
|
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.
|
inline |
lru_cache destructor
Iterates across every entry in the map and calls the deletor before calling the standard destructors
|
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
|
inline |
assign
Updates data at spot iter with data v.
|
inline |
at
Retrives the data for key k, not valid if k does not exist
|
inline |
contains
Returns true if key k exists in the cache
|
inline |
emplace
Places a new object in place k created with args If the key already exists, it is replaced.
|
inline |
erase
erases key k from the cache. If k does not exist, returns false. If k exists, returns true.
|
inline |
erase_oldest
Removes the oldest entry from the cache.
|
inline |
get
Retrives the data for key k. If no data exists, it will be created by calling the creator.
|
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.
|
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
|
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.
|
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.
|
inline |
touch
Touches key at iterator iter, moving it to most recently used position