Skip to content

Commit ec38f2e

Browse files
committed
update: hidden storage
1 parent 92be475 commit ec38f2e

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/_DataStructures_/Set/index.js

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
// XSet because ES6 already has a Set class
22
class XSet {
33
constructor() {
4-
this.data = {};
4+
this.data = this.getStore();
55
}
66

77
add(element) {
8-
if (!this.data[element]) {
9-
this.data[element] = true;
10-
}
8+
this.data.push(element);
119
}
1210

1311
remove(element) {
14-
if (this.data[element]) {
15-
delete this.data[element];
16-
}
12+
this.data.pop(element);
1713
}
1814

1915
has(element) {
20-
return !!this.data[element];
16+
return this.data.contains(element);
2117
}
2218

2319
values() {
24-
return Object.keys(this.data);
20+
return this.data.val();
2521
}
2622

2723
union(givenSet) {
@@ -37,6 +33,30 @@ class XSet {
3733

3834
return result;
3935
}
36+
37+
// eslint-disable-next-line class-methods-use-this
38+
getStore() {
39+
const store = {};
40+
41+
return {
42+
push(el) {
43+
if (!store[el]) {
44+
store[el] = true;
45+
}
46+
},
47+
pop(el) {
48+
if (store[el]) {
49+
delete store[el];
50+
}
51+
},
52+
contains(el) {
53+
return !!store[el];
54+
},
55+
val() {
56+
return Object.keys(store);
57+
},
58+
};
59+
}
4060
}
4161

4262
// const s = new XSet();

0 commit comments

Comments
 (0)