-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
42 lines (31 loc) · 755 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
package main
// stolen shamelessly from https://www.davidkaya.com/sets-in-golang/
// this is an implementation of a mathematical finite set in Go
var exists = struct{}{}
type set struct {
m map[string]struct{}
}
func NewSet() *set {
s := &set{}
s.m = make(map[string]struct{})
return s
}
func (s *set) Add(value string) {
s.m[value] = exists
}
func (s *set) Remove(value string) {
delete(s.m, value)
}
func (s *set) Contains(value string) bool {
_, c := s.m[value]
return c
}
// func main() {
// s := NewSet()
// s.Add("Peter")
// s.Add("David")
// fmt.Println(s.Contains("Peter")) // True
// fmt.Println(s.Contains("George")) // False
// s.Remove("David")
// fmt.Println(s.Contains("David")) // False
// }