-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol.go
50 lines (40 loc) · 855 Bytes
/
symbol.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
package main
type Symbol string
type Symbols interface {
Add(ss ...Symbol) Symbols
Contains(s Symbol) bool
Union(other Symbols) Symbols
ToSlice() []Symbol
}
func NewSymbols() Symbols {
return make(symbolsImpl)
}
type symbolsImpl map[Symbol]struct{}
func (ss symbolsImpl) copy() symbolsImpl {
newSs := make(symbolsImpl)
for k := range ss {
newSs[k] = struct{}{}
}
return newSs
}
func (ss symbolsImpl) Add(sList ...Symbol) Symbols {
newSS := ss.copy()
for _, s := range sList {
newSS[s] = struct{}{}
}
return newSS
}
func (ss symbolsImpl) Contains(s Symbol) bool {
_, ok := ss[s]
return ok
}
func (ss symbolsImpl) ToSlice() []Symbol {
symbols := make([]Symbol, 0)
for s := range ss {
symbols = append(symbols, s)
}
return symbols
}
func (ss symbolsImpl) Union(other Symbols) Symbols {
return ss.Add(other.ToSlice()...)
}