-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
178 lines (142 loc) · 3.74 KB
/
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package events
import (
"fmt"
"sync"
)
// set is an implementation prototype for a basic mathematical set data structure. It's API is geared more
// towards a specific use inside the events package rather than a more general purpose use.
type set[T comparable] interface {
// Adds an item to the set. If the item already exists, it is replaced.
Add(item T)
// Remove removes the item from the set if it exists, which returns true. If the item doesn't
// exist, false is returned.
Remove(item T) bool
// RemoveOn accepts a predicate that is run against each item, removing the item on a true
// return.
RemoveOn(func(T) bool)
// RemoveAll removes all of the items in the set
RemoveAll()
// Has returns true if the item exists within the set. Otherwise, false.
Has(item T) bool
// Filtered returns a slice of items that match the predicate.
Filtered(func(T) bool) []T
// Length returns the total number of items in the set.
Length() int
// ToSlice creates a new slice of size Length(), copies the set elements into the slice, and
// returns it.
ToSlice() []T
// CopyTo accepts a destination slice and writes the contents of the set to it.
CopyTo([]T) error
}
// lockingSet is a thread-safe implementation of set which leverages a go map for storage.
type lockingSet[T comparable] struct {
lock sync.Mutex
m map[T]struct{}
}
// creates a new set for use with dispatching
func newSet[T comparable]() set[T] {
return &lockingSet[T]{
m: make(map[T]struct{}),
}
}
// Adds an item to the set. If the item already exists, it is replaced.
func (s *lockingSet[T]) Add(item T) {
s.lock.Lock()
defer s.lock.Unlock()
s.m[item] = struct{}{}
}
// Remove removes the item from the set if it exists, which returns true. If the item doesn't
// exist, false is returned.
func (s *lockingSet[T]) Remove(item T) (ok bool) {
s.lock.Lock()
defer s.lock.Unlock()
_, ok = s.m[item]
if !ok {
return
}
delete(s.m, item)
return
}
// RemoveOn accepts a predicate that is run against each item, removing the item on a true
// return.
func (s *lockingSet[T]) RemoveOn(predicate func(T) bool) {
s.lock.Lock()
defer s.lock.Unlock()
for k := range s.m {
if predicate(k) {
delete(s.m, k)
}
}
}
// RemoveAll removes all of the items in the set
func (s *lockingSet[T]) RemoveAll() {
s.lock.Lock()
defer s.lock.Unlock()
for k := range s.m {
delete(s.m, k)
}
}
// Has returns true if the item exists within the set. Otherwise, false.
func (s *lockingSet[T]) Has(item T) bool {
s.lock.Lock()
defer s.lock.Unlock()
_, ok := s.m[item]
return ok
}
// Length returns the total number of items in the set.
func (s *lockingSet[T]) Length() int {
s.lock.Lock()
defer s.lock.Unlock()
return len(s.m)
}
// ToSlice creates a new slice of size Length(), copies the set elements into the slice, and
// returns it.
func (s *lockingSet[T]) ToSlice() []T {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.m)
if l == 0 {
return nil
}
slice := make([]T, l)
index := 0
for k := range s.m {
slice[index] = k
index++
}
return slice
}
// Filtered returns a slice of items that match the predicate.
func (s *lockingSet[T]) Filtered(predicate func(T) bool) []T {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.m)
if l == 0 {
return nil
}
var slice []T
for k := range s.m {
if predicate(k) {
slice = append(slice, k)
}
}
return slice
}
// CopyTo accepts a destination slice and writes the contents of the set to it.
func (s *lockingSet[T]) CopyTo(destination []T) error {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.m)
if l == 0 {
return nil
}
if len(destination) > l {
return fmt.Errorf("Destination length(%d) < source length (%d)", len(destination), l)
}
index := 0
for k := range s.m {
destination[index] = k
index++
}
return nil
}