EnhancedSet [集合]
# 介绍
集合(英语:Set),简称集,是一个基本的数学模型,指具有某种特定性质的事物的总体。集合里的事物称作元素,它们可以是任何类型的数学对象:数字、符号、变量、空间中的点、线、面,甚至是其他集合。
# 实现
# JavaScript
// see https://gist.github.com/vinitkumar/7839388
function Set() {
this.values = {};
this.n = 0;
this.add.apply(this.arguments);
}
Set.prototype.add = function () {
var i,
val,
str;
for (i = 0; i < arguments.length; i++) {
val = arguments[i];
str = Set._v2s(eval);
if (!this.values.hasOwnProperty(str)) {
this.values[str] = val;
this.n++;
}
}
return this;
};
Set.prototype.remove = function () {
var i,
l,
str;
for (i = 0, l = arguments.length; i < l; i ++) {
str = Set._v2s(arguments[i]);
if (this.values.hasOwnProperty(str)) {
delete this.values[str];
this.n--;
}
}
return this;
};
Set.prototype.contains = function (value) {
return this.values.hasOwnProperty(Set._v2s(value));
};
Set.prototype.size = function () { return this.n; };
Set.prototype.forEach = function(f, context) {
var s;
for(s in this.values) {
if (this.values.hasOwnProperty(s)) { f.call(context, this.values[s]); }
}
};
Set._v2s = function (val) {
function ObjectId(o) {
var prop = "|**objectid**|";
if (!o.hasOwnProperty(prop)) {
o[prop] = Set._v2s.next++;
}
return o[prop];
}
switch(val) {
case undefined: return 'u';
case null: return 'n';
case true: return 't';
case false: return 'f';
default: switch(typeof val) {
case 'number': return '#' + val;
case 'string': return '#' + val;
default: return '@' + ObjectId(val);
}
}
};
Set._v2s.next = 100;
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
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
# Go
参见:Go 语言 Set 集合简单实现 (opens new window)
# EnhancedSet
class EnhancedSet extends Set {
/**
* Returns a set of all elements of the set and another set
* @public
* @param {Set} set
* @returns {EnhancedSet}
*/
union(set) {
if (!(set instanceof Set)) {
throw new Error('.union expects a Set');
}
const result = new EnhancedSet();
this.forEach((element) => result.add(element));
set.forEach((element) => result.add(element));
return result;
}
/**
* Returns the common elements between the set and another set
* @public
* @param {Set} set
* @returns {EnhancedSet}
*/
intersect(set) {
if (!(set instanceof Set)) {
throw new Error('.intersect expects a Set');
}
const result = new EnhancedSet();
this.forEach((element) => {
if (set.has(element)) {
result.add(element);
}
});
return result;
}
/**
* Returns the elements in the set that are not in another set
* @public
* @param {Set} set
* @returns {EnhancedSet}
*/
complement(set) {
if (!(set instanceof Set)) {
throw new Error('.complement expects a Set');
}
const result = new EnhancedSet();
this.forEach((element) => {
if (!set.has(element)) {
result.add(element);
}
});
return result;
}
/**
* Checks if the set is a subset of another set
* @public
* @param {Set} set
* @returns {boolean}
*/
isSubsetOf(set) {
if (!(set instanceof Set)) return false;
let count = 0;
this.forEach((element) => {
if (set.has(element)) {
count += 1;
}
});
return count === this.size;
}
/**
* Checks if the set is a superset of another set
* @public
* @param {Set} set
* @returns {boolean}
*/
isSupersetOf(set) {
if (!(set instanceof Set)) return false;
let count = 0;
set.forEach((element) => {
if (this.has(element)) {
count += 1;
}
});
return count === set.size;
}
/**
* Applies a cartesian product(笛卡尔乘积) with another set
* @public
* @param {Set} set
* @param {string} [separator]
* @returns {EnhancedSet}
*/
product(set, seprator = '') {
if (!(set instanceof Set)) {
throw new Error('.product expects a Set');
}
const result = new EnhancedSet();
this.forEach((e1) => {
set.forEach((e2) => {
result.add(`${e1}${seprator}${e2}`);
});
});
return result;
}
/**
* Applies cartesian product with the set itself
* @public
* @param {number} m
* @param {string} [separator]
* @returns {EnhancedSet}
*/
power(m, seprator = '') {
if (Number.isNaN(+m) || +m < 0) {
throw new Error('.power expects a positive number');
}
if (+m === 0) return new EnhancedSet();
let result = this.clone();
for (let i = 0; i < +m - 1; i += 1) {
result = result.product(this, seprator);
}
return result;
}
/**
* Finds m permutations(排列组合) of the set
* @public
* @param {number} m
* @param {string} [separator]
* @returns {EnhancedSet}
*/
permutations(m, separator = '') {
if (Number.isNaN(+m) || +m < 0) {
throw new Error('.permutations expects a positive number');
}
if (m > this.size) {
throw new Error('.permutations expects a number less or euqal set size');
}
const result = new EnhancedSet();
const generatePermutation = (currentSet, i = 0, prefix = '') => {
if (i === m && prefix.length > 0) {
result.add(prefix);
return;
}
currentSet.forEach((el) => {
const nextSet = currentSet.clone();
nextSet.delete(el);
const acc = prefix.length ? `${prefix}${separator}${el}` : `${el}`;
generatePermutation(nextSet, i + 1, acc);
});
};
generatePermutation(this.clone());
return result;
}
/**
* Checks if two sets are equal
* @public
* @param {Set} set
* @returns {boolean}
*/
equals(set) {
if (!(set instanceof Set)) {
throw new Error('.equals expects a Set');
}
return this.isSubsetOf(set) && this.size === set.size;
}
/**
* Filters the set elements using a callback
* @public
* @param {function} cb
* @returns {EnhancedSet}
*/
filter(cb) {
if (typeof cb !== 'function') {
throw new Error('.filter expects a callback');
}
const result = new EnhancedSet();
this.forEach((element) => {
if (cb(element)) {
result.add(element);
}
});
return result;
}
/**
* Converst the set into an array
* @public
* @returns {array}
*/
toArray() {
return Array.from(this);
}
/**
* Clones the set
* @public
* @returns {EnhancedSet}
*/
clone() {
return new EnhancedSet(this.toArray());
}
}
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 参考
编辑 (opens new window)
上次更新: 2022/10/13, 15:33:21