-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
262 lines (219 loc) · 5.58 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright 2024 eventbus Author(s)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eventbus
import "slices"
var empty struct{}
const (
// use different sizes for the slice and map to balance the performance
mixSetMaxSliceSize = 128 // when the set size is greater than this value, use map instead of slice
mixSetMinMapSize = 64 // when the set size is less than this value, use slice instead of map
)
type Set[T comparable] map[T]struct{}
func NewSet[T comparable](cache uint) Set[T] {
return make(Set[T], cache)
}
func NewSetNoCache[T comparable]() Set[T] {
return NewSet[T](0)
}
// Add adds an element to the set.
func (s Set[T]) Add(e ...T) {
for _, v := range e {
s[v] = empty
}
}
// Remove removes an element from the set.
func (s Set[T]) Remove(e ...T) {
for _, v := range e {
delete(s, v)
}
}
// Contains checks if an element is in the set.
func (s Set[T]) Contains(e T) bool {
_, ok := s[e]
return ok
}
// Len returns the number of elements in the set.
func (s Set[T]) Len() int {
return len(s)
}
// AppendToSlice appends the elements in the set to a slice.
func (s Set[T]) AppendToSlice(slice []T) []T {
slice = slices.Grow(slice, s.Len())
for k := range s {
slice = append(slice, k)
}
return slice
}
// Clear
func (s Set[T]) Clear() {
clear(s)
}
// Range
func (s Set[T]) Range(f func(e T) bool) {
for k := range s {
if !f(k) {
break
}
}
}
// Transform transforms the set to a slice if the size is less than mixSetMinMapSize.
func (s Set[T]) Transform() ITransformSet[T] {
if len(s) > mixSetMinMapSize {
return s
}
set := NewSliceSet[T]()
for k := range s {
set.Add(k)
}
return set
}
type SliceSet[T comparable] struct {
cache [mixSetMaxSliceSize]T
elements []T
}
// NewSliceSet creates a new set.
func NewSliceSet[T comparable]() *SliceSet[T] {
var set SliceSet[T]
set.elements = set.cache[:0]
return &set
}
// Add adds an element to the set.
func (s *SliceSet[T]) Add(e ...T) {
for _, v := range e {
if s.Contains(v) {
continue
}
s.elements = append(s.elements, v)
}
}
// Contains checks if an element is in the set.
func (s *SliceSet[T]) Contains(e T) bool {
return s.find(e) != -1
}
// find returns the index of an element in the set.
func (s *SliceSet[T]) find(e T) int {
for i, v := range s.elements {
if v == e {
return i
}
}
return -1
}
// Remove removes an element from the set.
func (s *SliceSet[T]) Remove(e ...T) {
for _, v := range e {
s.remove(v)
}
}
// remove removes an element from the set.
func (s *SliceSet[T]) remove(e T) {
idx := s.find(e)
if idx == -1 {
return
}
// swap the element to the end of the slice and truncate the slice
lastIdx := len(s.elements) - 1
var empty T
s.elements[idx], s.elements[lastIdx] = s.elements[lastIdx], empty
s.elements = s.elements[:lastIdx]
}
// Len returns the number of elements in the set.
func (s *SliceSet[T]) Len() int {
return len(s.elements)
}
// AppendToSlice appends the elements in the set to a slice.
func (s *SliceSet[T]) AppendToSlice(slice []T) []T {
slice = slices.Grow(slice, s.Len())
return append(slice, s.elements...)
}
// Clear
func (s *SliceSet[T]) Clear() {
clear(s.elements)
s.elements = s.cache[:0]
}
// Range
func (s *SliceSet[T]) Range(f func(e T) bool) {
for _, v := range s.elements {
if !f(v) {
break
}
}
}
// transform transforms the set to a map if the size is greater than mixSetMaxSliceSize.
func (s *SliceSet[T]) Transform() ITransformSet[T] {
if s.Len() < mixSetMaxSliceSize {
return s
}
set := NewSet[T](uint(s.Len()))
set.Add(s.elements...)
return set
}
// MixSet is a set that uses a slice for small sets and a map for large sets.
type MixSet[T comparable] struct{ ITransformSet[T] }
// NewMixSet creates a new set.
func NewMixSet[T comparable]() *MixSet[T] {
s := InitMixSet[T]()
return &s
}
// InitMixSet creates a new set with an initial capacity.
func InitMixSet[T comparable]() MixSet[T] {
return MixSet[T]{ITransformSet: NewSliceSet[T]()}
}
// Transform transforms the set to a map if the size is greater than mixSetMaxSliceSize.
// or to a slice if the size is less than mixSetMinMapSize.
func (s *MixSet[T]) Transform() ITransformSet[T] {
s.ITransformSet = s.ITransformSet.Transform()
return s
}
// Add adds an element to the set.
func (s *MixSet[T]) Add(e ...T) {
for _, v := range e {
s.add(v)
}
}
// add adds an element to the set.
func (s *MixSet[T]) add(e T) {
s.ITransformSet.Add(e)
s.Transform()
}
// Remove removes an element from the set.
func (s *MixSet[T]) Remove(e ...T) {
for _, v := range e {
s.remove(v)
}
}
// remove removes an element from the set.
func (s *MixSet[T]) remove(e T) {
s.ITransformSet.Remove(e)
s.Transform()
}
// ISet is a set interface.
type ISet[T comparable] interface {
Add(e ...T)
Remove(e ...T)
Contains(e T) bool
Len() int
AppendToSlice(slice []T) []T
Clear()
Range(f func(e T) bool)
}
// ITransformSet is a set interface that can transform to another set.
type ITransformSet[T comparable] interface {
ISet[T]
Transform() ITransformSet[T]
}
var (
_ ISet[*Subscription] = Set[*Subscription]{}
_ ISet[*Subscription] = &SliceSet[*Subscription]{}
_ ISet[*Subscription] = &MixSet[*Subscription]{}
)