![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Public Member Functions | |
| LRUCache (int _capacity) | |
| Constructor, Initialize the head and tail pointers to nullptr and initialize the _capacity of the cache. | |
| void | put (K key, V value) |
| upsert a key-value pair | |
| V | get (K key) |
| get the value of the key-value pair if exists | |
| int | size () const |
| Returns the number of items present in the cache. | |
| int | capacity () const |
| Returns the total capacity of the cache. | |
| bool | empty () const |
| returns whether the cache is empty or not | |
| ~LRUCache () | |
| destructs the cache, iterates on the map and deletes every node present in the cache. | |
Private Member Functions | |
| void | push_front (CacheNode< K, V > *node_ptr) |
| push the node to the front of the linked list. | |
| void | make_recent (CacheNode< K, V > *node_ptr) |
| move the existing node in the list to the beginning of the list. | |
| void | pop_back () |
| pop the last node in the linked list. | |
Private Attributes | |
| CacheNode< K, V > * | head |
| head of the doubly linked list | |
| CacheNode< K, V > * | tail |
| tail of the doubly linked list | |
| std::uint32_t | _capacity |
| maximum capacity of the cache | |
| std::unordered_map< K, CacheNode< K, V > * > | node_map |
| maps the key to the node address | |
| K | type of key in the LRU |
| V | type of value in the LRU |
Definition at line 61 of file lru_cache2.cpp.
|
inlineexplicit |
Constructor, Initialize the head and tail pointers to nullptr and initialize the _capacity of the cache.
| _capacity | Total capacity of the cache |
Definition at line 75 of file lru_cache2.cpp.
|
inline |
destructs the cache, iterates on the map and deletes every node present in the cache.
Definition at line 205 of file lru_cache2.cpp.
|
inline |
Returns the total capacity of the cache.
Definition at line 193 of file lru_cache2.cpp.
|
inline |
returns whether the cache is empty or not
Definition at line 199 of file lru_cache2.cpp.
|
inline |
get the value of the key-value pair if exists
| key | key of the key-value pair |
| exception | is thrown if the key is not present in the cache |
Definition at line 172 of file lru_cache2.cpp.
|
inlineprivate |
move the existing node in the list to the beginning of the list.
| node_ptr | node to be moved to the beginning. |
Definition at line 99 of file lru_cache2.cpp.
|
inlineprivate |
pop the last node in the linked list.
Definition at line 122 of file lru_cache2.cpp.
|
inlineprivate |
|
inline |
upsert a key-value pair
| key | key of the key-value pair |
| value | value of the key-value pair |
Definition at line 145 of file lru_cache2.cpp.
|
inline |
Returns the number of items present in the cache.
Definition at line 187 of file lru_cache2.cpp.
|
private |
maximum capacity of the cache
Definition at line 64 of file lru_cache2.cpp.
|
private |
head of the doubly linked list
Definition at line 62 of file lru_cache2.cpp.
|
private |
maps the key to the node address
Definition at line 67 of file lru_cache2.cpp.
|
private |
tail of the doubly linked list
Definition at line 63 of file lru_cache2.cpp.