From 10cedcc11cf9b3497ebe1fbf7331a97e364589c4 Mon Sep 17 00:00:00 2001 From: Han Kang Date: Fri, 5 May 2023 13:11:43 -0700 Subject: [PATCH] address tim's comments --- set/ordered.go | 4 ++-- set/set.go | 12 ++++++------ set/set_test.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/set/ordered.go b/set/ordered.go index 6e5c6db3..2b2c11fc 100644 --- a/set/ordered.go +++ b/set/ordered.go @@ -16,11 +16,11 @@ limitations under the License. package set -// Ordered is a constraint that permits any ordered type: any type +// 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 { +type ordered interface { integer | float | ~string } diff --git a/set/set.go b/set/set.go index b115d0b7..172482cd 100644 --- a/set/set.go +++ b/set/set.go @@ -24,18 +24,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[comparable]struct{} for minimal memory consumption. -type Set[E Ordered] map[E]Empty +// 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 // New creates a new set. -func New[E Ordered](items ...E) Set[E] { +func New[E 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 ordered, A any](theMap map[E]A) Set[E] { ret := Set[E]{} for key := range theMap { ret.Insert(key) @@ -98,7 +98,7 @@ func (s Set[E]) Union(s2 Set[E]) Set[E] { return result } -// Len returns the size of the set. +// Len returns the number of elements in the set. func (s Set[E]) Len() int { return len(s) } @@ -158,7 +158,7 @@ func (s Set[E]) Equal(s2 Set[E]) bool { return s.Len() == s.Len() && s.IsSuperset(s2) } -type sortableSlice[E Ordered] []E +type sortableSlice[E ordered] []E func (s sortableSlice[E]) Len() int { return len(s) diff --git a/set/set_test.go b/set/set_test.go index c910ce0a..ea2d9d50 100644 --- a/set/set_test.go +++ b/set/set_test.go @@ -283,7 +283,7 @@ func TestStringIntersection(t *testing.T) { } } -func TestNewSetFromMapKeys(t *testing.T) { +func TestKeySet(t *testing.T) { m := map[string]string{ "hallo": "world", "goodbye": "and goodnight", @@ -359,7 +359,7 @@ func TestSetClearInSeparateFunction(t *testing.T) { } } -func clearSetAndAdd[T Ordered](s Set[T], a T) { +func clearSetAndAdd[T ordered](s Set[T], a T) { s.Clear() s.Insert(a) }