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