|
| LruCache (const AllocType &alloc=AllocType()) |
| Creates an LruCache instance. More...
|
|
| LruCache (std::size_t maxSize, CacheCostFunc cacheCostFunc=CacheCostFunc(), const CompareType &compare=CompareType(), const AllocType &alloc=AllocType()) |
| Creates an LruCache instance. More...
|
|
| LruCache (const LruCache &)=delete |
| The deleted copy constructor.
|
|
| LruCache (LruCache &&other) noexcept |
| The default move constructor.
|
|
LruCache & | operator= (const LruCache &)=delete |
| The deleted assignment operator.
|
|
LruCache & | operator= (LruCache &&other) noexcept |
| The default move assignment operator.
|
|
template<typename _Key , typename _Value > |
std::pair< const_iterator, bool > | Insert (_Key &&key, _Value &&value) |
| Inserts a key-value pair in the cache. More...
|
|
template<typename _Value > |
std::pair< const_iterator, bool > | InsertOrAssign (Key key, _Value &&value) |
| Inserts a key-value pair in the cache or updates an existing key-value pair. More...
|
|
bool | Erase (const Key &key) |
| Removes a key from the cache. More...
|
|
const_iterator | Erase (const_iterator &it) |
| Removes a key from the cache. More...
|
|
std::size_t | Size () const |
| Gets the current size of the cache. More...
|
|
std::size_t | GetMaxSize () const |
| Gets the maximum size of the cache. More...
|
|
void | Resize (size_t maxSize) |
| Sets the new maximum size of the cache. More...
|
|
const_iterator | Find (const Key &key) |
| Finds a value in the cache. More...
|
|
const_iterator | FindNoPromote (const Key &key) const |
| Finds a value in the cache. More...
|
|
const Value & | Find (const Key &key, const Value &nullValue) |
| Finds a value in the cache. More...
|
|
const_iterator | begin () const |
| Returns a constant iterator to the beginning.
|
|
const_iterator | end () const |
| Returns a constant iterator to the end.
|
|
const_iterator | rbegin () const |
| Returns a reverse constant iterator to the beginning.
|
|
const_iterator | rend () const |
| Returns a reverse constant iterator to the end.
|
|
void | Clear () |
| Removes all items from the cache. More...
|
|
void | SetEvictionCallback (EvictionFunction func) |
| Sets a function that is invoked when a value is evicted from the cache. More...
|
|
template<typename _Key , typename _Value > |
auto | Insert (_Key &&key, _Value &&value) -> std::pair< const_iterator, bool > |
| Inserts a key-value pair in the cache. More...
|
|
template<typename _Value > |
auto | InsertOrAssign (Key key, _Value &&value) -> std::pair< const_iterator, bool > |
| Inserts a key-value pair in the cache or updates an existing key-value pair. More...
|
|
template<typename Key, typename Value, typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
class olp::utils::LruCache< Key, Value, CacheCostFunc, Compare, Alloc >
A generic key-value LRU cache.
This cache stores elements in a map up to the specified maximum size. The cache eviction follows the LRU principle: the element that was accessed last is evicted last.
The specializations return a non-zero value for any given object.
- Template Parameters
-
Key | The LruCache key type. |
Value | The LruCache value type. |
CacheCostFunc | The cache cost functor. The specializations should return a non-zero value for any given object. The default implementation returns "1" as the size for each object. |
Compare | The comparison function to be used for sorting keys. The default value of std::less is used, which sorts the keys in ascending order. |
Alloc | The allocator to be used for allocating internal data. The default value of std::allocator is used. |
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
Removes all items from the cache.
Removes all content but does not reset the eviction callback or maximum size.
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
template<typename _Key , typename _Value >
Inserts a key-value pair in the cache.
- Note
- If the key already exists in the cache, it is promoted in the LRU, but its value and cost are not updated. To update or insert existing values, use
InsertOrAssign
instead.
If the key or value is an rvalue reference, they are moved; copied otherwise.
- Note
- This function behaves analogously to
std::map
. Even if the insertion fails, the key and value can be moved. Do not access them further.
- Parameters
-
key | The key to add. |
value | The value to add. |
- Returns
- A pair of bool and an iterator, analogously to
std::map::insert()
. If the bool is true, the item is inserted, and the iterator points to the newly inserted item. If the bool is false and the iterator points to end()
, the item cannot be inserted. Otherwise, the bool is false, and the iterator points to the item that prevented the insertion.
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
template<typename _Key , typename _Value >
Inserts a key-value pair in the cache.
- Note
- If the key already exists in the cache, it is promoted in the LRU, but its value and cost are not updated. To update or insert existing values, use
InsertOrAssign
instead.
If the key or value is an rvalue reference, they are moved; copied otherwise.
- Note
- This function behaves analogously to
std::map
. Even if the insertion fails, the key and value can be moved. Do not access them further.
- Parameters
-
key | The key to add. |
value | The value to add. |
- Returns
- A pair of bool and an iterator, analogously to
std::map::insert()
. If the bool is true, the item is inserted, and the iterator points to the newly inserted item. If the bool is false and the iterator points to end()
, the item cannot be inserted. Otherwise, the bool is false, and the iterator points to the item that prevented the insertion.
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
template<typename _Value >
Inserts a key-value pair in the cache or updates an existing key-value pair.
- Note
- If the key already exists in the cache, its value and cost are updated. Not to update the existing key-value pair, use
Insert
instead.
- Parameters
-
key | The key to add. |
value | The value to add. |
- Returns
- A pair of bool and an iterator, analogously to
std::map::insert_or_assign()
. If the bool is true, the item is inserted, and the iterator points to the newly inserted item. If the bool is false and the iterator points to end()
, the item cannot be inserted. Otherwise, the bool is false, and the iterator points to the item that is assigned.
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
template<typename _Value >
Inserts a key-value pair in the cache or updates an existing key-value pair.
- Note
- If the key already exists in the cache, its value and cost are updated. Not to update existing key-value pairs, use
Insert
instead.
- Parameters
-
key | The key to add. |
value | The value to add. |
- Returns
- A pair of bool and an iterator, analogously to
std::map::insert_or_assign()
. If the bool is true, the item is inserted, and the iterator points to the newly inserted item. If the bool is false and the iterator points to end()
, the item cannot be inserted. Otherwise, the bool is false, and the iterator points to the item that is assigned.
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
Sets the new maximum size of the cache.
If the new maximum size is smaller than the current size, items are evicted until the cache shrinks to less than or equal to the new maximum size.
- Parameters
-
maxSize | The new maximum size of the cache. |
template<typename Key , typename Value , typename CacheCostFunc = CacheCost<Value>, typename Compare = std::less<Key>, template< typename > class Alloc = std::allocator>
Sets a function that is invoked when a value is evicted from the cache.
- Note
- The function must not modify the cache in the callback. The value can be safely moved. If not, it is destroyed when the function returns.
To reset the eviction callback, pass nullptr
.
- Parameters
-
func | The function to be called on eviction. |