Skip to content

Commit 7e6f82f

Browse files
committed
Use cmp.Ordered instead of a private type
Go 1.21 introduced a language-maintained constraint permitting any ordered types, use that instead of the private version defined in sets. Signed-off-by: Stephen Kitt <skitt@redhat.com>
1 parent b8788ab commit 7e6f82f

3 files changed

Lines changed: 8 additions & 59 deletions

File tree

set/ordered.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

set/set.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ limitations under the License.
1717
package set
1818

1919
import (
20+
"cmp"
2021
"sort"
2122
)
2223

2324
// Empty is public since it is used by some internal API objects for conversions between external
2425
// string arrays and internal sets, and conversion logic requires public types today.
2526
type Empty struct{}
2627

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
28+
// Set is a set of the same type elements, implemented via map[cmp.Ordered]struct{} for minimal memory consumption.
29+
type Set[E cmp.Ordered] map[E]Empty
2930

3031
// New creates a new set.
31-
func New[E ordered](items ...E) Set[E] {
32+
func New[E cmp.Ordered](items ...E) Set[E] {
3233
ss := Set[E]{}
3334
ss.Insert(items...)
3435
return ss
@@ -43,7 +44,7 @@ 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+
func KeySet[E cmp.Ordered, A any](theMap map[E]A) Set[E] {
4748
ret := Set[E]{}
4849
for key := range theMap {
4950
ret.Insert(key)
@@ -166,7 +167,7 @@ func (s Set[E]) Equal(s2 Set[E]) bool {
166167
return s.Len() == s2.Len() && s.IsSuperset(s2)
167168
}
168169

169-
type sortableSlice[E ordered] []E
170+
type sortableSlice[E cmp.Ordered] []E
170171

171172
func (s sortableSlice[E]) Len() int {
172173
return len(s)

set/set_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package set
1818

1919
import (
20+
"cmp"
2021
"reflect"
2122
"testing"
2223
)
@@ -369,7 +370,7 @@ func TestSetClearInSeparateFunction(t *testing.T) {
369370
}
370371
}
371372

372-
func clearSetAndAdd[T ordered](s Set[T], a T) {
373+
func clearSetAndAdd[T cmp.Ordered](s Set[T], a T) {
373374
s.Clear()
374375
s.Insert(a)
375376
}

0 commit comments

Comments
 (0)