@@ -14,29 +14,28 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package sets
17
+ package set
18
18
19
- import "sort"
20
-
21
- type Ordered interface {
22
- int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | string
23
- }
19
+ import (
20
+ "sort"
21
+ )
24
22
25
23
// Empty is public since it is used by some internal API objects for conversions between external
26
24
// string arrays and internal sets, and conversion logic requires public types today.
27
25
type Empty struct {}
28
26
27
+ // Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption.
29
28
type Set [E Ordered ] map [E ]Empty
30
29
31
- // NewSet creates a new set.
32
- func NewSet [E Ordered ](items ... E ) Set [E ] {
30
+ // New creates a new set.
31
+ func New [E Ordered ](items ... E ) Set [E ] {
33
32
ss := Set [E ]{}
34
33
ss .Insert (items ... )
35
34
return ss
36
35
}
37
36
38
- // NewSetFromMapKeys creates a Set[E] from a keys of a map[E](? extends interface{}).
39
- func NewSetFromMapKeys [E Ordered , A any ](theMap map [E ]A ) Set [E ] {
37
+ // KeySet creates a Set[E] from a keys of a map[E](? extends interface{}).
38
+ func KeySet [E Ordered , A any ](theMap map [E ]A ) Set [E ] {
40
39
ret := Set [E ]{}
41
40
for key := range theMap {
42
41
ret .Insert (key )
@@ -86,10 +85,16 @@ func (s Set[E]) HasAny(items ...E) bool {
86
85
return false
87
86
}
88
87
88
+ // Union returns a new set which includes items in either s1 or s2.
89
+ // For example:
90
+ // s1 = {a1, a2}
91
+ // s2 = {a3, a4}
92
+ // s1.Union(s2) = {a1, a2, a3, a4}
93
+ // s2.Union(s1) = {a1, a2, a3, a4}
89
94
func (s Set [E ]) Union (s2 Set [E ]) Set [E ] {
90
95
result := Set [E ]{}
91
- result .Insert (s .List ()... )
92
- result .Insert (s2 .List ()... )
96
+ result .Insert (s .UnsortedList ()... )
97
+ result .Insert (s2 .UnsortedList ()... )
93
98
return result
94
99
}
95
100
@@ -98,6 +103,11 @@ func (s Set[E]) Len() int {
98
103
return len (s )
99
104
}
100
105
106
+ // Intersection returns a new set which includes the item in BOTH s1 and s2
107
+ // For example:
108
+ // s1 = {a1, a2}
109
+ // s2 = {a2, a3}
110
+ // s1.Intersection(s2) = {a2}
101
111
func (s Set [E ]) Intersection (s2 Set [E ]) Set [E ] {
102
112
var walk , other Set [E ]
103
113
result := Set [E ]{}
@@ -144,7 +154,6 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] {
144
154
145
155
// Equal returns true if and only if s1 is equal (as a set) to s2.
146
156
// Two sets are equal if their membership is identical.
147
- // (In practice, this means same elements, order doesn't matter)
148
157
func (s Set [E ]) Equal (s2 Set [E ]) bool {
149
158
return s .Len () == s .Len () && s .IsSuperset (s2 )
150
159
}
@@ -157,8 +166,8 @@ func (s sortableSlice[E]) Len() int {
157
166
func (s sortableSlice [E ]) Less (i , j int ) bool { return s [i ] < s [j ] }
158
167
func (s sortableSlice [E ]) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
159
168
160
- // List returns the contents as a sorted int slice.
161
- func (s Set [E ]) List () []E {
169
+ // SortedList returns the contents as a sorted slice.
170
+ func (s Set [E ]) SortedList () []E {
162
171
res := make (sortableSlice [E ], 0 , s .Len ())
163
172
for key := range s {
164
173
res = append (res , key )
@@ -169,7 +178,7 @@ func (s Set[E]) List() []E {
169
178
170
179
// UnsortedList returns the slice with contents in random order.
171
180
func (s Set [E ]) UnsortedList () []E {
172
- res := make (sortableSlice [ E ] , 0 , len (s ))
181
+ res := make ([] E , 0 , len (s ))
173
182
for key := range s {
174
183
res = append (res , key )
175
184
}
@@ -185,3 +194,36 @@ func (s Set[E]) PopAny() (E, bool) {
185
194
var zeroValue E
186
195
return zeroValue , false
187
196
}
197
+
198
+ // Clone returns a new set which is a copy of the current set.
199
+ func (s Set [T ]) Clone () Set [T ] {
200
+ result := make (Set [T ], len (s ))
201
+ for key := range s {
202
+ result .Insert (key )
203
+ }
204
+ return result
205
+ }
206
+
207
+ // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
208
+ // For example:
209
+ // s1 = {a1, a2, a3}
210
+ // s2 = {a1, a2, a4, a5}
211
+ // s1.SymmetricDifference(s2) = {a3, a4, a5}
212
+ // s2.SymmetricDifference(s1) = {a3, a4, a5}
213
+ func (s1 Set [T ]) SymmetricDifference (s2 Set [T ]) Set [T ] {
214
+ return s1 .Difference (s2 ).Union (s2 .Difference (s1 ))
215
+ }
216
+
217
+ // Clear empties the set.
218
+ // It is preferable to replace the set with a newly constructed set,
219
+ // but not all callers can do that (when there are other references to the map).
220
+ // In some cases the set *won't* be fully cleared, e.g. a Set[float32] containing NaN
221
+ // can't be cleared because NaN can't be removed.
222
+ // For sets containing items of a type that is reflexive for ==,
223
+ // this is optimized to a single call to runtime.mapclear().
224
+ func (s Set [T ]) Clear () Set [T ] {
225
+ for key := range s {
226
+ delete (s , key )
227
+ }
228
+ return s
229
+ }
0 commit comments