-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.go
More file actions
64 lines (55 loc) · 824 Bytes
/
set.go
File metadata and controls
64 lines (55 loc) · 824 Bytes
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
package main
import (
"sort"
"strconv"
)
type set map[int]struct{}
func (s set) Set(i int) {
s[i] = struct{}{}
}
func (s set) Clear(i int) {
delete(s, i)
}
func (s set) Test(i int) bool {
_, ok := s[i]
return ok
}
func (s set) Flip(i int) {
if s.Test(i) {
s.Clear(i)
} else {
s.Set(i)
}
}
func (s set) Clone() set {
s1 := set{}
for i := range s {
s1.Set(i)
}
return s1
}
func (s set) Union(s1 set) (changed bool) {
for i := range s1 {
if !s.Test(i) {
changed = true
s.Set(i)
}
}
return
}
func (s set) String() string {
ints := make([]int, 0, len(s))
for i := range s {
ints = append(ints, i)
}
sort.Ints(ints)
b := []byte{'{'}
for i, n := range ints {
if i > 0 {
b = append(b, ',')
}
b = strconv.AppendInt(b, int64(n), 10)
}
b = append(b, '}')
return string(b)
}