forked from ramalho/strset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.go
More file actions
52 lines (48 loc) · 1.15 KB
/
operators.go
File metadata and controls
52 lines (48 loc) · 1.15 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
package strset
// Intersection returns a new set with elements that are in s AND in other.
// Math: S ∩ Z.
func (s Set) Intersection(other Set) Set {
var longer, shorter Set
if s.Len() > other.Len() {
longer = s
shorter = other
} else {
longer = other
shorter = s
}
res := Make()
for elem := range shorter.store {
if longer.Contains(elem) {
res.store[elem] = struct{}{}
}
}
return res
}
// Union returns a new Set: with elements that are in s OR in other.
// Math: S ∪ Z.
func (s Set) Union(other Set) Set {
res := s.Copy()
for elem := range other.store {
res.store[elem] = struct{}{}
}
return res
}
// Difference returns a new Set with the elements of s minus the elements of other.
// Math: S \ Z.
func (s Set) Difference(other Set) Set {
res := Make()
for elem := range s.store {
if !other.Contains(elem) {
res.store[elem] = struct{}{}
}
}
return res
}
// SymmetricDifference returns a new Set with elements present
// in either set but not on both. Think boolean XOR.
// Math: S ∆ Z.
func (s Set) SymmetricDifference(other Set) Set {
all := s.Union(other)
common := s.Intersection(other)
return all.Difference(common)
}