* Evicts (removes) the node from cache linked list. * @param {LinkedListNode} node
(node)
| 116 | * @param {LinkedListNode} node |
| 117 | */ |
| 118 | evict(node) { |
| 119 | delete this.nodesMap[node.key]; |
| 120 | this.size -= 1; |
| 121 | |
| 122 | const prevNode = node.prev; |
| 123 | const nextNode = node.next; |
| 124 | |
| 125 | // If one and only node. |
| 126 | if (prevNode === this.head && nextNode === this.tail) { |
| 127 | this.head.next = null; |
| 128 | this.tail.prev = null; |
| 129 | this.size = 0; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // If this is a Head node. |
| 134 | if (prevNode === this.head) { |
| 135 | nextNode.prev = this.head; |
| 136 | this.head.next = nextNode; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // If this is a Tail node. |
| 141 | if (nextNode === this.tail) { |
| 142 | prevNode.next = this.tail; |
| 143 | this.tail.prev = prevNode; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // If the node is in the middle. |
| 148 | prevNode.next = nextNode; |
| 149 | nextNode.prev = prevNode; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | export default LRUCache; |