-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
string_set.go
89 lines (72 loc) · 1.63 KB
/
string_set.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
82
83
84
85
86
87
88
89
package game
import (
"bytes"
"encoding/gob"
"github.com/samber/lo"
"sort"
)
// StringSet represents a string set that can be serialized by Gob.
//
// Note: As the GobDecode needs to overwrite its receiver we need to have the map
// behind a struct pointer.
type StringSet struct {
values map[string]struct{}
}
func NewStringSet() *StringSet {
return &StringSet{
values: map[string]struct{}{},
}
}
func (s *StringSet) Has(val string) bool {
_, ok := s.values[val]
return ok
}
func (s *StringSet) Add(val string) {
s.values[val] = struct{}{}
}
func (s *StringSet) Remove(val string) {
delete(s.values, val)
}
func (s *StringSet) Append(vals ...string) {
for _, val := range vals {
s.Add(val)
}
}
func (s *StringSet) Clear() {
for key := range s.values {
delete(s.values, key)
}
}
func (s *StringSet) ToSlice() []string {
keys := lo.Keys(s.values)
sort.Strings(keys)
return keys
}
func (s *StringSet) Clone() *StringSet {
return &StringSet{values: CopyMap(s.values)}
}
func (s *StringSet) GobEncode() ([]byte, error) {
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
err := enc.Encode(s.ToSlice())
return buf.Bytes(), err
}
func (s *StringSet) GobDecode(data []byte) error {
*s = StringSet{values: map[string]struct{}{}}
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
var keys []string
if err := dec.Decode(&keys); err != nil {
return err
}
s.Append(keys...)
return nil
}
// CopyMap copies a map. If the value is a pointer, the pointer is copied, not the value.
func CopyMap[K comparable, V any](m map[K]V) map[K]V {
result := make(map[K]V)
for k, v := range m {
result[k] = v
}
return result
}