-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFUCache.cpp
108 lines (97 loc) · 2.2 KB
/
LFUCache.cpp
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
#include <bits/stdc++.h>
struct Node {
int key;
int val;
int cnt;
Node *prev, *next;
Node(int k, int v) {
key = k;
val = v;
cnt = 1;
}
};
struct List {
Node *head, *tail;
int size;
List() {
head = new Node(0, 0);
tail = new Node(0, 0);
head->next = tail;
tail->prev = head;
size = 0;
}
void addFront(Node* node) {
node->next = head->next;
node->prev = head;
head->next = node;
node->next->prev = node;
size++;
}
void removeNode(Node* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
node->next = node->prev = NULL;
size--;
}
};
class LFUCache
{
unordered_map<int, Node*> keyNode;
unordered_map<int, List*> freqList;
int maxSize;
int curSize;
int minFreq;
public:
LFUCache(int capacity)
{
maxSize = capacity;
curSize = 0;
minFreq = 0;
}
void updateFreq(Node* t) {
freqList[t->cnt]->removeNode(t);
if(freqList[t->cnt] == 0 && t->cnt == minFreq) {
minFreq++;
}
t->cnt+=1;
if(freqList.count(t->cnt) == 0) {
freqList[t->cnt] = new List();
}
freqList[t->cnt]->addFront(t);
}
int get(int key)
{
if(!keyNode.count(key)) {
return -1;
}
Node* t = keyNode[key];
int val = t->val;
updateFreq(t);
return val;
}
void put(int key, int value)
{
if(maxSize == 0)
return;
if(keyNode.count(key)) {
Node *t = keyNode[key];
t->val = value;
updateFreq(t);
}else {
if(curSize == maxSize) {
List *l = freqList[minFreq];
keyNode.erase(l->tail->prev->key);
l->removeNode(l->tail->prev);
curSize--;
}
curSize++;
minFreq = 1;
if(freqList.count(minFreq) == 0) {
freqList[minFreq] = new List();
}
Node* nn = new Node(key, value);
freqList[minFreq]->addFront(nn);
keyNode[key] = nn;
}
}
};