UNPKG

1.25 kBJavaScriptView Raw
1export class AbstractSet {
2 diff(set) {
3 if (!set) {
4 return this.entries();
5 }
6 const items = this.entries();
7 const result = [];
8 for (const element of items) {
9 if (!set.has(element)) {
10 result.push(element);
11 }
12 }
13 return result;
14 }
15 union(set) {
16 if (!set) {
17 return this.entries();
18 }
19 const items = set.entries();
20 const thisItems = this.entries();
21 for (const element of items) {
22 if (!this.has(element)) {
23 thisItems.push(element);
24 }
25 }
26 return thisItems;
27 }
28 intersect(set) {
29 if (!set) {
30 return [];
31 }
32 const result = [];
33 let largeSet, smallItems;
34 if (this.Size > set.Size) {
35 largeSet = this;
36 smallItems = set.entries();
37 }
38 else {
39 largeSet = set;
40 smallItems = this.entries();
41 }
42 for (const element of smallItems) {
43 if (largeSet.has(element)) {
44 result.push(element);
45 }
46 }
47 return result;
48 }
49 isEmpty() {
50 return this.Size === 0;
51 }
52}