forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru-map.js
57 lines (49 loc) · 1.59 KB
/
lru-map.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
"use strict";
var Shim = require("./shim");
var LruSet = require("./lru-set");
var GenericCollection = require("./generic-collection");
var GenericMap = require("./generic-map");
var PropertyChanges = require("./listen/property-changes");
module.exports = LruMap;
function LruMap(values, maxLength, equals, hash, getDefault) {
if (!(this instanceof LruMap)) {
return new LruMap(values, maxLength, equals, hash, getDefault);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
this.getDefault = getDefault;
this.store = new LruSet(
undefined,
maxLength,
function keysEqual(a, b) {
return equals(a.key, b.key);
},
function keyHash(item) {
return hash(item.key);
}
);
this.length = 0;
this.addEach(values);
}
Object.addEach(LruMap.prototype, GenericCollection.prototype);
Object.addEach(LruMap.prototype, GenericMap.prototype);
Object.addEach(LruMap.prototype, PropertyChanges.prototype);
LruMap.prototype.constructClone = function (values) {
return new this.constructor(
values,
this.maxLength,
this.contentEquals,
this.contentHash,
this.getDefault
);
};
LruMap.prototype.log = function (charmap, stringify) {
stringify = stringify || this.stringify;
this.store.log(charmap, stringify);
};
LruMap.prototype.stringify = function (item, leader) {
return leader + JSON.stringify(item.key) + ": " + JSON.stringify(item.value);
}