3
3
* @brief Implementation for [LFU Cache]
4
4
* (https://en.wikipedia.org/wiki/Least_frequently_used)
5
5
*
6
+ * @details
6
7
* LFU discards the least frequently used value. if there are multiple items
7
8
* with the same minimum frequency then, the least recently used among them is
8
9
* discarded. Data structures used - doubly linked list and unordered_map(hash
28
29
* @brief Other algorithms
29
30
*/
30
31
namespace others {
32
+
33
+ /* *
34
+ * @namespace
35
+ * @brief Cache algorithm
36
+ */
31
37
namespace Cache {
32
38
39
+ /* *
40
+ * @class
41
+ * @brief Node for a doubly linked list with data, prev and next pointers
42
+ */
33
43
template <typename T>
34
44
class D_Node {
35
45
public:
@@ -43,15 +53,19 @@ class D_Node {
43
53
template <typename K, typename V>
44
54
using CacheNode = D_Node<std::pair<K, V>>;
45
55
56
+ /* *
57
+ * @class
58
+ * @brief LFUCache
59
+ */
46
60
template <typename K, typename V>
47
61
class LFUCache {
48
62
std::unordered_map<K, std::pair<CacheNode<K, V> *, int >>
49
- node_map; // maps the key to the node address and frequency
63
+ node_map; // /< maps the key to the node address and frequency
50
64
std::unordered_map<int , std::pair<CacheNode<K, V> *, CacheNode<K, V> *>>
51
- freq_map; // maps the frequency to doubly linked list
65
+ freq_map; // /< maps the frequency to doubly linked list
52
66
53
- int minFreq; // minimum frequency in the cache
54
- int _capacity; // maximum capacity of the cache
67
+ int minFreq; // /< minimum frequency in the cache
68
+ int _capacity; // /< maximum capacity of the cache
55
69
56
70
public:
57
71
/* *
@@ -281,7 +295,6 @@ void test2() {
281
295
* @brief test method with 2 tests
282
296
* @return void
283
297
*/
284
-
285
298
void test () {
286
299
test1 ();
287
300
test2 ();
@@ -291,8 +304,7 @@ void test() {
291
304
* @brief main function
292
305
* @return 0 on exit
293
306
*/
294
-
295
307
int main () {
296
308
test ();
297
309
return 0 ;
298
- }
310
+ }
0 commit comments