Skip to content

Commit 78fd9b9

Browse files
committed
implement LRUCache
1 parent caf0e23 commit 78fd9b9

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

‎src/data_structure/LRUCache.java‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package data_structure;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class LRUCache {
7+
8+
static class Node {
9+
int key;
10+
int value;
11+
Node next;
12+
Node prev;
13+
14+
public Node(int key, int value) {
15+
this.key = key;
16+
this.value = value;
17+
}
18+
}
19+
20+
private final Map<Integer, Node> nodeMap;
21+
private final Node head;
22+
private final Node tail;
23+
private final int capacity;
24+
25+
public LRUCache(int capacity) {
26+
this.nodeMap = new HashMap<>();
27+
this.capacity = capacity;
28+
this.head = new Node(-1, -1);
29+
this.tail = new Node(-1, -1);
30+
head.next = tail;
31+
tail.prev = head;
32+
}
33+
34+
/*
35+
* insert
36+
* */
37+
public void add(int key, int value) {
38+
if (nodeMap.containsKey(key)) {
39+
Node node = nodeMap.get(key);
40+
node.value = value;
41+
moveToFront(node);
42+
} else {
43+
Node node = new Node(key, value);
44+
insertToFront(node);
45+
nodeMap.put(node.key, node);
46+
47+
if (capacity < nodeMap.size()) {
48+
removeLRU();
49+
}
50+
}
51+
}
52+
53+
private void moveToFront(Node node) {
54+
cutChain(node);
55+
insertToFront(node);
56+
}
57+
58+
private void insertToFront(Node node) {
59+
Node next = head.next;
60+
next.prev = node;
61+
head.next = node;
62+
}
63+
64+
private void cutChain(Node node) {
65+
Node prev = node.prev;
66+
Node next = node.next;
67+
prev.next = next;
68+
next.prev = prev;
69+
}
70+
71+
private void removeLRU() {
72+
Node last = tail.prev;
73+
Node prev = last.prev;
74+
prev.next = tail;
75+
tail.prev = prev;
76+
nodeMap.remove(last.key);
77+
}
78+
79+
/*
80+
* get
81+
* */
82+
public int get(int key) {
83+
if (!nodeMap.containsKey(key)) {
84+
return -1;
85+
}
86+
Node node = nodeMap.get(key);
87+
moveToFront(node);
88+
return node.value;
89+
}
90+
91+
/*
92+
* delete
93+
* */
94+
public void evict(int key) {
95+
if (!nodeMap.containsKey(key)) {
96+
return;
97+
}
98+
Node node = nodeMap.get(key);
99+
cutChain(node);
100+
nodeMap.remove(key);
101+
}
102+
103+
/*
104+
* for test
105+
* */
106+
public void print() {
107+
System.out.println("size : " + nodeMap.size());
108+
for (Map.Entry<Integer, Node> node : nodeMap.entrySet()) {
109+
System.out.println("key : %s, value : %s".formatted(node.getKey(), node.getValue().value));
110+
}
111+
}
112+
113+
public static void main(String[] args) {
114+
LRUCache lruCache = new LRUCache(3);
115+
lruCache.add(1, 10);
116+
lruCache.add(2, 11);
117+
lruCache.add(3, 8);
118+
lruCache.print();
119+
120+
lruCache.add(5, 11);
121+
lruCache.print();
122+
123+
lruCache.add(2, 14);
124+
lruCache.print();
125+
126+
lruCache.get(3);
127+
lruCache.add(7, 99);
128+
lruCache.print();
129+
}
130+
}

0 commit comments

Comments
 (0)