TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Public Member Functions | |
LFUCache (int _capacity) | |
Constructor, Initialize with minFreq and _capacity. | |
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 true if the cache is empty, false otherwise. | |
~LFUCache () | |
destructs the cache, iterates on the map and deletes every node present in the cache. | |
Private Member Functions | |
void | push (int freq, CacheNode< K, V > *node) |
push the node at first position in the linked list of given frequency | |
void | increase_frequency (std::pair< CacheNode< K, V > *, int > &p_node) |
increase the frequency of node and push it in the respective list. | |
void | pop () |
pop the last node in the least frequently used linked list | |
Private Attributes | |
std::unordered_map< K, std::pair< CacheNode< K, V > *, int > > | node_map |
maps the key to the node address and frequency | |
std::unordered_map< int, std::pair< CacheNode< K, V > *, CacheNode< K, V > * > > | freq_map |
maps the frequency to doubly linked list | |
int | minFreq |
minimum frequency in the cache | |
int | _capacity |
maximum capacity of the cache | |
K | type of key in the LFU |
V | type of value in the LFU |
Definition at line 64 of file lfu_cache.cpp.
|
inlineexplicit |
Constructor, Initialize with minFreq and _capacity.
_capacity | Total capacity of the cache. |
Definition at line 78 of file lfu_cache.cpp.
|
inline |
destructs the cache, iterates on the map and deletes every node present in the cache.
Definition at line 235 of file lfu_cache.cpp.
|
inline |
Returns the total capacity of the cache.
Definition at line 223 of file lfu_cache.cpp.
|
inline |
returns true if the cache is empty, false otherwise.
Definition at line 229 of file lfu_cache.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 202 of file lfu_cache.cpp.
|
inlineprivate |
increase the frequency of node and push it in the respective list.
p_node | the node to be updated |
Definition at line 109 of file lfu_cache.cpp.
|
inlineprivate |
pop the last node in the least frequently used linked list
Definition at line 149 of file lfu_cache.cpp.
|
inlineprivate |
push the node at first position in the linked list of given frequency
freq | the frequency mapping to the linked list where node should be pushed. |
node | node to be pushed to the linked list. |
Definition at line 88 of file lfu_cache.cpp.
|
inline |
upsert a key-value pair
key | key of the key-value pair |
value | value of the key-value pair |
Definition at line 174 of file lfu_cache.cpp.
|
inline |
Returns the number of items present in the cache.
Definition at line 217 of file lfu_cache.cpp.
|
private |
maximum capacity of the cache
Definition at line 71 of file lfu_cache.cpp.
|
private |
maps the frequency to doubly linked list
Definition at line 68 of file lfu_cache.cpp.
|
private |
minimum frequency in the cache
Definition at line 70 of file lfu_cache.cpp.
|
private |
maps the key to the node address and frequency
Definition at line 66 of file lfu_cache.cpp.