1
+ let Set2 = (function () {
2
+
3
+ const items = new WeakMap();
4
+
5
+ class Set2 {
6
+
7
+ constructor () {
8
+ items.set(this, {});
9
+ }
10
+
11
+ add(value){
12
+ if (!this.has(value)){
13
+ let items_ = items.get(this);
14
+ items_[value] = value;
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+
20
+ delete(value){
21
+ if (this.has(value)){
22
+ let items_ = items.get(this);
23
+ delete items_[value];
24
+ return true;
25
+ }
26
+ return false;
27
+ }
28
+
29
+ has(value){
30
+ let items_ = items.get(this);
31
+ return items_.hasOwnProperty(value);
32
+ }
33
+
34
+ clear(){
35
+ items.set(this, {});
36
+ }
37
+
38
+ size(){
39
+ let items_ = items.get(this);
40
+ return Object.keys(items_).length;
41
+ }
42
+
43
+
44
+ values(){
45
+ let values = [];
46
+ let items_ = items.get(this);
47
+ for (let i=0, keys=Object.keys(items_); i<keys.length; i++) {
48
+ values.push(items_[keys[i]]);
49
+ }
50
+ return values;
51
+ }
52
+
53
+ getItems(){
54
+ return items.get(this);
55
+ }
56
+
57
+ union(otherSet){
58
+ let unionSet = new Set();
59
+
60
+ let values = this.values();
61
+ for (let i=0; i<values.length; i++){
62
+ unionSet.add(values[i]);
63
+ }
64
+
65
+ values = otherSet.values();
66
+ for (let i=0; i<values.length; i++){
67
+ unionSet.add(values[i]);
68
+ }
69
+
70
+ return unionSet;
71
+ }
72
+
73
+ intersection(otherSet){
74
+ let intersectionSet = new Set();
75
+
76
+ let values = this.values();
77
+ for (let i=0; i<values.length; i++){
78
+ if (otherSet.has(values[i])){
79
+ intersectionSet.add(values[i]);
80
+ }
81
+ }
82
+
83
+ return intersectionSet;
84
+ }
85
+
86
+ difference(otherSet){
87
+ let differenceSet = new Set();
88
+
89
+ let values = this.values();
90
+ for (let i=0; i<values.length; i++){
91
+ if (!otherSet.has(values[i])){
92
+ differenceSet.add(values[i]);
93
+ }
94
+ }
95
+
96
+ return differenceSet;
97
+ };
98
+
99
+ subset(otherSet){
100
+
101
+ if (this.size() > otherSet.size()){
102
+ return false;
103
+ } else {
104
+ let values = this.values();
105
+ for (let i=0; i<values.length; i++){
106
+ if (!otherSet.has(values[i])){
107
+ return false;
108
+ }
109
+ }
110
+ return true;
111
+ }
112
+ };
113
+ }
114
+ return Set2;
115
+ })();
0 commit comments