-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidset.go
executable file
·161 lines (145 loc) · 2.58 KB
/
pidset.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package actor
const pidSetSliceLen = 16
type PIDSet struct {
s []string
m map[string]struct{}
}
// NewPIDSet returns a new PIDSet with the given pids.
func NewPIDSet(pids ...*PID) *PIDSet {
var s PIDSet
for _, pid := range pids {
s.Add(pid)
}
return &s
}
func (p *PIDSet) indexOf(v *PID) int {
key := v.key()
for i, pid := range p.s {
if key == pid {
return i
}
}
return -1
}
func (p *PIDSet) migrate() {
p.m = make(map[string]struct{}, pidSetSliceLen)
for _, v := range p.s {
p.m[v] = struct{}{}
}
p.s = p.s[:0]
}
// Add adds the element v to the set
func (p *PIDSet) Add(v *PID) {
if p.m == nil {
if p.indexOf(v) > -1 {
return
}
if len(p.s) < pidSetSliceLen {
if p.s == nil {
p.s = make([]string, 0, pidSetSliceLen)
}
p.s = append(p.s, v.key())
return
}
p.migrate()
}
p.m[v.key()] = struct{}{}
}
// Remove removes v from the set and returns true if them element existed
func (p *PIDSet) Remove(v *PID) bool {
if p.m == nil {
i := p.indexOf(v)
if i == -1 {
return false
}
l := len(p.s) - 1
p.s[i] = p.s[l]
p.s = p.s[:l]
return true
}
_, ok := p.m[v.key()]
if !ok {
return false
}
delete(p.m, v.key())
return true
}
// Contains reports whether v is an element of the set
func (p *PIDSet) Contains(v *PID) bool {
if p.m == nil {
return p.indexOf(v) != -1
}
_, ok := p.m[v.key()]
return ok
}
// Len returns the number of elements in the set
func (p *PIDSet) Len() int {
if p.m == nil {
return len(p.s)
}
return len(p.m)
}
// Clear removes all the elements in the set
func (p *PIDSet) Clear() {
if p.m == nil {
p.s = p.s[:0]
} else {
p.m = nil
}
}
// Empty reports whether the set is empty
func (p *PIDSet) Empty() bool {
return p.Len() == 0
}
// Values returns all the elements of the set as a slice
func (p *PIDSet) Values() []PID {
if p.Len() == 0 {
return nil
}
r := make([]PID, p.Len())
if p.m == nil {
for i, v := range p.s {
pidFromKey(v, &r[i])
}
} else {
i := 0
for v := range p.m {
pidFromKey(v, &r[i])
i++
}
}
return r
}
// ForEach invokes f for every element of the set
func (p *PIDSet) ForEach(f func(i int, pid PID)) {
var pid PID
if p.m == nil {
for i, v := range p.s {
pidFromKey(v, &pid)
f(i, pid)
}
} else {
i := 0
for v := range p.m {
pidFromKey(v, &pid)
f(i, pid)
i++
}
}
}
func (p *PIDSet) Clone() *PIDSet {
var s PIDSet
if p.s != nil {
s.s = make([]string, len(p.s))
for i, v := range p.s {
s.s[i] = v
}
}
if p.m != nil {
s.m = make(map[string]struct{}, len(p.m))
for v := range p.m {
s.m[v] = struct{}{}
}
}
return &s
}