-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
60 lines (48 loc) · 941 Bytes
/
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
package main
const present = ""
type Set struct {
elements map[string]string
}
func NewSet() *Set {
return &Set{make(map[string]string)}
}
func (s *Set) Add(element string) {
s.elements[element] = present
}
func (s *Set) AddAll(elements []string) {
for _, v := range elements {
s.Add(v)
}
}
func (s *Set) Remove(element string) {
delete(s.elements, element)
}
func (s *Set) Contains(element string) bool {
_, found := s.elements[element]
return found
}
func (s *Set) Size() int {
return len(s.elements)
}
func (s *Set) IsEmpty() bool {
return s.Size() == 0
}
func (s *Set) Clear() {
s.elements = make(map[string]string)
}
func (s *Set) Values() []string {
values := make([]string, 0, s.Size())
for value := range s.elements {
values = append(values, value)
}
return values
}
func (s *Set) Toggle(val string) bool {
if s.Contains(val) {
s.Remove(val)
return false
} else {
s.Add(val)
return true
}
}