-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwomap.js
More file actions
66 lines (60 loc) · 1.51 KB
/
twomap.js
File metadata and controls
66 lines (60 loc) · 1.51 KB
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
var _ = require('underscorem');
function TwoMap(){
this.data = Object.create(null);
}
TwoMap.prototype.value = function(aKey, bKey){
var m = this.data[aKey];
if(m === undefined) return;
return m[bKey];
}
TwoMap.prototype.remove = function(aKey, bKey){
var m = this.data[aKey];
if(m === undefined) _.errout('cannot remove non-existent pair (' + aKey + ',*(' + bKey + '))');
if(m[bKey] === undefined) _.errout('cannot remove non-existent pair (' + aKey + ',' + bKey + ')');
delete m[bKey];
if(Object.keys(m).length === 0){
delete this.data[aKey];
}
}
TwoMap.prototype.set = function(aKey, bKey, value){
var m = this.data[aKey];
if(m === undefined) m = this.data[aKey] = Object.create(null);
m[bKey] = value;
}
TwoMap.prototype.part = function(aKey, cb){
var m = this.data[aKey];
if(m !== undefined){
var keys = Object.keys(m);
for(var i=0;i<keys.length;++i){
var k = keys[i];
cb(m[k], k);
}
}
}
TwoMap.prototype.partRef = function(aKey){
var m = this.data[aKey];
if(m === undefined){
m = this.data[aKey] = Object.create(null);
}
return m;
}
TwoMap.prototype.has = function(aKey, bKey){
var m = this.data[aKey];
if(m === undefined) return false;
return m[bKey] !== undefined;
}
TwoMap.prototype.all = function(cb){
var aKeys = Object.keys(this.data);
for(var i=0;i<aKeys.length;++i){
var aKey = aKeys[i];
var m = this.data[aKey];
var bKeys = Object.keys(m);
for(var j=0;j<bKeys.length;++j){
var k = bKeys[j];
cb(m[k], aKey, k);
}
}
}
exports.make = function(){
return new TwoMap();
}