-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringSet.go
63 lines (53 loc) · 1.01 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 string
import (
"sort"
)
// StringSet wraps map[string]struct{} with some useful methods.
type StringSet map[string]struct{}
func (set StringSet) Has(s string) bool {
_, found := set[s]
return found
}
func (set StringSet) Set(s string) {
set[s] = struct{}{}
}
func (set StringSet) Delete(s string) {
delete(set, s)
}
func (set StringSet) Join(other StringSet) {
for s := range other {
set[s] = struct{}{}
}
}
func (set StringSet) Exclude(other StringSet) {
for s := range other {
delete(set, s)
}
}
func (set StringSet) Clone() StringSet {
clone := make(StringSet, len(set))
for s := range set {
clone[s] = struct{}{}
}
return clone
}
func (set StringSet) Sorted() []string {
list := make([]string, len(set))
i := 0
for s := range set {
list[i] = s
i++
}
sort.Strings(list)
return list
}
func (set StringSet) ReverseSorted() []string {
list := make([]string, len(set))
i := 0
for s := range set {
list[i] = s
i++
}
sort.Sort(sort.Reverse(sort.StringSlice(list)))
return list
}