-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringset.go
63 lines (57 loc) · 1.11 KB
/
stringset.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
package main
import "sort"
type stringSet map[string]struct{}
func (set stringSet) Add(s string) stringSet {
set[s] = struct{}{}
return set
}
func (set stringSet) AddSlice(s []string) stringSet {
for _, k := range s {
set.Add(k)
}
return set
}
func (set stringSet) Sub(s string) stringSet {
delete(set, s)
return set
}
func (set stringSet) Slice() []string {
ret := make([]string, 0, len(set))
for s := range set {
ret = append(ret, s)
}
sort.StringSlice(ret).Sort()
return ret
}
// bool operations
type strSliceBoolOp func([]string, []string) []string
func strUnion(s1, s2 []string) []string {
set := make(stringSet, len(s1)+len(s2))
for _, s := range s1 {
set.Add(s)
}
for _, s := range s2 {
set.Add(s)
}
return set.Slice()
}
func strSub(s1, s2 []string) []string {
set := make(stringSet, len(s1)+len(s2))
for _, s := range s1 {
set.Add(s)
}
for _, s := range s2 {
set.Sub(s)
}
return set.Slice()
}
func strCut(s1, s2 []string) []string {
set2 := stringSet{}.AddSlice(s2)
ret := stringSet{}
for _, s := range s1 {
if _, ok := set2[s]; ok {
ret.Add(s)
}
}
return ret.Slice()
}