-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU cache.java
More file actions
174 lines (136 loc) · 4.24 KB
/
Copy pathLRU cache.java
File metadata and controls
174 lines (136 loc) · 4.24 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
/*
LRU Cache - LeetCode: https://leetcode.com/problems/lru-cache/
An adaption of the answer from user "liaison" on Leetcode.
Link: https://leetcode.com/problems/lru-cache/discuss/45911/Java-Hashtable-%2B-Double-linked-list-(with-a-touch-of-pseudo-nodes)
Runtime: 77 ms, faster than 95.85% of Java online submissions for LRU Cache.
The video to explain this code is here: https://www.youtube.com/watch?v=S6IfqDXWa10
*/
class LRUCache {
/*
Our internal definition of a doubly linked list
node, put in this class for convenience since
this is submitted in one file on Leetcode
*/
private class DNode {
int key;
int value;
DNode prev;
DNode next;
}
private Map<Integer, DNode> hashtable = new HashMap<Integer, DNode>();
private DNode head, tail;
private int totalItemsInCache;
private int maxCapacity;
public LRUCache(int maxCapacity) {
// Cache starts empty and capacity is set by client
totalItemsInCache = 0;
this.maxCapacity = maxCapacity;
// Initialize the dummy head of the cache
head = new DNode();
head.prev = null;
// Init the dummy tail of the cache
tail = new DNode();
tail.next = null;
// Wire the head and tail together
head.next = tail;
tail.prev = head;
}
/*
Retrieve an item from the cache
*/
public int get(int key) {
DNode node = hashtable.get(key);
boolean itemFoundInCache = node != null;
// Empty state check. Should raise exception here.
if(!itemFoundInCache){
return -1;
}
// Item has been accessed. Move to the front of the list.
moveToHead(node);
return node.value;
}
/*
Add an item to the cache if it is not already there,
if it is already there update the value and move it
to the front of the cache
*/
public void put(int key, int value) {
DNode node = hashtable.get(key);
boolean itemFoundInCache = node != null;
if(!itemFoundInCache){
// Create a new node
DNode newNode = new DNode();
newNode.key = key;
newNode.value = value;
// Add to the hashtable and the doubly linked list
hashtable.put(key, newNode);
addNode(newNode);
// We just added an item to the cache
totalItemsInCache++;
// If over capacity evict an item with LRU cache eviction policy
if(totalItemsInCache > maxCapacity){
removeLRUEntryFromStructure();
}
} else {
// If item is in cache just update and move it to the head
node.value = value;
moveToHead(node);
}
}
/*
Remove the least used entry from the doubly linked
list as well as the hashtable. Hence it is evicted
from the whole LRUCache structure
*/
private void removeLRUEntryFromStructure() {
DNode tail = popTail();
hashtable.remove(tail.key);
--totalItemsInCache;
}
/*
Insertions to the doubly linked list will always
be right after the dummy head
*/
private void addNode(DNode node){
// Wire the node being inserted
node.prev = head;
node.next = head.next;
// Re-wire the head's old next
head.next.prev = node;
// Re-wire the head to point to the inserted node
head.next = node;
}
/*
Remove the given node from the doubly linked list
*/
private void removeNode(DNode node){
// Grab reference to the prev and next of the node
DNode savedPrev = node.prev;
DNode savedNext = node.next;
// Cut out going forwards
savedPrev.next = savedNext;
// Cut out going backards
savedNext.prev = savedPrev;
}
/*
Move a node to the head of the doubly linked lsit
*/
private void moveToHead(DNode node){
removeNode(node);
addNode(node);
}
/*
Pop the last item from the structure
*/
private DNode popTail(){
DNode itemBeingRemoved = tail.prev;
removeNode(itemBeingRemoved);
return itemBeingRemoved;
}
}
/**
* The LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(maxCapacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/