/
set.js
94 lines (78 loc) · 2.39 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
// typical use case of a Set is to check for the presence of an item
// ES6 has a Set data type but doesn't implement all methods you might want
function mySet() {
// the var collection will hold the set in this array
var collection = [];
// this method checks for presence of element and returns true or false
this.has = function(value) {
return (collection.indexOf(value) !== -1);
}
// returns all values in set
this.values = function() {
return collection;
}
// add an element to the set if it doesn't already exist in set
this.add = function(element) {
if(!this.has(element)) {
collection.push(element);
return true;
}
return false;
}
// this method will remove an element from a set
this.remove = function(element) {
if (this.has(element)) {
index = collection.indexOf(element);
collection.splice(index, 1); // this is how you remove an element from an array
return true;
}
return false;
}
// this method returns the size of the collection
this.size = function() {
return collection.length;
}
// this method will return the union of two sets in a new set
this.union = function(otherSet) {
var unionSet = new mySet();
var firstSet = this.values();
var secondSet = otherSet.values();
firstSet.forEach(function(e) {
unionSet.add(e);
});
secondSet.forEach(function(e) {
unionSet.add(e);
});
return unionSet;
}
// this method returns intersection of two sets as a new set
this.intersection = function(otherSet) {
var intersectionSet = new mySet();
var firstSet = this.values();
firstSet.forEach(function(e) {
if (otherSet.has(e)) { // inverse of difference
intersectionSet.add(e);
}
})
return intersectionSet;
}
// Intersection and Difference methods are inverses of each other
// this method returns the difference of two sets as a new set
this.difference = function(otherSet) {
var differenceSet = new mySet();
var firstSet = this.values();
firstSet.forEach(function(e) {
if (!otherSet.has(e)) { // inverse of intersection
differenceSet.add(e);
}
});
return differenceSet;
}
// this method tests if set is subset of different set
this.subset = function(otherSet) {
var firstSet = this.values();
return firstSet.every(function(e) {
return otherSet.has(e);
});
}
}