forked from paul-mannino/go-fuzzywuzzy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setoperations.go
67 lines (59 loc) · 1.42 KB
/
setoperations.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
package fuzzy
type StringSet struct {
elements map[string]bool
}
func NewStringSet(slice []string) *StringSet {
sliceStringSet := make(map[string]bool)
for _, b := range slice {
sliceStringSet[b] = true
}
s := new(StringSet)
s.elements = sliceStringSet
return s
}
// Difference returns the set of strings that are present in this set
// but not the other set
func (s *StringSet) Difference(other *StringSet) *StringSet {
diff := new(StringSet)
diff.elements = make(map[string]bool)
for k, v := range s.elements {
if _, ok := other.elements[k]; !ok {
diff.elements[k] = v
}
}
return diff
}
// Intersection returns the set of strings that are contained in
// both sets
func (s *StringSet) Intersect(other *StringSet) *StringSet {
intersection := new(StringSet)
intersection.elements = make(map[string]bool)
for k, v := range s.elements {
if _, ok := other.elements[k]; ok {
intersection.elements[k] = v
}
}
return intersection
}
// Equals returns true if two sets contain the same elements
func (s *StringSet) Equals(other *StringSet) bool {
if len(s.elements) != len(other.elements) {
return false
}
for k, _ := range s.elements {
if _, ok := other.elements[k]; !ok {
return false
}
}
return true
}
// ToSlice produces a string slice from the set
func (s *StringSet) ToSlice() []string {
keys := make([]string, len(s.elements))
i := 0
for k := range s.elements {
keys[i] = k
i++
}
return keys
}