TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
others::Cache::LFUCache< K, V > Class Template Reference

LFUCache. More...

Collaboration diagram for others::Cache::LFUCache< K, V >:
[legend]

Public Member Functions

 LFUCache (int _capacity)
 Constructor, Initialize with minFreq and _capacity.
 
void put (K key, V value)
 upsert a key-value pair
 
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
 

Detailed Description

template<typename K, typename V>
class others::Cache::LFUCache< K, V >

LFUCache.

Template Parameters
Ktype of key in the LFU
Vtype of value in the LFU

Definition at line 64 of file lfu_cache.cpp.

Constructor & Destructor Documentation

◆ LFUCache()

template<typename K , typename V >
others::Cache::LFUCache< K, V >::LFUCache ( int _capacity)
inlineexplicit

Constructor, Initialize with minFreq and _capacity.

Parameters
_capacityTotal capacity of the cache.

Definition at line 78 of file lfu_cache.cpp.

int _capacity
maximum capacity of the cache
Definition lfu_cache.cpp:71
int minFreq
minimum frequency in the cache
Definition lfu_cache.cpp:70

◆ ~LFUCache()

template<typename K , typename V >
others::Cache::LFUCache< K, V >::~LFUCache ( )
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.

235 {
236 auto it = node_map.begin();
237 while (it != node_map.end()) {
238 delete it->second.first;
239 ++it;
240 }
241 }
std::unordered_map< K, std::pair< CacheNode< K, V > *, int > > node_map
maps the key to the node address and frequency
Definition lfu_cache.cpp:66

Member Function Documentation

◆ capacity()

template<typename K , typename V >
int others::Cache::LFUCache< K, V >::capacity ( ) const
inline

Returns the total capacity of the cache.

Returns
Total capacity of the cache

Definition at line 223 of file lfu_cache.cpp.

223{ return _capacity; }

◆ empty()

template<typename K , typename V >
bool others::Cache::LFUCache< K, V >::empty ( ) const
inline

returns true if the cache is empty, false otherwise.

Returns
true if the cache is empty, false otherwise.

Definition at line 229 of file lfu_cache.cpp.

229{ return node_map.empty(); }

◆ get()

template<typename K , typename V >
V others::Cache::LFUCache< K, V >::get ( K key)
inline

get the value of the key-value pair if exists

Parameters
keykey of the key-value pair
Returns
the value mapped to the given key
Exceptions
exceptionis thrown if the key is not present in the cache

Definition at line 202 of file lfu_cache.cpp.

202 {
203 if (!node_map.count(key)) {
204 throw std::runtime_error("key is not present in the cache");
205 }
206
207 // increase the frequency and return the value
208 V value = node_map[key].first->data.second;
210 return value;
211 }
void increase_frequency(std::pair< CacheNode< K, V > *, int > &p_node)
increase the frequency of node and push it in the respective list.

◆ increase_frequency()

template<typename K , typename V >
void others::Cache::LFUCache< K, V >::increase_frequency ( std::pair< CacheNode< K, V > *, int > & p_node)
inlineprivate

increase the frequency of node and push it in the respective list.

Parameters
p_nodethe node to be updated

Definition at line 109 of file lfu_cache.cpp.

109 {
110 CacheNode<K, V> *node = p_node.first;
111 int freq = p_node.second;
112
113 std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = freq_map[freq];
114
115 // if the given node is the only node in the list,
116 // then erase the frequency from map
117 // and increase minFreq by 1.
118 if (p.first == node && p.second == node) {
119 freq_map.erase(freq);
120 if (minFreq == freq) {
121 minFreq = freq + 1;
122 }
123 } else {
124 // remove the given node from current freq linked list
125 CacheNode<K, V> *prev = node->prev;
126 CacheNode<K, V> *next = node->next;
127 node->prev = nullptr;
128 node->next = nullptr;
129
130 if (prev) {
131 prev->next = next;
132 } else {
133 p.first = next;
134 }
135
136 if (next) {
137 next->prev = prev;
138 } else {
139 p.second = prev;
140 }
141 }
142 push(freq + 1, node);
143 ++p_node.second;
144 }
std::unordered_map< int, std::pair< CacheNode< K, V > *, CacheNode< K, V > * > > freq_map
maps the frequency to doubly linked list
Definition lfu_cache.cpp:68
void push(int freq, CacheNode< K, V > *node)
push the node at first position in the linked list of given frequency
Definition lfu_cache.cpp:88

