@@ -24,12 +24,13 @@ import (
2424// string arrays and internal sets, and conversion logic requires public types today.
2525type Empty struct {}
2626
27- // Set is a set of the same type elements, implemented via map[ordered ]struct{} for minimal memory consumption.
28- type Set [E ordered ] map [E ]Empty
27+ // Set is a set of the same type elements, implemented via map[comparable ]struct{} for minimal memory consumption.
28+ type Set [E comparable ] map [E ]Empty
2929
3030// New creates a new set.
31- func New [E ordered ](items ... E ) Set [E ] {
32- ss := Set [E ]{}
31+ // NOTE: type param must be explicitly instantiated if given items are empty.
32+ func New [E comparable ](items ... E ) Set [E ] {
33+ ss := make (Set [E ], len (items ))
3334 ss .Insert (items ... )
3435 return ss
3536}
@@ -43,23 +44,24 @@ func (s Set[T]) Clear() Set[T] {
4344}
4445
4546// KeySet creates a Set[E] from a keys of a map[E](? extends interface{}).
46- func KeySet [E ordered , A any ](theMap map [E ]A ) Set [E ] {
47+ // If the value passed in is not actually a map, this will panic.
48+ func KeySet [E comparable , A any ](theMap map [E ]A ) Set [E ] {
4749 ret := Set [E ]{}
4850 for key := range theMap {
4951 ret .Insert (key )
5052 }
5153 return ret
5254}
5355
54- // Insert adds items to the set.
56+ // Insert adds the given items to the set.
5557func (s Set [E ]) Insert (items ... E ) Set [E ] {
5658 for _ , item := range items {
5759 s [item ] = Empty {}
5860 }
5961 return s
6062}
6163
62- // Delete removes all items from the set.
64+ // Delete removes the given items from the set.
6365func (s Set [E ]) Delete (items ... E ) Set [E ] {
6466 for _ , item := range items {
6567 delete (s , item )
@@ -93,16 +95,17 @@ func (s Set[E]) HasAny(items ...E) bool {
9395 return false
9496}
9597
96- // Union returns a new set which includes items in either s1 or s2.
98+ // Union returns a new set which includes items in either s or s2.
9799// For example:
98- // s1 = {a1, a2}
100+ // s = {a1, a2}
99101// s2 = {a3, a4}
100- // s1 .Union(s2) = {a1, a2, a3, a4}
101- // s2.Union(s1 ) = {a1, a2, a3, a4}
102+ // s .Union(s2) = {a1, a2, a3, a4}
103+ // s2.Union(s ) = {a1, a2, a3, a4}
102104func (s Set [E ]) Union (s2 Set [E ]) Set [E ] {
103- result := Set [E ]{}
104- result .Insert (s .UnsortedList ()... )
105- result .Insert (s2 .UnsortedList ()... )
105+ result := s .Clone ()
106+ for k2 := range s2 {
107+ result .Insert (k2 )
108+ }
106109 return result
107110}
108111
@@ -111,11 +114,11 @@ func (s Set[E]) Len() int {
111114 return len (s )
112115}
113116
114- // Intersection returns a new set which includes the item in BOTH s1 and s2
117+ // Intersection returns a new set which includes the item in BOTH s and s2
115118// For example:
116- // s1 = {a1, a2}
119+ // s = {a1, a2}
117120// s2 = {a2, a3}
118- // s1 .Intersection(s2) = {a2}
121+ // s .Intersection(s2) = {a2}
119122func (s Set [E ]) Intersection (s2 Set [E ]) Set [E ] {
120123 var walk , other Set [E ]
121124 result := Set [E ]{}
@@ -134,7 +137,7 @@ func (s Set[E]) Intersection(s2 Set[E]) Set[E] {
134137 return result
135138}
136139
137- // IsSuperset returns true if and only if s1 is a superset of s2.
140+ // IsSuperset returns true if and only if s is a superset of s2.
138141func (s Set [E ]) IsSuperset (s2 Set [E ]) bool {
139142 for item := range s2 {
140143 if ! s .Has (item ) {
@@ -146,10 +149,10 @@ func (s Set[E]) IsSuperset(s2 Set[E]) bool {
146149
147150// Difference returns a set of objects that are not in s2
148151// For example:
149- // s1 = {a1, a2, a3}
152+ // s = {a1, a2, a3}
150153// s2 = {a1, a2, a4, a5}
151- // s1 .Difference(s2) = {a3}
152- // s2.Difference(s1 ) = {a4, a5}
154+ // s .Difference(s2) = {a3}
155+ // s2.Difference(s ) = {a4, a5}
153156func (s Set [E ]) Difference (s2 Set [E ]) Set [E ] {
154157 result := Set [E ]{}
155158 for key := range s {
@@ -160,7 +163,7 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] {
160163 return result
161164}
162165
163- // Equal returns true if and only if s1 is equal (as a set) to s2.
166+ // Equal returns true if and only if s is equal (as a set) to s2.
164167// Two sets are equal if their membership is identical.
165168func (s Set [E ]) Equal (s2 Set [E ]) bool {
166169 return s .Len () == s2 .Len () && s .IsSuperset (s2 )
@@ -174,9 +177,12 @@ func (s sortableSlice[E]) Len() int {
174177func (s sortableSlice [E ]) Less (i , j int ) bool { return s [i ] < s [j ] }
175178func (s sortableSlice [E ]) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
176179
177- // SortedList returns the contents as a sorted slice.
178- func (s Set [E ]) SortedList () []E {
179- res := make (sortableSlice [E ], 0 , s .Len ())
180+ // List returns the contents as a sorted T slice.
181+ //
182+ // This is a separate function and not a method because not all types supported
183+ // by Generic are ordered and only those can be sorted.
184+ func List [E ordered ](s Set [E ]) []E {
185+ res := make (sortableSlice [E ], 0 , len (s ))
180186 for key := range s {
181187 res = append (res , key )
182188 }
@@ -214,10 +220,10 @@ func (s Set[T]) Clone() Set[T] {
214220
215221// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
216222// For example:
217- // s1 = {a1, a2, a3}
223+ // s = {a1, a2, a3}
218224// s2 = {a1, a2, a4, a5}
219- // s1 .SymmetricDifference(s2) = {a3, a4, a5}
220- // s2.SymmetricDifference(s1 ) = {a3, a4, a5}
225+ // s .SymmetricDifference(s2) = {a3, a4, a5}
226+ // s2.SymmetricDifference(s ) = {a3, a4, a5}
221227func (s Set [T ]) SymmetricDifference (s2 Set [T ]) Set [T ] {
222228 return s .Difference (s2 ).Union (s2 .Difference (s ))
223229}
0 commit comments