-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmatch_set.go
More file actions
57 lines (48 loc) · 1.23 KB
/
match_set.go
File metadata and controls
57 lines (48 loc) · 1.23 KB
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 quamina
// matchSet is what it says on the tin; implements a set semantic on matches, which are of type X. These could all
// be implemented as match[X]bool but this makes the calling code more readable.
type matchSet struct {
set map[X]bool
}
func newMatchSet() *matchSet {
return &matchSet{set: make(map[X]bool)}
}
// reset clears the matchSet for reuse, preserving the allocated map capacity.
func (m *matchSet) reset() {
clear(m.set)
}
func (m *matchSet) addX(exes ...X) *matchSet {
if len(exes) == 0 {
return m
}
// for concurrency, can't update in place
newSet := make(map[X]bool, len(m.set)+1)
for k := range m.set {
newSet[k] = true
}
for _, x := range exes {
newSet[x] = true
}
return &matchSet{set: newSet}
}
func (m *matchSet) addXSingleThreaded(exes ...X) *matchSet {
for _, x := range exes {
m.set[x] = true
}
return m
}
func (m *matchSet) matches() []X {
matches := make([]X, 0, len(m.set))
for x := range m.set {
matches = append(matches, x)
}
return matches
}
// matchesInto appends matches to the provided buffer and returns it.
// This avoids allocating a new slice for the result.
func (m *matchSet) matchesInto(buf []X) []X {
for x := range m.set {
buf = append(buf, x)
}
return buf
}