-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.js
More file actions
105 lines (88 loc) · 1.9 KB
/
Copy pathset.js
File metadata and controls
105 lines (88 loc) · 1.9 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
function Set() {
let items = {};
this.has = function (value) {
return items.hasOwnProperty(value);
};
this.add = function (value) {
if (!this.has(value)) {
items[value] = value;
return true;
}
return false;
};
this.remove = function (value) {
if (this.has(value)) {
delete items[value];
return true;
}
return false;
};
this.print = function () {
console.log(items);
}
this.clear = function() {
items = {};
};
this.size = function () {
return Object.keys(items).length;
};
this.value = function () {
let values = [];
for (let value in items) {
values.push(value);
}
return values;
};
this.union = function (otherSet) { //并集
let unionSet = new Set();
let values = this.value();
for (var i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
values = otherSet.value();
for (var i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
return unionSet;
};
this.intersection = function (otherSet) { //交集
let intersectionSet = new Set();
let values = this.value();
for (var i = 0; i < values.length; i++) {
if (otherSet.has(values[i])) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
};
this.difference = function (otherSet) { //差集
let differenceSet = new Set();
let values = this.value();
for (var i = 0; i < values.length; i++) {
if (!otherSet.has(values[i])) {
differenceSet.add(values[i]);
}
}
return differenceSet;
};
this.subset = function(otherSet) { //子集
if (this.size() > otherSet.size()) {
return false;
} else {
let values = this.value();
for (let i = 0; i < values.length; i++) {
if (!otherSet.has(values[i])) {
return false;
}
}
return true;
}
};
}
let setA = new Set();
setA.add('Cyclone');
let setB = new Set();
setB.add('Cyclone');
setB.add('Luna');
setB.add('Metal');
console.log(setA.subset(setB));