-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlfu.cpp
More file actions
169 lines (156 loc) · 3.92 KB
/
lfu.cpp
File metadata and controls
169 lines (156 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <vector>
#include <list>
#include <unordered_map>
#include <unordered_set>
using namespace std;
// LFU - Least Frequently Used
class FreqNode{
public:
int freq;
// We use a SET in lieu of a linked list for storing elements with the same access frequency
// for simplicitly of implementation.
// HASH SET structure which holds the keys of such elements that have the same access frequency.
// Its insertion, lookup and deletion runtime complexity is O(1).
unordered_set<int> items;
FreqNode(int f){
this->freq = f;
}
};
// LFU - Least Frequently Used Cache Algorithm
// 1 Linked List + 1 Set + 1 Hash
// O(1) for all: Insert, Delete and Lookup
class LFUCache{
public:
list<FreqNode*> freqNodes;
// key : item (key of element)
// value : point to its parent freqNode.
// Those nodes with the same access frequency share a single parent.
unordered_map<int, list<FreqNode*>::iterator> bykey;
FreqNode* createNode(int freq,
list<FreqNode*>::iterator next){
FreqNode* newNode = new FreqNode(freq);
if(newNode == NULL){
return NULL;
}
freqNodes.insert(next, newNode);
return newNode;
}
void deleteNode(list<FreqNode*>::iterator it){
freqNodes.erase(it);
}
public:
LFUCache(){
}
~LFUCache(){
for(auto it = freqNodes.begin(); it != freqNodes.end(); it++){
delete(*it);
}
}
// Access (fetch) an element from the LFU cache,
// simultaneously incrementing its usage count.
// Assuming the element(key) already exists beforehand.
// O(1)
void access(int key){
if(bykey.find(key) == bykey.end()){
// not exist
return ;
}
list<FreqNode*>::iterator nodeIt = bykey[key];
int freqNow =(*nodeIt)->freq;
list<FreqNode*>::iterator nextNodeIt = nodeIt;
nextNodeIt++;
if(nextNodeIt == freqNodes.end() || (*nextNodeIt)->freq != (freqNow + 1)){
createNode(freqNow+1, nextNodeIt);
}
nextNodeIt = nodeIt;
nextNodeIt++;
bykey[key] = nextNodeIt;
(*nextNodeIt)->items.insert(key);
(*nodeIt)->items.erase(key);
if((*nodeIt)->items.empty()){
deleteNode(nodeIt);
}
}
// Insert a new element.
// You must make sure no existing element(key) before calling this.
// O(1)
void insert(int key){
if(freqNodes.empty() || (*(freqNodes.begin()))->freq != 1){
FreqNode* newNode = new FreqNode(1);
freqNodes.insert(freqNodes.begin(), newNode);
}
auto nodeIt = freqNodes.begin();
(*nodeIt)->items.insert(key);
bykey[key] = nodeIt;
}
// Look up an item with key, return its usage count
// O(1)
int query(int key){
if(bykey.find(key) == bykey.end()){
// error out
return -1;
}
return (*bykey[key])->freq;
}
// Fetches an item with the least usage count (the least frequently used item)
// in the cache
// O(1)
int getLFUItem(){
if(freqNodes.empty()){
// error out
return -1;
}
return (*freqNodes.begin())->freq;
}
};
// TEST Program
// Test data from Paper
void generateTestData(LFUCache& lfu){
vector<int> keys = {'a', 'b', 'c', 'x', 'y', 'z'};
for(int i = 0; i < 6; i++){
lfu.insert(keys[i]);
}
lfu.access('a');
for(int i = 1; i < 3; i++){
lfu.access('z');
}
for(int i = 1; i < 5; i++){
lfu.access('b');
}
for(int i = 1; i < 9; i++){
lfu.access('c');
}
}
void printFreq(LFUCache& lfu){
for(auto it = lfu.freqNodes.begin();
it != lfu.freqNodes.end();
it++){
cout<<"Freq = "<<(*it)->freq << ": ";
for(auto iIt = (*it)->items.begin();
iIt != (*it)->items.end();
iIt++){
cout<<(char)*iIt<<" ";
}
cout<<endl;
}
}
int main(void){
LFUCache lfu;
generateTestData(lfu);
printFreq(lfu);
cout<<endl;
cout<<"Accessing z"<<endl;
lfu.access('z');
printFreq(lfu);
cout<<endl;
cout<<"Least Frequently Used : "<<lfu.getLFUItem()<<endl;
cout<<endl;
cout<<"Query c's Freq : "<<lfu.query('c')<<endl;
cout<<"Query z's Freq : "<<lfu.query('z')<<endl;
cout<<"Accessing z again"<<endl;
lfu.access('z');
cout<<"Query z's Freq again: "<<lfu.query('z')<<endl;
printFreq(lfu);
return 0;
}