25#include <unordered_map>   
   54template <
typename K, 
typename V>
 
   55using CacheNode = D_Node<std::pair<K, V>>;
 
   63template <
typename K, 
typename V>
 
   65    std::unordered_map<K, std::pair<CacheNode<K, V> *, 
int>>
 
   67    std::unordered_map<int, std::pair<CacheNode<K, V> *, CacheNode<K, V> *>>
 
   96        std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = 
freq_map[freq];
 
  100        p.first->prev = 
node;
 
  101        node->next = p.first;
 
 
  110        CacheNode<K, V> *
node = p_node.first;
 
  111        int freq = p_node.second;
 
  113        std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = 
freq_map[freq];
 
  118        if (p.first == 
node && p.second == 
node) {
 
  125            CacheNode<K, V> *prev = 
node->prev;
 
  126            CacheNode<K, V> *next = 
node->next;
 
  127            node->prev = 
nullptr;
 
  128            node->next = 
nullptr;
 
 
  150        std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = 
freq_map[
minFreq];
 
  155        if (p.first == p.second) {
 
  162        CacheNode<K, V> *temp = p.second;
 
  163        p.second = temp->
prev;
 
  164        p.second->next = 
nullptr;
 
 
  174    void put(K key, V value) {
 
  177            node_map[key].first->data.second = value;
 
  190        CacheNode<K, V> *
node = 
new CacheNode<K, V>({key, value});
 
 
  204            throw std::runtime_error(
"key is not present in the cache");
 
  208        V value = 
node_map[key].first->data.second;
 
 
  238            delete it->second.first;
 
 
 
 
  254    assert(cache.
size() == 0);
 
  256    assert(cache.
empty());
 
  263    assert(cache.
size() == 2);
 
  265    assert(!cache.
empty());
 
  268    assert(cache.
get(1) == 10);
 
  269    assert(cache.
get(-2) == 20);
 
  277    assert(cache.
size() == 5);
 
  279    assert(!cache.
empty());
 
  282    assert(cache.
get(1) == 10);
 
  283    assert(cache.
get(-2) == 20);
 
  290    assert(cache.
get(4) == 40);
 
  291    assert(cache.
get(5) == -50);
 
  292    assert(cache.
get(6) == 60);
 
  294    std::cout << 
"test - passed\n";
 
 
D_Node< std::pair< K, V > > * next
D_Node< std::pair< K, V > > * prev
void increase_frequency(std::pair< CacheNode< K, V > *, int > &p_node)
increase the frequency of node and push it in the respective list.
int _capacity
maximum capacity of the cache
V get(K key)
get the value of the key-value pair if exists
bool empty() const
returns true if the cache is empty, false otherwise.
int minFreq
minimum frequency in the cache
~LFUCache()
destructs the cache, iterates on the map and deletes every node present in the cache.
void pop()
pop the last node in the least frequently used linked list
std::unordered_map< int, std::pair< CacheNode< K, V > *, CacheNode< K, V > * > > freq_map
maps the frequency to doubly linked list
int capacity() const
Returns the total capacity of the cache.
std::unordered_map< K, std::pair< CacheNode< K, V > *, int > > node_map
maps the key to the node address and frequency
void push(int freq, CacheNode< K, V > *node)
push the node at first position in the linked list of given frequency
int size() const
Returns the number of items present in the cache.
void put(K key, V value)
upsert a key-value pair
LFUCache(int _capacity)
Constructor, Initialize with minFreq and _capacity.
static void test()
self test implementation