forked from PearInc/PearPlayer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.js
executable file
·84 lines (81 loc) · 2.08 KB
/
set.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
/**
* Created by snow on 17-6-22.
*/
module.exports = Set;
function Set() {
this.items = {};
}
Set.prototype = {
constructer: Set,
has: function(value) {
return value in this.items;
},
add: function(value) {
if (!this.has(value)) {
this.items[value] = value;
return true;
}
return false;
},
remove: function(value) {
if (this.has(value)) {
delete this.items[value];
return true;
}
return false;
},
clear: function() {
this.items = {};
},
size: function() {
return Object.keys(this.items).length;
},
values: function() {
return Object.keys(this.items); //values是数组
},
union: function(otherSet) {
var unionSet = new Set();
var values = this.values();
for (var i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
values = otherSet.values();
for (var i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
return unionSet;
},
intersection: function(otherSet) {
var intersectionSet = new Set();
var values = this.values();
for (var i = 0; i < values.length; i++) {
if (otherSet.has(values[i])) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
},
difference: function(otherSet) {
var differenceSet = new Set();
var values = otherSet.values();
for (var i = 0; i < values.length; i++) {
if (!this.has(values[i])) {
differenceSet.add(values[i]);
}
}
return differenceSet;
},
subset: function(otherSet) {
if (this.size() > otherSet.size()) {
return false;
} else {
var values = this.values();
for (var i = 0; i < values.length; i++) {
if (!otherSet.has(values[i])) {
return false;
}
}
}
return true;
}
}