TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation for [LFU Cache] (https://en.wikipedia.org/wiki/Least_frequently_used) More...
#include <cassert>
#include <iostream>
#include <unordered_map>
Go to the source code of this file.
Classes | |
class | others::Cache::D_Node< T > |
Node for a doubly linked list with data, prev and next pointers. More... | |
class | others::Cache::LFUCache< K, V > |
LFUCache. More... | |
Namespaces | |
namespace | others |
for vector | |
namespace | others::Cache |
Cache algorithm. | |
Typedefs | |
template<typename K , typename V > | |
using | others::Cache::CacheNode = D_Node<std::pair<K, V>> |
Functions | |
static void | test () |
self test implementation | |
int | main () |
main function | |
Implementation for [LFU Cache] (https://en.wikipedia.org/wiki/Least_frequently_used)
LFU discards the least frequently used value. if there are multiple items with the same minimum frequency then, the least recently used among them is discarded. Data structures used - doubly linked list and unordered_map(hash map).
Hashmap maps the key to the address of the node of the linked list and its current usage frequency. If the element is accessed the element is removed from the linked list of the current frequency and added to the linked list of incremented frequency.
When the cache is full, the last element in the minimum frequency linked list is popped.
Definition in file lfu_cache.cpp.
int main | ( | void | ) |
main function
Definition at line 301 of file lfu_cache.cpp.
|
static |
self test implementation
Definition at line 250 of file lfu_cache.cpp.