-
Notifications
You must be signed in to change notification settings - Fork 4
/
LruCache.java
72 lines (63 loc) · 1.83 KB
/
LruCache.java
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
package com.dbc;
import java.util.HashMap;
import java.util.Map;
public class LruCache {
class DListNode {
public int key;
public int val;
public DListNode next;
public DListNode prev;
public DListNode(int key, int val) {
this.key = key;
this.val = val;
}
}
private final DListNode head = new DListNode(0, 0);
private final DListNode tail = new DListNode(0, 0);
private final int capacity;
private int size = 0;
private final Map<Integer, DListNode> map = new HashMap();
public LruCache(int capacity) {
this.capacity = capacity;
this.head.next = this.tail;
this.tail.prev = this.head;
}
public int get(int key) {
if (!this.map.containsKey(key)) {
return -1;
}
DListNode node = this.map.get(key);
removeNode(node);
addHead(node);
return node.val;
}
public void put(int key, int value) {
if (!this.map.containsKey(key)) {
DListNode node = new DListNode(key, value);
this.map.put(key, node);
addHead(node);
this.size++;
if (this.size > this.capacity) {
DListNode temp = this.tail.prev;
removeNode(temp);
this.map.remove(temp.key);
this.size--;
}
} else {
DListNode node = this.map.get(key);
node.val = value;
removeNode(node);
addHead(node);
}
}
private void removeNode(DListNode node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void addHead(DListNode node) {
node.next = this.head.next;
node.prev = this.head;
this.head.next.prev = node;
this.head.next = node;
}
}