-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLFU.js
110 lines (103 loc) · 2.6 KB
/
LFU.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
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
class LFUNode {
constructor(key, value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
this.frequency = 1;
}
}
class LFUDoublyLinkedList {
constructor() {
this.head = new LFUNode("BUFFER HEAD", null);
this.tail = new LFUNode("BUFFER TAIL", null);
this.head.next = this.tail;
this.tail.prev = this.head;
this.size = 0;
}
insertAtHead(node) {
node.next = this.head.next;
this.head.next.prev = node;
this.head.next = node;
node.prev = this.head;
this.size++;
}
removeAtTail() {
const nodeToBeRemoved = this.tail.prev;
this.tail.prev = nodeToBeRemoved.prev;
nodeToBeRemoved.prev.next = this.tail;
this.size--;
return nodeToBeRemoved;
}
removeNode(node) {
node.prev.next = node.next;
node.next.prev = node.prev;
this.size--;
}
}
class LFUCache {
constructor(capacity) {
this.keys = {};
this.freq = {};
this.capacity = capacity;
this.minFreq = 0;
this.size = 0;
}
set(key, value) {
let node = this.keys[key];
if (!node) {
node = new LFUNode(key, value);
this.keys[key] = node;
if (this.size !== this.capacity) {
if (!this.freq[1]) {
this.freq[1] = new LFUDoublyLinkedList();
}
this.freq[1].insertAtHead(node);
this.size++;
} else {
const oldTail = this.freq[this.minFreq].removeAtTail();
delete this.keys[oldTail.key];
if (!this.freq[1]) {
this.freq[1] = new LFUDoublyLinkedList();
}
this.freq[1].insertAtHead(node);
}
this.minFreq = 1;
} else {
const oldFreqCount = node.frequency;
node.data = value;
node.frequency++;
this.freq[oldFreqCount].removeNode(node);
if (!this.freq[node.frequency]) {
this.freq[node.frequency] = new LFUDoublyLinkedList();
}
this.freq[node.frequency].insertAtHead(node);
if (
oldFreqCount === this.minFreq &&
Object.keys(this.freq[oldFreqCount]).length === 0
) {
this.minFreq++;
}
}
}
get(key) {
let node = this.keys[key];
if (node) {
const oldFreqCount = node.frequency;
node.frequency++;
this.freq[oldFreqCount].removeNode(node);
if (!this.freq[node.frequency]) {
this.freq[node.frequency] = new LFUDoublyLinkedList();
}
this.freq[node.frequency].insertAtHead(node);
if (
oldFreqCount === this.minFreq &&
Object.keys(this.freq[oldFreqCount]).length === 0
) {
this.minFreq++;
}
return node.value;
}
return null;
}
}