Repository navigation
Expand file tree
/
Copy pathsets.js
More file actions
91 lines (83 loc) · 1.93 KB
/
Copy pathsets.js
File metadata and controls
91 lines (83 loc) · 1.93 KB
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
let mySet = function() {
this.collection = []
this.count = 0
this.has = function(value) {
let has = false;
this.collection.indexOf(value) != -1 ? has = true : has = false
return has
}
this.values = function() {
return this.collection
}
this.add = function(value) {
if (!this.has(value)) {
this.collection[this.count] = value
this.count++;
return true
}
return false
}
this.remove = function(value) {
if (this.has(value)) {
let index = collection.indexOf(value)
this.collection.splice(index, 1)
return true
}
return false
}
this.size = function() {
return this.count
}
this.union = function(set) {
let union = new mySet
let first = this.values()
let second = set.values()
first.forEach(function(e) {
union.add(e);
});
second.forEach(function(e) {
union.add(e);
});
return union
}
this.lastIndex = function() {
return this.count - 1
}
this.intersection = function(set) {
let inter = new mySet
let i = j = 0
while (i < this.count) {
if (set.has(this.collection[i])) {
inter.add(this.collection[i])
}
i++
}
return inter
}
this.difference = function(set) {
let diff = new mySet
let i = j = 0
while (i < this.count) {
if (!set.has(this.collection[i])) {
diff.add(this.collection[i])
}
i++
}
return diff
}
}
let first = new mySet;
let second = new mySet;
first.add(1)
first.add(2)
first.add(3)
first.add(4)
first.add(3)
first.add(99)
second.add(10)
second.add(2)
second.add(3)
second.add(13)
second.add(99)
second.add(99)
console.log(first.difference(second))