-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.js
More file actions
147 lines (127 loc) · 3.45 KB
/
Set.js
File metadata and controls
147 lines (127 loc) · 3.45 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
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
(function() {
var $encoder;
// the following is commented out until the Native JSON parse in FireFox becomes more lax - David 8/19/09
/*
if((Browser.Engine.gecko && Browser.Engine.version) >= 19 ||
(Browser.Engine.webkit && Browser.Engine.version >= 525)) {
// remove MooTools JSON
delete Hash.prototype.toJSON;
delete Array.prototype.toJSON;
delete String.prototype.toJSON;
delete Number.prototype.toJSON;
delete window.JSON;
$encoder = JSON.encode = JSON.stringify;
$decoder = JSON.decode = JSON.parse;
} else {*/
$encoder = JSON.encode;
$decoder = JSON.decoder;
//}
function $normalize(v) {
if($type(v) == "array") return v.normalize();
if($type(v) == "hash") v = v.getClean();
if($type(v) == "object") {
var result = [];
var sortArray = [];
for(var k in v) sortArray.push(k);
sortArray.sort();
for(var i = 0, l = sortArray.length; i < l; i++) {
var k = sortArray[i];
result.push([k, $normalize(v[k])]);
}
return result;
}
return v;
}
window.$hash = function(v) {
return $encoder($normalize(v));
};
Array.implement({
normalize: function() {
var result = [];
for(var i = 0, l = this.length; i < l; i++) result.push($normalize(this[i]));
return result;
}
});
window.Set = new Class({
initialize: function(ary) {
this.isset = true;
this.rep = {};
if(ary && !ary.isset) {
for(var i = 0, len = ary.length; i < len; i++) this.rep[$hash(ary[i])] = ary[i];
} else if(ary) {
this.rep = ary.rep;
}
},
put: function(v) {
this.rep[$hash(v)] = v;
},
get: function(v) {
return this.rep[$hash(v)];
},
remove: function(v) {
delete this.rep[$hash(v)];
},
intersection: function(set) {
var result = new Set();
set = new Set(set);
for(var k in this.rep) {
var v = this.rep[k], hashed = $hash(v);
if(set.rep[hashed]) result.rep[hashed] = v;
}
return result;
},
aintersection: function(set){ return this.intersection(set).toArray(); },
difference: function(set) {
var result = new Set();
set = new Set(set);
for(var k in this.rep) {
var v = this.rep[k], hashed = $hash(v);
if(!set.rep[hashed]) result.rep[hashed] = v;
}
for(var k in set.rep) {
var v = set.rep[k], hashed = $hash(v);
if(!this.rep[hashed]) result.rep[hashed] = v;
}
return result;
},
adifference: function(set){ return this.difference(set).toArray(); },
union: function(set) {
var result = new Set();
set = new Set(set);
for(var k in this.rep) result.rep[k] = this.rep[k];
for(var k in set.rep) result.rep[k] = set.rep[k];
return result;
},
aunion: function(set) { return this.union(set).toArray(); },
toArray: function() {
var result = [];
for(var k in this.rep) result.push(this.rep[k]);
return result;
},
isEqual: function(set) {
set = new Set(set);
for(var k in this.rep) if(!set.rep[k]) return false;
return true;
},
each: function(fn, bind) {
for(var k in this.rep) fn.call(bind, this.rep[k], this);
},
map: function (fn, bind) {
var result = new Set();
for(var k in this.rep) {
var v = fn.call(bind, this.rep[k], this);
result.rep[$hash(v)] = v;
}
return result;
},
filter: function(fn, bind) {
var result = new Set();
for(var k in this.rep) {
var v = this.rep[k];
var bool = fn.call(bind, v, this);
if(bool) result.rep[k] = v;
}
return result;
}
});
})()