-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltimateLRU.java
More file actions
175 lines (143 loc) · 3.29 KB
/
UltimateLRU.java
File metadata and controls
175 lines (143 loc) · 3.29 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
//A fucking great LRU (Least Recently Used)
import java.util.HashMap;
public class UltimateLRU {
private DoubleLinkedList circularArrayQueue; // Contains the keys only
private HashMap<Integer, DoubleLinkedListNode> map = new HashMap<Integer, DoubleLinkedListNode>();
int capacity;
int size;
public UltimateLRU(int capacity) {
this.capacity = capacity;
circularArrayQueue = new DoubleLinkedList(capacity);
}
public void insert(int key, int value) {
DoubleLinkedListNode node = new DoubleLinkedListNode(key, value);
// In case we get the same key again, we keep the most recent value
if (map.containsKey(key)) {
map.get(key).value = value;
}
map.put(key, node);
DoubleLinkedListNode nodeToDelete = circularArrayQueue.insert(node);
if (nodeToDelete != null && map.containsKey(nodeToDelete.key)) {
map.remove(nodeToDelete.key);
}
}
public void print() {
circularArrayQueue.print();
}
public int getValue(int key) {
DoubleLinkedListNode node = map.get(key);
if (node == null) {
return Integer.MIN_VALUE;
}
circularArrayQueue.moveAtTheEnd(node);
return node.value;
}
public static void main(String[] args) {
UltimateLRU LRU = new UltimateLRU(3);
LRU.insert(1, 111);
LRU.print();
LRU.getValue(1);
LRU.print();
LRU.insert(2, 222);
LRU.print();
LRU.insert(3, 333);
LRU.print();
LRU.insert(4, 444);
LRU.print();
LRU.insert(5, 555);
LRU.print();
LRU.getValue(4);
LRU.print();
LRU.insert(6, 666);
LRU.print();
LRU.getValue(5);
LRU.print();
}
public class DoubleLinkedList {
// The item at root is the oldest one
int size;
int capacity;
DoubleLinkedListNode root;
DoubleLinkedListNode last;
DoubleLinkedList(int capacity) {
size = 0;
this.capacity = capacity;
root = null;
}
public DoubleLinkedListNode insert(DoubleLinkedListNode node) {
DoubleLinkedListNode nodeToDelete = null;
if (node == null || capacity <= 0) {
return null;
}
if (root == null) {
root = node;
last = node;
node.next = null;
node.previous = null;
size++;
return null;
}
if (size >= capacity) {
// delete first
nodeToDelete = root;
root = root.next;
root.previous = null;
}
// add last
node.previous = last;
last.next = node;
last = node;
size++;
return nodeToDelete;
}
public void print() {
DoubleLinkedListNode current = root;
while (current != null) {
System.out.print("[" + current.key + "," + current.value + "] ");
current = current.next;
}
System.out.println();
}
public void moveAtTheEnd(DoubleLinkedListNode node) {
if (node == null || size <= 1) {
return;
}
if (root == null) { // This should not happen
root = node;
last = node;
node.next = null;
node.previous = null;
return;
}
if (node.next != null) {
node.next.previous = node.previous;
}
if (node.previous != null) {
node.previous.next = node.next;
}
if (node == root) {
if (node.next != null) {
root = node.next;
}
}
if (node != last) {
last.next = node;
node.previous = last;
}
node.next = null;
last = node;
}
}
public class DoubleLinkedListNode {
int key;
int value;
DoubleLinkedListNode previous;
DoubleLinkedListNode next;
DoubleLinkedListNode(int key, int value){
this.key = key;
this.value=value;
this.previous = null;
this.next = null;
}
}
}