-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathlfu-set.js
247 lines (205 loc) · 7.28 KB
/
lfu-set.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"use strict";
// Based on http://dhruvbird.com/lfu.pdf
var Shim = require("./shim");
var Set = require("./set").CollectionsSet;
var GenericCollection = require("./generic-collection");
var GenericSet = require("./generic-set");
var PropertyChanges = require("./listen/property-changes");
var RangeChanges = require("./listen/range-changes");
module.exports = LfuSet;
function LfuSet(values, capacity, equals, hash, getDefault) {
if (!(this instanceof LfuSet)) {
return new LfuSet(values, capacity, equals, hash, getDefault);
}
capacity = capacity || Infinity;
equals = equals || Object.equals;
hash = hash || Object.hash;
getDefault = getDefault || Function.noop;
// TODO
this.store = new Set(
undefined,
function valueEqual(a, b) {
return equals(a.value, b.value);
},
function valueHash(node) {
return hash(node.value);
}
);
this.frequencyHead = new this.FrequencyNode(0);
this.contentEquals = equals;
this.contentHash = hash;
this.getDefault = getDefault;
this.capacity = capacity;
this.length = 0;
this.addEach(values);
}
LfuSet.LfuSet = LfuSet; // hack so require("lfu-set").LfuSet will work in MontageJS
Object.addEach(LfuSet.prototype, GenericCollection.prototype);
Object.addEach(LfuSet.prototype, GenericSet.prototype);
Object.addEach(LfuSet.prototype, PropertyChanges.prototype);
Object.addEach(LfuSet.prototype, RangeChanges.prototype);
Object.defineProperty(LfuSet.prototype,"size",GenericCollection._sizePropertyDescriptor);
LfuSet.from = GenericCollection.from;
LfuSet.prototype.constructClone = function (values) {
return new this.constructor(
values,
this.capacity,
this.contentEquals,
this.contentHash,
this.getDefault
);
};
LfuSet.prototype.has = function (value) {
return this.store.has(new this.Node(value));
};
LfuSet.prototype.get = function (value, equals) {
if (equals) {
throw new Error("LfuSet#get does not support second argument: equals");
}
var node = this.store.get(new this.Node(value));
if (node !== undefined) {
var frequencyNode = node.frequencyNode;
var nextFrequencyNode = frequencyNode.next;
if (nextFrequencyNode.frequency !== frequencyNode.frequency + 1) {
nextFrequencyNode = new this.FrequencyNode(frequencyNode.frequency + 1, frequencyNode, nextFrequencyNode);
}
nextFrequencyNode.values.add(node);
node.frequencyNode = nextFrequencyNode;
frequencyNode.values["delete"](node);
if (frequencyNode.values.length === 0) {
frequencyNode.prev.next = frequencyNode.next;
frequencyNode.next.prev = frequencyNode.prev;
}
return node.value;
} else {
return this.getDefault(value);
}
};
LfuSet.prototype.add = function (value) {
// if the value already exists, get it so that its frequency increases
if (this.has(value)) {
this.get(value);
return false;
}
var plus = [], minus = [], leastFrequentNode, leastFrequent;
if (this.capacity > 0) {
plus.push(value);
if (this.length + 1 > this.capacity) {
leastFrequentNode = this.frequencyHead.next;
leastFrequent = leastFrequentNode.values.order.head.next.value;
minus.push(leastFrequent.value);
}
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange(plus, minus, 0);
}
// removal must happen before addition, otherwise we could remove
// the value we are about to add
if (minus.length > 0) {
this.store["delete"](leastFrequent);
leastFrequentNode.values["delete"](leastFrequent);
// Don't remove the frequencyNode with value of 1, because we
// are about to use it again in the addition.
if (leastFrequentNode.value !== 1 && leastFrequentNode.values.length === 0) {
this.frequencyHead.next = leastFrequentNode.next;
leastFrequentNode.next.prev = this.frequencyHead;
}
}
var node = new this.Node(value);
var frequencyNode = this.frequencyHead.next;
if (frequencyNode.frequency !== 1) {
frequencyNode = new this.FrequencyNode(1, this.frequencyHead, frequencyNode);
}
this.store.add(node);
frequencyNode.values.add(node);
node.frequencyNode = frequencyNode;
this.length = this.length + plus.length - minus.length;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(plus, minus, 0);
}
}
// whether it grew
return plus.length !== minus.length;
};
LfuSet.prototype["delete"] = function (value, equals) {
if (equals) {
throw new Error("LfuSet#delete does not support second argument: equals");
}
var node = this.store.get(new this.Node(value));
var found = !!node;
if (found) {
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange([], [value], 0);
}
var frequencyNode = node.frequencyNode;
this.store["delete"](node);
frequencyNode.values["delete"](node);
if (frequencyNode.values.length === 0) {
frequencyNode.prev.next = frequencyNode.next;
frequencyNode.next.prev = frequencyNode.prev;
}
this.length--;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange([], [value], 0);
}
}
return found;
};
LfuSet.prototype.one = function () {
if (this.length > 0) {
return this.frequencyHead.next.values.one().value;
}
};
LfuSet.prototype.clear = function () {
this.store.clear();
this.frequencyHead.next = this.frequencyHead;
this.length = 0;
};
LfuSet.prototype.reduce = function (callback, basis /*, thisp*/) {
var thisp = arguments[2];
var index = 0;
var frequencyNode = this.frequencyHead.next;
while (frequencyNode.frequency !== 0) {
var set = frequencyNode.values;
basis = set.reduce(function (basis, node) {
return callback.call(thisp, basis, node.value, index++, this);
}, basis, this);
frequencyNode = frequencyNode.next;
}
return basis;
};
LfuSet.prototype.reduceRight = function (callback, basis /*, thisp*/) {
var thisp = arguments[2];
var index = this.length - 1;
var frequencyNode = this.frequencyHead.prev;
while (frequencyNode.frequency !== 0) {
var set = frequencyNode.values;
basis = set.reduceRight(function (basis, node) {
return callback.call(thisp, basis, node.value, index--, this);
}, basis, this);
frequencyNode = frequencyNode.prev;
}
return basis;
};
LfuSet.prototype.iterate = function () {
return this.store.map(function (node) {
return node.value;
}).iterate();
};
LfuSet.prototype.Node = Node;
function Node(value, frequencyNode) {
this.value = value;
this.frequencyNode = frequencyNode;
}
LfuSet.prototype.FrequencyNode = FrequencyNode;
function FrequencyNode(frequency, prev, next) {
this.frequency = frequency;
this.values = new Set();
this.prev = prev || this;
this.next = next || this;
if (prev) {
prev.next = this;
}
if (next) {
next.prev = this;
}
}