-
Notifications
You must be signed in to change notification settings - Fork 184
/
lru-set.js
149 lines (134 loc) · 4.56 KB
/
lru-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
"use strict";
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 = LruSet;
function LruSet(values, capacity, equals, hash, getDefault) {
if (!(this instanceof LruSet)) {
return new LruSet(values, capacity, equals, hash, getDefault);
}
capacity = capacity || Infinity;
equals = equals || Object.equals;
hash = hash || Object.hash;
getDefault = getDefault || Function.noop;
this.store = new Set(undefined, equals, hash);
this.contentEquals = equals;
this.contentHash = hash;
this.getDefault = getDefault;
this.capacity = capacity;
this.length = 0;
this.addEach(values);
}
LruSet.LruSet = LruSet; // hack so require("lru-set").LruSet will work in MontageJS
Object.addEach(LruSet.prototype, GenericCollection.prototype);
Object.addEach(LruSet.prototype, GenericSet.prototype);
Object.addEach(LruSet.prototype, PropertyChanges.prototype);
Object.addEach(LruSet.prototype, RangeChanges.prototype);
Object.defineProperty(LruSet.prototype,"size",GenericCollection._sizePropertyDescriptor);
LruSet.from = GenericCollection.from;
LruSet.prototype.constructClone = function (values) {
return new this.constructor(
values,
this.capacity,
this.contentEquals,
this.contentHash,
this.getDefault
);
};
LruSet.prototype.has = function (value) {
return this.store.has(value);
};
LruSet.prototype.get = function (value, equals) {
if (equals) {
throw new Error("LruSet#get does not support second argument: equals");
}
value = this.store.get(value);
if (value !== undefined) {
this.store["delete"](value);
this.store.add(value);
} else {
value = this.getDefault(value);
}
return value;
};
LruSet.prototype.add = function (value) {
var found = this.store.has(value);
var plus = [], minus = [], eldest;
// if the value already exists, we delete it and add it back again so it
// appears at the end of the list of values to truncate
if (found) { // update
this.store["delete"](value);
this.store.add(value);
} else if (this.capacity > 0) { // add
// because minus is constructed before adding value, we must ensure the
// set has positive length. hence the capacity check.
plus.push(value);
if (this.length >= this.capacity) {
eldest = this.store.order.head.next;
minus.push(eldest.value);
}
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange(plus, minus, 0);
}
this.store.add(value);
if (minus.length > 0) {
this.store['delete'](eldest.value);
}
// only assign to length once to avoid jitter on length observers
this.length = this.length + plus.length - minus.length;
// after change
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(plus, minus, 0);
}
}
// whether it grew
return plus.length !== minus.length;
};
LruSet.prototype["delete"] = function (value, equals) {
if (equals) {
throw new Error("LruSet#delete does not support second argument: equals");
}
var found = this.store.has(value);
if (found) {
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange([], [value], 0);
}
this.store["delete"](value);
this.length--;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange([], [value], 0);
}
}
return found;
};
LruSet.prototype.one = function () {
if (this.length > 0) {
return this.store.one();
}
};
LruSet.prototype.clear = function () {
this.store.clear();
this.length = 0;
};
LruSet.prototype.reduce = function (callback, basis /*, thisp*/) {
var thisp = arguments[2];
var set = this.store;
var index = 0;
return set.reduce(function (basis, value) {
return callback.call(thisp, basis, value, index++, this);
}, basis, this);
};
LruSet.prototype.reduceRight = function (callback, basis /*, thisp*/) {
var thisp = arguments[2];
var set = this.store;
var index = this.length - 1;
return set.reduceRight(function (basis, value) {
return callback.call(thisp, basis, value, index--, this);
}, basis, this);
};
LruSet.prototype.iterate = function () {
return this.store.iterate();
};