Skip to content

Commit f59af67

Browse files
authored
Update LRU.java
Meet Gradle requirements
1 parent 73b815c commit f59af67

File tree

1 file changed

+51
-20
lines changed
  • src/main/java/dataStructures/lruCache

1 file changed

+51
-20
lines changed

src/main/java/dataStructures/lruCache/LRU.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,46 @@
22

33
import java.util.HashMap;
44

5-
public class LRU { // doublyLinkedList
6-
int key;
7-
int val;
8-
LRU next;
9-
LRU prev;
10-
}
5+
/**
6+
* Implementation of Least Recently Used (LRU) Cache
7+
*
8+
* Constructor:
9+
* LRU(int capacity)
10+
* Client methods:
11+
* get(K key)
12+
* put(K key, V value)
13+
* Both methods above run in O(1) average time complexity
14+
*/
15+
class LRU {
16+
/**
17+
* Helper node class that implements doubly linked list
18+
*/
19+
private class doublyLinkedList {
20+
private int key;
21+
private int val;
22+
private doublyLinkedList next;
23+
private doublyLinkedList prev;
24+
}
1125

12-
class LRUCache {
13-
LRU dllHead;
14-
LRU dllTail;
15-
HashMap<Integer, LRU> keyToNode = new HashMap();
16-
int capacity;
17-
int lengthOfList = 0;
26+
private doublyLinkedList dllHead;
27+
private doublyLinkedList dllTail;
28+
private HashMap<Integer, doublyLinkedList> keyToNode = new HashMap();
29+
private int capacity;
30+
private int lengthOfList = 0;
1831

19-
public LRUCache(int capacity) {
32+
/**
33+
* Constructs an instance of Least Recently Used Cache
34+
*
35+
* @param capacity the maximum capacity of the cache
36+
*/
37+
public LRU(int capacity) {
2038
this.capacity = capacity;
2139

22-
dllHead = new LRU();
40+
dllHead = new doublyLinkedList();
2341
dllHead.key = -1;
2442
dllHead.val = -1;
2543

26-
dllTail = new LRU();
44+
dllTail = new doublyLinkedList();
2745
dllTail.key = -1;
2846
dllTail.val = -1;
2947

@@ -33,10 +51,17 @@ public LRUCache(int capacity) {
3351
dllTail.next = null;
3452
}
3553

54+
/**
55+
* Return the value of the key if it exists or return null
56+
*
57+
* @param key key of the value to be obtained from LRU cache
58+
*/
3659
public int get(int key) {
37-
if(!keyToNode.containsKey(key)) return -1;
38-
39-
LRU temp = keyToNode.get(key);
60+
if(!keyToNode.containsKey(key)) {
61+
return -1;
62+
}
63+
64+
doublyLinkedList temp = keyToNode.get(key);
4065
temp.prev.next = temp.next;
4166
temp.next.prev = temp.prev;
4267

@@ -48,13 +73,19 @@ public int get(int key) {
4873
return keyToNode.get(key).val;
4974
}
5075

76+
/**
77+
* Insert key-value pair to LRU cache
78+
*
79+
* @param key key of the value to be inserted to LRU cache
80+
* @param value value to be inserted to LRU cache
81+
*/
5182
public void put(int key, int value) {
5283
boolean addingNewNode = true;
5384

54-
LRU newlyCached;
85+
doublyLinkedList newlyCached;
5586

5687
if(!keyToNode.containsKey(key)) {
57-
newlyCached = new LRU();
88+
newlyCached = new doublyLinkedList();
5889
newlyCached.key = key;
5990
newlyCached.val = value;
6091
keyToNode.put(key, newlyCached);

0 commit comments

Comments
 (0)