|
1 | 1 | package dataStructures.lruCache;
|
2 | 2 |
|
3 | 3 | import java.util.HashMap;
|
4 |
| -import java.util.Map; |
5 | 4 |
|
6 | 5 | /**
|
7 | 6 | * Implementation of Least Recently Used (LRU) Cache
|
|
13 | 12 | * Client methods:
|
14 | 13 | * get(K key)
|
15 | 14 | * put(K key, V value)
|
16 |
| - * Both methods above run in O(1) average time complexity |
| 15 | + * Both methods above run in expected O(1) time complexity |
17 | 16 | */
|
18 |
| -public class LRU<K, V> { |
19 |
| - private final int cap; |
20 |
| - private final Map<K, Node<K, V>> map; |
21 |
| - private final Node<K, V> left; // dummy left node to point to the left end |
22 |
| - private final Node<K, V> right; // dummy right node to point to the right end |
23 |
| - |
| 17 | +class LRU<K, V> { |
24 | 18 | /**
|
25 |
| - * Helper node class that encapsulates key-value pair and act as linked list to neighbour nodes. |
| 19 | + * Helper node class that implements doubly linked list |
26 | 20 | */
|
27 |
| - private class Node<K, V> { |
28 |
| - private final K key; |
| 21 | + private class doublyLinkedListNode<K, V> { |
| 22 | + private K key; |
29 | 23 | private V val;
|
30 |
| - private Node<K, V> next; |
31 |
| - private Node<K, V> prev; |
32 |
| - |
33 |
| - Node(K key, V value) { |
34 |
| - this.key = key; |
35 |
| - this.val = value; |
36 |
| - this.next = null; |
37 |
| - this.prev = null; |
38 |
| - } |
| 24 | + private doublyLinkedListNode<K, V> next; |
| 25 | + private doublyLinkedListNode<K, V> prev; |
39 | 26 | }
|
40 | 27 |
|
| 28 | + private doublyLinkedListNode<K, V> dllHead; |
| 29 | + private doublyLinkedListNode<K, V> dllTail; |
| 30 | + private HashMap<K, doublyLinkedListNode<K, V>> keyToNode = new HashMap<>(); |
| 31 | + private int capacity; |
| 32 | + private int lengthOfList = 0; |
| 33 | + |
41 | 34 | /**
|
42 |
| - * Constructs an instance of Least Recently Used Cache. |
| 35 | + * Constructs an instance of Least Recently Used Cache |
43 | 36 | *
|
44 |
| - * @param capacity the maximum capacity of the cache. |
| 37 | + * @param capacity the maximum capacity of the cache |
45 | 38 | */
|
46 | 39 | public LRU(int capacity) {
|
47 |
| - this.cap = capacity; |
48 |
| - this.map = new HashMap<>(); |
49 |
| - this.left = new Node<>(null, null); |
50 |
| - this.right = new Node<>(null, null); |
51 |
| - this.left.next = this.right; |
52 |
| - this.right.prev = this.left; |
53 |
| - } |
| 40 | + this.capacity = capacity; |
54 | 41 |
|
55 |
| - /** |
56 |
| - * Helper method to remove the specified node from the doubly linked list |
57 |
| - * |
58 |
| - * @param node to be removed from the linked list |
59 |
| - */ |
60 |
| - private void remove(Node<K, V> node) { |
61 |
| - Node<K, V> prev = node.prev; |
62 |
| - Node<K, V> nxt = node.next; |
63 |
| - prev.next = nxt; |
64 |
| - nxt.prev = prev; |
| 42 | + dllHead = new doublyLinkedListNode<>(); |
| 43 | + dllTail = new doublyLinkedListNode<>(); |
| 44 | + dllHead.next = dllTail; |
| 45 | + dllTail.prev = dllHead; |
65 | 46 | }
|
66 | 47 |
|
67 | 48 | /**
|
68 |
| - * Helper method to insert a node to the right end of the double linked list (Most Recently Used) |
| 49 | + * Return the value of the key if it exists or return null |
69 | 50 | *
|
70 |
| - * @param node to be inserted |
71 |
| - */ |
72 |
| - private void insert(Node<K, V> node) { |
73 |
| - Node<K, V> prev = this.right.prev; |
74 |
| - prev.next = node; |
75 |
| - node.prev = prev; |
76 |
| - node.next = this.right; |
77 |
| - this.right.prev = node; |
78 |
| - } |
79 |
| - |
80 |
| - /** |
81 |
| - * return the value of the key if it exists; otherwise null |
82 |
| - * |
83 |
| - * @param key whose value, if exists, to be obtained |
| 51 | + * @param key key of the value to be obtained from LRU cache |
84 | 52 | */
|
85 | 53 | public V get(K key) {
|
86 |
| - if (this.map.containsKey(key)) { |
87 |
| - Node<K, V> node = this.map.get(key); |
88 |
| - this.remove(node); |
89 |
| - this.insert(node); |
90 |
| - return node.val; |
| 54 | + if (!keyToNode.containsKey(key)) { |
| 55 | + return null; |
91 | 56 | }
|
92 |
| - return null; |
| 57 | + |
| 58 | + doublyLinkedListNode<K, V> temp = keyToNode.get(key); |
| 59 | + temp.prev.next = temp.next; |
| 60 | + temp.next.prev = temp.prev; |
| 61 | + |
| 62 | + temp.next = dllHead.next; |
| 63 | + dllHead.next.prev = temp; |
| 64 | + temp.prev = dllHead; |
| 65 | + dllHead.next = temp; |
| 66 | + |
| 67 | + return keyToNode.get(key).val; |
93 | 68 | }
|
94 | 69 |
|
95 | 70 | /**
|
96 |
| - * Update the value of the key if the key exists. |
97 |
| - * Otherwise, add the key-value pair to the cache. |
98 |
| - * If the number of keys exceeds the capacity from this operation, evict the least recently used key |
| 71 | + * Insert key-value pair to LRU cache |
99 | 72 | *
|
100 |
| - * @param key the key |
101 |
| - * @param val the associated value |
| 73 | + * @param key key of the value to be inserted to LRU cache |
| 74 | + * @param value value to be inserted to LRU cache |
102 | 75 | */
|
103 |
| - public void update(K key, V val) { |
104 |
| - if (this.map.containsKey(key)) { |
105 |
| - Node<K, V> node = this.map.get(key); |
106 |
| - this.remove(node); |
107 |
| - node.val = val; |
108 |
| - this.insert(node); // make most recently used |
| 76 | + public void put(K key, V value) { |
| 77 | + boolean addingNewNode = true; |
| 78 | + |
| 79 | + doublyLinkedListNode<K, V> newlyCached; |
| 80 | + |
| 81 | + if (!keyToNode.containsKey(key)) { |
| 82 | + newlyCached = new doublyLinkedListNode<>(); |
| 83 | + newlyCached.key = key; |
| 84 | + newlyCached.val = value; |
| 85 | + keyToNode.put(key, newlyCached); |
109 | 86 | } else {
|
110 |
| - Node<K, V> node = new Node<>(key, val); |
111 |
| - this.map.put(node.key, node); |
112 |
| - this.insert(node); |
113 |
| - } |
| 87 | + newlyCached = keyToNode.get(key); |
| 88 | + newlyCached.val = value; |
| 89 | + addingNewNode = false; |
114 | 90 |
|
115 |
| - if (this.map.size() > this.cap) { // evict LRU since capacity exceeded |
116 |
| - Node<K, V> toRemove = this.left.next; |
117 |
| - this.map.remove(toRemove.key); |
118 |
| - this.remove(toRemove); |
| 91 | + newlyCached.prev.next = newlyCached.next; |
| 92 | + newlyCached.next.prev = newlyCached.prev; |
119 | 93 | }
|
120 |
| - } |
121 | 94 |
|
122 |
| - /** |
123 |
| - * Custom print for testing |
124 |
| - * prints from LRU to MRU (Most recently used) |
125 |
| - */ |
126 |
| - public void print() { |
127 |
| - Node<K, V> trav = this.left.next; |
128 |
| - System.out.print("Dummy"); |
129 |
| - while (trav != this.right) { |
130 |
| - System.out.print(" ->"); |
131 |
| - System.out.print(trav.key); |
132 |
| - System.out.print(","); |
133 |
| - System.out.print(trav.val); |
134 |
| - trav = trav.next; |
| 95 | + newlyCached.next = dllHead.next; |
| 96 | + dllHead.next.prev = newlyCached; |
| 97 | + newlyCached.prev = dllHead; |
| 98 | + dllHead.next = newlyCached; |
| 99 | + |
| 100 | + if (addingNewNode) { |
| 101 | + if (lengthOfList == capacity) { |
| 102 | + keyToNode.remove(dllTail.prev.key); |
| 103 | + dllTail.prev.prev.next = dllTail; |
| 104 | + dllTail.prev = dllTail.prev.prev; |
| 105 | + } else { |
| 106 | + lengthOfList++; |
| 107 | + } |
135 | 108 | }
|
136 |
| - System.out.println(); |
137 | 109 | }
|
138 | 110 | }
|
0 commit comments