Skip to content

Commit 0333f19

Browse files
committed
docs: document template params and class data members
1 parent e39f515 commit 0333f19

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

others/lfu_cache.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*/
2222

2323
#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
2626

2727
/**
2828
* @namespace
@@ -39,13 +39,14 @@ namespace Cache {
3939
/**
4040
* @class
4141
* @brief Node for a doubly linked list with data, prev and next pointers
42+
* @tparam T type of the data of the node
4243
*/
4344
template <typename T>
4445
class D_Node {
4546
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
4950

5051
explicit D_Node(T data) : data(data), prev(nullptr), next(nullptr) {}
5152
};
@@ -56,6 +57,8 @@ using CacheNode = D_Node<std::pair<K, V>>;
5657
/**
5758
* @class
5859
* @brief LFUCache
60+
* @tparam K type of key in the LFU
61+
* @tparam V type of value in the LFU
5962
*/
6063
template <typename K, typename V>
6164
class LFUCache {
@@ -103,7 +106,7 @@ class LFUCache {
103106
* @brief increase the frequency of node and push it in the respective list.
104107
* @param p_node the node to be updated
105108
*/
106-
void inc_freq(std::pair<CacheNode<K, V> *, int> &p_node) {
109+
void increase_frequency(std::pair<CacheNode<K, V> *, int> &p_node) {
107110
CacheNode<K, V> *node = p_node.first;
108111
int freq = p_node.second;
109112

@@ -172,7 +175,7 @@ class LFUCache {
172175
// update the value if key already exists
173176
if (node_map.count(key)) {
174177
node_map[key].first->data.second = value;
175-
inc_freq(node_map[key]);
178+
increase_frequency(node_map[key]);
176179
return;
177180
}
178181

@@ -203,7 +206,7 @@ class LFUCache {
203206

204207
// increase the frequency and return the value
205208
V value = node_map[key].first->data.second;
206-
inc_freq(node_map[key]);
209+
increase_frequency(node_map[key]);
207210
return value;
208211
}
209212

@@ -292,7 +295,7 @@ void test2() {
292295
}
293296

294297
/**
295-
* @brief test method with 2 tests
298+
* @brief self test implementation
296299
* @return void
297300
*/
298301
void test() {
@@ -305,6 +308,6 @@ void test() {
305308
* @return 0 on exit
306309
*/
307310
int main() {
308-
test();
311+
test(); // run the self test implementation
309312
return 0;
310313
}

0 commit comments

Comments
 (0)