-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (43 loc) · 1.01 KB
/
main.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
package main
import (
"sort"
"strings"
)
type Set struct {
internalMap map[string]bool
}
func NewSet(elements ...string) Set {
internalMap := map[string]bool{}
for _, element := range elements {
internalMap[element] = true
}
return Set{internalMap}
}
func (s Set) Size() int {
return len(s.internalMap)
}
func (s *Set) Add(element string) {
s.internalMap[element] = true
}
func (s *Set) Remove(element string) {
delete(s.internalMap, element)
}
func (s Set) Contains(element string) bool {
// Since we only store true values for keys that are existing,
// we can just return the value. Missing key will result in default
// value which is false for bool types.
return s.internalMap[element]
}
func (s Set) Slice() []string {
slice := make([]string, 0, len(s.internalMap))
for element := range s.internalMap {
slice = append(slice, element)
}
return slice
}
// Return elements ordered ascending.
func (s Set) String() string {
slice := s.Slice()
sort.Strings(slice)
return strings.Join(slice, ", ")
}