@@ -14,29 +14,28 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- package sets
17+ package set
1818
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+ )
2422
2523// Empty is public since it is used by some internal API objects for conversions between external
2624// string arrays and internal sets, and conversion logic requires public types today.
2725type Empty struct {}
2826
27+ // Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption.
2928type Set [E Ordered ] map [E ]Empty
3029
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 ] {
3332 ss := Set [E ]{}
3433 ss .Insert (items ... )
3534 return ss
3635}
3736
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 ] {
4039 ret := Set [E ]{}
4140 for key := range theMap {
4241 ret .Insert (key )
@@ -86,10 +85,16 @@ func (s Set[E]) HasAny(items ...E) bool {
8685 return false
8786}
8887
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}
8994func (s Set [E ]) Union (s2 Set [E ]) Set [E ] {
9095 result := Set [E ]{}
91- result .Insert (s .List ()... )
92- result .Insert (s2 .List ()... )
96+ result .Insert (s .UnsortedList ()... )
97+ result .Insert (s2 .UnsortedList ()... )
9398 return result
9499}
95100
@@ -98,6 +103,11 @@ func (s Set[E]) Len() int {
98103 return len (s )
99104}
100105
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}
101111func (s Set [E ]) Intersection (s2 Set [E ]) Set [E ] {
102112 var walk , other Set [E ]
103113 result := Set [E ]{}
@@ -144,7 +154,6 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] {
144154
145155// Equal returns true if and only if s1 is equal (as a set) to s2.
146156// Two sets are equal if their membership is identical.
147- // (In practice, this means same elements, order doesn't matter)
148157func (s Set [E ]) Equal (s2 Set [E ]) bool {
149158 return s .Len () == s .Len () && s .IsSuperset (s2 )
150159}
@@ -157,8 +166,8 @@ func (s sortableSlice[E]) Len() int {
157166func (s sortableSlice [E ]) Less (i , j int ) bool { return s [i ] < s [j ] }
158167func (s sortableSlice [E ]) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
159168
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 {
162171 res := make (sortableSlice [E ], 0 , s .Len ())
163172 for key := range s {
164173 res = append (res , key )
@@ -169,7 +178,7 @@ func (s Set[E]) List() []E {
169178
170179// UnsortedList returns the slice with contents in random order.
171180func (s Set [E ]) UnsortedList () []E {
172- res := make (sortableSlice [ E ] , 0 , len (s ))
181+ res := make ([] E , 0 , len (s ))
173182 for key := range s {
174183 res = append (res , key )
175184 }
@@ -185,3 +194,36 @@ func (s Set[E]) PopAny() (E, bool) {
185194 var zeroValue E
186195 return zeroValue , false
187196}
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