-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFUCache.java
More file actions
209 lines (185 loc) · 4.18 KB
/
LFUCache.java
File metadata and controls
209 lines (185 loc) · 4.18 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package code.lintcode.cache;
import java.util.HashMap;
import java.util.Map;
public class LFUCache {
private int capacity;
private Map<Integer, Node> cache;
private static NodeQueue head = null, tail = null;
static class NodeQueue {
Node head, tail;
NodeQueue next, pre;
public NodeQueue(NodeQueue next, NodeQueue pre, Node head, Node tail) {
this.next = next;
this.pre = pre;
this.head = head;
this.tail = tail;
}
public void addNode(Node node) {
node.currQueue = this;
if (this.head == null) {
this.tail = this.head = node;
return;
}
Node temp = this.head;
node.next = temp;
temp.pre = node;
this.head = node;
}
public static void removeNode(Node node) {
if (node == null)
return;
NodeQueue cq = node.currQueue;
node.currQueue = null;
// one node queue, remove queue
if (cq.head == cq.tail) {
if (cq.pre != null)
cq.pre.next = cq.next;
if (cq.next != null)
cq.next.pre = cq.pre;
if (LFUCache.head == cq)
LFUCache.head = cq.next;
if (LFUCache.tail == cq)
LFUCache.tail = cq.pre;
cq.next = cq.pre = null;
return;
}
if (cq.head == node)
cq.head = node.next;
else if (cq.tail == node)
cq.tail = node.pre;
else {
node.pre.next = node.next;
node.next.pre = node.pre;
}
node.pre = null;
node.next = null;
}
public static void pullUpNode(Node node) {
if (node == null)
return;
node.record.pullUp();
NodeQueue cq = node.currQueue;
if (cq == LFUCache.head)
return;
NodeQueue.removeNode(node);
NodeQueue pq = cq.pre;
if (pq == null) {
pq = new NodeQueue(cq, pq, null, null);
cq.pre = pq;
LFUCache.head = pq;
} else if (pq.head.record.freq != node.record.freq) {
NodeQueue temp = cq.pre;
pq = new NodeQueue(cq, pq, null, null);
cq.pre = pq;
temp.next = pq;
}
pq.addNode(node);
}
}
static class Node {
Node next, pre;
NodeQueue currQueue;
int key, val;
Record record;
public Node(int key, int val) {
this.key = key;
this.val = val;
this.record = new Record();
}
}
static class Record {
int freq;
long timestmp;
public Record() {
this.freq = 1;
this.timestmp = System.currentTimeMillis();
}
public void pullUp() {
this.freq++;
this.timestmp = System.currentTimeMillis();
}
}
/*
* @param capacity: An integer
*/
public LFUCache(int capacity) {
this.capacity = capacity;
this.cache = new HashMap<Integer, LFUCache.Node>(capacity);
}
/*
* @param key: An integer
*
* @param value: An integer
*
* @return: nothing
*/
public void set(int key, int value) {
if (this.capacity == 0)
return;
// if exists
Node node = this.getNode(key);
if (node != null) {
node.val = value;
return;
}
// remove least frequnce node
if (this.cache.size() == this.capacity) {
Node lfn = tail.tail;
NodeQueue.removeNode(lfn);
this.cache.remove(lfn.key);
}
node = new Node(key, value);
if (tail == null) {
head = tail = new NodeQueue(null, null, null, null);
}
if (tail.tail != null && tail.tail.record.freq != node.record.freq) {
NodeQueue temp = new NodeQueue(null, tail, node, node);
tail.next = temp;
tail = temp;
}
tail.addNode(node);
this.cache.put(key, node);
}
/*
* @param key: An integer
*
* @return: An integer
*/
public int get(int key) {
Node node;
return (node = getNode(key)) == null ? -1 : node.val;
}
private Node getNode(int key) {
Node node = this.cache.get(key);
if (node != null)
NodeQueue.pullUpNode(node);
return node;
}
public static void main(String[] args) {
LFUCache cache = new LFUCache(3);
/*
* cache.set(2, 2); cache.set(1, 1); cache.get(2); cache.get(1); cache.get(2);
* cache.set(3, 3); cache.set(4, 4); cache.get(3); cache.get(2); cache.get(1);
* cache.get(4);
*/
cache.set(1, 10);
cache.set(2, 20);
cache.set(3, 30);
cache.get(1);
cache.set(4, 40);
cache.get(4);
cache.get(3);
cache.get(2);
cache.get(1);
cache.set(5, 50);
cache.get(1);
cache.get(2);
cache.get(3);
cache.get(4);
cache.get(5);
/*
* LFUCache cache = new LFUCache(1); cache.set(2, 1); cache.get(2); cache.set(3,
* 2); cache.get(2); cache.get(3);
*/
}
}