-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
199 lines (161 loc) · 4.58 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* LFU cache implementation
* Items cached in object hash, frequency managed by doubly-linked list
* of frequency sets.
* Higher frequency sets are at the tail of the list, so we always
* clean up from the head.
*/
function LFUCache(maxSize) {
this._setup();
this.maxSize = maxSize;
}
LFUCache.prototype.put = function (key, value) {
var node = this.cache[key];
// If key already exists, just set its new value and get out.
if (node) {
node.value = value;
return;
}
if (this.cacheSize === this.maxSize) {
this._evict();
}
// If head frequency set is not 0 weight, we need to create
// a new one and make it head.
if (this.head.weight > 0) {
var oldHead = this.head;
this.head = new FrequencySet({
weight: 0,
next: oldHead
});
// Push the old head down the list.
oldHead.prev = this.head;
}
this.head.add(key);
this.cache[key] = {
value: value,
frequencySet: this.head
};
this.cacheSize = this.cacheSize + 1;
};
LFUCache.prototype.get = function (key) {
var node = this.cache[key];
if (!node) {
return null;
}
var currentFrequencySet = node.frequencySet;
var nextFrequencySet = currentFrequencySet.next;
// If there isn't a set for the next frequency create a new one.
if (!nextFrequencySet || nextFrequencySet.weight !== currentFrequencySet.weight + 1) {
nextFrequencySet = new FrequencySet({
weight: currentFrequencySet.weight + 1,
prev: currentFrequencySet,
next: currentFrequencySet.next
});
currentFrequencySet.next = nextFrequencySet;
}
// Now we know that a nextFrequencySet exists, remove the
// key from the current on and move it to the next.
currentFrequencySet.remove(key);
nextFrequencySet.add(key);
// Then tell the node its new frequency set.
node.frequencySet = nextFrequencySet;
if (currentFrequencySet.isEmpty()) {
this._removeFrequencySet(currentFrequencySet);
}
return node.value;
};
LFUCache.prototype.flush = function () {
this._setup();
};
LFUCache.prototype._setup = function () {
this.cache = Object.create(null);
this.head = new FrequencySet({
weight: 0
});
this.cacheSize = 0;
};
LFUCache.prototype._evict = function () {
var tailKey = this.head && this.head.getTail();
var node = this.cache[tailKey];
if (!node) {
return;
}
delete this.cache[tailKey];
node.frequencySet.remove(tailKey);
if (node.frequencySet.isEmpty()) {
this._removeFrequencySet(node.frequencySet);
}
this.cacheSize = this.cacheSize - 1;
};
LFUCache.prototype._removeFrequencySet = function (set) {
var nextSet = set.next;
if (set === this.head) {
nextSet.prev = null;
this.head = nextSet;
} else {
set.prev.next = set.next;
set.next.prev = set.prev;
}
};
function FrequencySet(opts) {
opts = opts || {};
this.weight = opts.weight || 0;
this.next = opts.next || null;
this.prev = opts.prev || null;
// Keep track of the set's item recency as a linked list.
this._items = {};
this._head = null;
this._tail = null;
// We keep track of length, for O(1) lookup. Avoid Object.keys().
this._length = 0;
}
FrequencySet.prototype.add = function (key) {
if (this._items[key]) {
return;
}
var node = {
next: this.head,
prev: null,
value: key
};
// If there is a head already it's now going to have a prev.
if (this.head) {
this.head.prev = node;
} else {
// If there's no head, there's no tail either.
this.tail = node;
}
// New node becomes head
this.head = node;
this._items[key] = node;
this._length = this._length + 1;
};
FrequencySet.prototype.remove = function (key) {
var node = this._items[key];
if (!node) {
return;
}
var prev = node.prev;
var next = node.next;
// Pull the node out of the list. prev -> next, next -> prev.
// If any is missing it must be head or tail node.
if (prev) {
prev.next = next;
} else {
this.head = next;
}
if (next) {
next.prev = prev;
} else {
this.tail = prev;
}
delete this._items[key];
this._length = this._length - 1;
};
FrequencySet.prototype.isEmpty = function () {
return this._length < 1;
};
FrequencySet.prototype.getTail = function () {
return this.tail && this.tail.value;
};
module.exports = LFUCache;