@@ -145,6 +145,9 @@ type Set[T comparable] interface {
145
145
// Remove removes a single element from the set.
146
146
Remove (i T )
147
147
148
+ // RemoveAll removes multiple elements from the set.
149
+ RemoveAll (i ... T )
150
+
148
151
// String provides a convenient string representation
149
152
// of the current state of the set.
150
153
String () string
@@ -182,27 +185,41 @@ type Set[T comparable] interface {
182
185
// NewSet creates and returns a new set with the given elements.
183
186
// Operations on the resulting set are thread-safe.
184
187
func NewSet [T comparable ](vals ... T ) Set [T ] {
185
- s := newThreadSafeSet [T ]()
188
+ s := newThreadSafeSetWithSize [T ](len ( vals ) )
186
189
for _ , item := range vals {
187
190
s .Add (item )
188
191
}
189
192
return s
190
193
}
191
194
195
+ // NewSetWithSize creates and returns a reference to an empty set with a specified
196
+ // capacity. Operations on the resulting set are thread-safe.
197
+ func NewSetWithSize [T comparable ](cardinality int ) Set [T ] {
198
+ s := newThreadSafeSetWithSize [T ](cardinality )
199
+ return s
200
+ }
201
+
192
202
// NewThreadUnsafeSet creates and returns a new set with the given elements.
193
203
// Operations on the resulting set are not thread-safe.
194
204
func NewThreadUnsafeSet [T comparable ](vals ... T ) Set [T ] {
195
- s := newThreadUnsafeSet [T ]()
205
+ s := newThreadUnsafeSetWithSize [T ](len ( vals ) )
196
206
for _ , item := range vals {
197
207
s .Add (item )
198
208
}
199
209
return s
200
210
}
201
211
202
- // Creates and returns a new set with the given keys of the map.
212
+ // NewThreadUnsafeSetWithSize creates and returns a reference to an empty set with
213
+ // a specified capacity. Operations on the resulting set are not thread-safe.
214
+ func NewThreadUnsafeSetWithSize [T comparable ](cardinality int ) Set [T ] {
215
+ s := newThreadUnsafeSetWithSize [T ](cardinality )
216
+ return s
217
+ }
218
+
219
+ // NewSetFromMapKeys creates and returns a new set with the given keys of the map.
203
220
// Operations on the resulting set are thread-safe.
204
221
func NewSetFromMapKeys [T comparable , V any ](val map [T ]V ) Set [T ] {
205
- s := NewSet [T ]()
222
+ s := NewSetWithSize [T ](len ( val ) )
206
223
207
224
for k := range val {
208
225
s .Add (k )
@@ -211,10 +228,10 @@ func NewSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
211
228
return s
212
229
}
213
230
214
- // Creates and returns a new set with the given keys of the map.
231
+ // NewThreadUnsafeSetFromMapKeys creates and returns a new set with the given keys of the map.
215
232
// Operations on the resulting set are not thread-safe.
216
233
func NewThreadUnsafeSetFromMapKeys [T comparable , V any ](val map [T ]V ) Set [T ] {
217
- s := NewThreadUnsafeSet [T ]()
234
+ s := NewThreadUnsafeSetWithSize [T ](len ( val ) )
218
235
219
236
for k := range val {
220
237
s .Add (k )
0 commit comments