-
Notifications
You must be signed in to change notification settings - Fork 33
/
Problem_25.java
304 lines (275 loc) · 6.09 KB
/
Problem_25.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Cracking-The-Coding-Interview
* Problem_25.java
*/
package com.deepak.ctci.Ch16_Moderate;
import java.util.HashMap;
/**
* <br> Problem Statement :
*
* Design a LRU Cache
*
* </br>
*
* @author Deepak
*/
public class Problem_25 {
/**
* Class for LRU Cache
*
* @author Deepak
*
* @param <K>
* @param <V>
*/
public static class LRUCache<K, V> {
/**
* HashMap for cache
*/
private HashMap<K, Node<K, V>> cache = new HashMap<>();
/**
* Nodes for head and tail
*/
private Node<K, V> head, tail;
/**
* Capacity of cache
*/
private int capacity;
/**
* Default capacity of cache
*/
private static final int DEFAULT_CAPACITY = 100;
/**
* Default Constructor
*/
public LRUCache() {
setCapacity(DEFAULT_CAPACITY);
}
/**
* Parameterized Constructor
*
* @param capacity
*/
public LRUCache(int capacity) {
setCapacity(capacity);
}
/**
* Method to get the value associated
* with key from cache
*
* @param key
* @return {@link V}
*/
public V get(K key) {
/* If cache does not contains key, return null */
if (!cache.containsKey(key)) {
return null;
}
/* If key exists, get the key and return the value */
Node<K, V> node = cache.get(key);
/* Since this node is least recently used now, move it to the end.
* When eviction will happen, this will be the first entry to be removed.
* Removal will happen at head. */
moveNodeToLast(node);
return node.getValue();
}
/**
* Method to add the key value pair to cache
*
* @param key
* @param value
*/
public void put(K key, V value) {
/* If cache contains the key, find that key value pair,
* Since this is a map and only key is same. Value can be different.
* Replace this current value and push this node to end as we have
* accessed it already */
if (cache.containsKey(key)) {
Node<K, V> existing = cache.get(key);
existing.setValue(value);
moveNodeToLast(existing);
return;
}
/* If cache has reached the capacity,
* evict the LRU node and remove it from cache */
Node<K, V> newNode;
if (cache.size() == capacity) {
newNode = evict();
cache.remove(newNode.getKey());
} else {
newNode = new Node<>();
}
/* Create this new node, set key and values.
* Add this node to appropriate place and put in cache */
newNode.setKey(key);
newNode.setValue(value);
addNewNode(newNode);
cache.put(key, newNode);
}
/**
* Method to set capacity for cache
*
* @param capacity
*/
public void setCapacity(int capacity) {
/* Check if capacity is valid */
checkCapacity(capacity);
/* Now, start from the end and begin evicting
* until we meet the new capacity. Remove
* entries from cache as well */
for (int i = cache.size(); i > capacity; i--) {
Node<K, V> evicted = evict();
cache.remove(evicted.getKey());
}
this.capacity = capacity;
}
/**
* Method to check the size of cache
*
* @return {@link int}
*/
public int size() {
return cache.size();
}
/**
* Method to get the capacity of cache
*
* @return {@link int}
*/
public int getCapacity() {
return capacity;
}
/**
* Method to add a new node
*
* @param node
*/
private void addNewNode(Node<K, V> node) {
/* If cache is empty, both head and tail
* points to same new node */
if (cache.isEmpty()) {
head = tail = node;
return;
}
/* Else, append the new node to tail and
* update the position of tail */
tail.setNext(node);
node.setPrevious(tail);
node.setNext(null);
tail = node;
}
/**
* Method to evict the entry from cache
*
* @return {@link Node<K, V>}
*/
private Node<K, V> evict() {
/* If cache is empty, nothing to evict */
if (head == null) {
throw new AssertionError("Cannot evict from an empty cache!!");
}
/* Evict the head, update next and evicted node */
Node<K, V> evicted = head;
head = evicted.getNext();
head.setPrevious(null);
evicted.setNext(null);
return evicted;
}
/**
* Method to check capacity
*
* @param capacity
*/
private void checkCapacity(int capacity) {
/* Invalid capacity if less then 0 */
if (capacity <= 0) {
throw new IllegalArgumentException("Invalid Capacity. Should be positive!!");
}
}
/**
* Method to move node to last
*
* @param node
*/
private void moveNodeToLast(Node<K, V> node) {
/* If node is already at last, nothing to process */
if (tail == node) {
return;
}
/* Update previous and next nodes */
Node<K, V> previous = node.getPrevious();
Node<K, V> next = node.getNext();
if (previous != null) {
previous.setNext(next);
}
if (next != null) {
next.setPrevious(previous);
}
/* Update head and tail position */
if (head == node) {
head = next;
}
tail.setNext(node);
node.setPrevious(tail);
node.setNext(null);
tail = node;
}
/**
* Class Node for LRU cache
*
* @author Deepak
*
* @param <T>
* @param <U>
*/
public class Node<T, U> {
/* Variables to store key, value and pointers to next and previous nodes */
private Node<T, U> previous;
private Node<T, U> next;
private T key;
private U value;
/**
* Default Constructor
*/
public Node() {}
/**
* Parameterized Constructor
*
* @param previous
* @param next
* @param key
* @param value
*/
public Node(Node<T, U> previous, Node<T, U> next, T key, U value) {
this.previous = previous;
this.next = next;
this.key = key;
this.value = value;
}
public Node<T, U> getPrevious() {
return previous;
}
public void setPrevious(Node<T, U> previous) {
this.previous = previous;
}
public Node<T, U> getNext() {
return next;
}
public void setNext(Node<T, U> next) {
this.next = next;
}
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
public U getValue() {
return value;
}
public void setValue(U value) {
this.value = value;
}
}
}
}