21
21
*/
22
22
23
23
#include < cassert> // for assert
24
- #include < iostream> // for IO operations
25
- #include < unordered_map> // for hash map
24
+ #include < iostream> // for std::cout
25
+ #include < unordered_map> // for std::unordered_map
26
26
27
27
/* *
28
28
* @namespace
@@ -39,13 +39,14 @@ namespace Cache {
39
39
/* *
40
40
* @class
41
41
* @brief Node for a doubly linked list with data, prev and next pointers
42
+ * @tparam T type of the data of the node
42
43
*/
43
44
template <typename T>
44
45
class D_Node {
45
46
public:
46
- T data;
47
- D_Node<T> *prev;
48
- D_Node<T> *next;
47
+ T data; // /< data of the node
48
+ D_Node<T> *prev; // /< previous node in the doubly linked list
49
+ D_Node<T> *next; // /< next node in the doubly linked list
49
50
50
51
explicit D_Node (T data) : data(data), prev(nullptr ), next(nullptr ) {}
51
52
};
@@ -56,6 +57,8 @@ using CacheNode = D_Node<std::pair<K, V>>;
56
57
/* *
57
58
* @class
58
59
* @brief LFUCache
60
+ * @tparam K type of key in the LFU
61
+ * @tparam V type of value in the LFU
59
62
*/
60
63
template <typename K, typename V>
61
64
class LFUCache {
@@ -103,7 +106,7 @@ class LFUCache {
103
106
* @brief increase the frequency of node and push it in the respective list.
104
107
* @param p_node the node to be updated
105
108
*/
106
- void inc_freq (std::pair<CacheNode<K, V> *, int > &p_node) {
109
+ void increase_frequency (std::pair<CacheNode<K, V> *, int > &p_node) {
107
110
CacheNode<K, V> *node = p_node.first ;
108
111
int freq = p_node.second ;
109
112
@@ -172,7 +175,7 @@ class LFUCache {
172
175
// update the value if key already exists
173
176
if (node_map.count (key)) {
174
177
node_map[key].first ->data .second = value;
175
- inc_freq (node_map[key]);
178
+ increase_frequency (node_map[key]);
176
179
return ;
177
180
}
178
181
@@ -203,7 +206,7 @@ class LFUCache {
203
206
204
207
// increase the frequency and return the value
205
208
V value = node_map[key].first ->data .second ;
206
- inc_freq (node_map[key]);
209
+ increase_frequency (node_map[key]);
207
210
return value;
208
211
}
209
212
@@ -292,7 +295,7 @@ void test2() {
292
295
}
293
296
294
297
/* *
295
- * @brief test method with 2 tests
298
+ * @brief self test implementation
296
299
* @return void
297
300
*/
298
301
void test () {
@@ -305,6 +308,6 @@ void test() {
305
308
* @return 0 on exit
306
309
*/
307
310
int main () {
308
- test ();
311
+ test (); // run the self test implementation
309
312
return 0 ;
310
313
}
0 commit comments