-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.js
165 lines (130 loc) · 3.2 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
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
function Set () {
var 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.clear = function () {
items = {}
}
this.size = function () {
return Object.keys(items).length
}
this.values = function () {
return Object.keys(items)
}
this.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
}
// 自己实现的T T,和下面一对比被秒成渣
// this.intersection = function (otherSet) {
// var intersectionSet = new Set()
// var values1 = this.values()
// var values2 = otherSet.values()
// for (var i = 0; i < values1.length; i++) {
// for (var j = 0; j < values2.length; j++) {
// if (values1[i] === values2[j]) {
// intersectionSet.add(values1[i])
// break
// }
// }
// }
// return intersectionSet
// }
this.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
}
this.difference = function (otherSet) {
var differenceSet = new Set()
var values = this.values()
for (var i = 0; i < values.length; i++) {
if (!otherSet.has(values[i])) {
differenceSet.add(values[i])
}
}
return differenceSet
}
// 自己实现的
// this.subSet = function (otherSet) {
// var values = this.values()
// return values.every(function (value) {
// return otherSet.has(value)
// })
// }
this.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
}
}
}
// var setA = new Set()
// setA.add(1)
// setA.add(2)
// setA.add(3)
// var setB = new Set()
// setB.add(2)
// setB.add(3)
// setB.add(4)
// setB.add(5)
// var setC = new Set()
// setC.add(2)
// setA.add(3)
// var unionAB = setA.union(setB)
// console.log(unionAB.values())
// var intersectionAB = setA.intersection(setB)
// console.log(intersectionAB.values())
// var differenceAB = setA.difference(setB)
// console.log(differenceAB.values())
// console.log(setA.subSet(setB))
// console.log(setC.subSet(setA))
// console.log(setC.subSet(setB))
var set = new Set()
set.add(1)
console.log(set.values())
console.log(set.has(1))
console.log(set.size())
set.add(2)
console.log(set.values())
console.log(set.has(2))
console.log(set.size())
set.remove(1)
console.log(set.values())
set.remove(2)
console.log(set.values())