From 528289676d73cbf2e985675e3f307b50b2512ee9 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Thu, 23 Nov 2023 15:14:41 +0100 Subject: [PATCH] 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 --- set/ordered.go | 53 ------------------------------------------------- set/set.go | 11 +++++----- set/set_test.go | 3 ++- 3 files changed, 8 insertions(+), 59 deletions(-) delete mode 100644 set/ordered.go diff --git a/set/ordered.go b/set/ordered.go deleted file mode 100644 index 2b2c11fc..00000000 --- a/set/ordered.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -Copyright 2023 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package set - -// ordered is a constraint that permits any ordered type: any type -// that supports the operators < <= >= >. -// If future releases of Go add new ordered types, -// this constraint will be modified to include them. -type ordered interface { - integer | float | ~string -} - -// integer is a constraint that permits any integer type. -// If future releases of Go add new predeclared integer types, -// this constraint will be modified to include them. -type integer interface { - signed | unsigned -} - -// float is a constraint that permits any floating-point type. -// If future releases of Go add new predeclared floating-point types, -// this constraint will be modified to include them. -type float interface { - ~float32 | ~float64 -} - -// signed is a constraint that permits any signed integer type. -// If future releases of Go add new predeclared signed integer types, -// this constraint will be modified to include them. -type signed interface { - ~int | ~int8 | ~int16 | ~int32 | ~int64 -} - -// unsigned is a constraint that permits any unsigned integer type. -// If future releases of Go add new predeclared unsigned integer types, -// this constraint will be modified to include them. -type unsigned interface { - ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr -} diff --git a/set/set.go b/set/set.go index fc25b2fe..8e42d941 100644 --- a/set/set.go +++ b/set/set.go @@ -17,6 +17,7 @@ limitations under the License. package set import ( + "cmp" "sort" ) @@ -24,18 +25,18 @@ import ( // string arrays and internal sets, and conversion logic requires public types today. type Empty struct{} -// Set is a set of the same type elements, implemented via map[ordered]struct{} for minimal memory consumption. -type Set[E ordered] map[E]Empty +// Set is a set of the same type elements, implemented via map[cmp.Ordered]struct{} for minimal memory consumption. +type Set[E cmp.Ordered] map[E]Empty // New creates a new set. -func New[E ordered](items ...E) Set[E] { +func New[E cmp.Ordered](items ...E) Set[E] { ss := Set[E]{} ss.Insert(items...) return ss } // KeySet creates a Set[E] from a keys of a map[E](? extends interface{}). -func KeySet[E ordered, A any](theMap map[E]A) Set[E] { +func KeySet[E cmp.Ordered, A any](theMap map[E]A) Set[E] { ret := Set[E]{} for key := range theMap { ret.Insert(key) @@ -158,7 +159,7 @@ func (s Set[E]) Equal(s2 Set[E]) bool { return s.Len() == s2.Len() && s.IsSuperset(s2) } -type sortableSlice[E ordered] []E +type sortableSlice[E cmp.Ordered] []E func (s sortableSlice[E]) Len() int { return len(s) diff --git a/set/set_test.go b/set/set_test.go index b44ed15b..b4a08983 100644 --- a/set/set_test.go +++ b/set/set_test.go @@ -17,6 +17,7 @@ limitations under the License. package set import ( + "cmp" "reflect" "testing" ) @@ -369,7 +370,7 @@ func TestSetClearInSeparateFunction(t *testing.T) { } } -func clearSetAndAdd[T ordered](s Set[T], a T) { +func clearSetAndAdd[T cmp.Ordered](s Set[T], a T) { s.Clear() s.Insert(a) }