◆ pop()

template<typename K , typename V >
void others::Cache::LFUCache< K, V >::pop ( )
inlineprivate

pop the last node in the least frequently used linked list

Definition at line 149 of file lfu_cache.cpp.

149 {
150 std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = freq_map[minFreq];
151
152 // if there is only one node
153 // remove the node and erase
154 // the frequency from freq_map
155 if (p.first == p.second) {
156 delete p.first;
157 freq_map.erase(minFreq);
158 return;
159 }
160
161 // remove the last node in the linked list
162 CacheNode<K, V> *temp = p.second;
163 p.second = temp->prev;
164 p.second->next = nullptr;
165 delete temp;
166 }

◆ push()

template<typename K , typename V >
void others::Cache::LFUCache< K, V >::push ( int freq,
CacheNode< K, V > * node )
inlineprivate

push the node at first position in the linked list of given frequency

Parameters
freqthe frequency mapping to the linked list where node should be pushed.
nodenode to be pushed to the linked list.

Definition at line 88 of file lfu_cache.cpp.

88 {
89 // if freq is not present, then make a new list with node as the head as
90 // well as tail.
91 if (!freq_map.count(freq)) {
92 freq_map[freq] = {node, node};
93 return;
94 }
95
96 std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = freq_map[freq];
97
98 // insert the node at the beginning of the linked list and update the
99 // head.
100 p.first->prev = node;
101 node->next = p.first;
102 p.first = node;
103 }
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13

◆ put()

template<typename K , typename V >
void others::Cache::LFUCache< K, V >::put ( K key,
V value )
inline

upsert a key-value pair

Parameters
keykey of the key-value pair
valuevalue of the key-value pair

Definition at line 174 of file lfu_cache.cpp.

174 {
175 // update the value if key already exists
176 if (node_map.count(key)) {
177 node_map[key].first->data.second = value;
179 return;
180 }
181
182 // if the cache is full
183 // remove the least frequently used item
184 if (node_map.size() == _capacity) {
185 node_map.erase(freq_map[minFreq].second->data.first);
186 pop();
187 }
188
189 // insert the new node and set minFreq to 1
190 CacheNode<K, V> *node = new CacheNode<K, V>({key, value});
191 node_map[key] = {node, 1};
192 minFreq = 1;
193 push(1, node);
194 }
void pop()
pop the last node in the least frequently used linked list

◆ size()

template<typename K , typename V >
int others::Cache::LFUCache< K, V >::size ( ) const
inline

Returns the number of items present in the cache.

Returns
number of items in the cache

Definition at line 217 of file lfu_cache.cpp.

217{ return node_map.size(); }

Member Data Documentation

◆ _capacity

template<typename K , typename V >
int others::Cache::LFUCache< K, V >::_capacity
private

maximum capacity of the cache

Definition at line 71 of file lfu_cache.cpp.

◆ freq_map

template<typename K , typename V >
std::unordered_map<int, std::pair<CacheNode<K, V> *, CacheNode<K, V> *> > others::Cache::LFUCache< K, V >::freq_map
private

maps the frequency to doubly linked list

Definition at line 68 of file lfu_cache.cpp.

◆ minFreq

template<typename K , typename V >
int others::Cache::LFUCache< K, V >::minFreq
private

minimum frequency in the cache

Definition at line 70 of file lfu_cache.cpp.

◆ node_map

template<typename K , typename V >
std::unordered_map<K, std::pair<CacheNode<K, V> *, int> > others::Cache::LFUCache< K, V >::node_map
private

maps the key to the node address and frequency

Definition at line 66 of file lfu_cache.cpp.


The documentation for this class was generated from the following file: