-
Notifications
You must be signed in to change notification settings - Fork 0
/
sets.js
180 lines (137 loc) · 4.38 KB
/
sets.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Junq library v 1.0.0.0
Copyright Gabriele Cannata 2013-2014
Sets extensions
*/
var junq = junq || require('./junq.js');
var sets;
(function (sets, undefined) {
var Set = (function () {
var internalRep = function (objects) {
var self = this;
self.set = [];
junq(objects).forEach(function (o) {
self.add(o);
});
};
function Set(objects) {
if (objects !== undefined) {
if (arguments.length == 1)
internalRep.call(this, objects);
else
internalRep.call(this, arguments);
} else {
internalRep.call(this, []);
}
}
var isSet = function isSet(o) {
if (o !== undefined) {
return o.constructor === Set;
} else {
return false;
}
};
sets.isSet = isSet;
Set.prototype.toString = function () {
return '{' + this.toArray().join(', ') + '}';
};
Set.prototype.forEach = function (func) {
this.set.forEach(func);
return this;
};
Set.prototype.contains = function (element) {
return (this.set.indexOf(element) >= 0);
// var l=this.set.length
// for(var i=0; i<l;i++){
// if(this.set[i]===element) return true;
// }
// return false;
};
Set.prototype.add = function (element) {
if (!this.contains(element)){
this.set.push(element);
return true;
}
};
Set.prototype.addSet = function (other) {
var done = false;
var self = this;
other.forEach(function (e) {
done = done || self.add(e);
});
return done;
};
Set.prototype.remove = function (element) {
var l = this.set.length;
for (var i = 0; i < l; i++) {
if (this.set[i] === element) {
this.set.splice(i, 1);
return true;
}
}
return false;
};
Set.prototype.union = function (other) {
var res = this.clone();
var done = false;
if (isSet(other)) {
other.forEach(function (e) {
done = res.add(e) || done;
});
} else {
done = res.add(other);
}
return res;
};
Set.prototype.clone = function () {
var res = new Set();
res.set = this.set.slice(0, this.set.length);
return res;
};
Set.prototype.intersect = function (other) {
var res = new Set();
this.forEach(function (e) {
if (other.contains(e))
res.add(e);
});
return res;
};
Set.prototype.cardinality = function () {
return this.set.length;
};
Set.prototype.subtract = function (other) {
var res = this.clone();
other.forEach(function (e) {
res.remove(e);
});
return res;
};
Set.prototype.equalTo = function (other) {
if (this.cardinality() !== other.cardinality()) return false;
return this.subtract(other).isEmpty();// && other.subtract(this).isEmpty();
};
Set.prototype.isEmpty = function () {
return this.set.length === 0;
};
Set.prototype.getEnumerator = function () {
return junq.enumerate(this.toArray());
};
Set.prototype.toArray = function () {
return this.set;
};
Set.prototype.isSingleton = function () {
return this.set.length === 1;
};
Set.prototype.getElement = function (n) {
n = n | 0;
if (this.set.length <= n)
throw new Error('Not enough elements!');
return this.set[n];
};
Set.prototype.getEnumerator = function () {
return new junq.ArrayEnumerator(this.set);
};
return Set;
})();
sets.Set = Set;
})(sets || (sets = {}));
if (typeof(module) !== 'undefined') { module.exports = sets; }