-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarset.go
81 lines (70 loc) · 1.69 KB
/
varset.go
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
package tnt
import (
"fmt"
"sort"
)
// VariableSet is a set of Variables.
type VariableSet map[Variable]struct{}
func NewVariableSetString(strs ...string) VariableSet {
v := make(VariableSet)
for _, str := range strs {
v[Variable(str)] = struct{}{}
}
return v
}
func NewVariableSet(vars ...Variable) VariableSet {
vs := make(VariableSet)
for _, v := range vars {
vs[v] = struct{}{}
}
return vs
}
// Union returns a set including elements from v and v2
func (v VariableSet) Union(v2 VariableSet) VariableSet {
union := make(VariableSet)
union.add(v)
union.add(v2)
return union
}
// Complement returns a set of elements that are in v but not in v2
func (v VariableSet) Complement(v2 VariableSet) VariableSet {
complement := make(VariableSet)
complement.add(v)
complement.subtract(v2)
return complement
}
// Intersection returns a set of elements that are in both v and v2
func (v VariableSet) Intersection(v2 VariableSet) VariableSet {
intersection := make(VariableSet)
for item := range v {
if _, ok := v2[item]; ok {
intersection[item] = struct{}{}
}
}
return intersection
}
// SymmetricDifference returns a set of elements that are in v or v2,
// but not both.
func (v VariableSet) SymmetricDifference(v2 VariableSet) VariableSet {
diff := v.Complement(v2)
diff.add(v2.Complement(v))
return diff
}
func (v VariableSet) String() string {
slice := make([]string, 0, len(v))
for k, _ := range v {
slice = append(slice, string(k))
}
sort.Strings(slice)
return fmt.Sprintf("%s", slice)
}
func (v VariableSet) add(v2 VariableSet) {
for item := range v2 {
v[item] = struct{}{}
}
}
func (v VariableSet) subtract(v2 VariableSet) {
for item := range v2 {
delete(v, item)
}
}