@@ -145,6 +145,9 @@ type Set[T comparable] interface {
145145 // Remove removes a single element from the set.
146146 Remove (i T )
147147
148+ // RemoveAll removes multiple elements from the set.
149+ RemoveAll (i ... T )
150+
148151 // String provides a convenient string representation
149152 // of the current state of the set.
150153 String () string
@@ -182,27 +185,41 @@ type Set[T comparable] interface {
182185// NewSet creates and returns a new set with the given elements.
183186// Operations on the resulting set are thread-safe.
184187func NewSet [T comparable ](vals ... T ) Set [T ] {
185- s := newThreadSafeSet [T ]()
188+ s := newThreadSafeSetWithSize [T ](len ( vals ) )
186189 for _ , item := range vals {
187190 s .Add (item )
188191 }
189192 return s
190193}
191194
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+
192202// NewThreadUnsafeSet creates and returns a new set with the given elements.
193203// Operations on the resulting set are not thread-safe.
194204func NewThreadUnsafeSet [T comparable ](vals ... T ) Set [T ] {
195- s := newThreadUnsafeSet [T ]()
205+ s := newThreadUnsafeSetWithSize [T ](len ( vals ) )
196206 for _ , item := range vals {
197207 s .Add (item )
198208 }
199209 return s
200210}
201211
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.
203220// Operations on the resulting set are thread-safe.
204221func NewSetFromMapKeys [T comparable , V any ](val map [T ]V ) Set [T ] {
205- s := NewSet [T ]()
222+ s := NewSetWithSize [T ](len ( val ) )
206223
207224 for k := range val {
208225 s .Add (k )
@@ -211,10 +228,10 @@ func NewSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
211228 return s
212229}
213230
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.
215232// Operations on the resulting set are not thread-safe.
216233func NewThreadUnsafeSetFromMapKeys [T comparable , V any ](val map [T ]V ) Set [T ] {
217- s := NewThreadUnsafeSet [T ]()
234+ s := NewThreadUnsafeSetWithSize [T ](len ( val ) )
218235
219236 for k := range val {
220237 s .Add (k )
0 commit comments