-
Notifications
You must be signed in to change notification settings - Fork 931
/
Copy pathlru-cache-3.js
51 lines (44 loc) · 1.27 KB
/
lru-cache-3.js
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
const DLinkedList = require('../linked-lists/linked-list');
/**
* Least Recently Used (LRU) cache.
* Map + Double LinkedList: O(1)
* @param {number} capacity - Number of items to hold.
*/
class LRUCache extends Map {
constructor(capacity) {
super(); // initialize map
this.capacity = capacity;
this.list = new DLinkedList();
}
get(key) {
if (!super.has(key)) { return -1; }
// console.log('get', {key});
const node = super.get(key);
this.moveToHead(key, node);
return node.value.value;
}
put(key, value) {
// console.log('put', {key, value});
let node;
if (super.has(key)) {
node = super.get(key);
node.value.value = value;
} else {
node = this.list.addLast({key, value});
}
this.moveToHead(key, node);
if (this.list.size > this.capacity) {
const firstNode = this.list.removeFirst();
super.delete(firstNode.key);
}
}
moveToHead(key, node) {
// remove node and put it in front
this.list.removeByNode(node);
const newNode = this.list.addLast(node.value);
super.set(key, newNode);
// console.log('\tlist', Array.from(this.list).map(l => l.node.value));
// console.log('\tlist', Array.from(this.list).map(l => l.node.value.key));
}
}
module.exports = LRUCache;