-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLRUcache.java
More file actions
181 lines (151 loc) · 4.47 KB
/
LRUcache.java
File metadata and controls
181 lines (151 loc) · 4.47 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
//need O(1) time for GET and PUT operations
//Use HashMap for O(1) lookup(GET)
//Use a DLL for O(1) removals/insertions(PUT) , also because WE NEED ORDERING , hashmap will have no ordering by itself!
class LRUCache {
private class ListNode { //definition of Node needed for this problem
int key;
int value;
ListNode prev;
ListNode next;
}
Map<Integer, ListNode> hm = new HashMap<>(); //give us O(1) access to get an item in dll
ListNode head;
ListNode tail;
int totalItemsInCache; //# of items currently in the cahce
int capacity; //items the cache can hold
public LRUCache(int capacity) {
// Cache starts empty and capacity is set by client
totalItemsInCache = 0;
this.capacity = capacity;
// Dummy head and tail nodes to avoid empty states
head = new ListNode();
tail = new ListNode();
// Wire the head and tail together
head.next = tail;
tail.prev = head;
}
public int get(int key) {
ListNode node = hm.get(key);
if(node == null)
{
return -1;
}
moveToHead(node); // item has been accessed, move to front
return node.value;
}
public void put(int key, int value) {
ListNode node = hm.get(key);
if(node == null)
{
ListNode newNode = new ListNode();
newNode.key = key;
newNode.value = value;
// Add to the hashtable and the actual list that represents the cache
hm.put(key, newNode);
addToFront(newNode);
totalItemsInCache++;
// If over capacity remove the LRU item
if (totalItemsInCache > capacity) {
removeLRUEntry();
}
}
else{
node.value=value;
moveToHead(node);
}
}
private void removeLRUEntry() {
ListNode tail = popTail();
hashtable.remove(tail.key);
--totalItemsInCache;
}
private ListNode popTail() {
ListNode tailItem = tail.prev;
removeFromList(tailItem);
return tailItem;
}
private void addToFront(ListNode node) {
// Wire up the new node being to be inserted
node.prev = head;
node.next = head.next;
/*
Re-wire the node after the head. Our node is still sitting "in the middle of nowhere".
We got the new node pointing to the right things, but we need to fix up the original
head & head's next.
head <-> head.next <-> head.next.next <-> head.next.next.next <-> ...
^ ^
|- new node -|
That's where we are before these next 2 lines.
*/
head.next.prev = node;
head.next = node;
}
//just helder method to remove a node from linkedlist
private void removeFromList(ListNode node) {
ListNode savedPrev = node.prev;
ListNode savedNext = node.next;
savedPrev.next = savedNext;
savedNext.prev = savedPrev;
}
private void moveToHead(ListNode node) {
removeFromList(node);
addToFront(node);
}
}
//CLEANER
class LRUCache {
class Node {
Node prev;
Node next;
int key;
int val;
public Node(int val, int key){
this.val = val;
this.key = key;
}
}
HashMap<Integer, Node> map;
Node head;
Node tail;
int capacity;
public LRUCache(int capacity) {
head = new Node(-1, -1);
tail = new Node(-1, -1);
head.next = tail;
tail.prev = head;
map = new HashMap<>();
this.capacity = capacity;
}
public int get(int key) {
if(map.containsKey(key)){
Node curr = map.get(key);
remove(curr);
insert(curr);
return curr.val;
} else {
return -1;
}
}
public void put(int key, int value) {
if(map.containsKey(key)){
remove(map.get(key));
}
if(map.size() == capacity){
remove(tail.prev);
}
insert(new Node(value, key));
}
public void remove(Node node){
map.remove(node.key);
node.prev.next = node.next;
node.next.prev = node.prev;
}
public void insert(Node node){
map.put(node.key, node);
Node next = head.next;
head.next = node;
node.prev = head;
next.prev = node;
node.next = next;
}
}