From 3d9ba3cc2497ef9527fc616096c7b71117a50c17 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Mon, 20 Feb 2023 19:51:24 -0800 Subject: [PATCH] go back to comparable for base type, make Sort a function as a result --- sets.go | 12 ++++++------ sets_test.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sets.go b/sets.go index 65b2e06..f48a02d 100644 --- a/sets.go +++ b/sets.go @@ -15,10 +15,10 @@ import ( "golang.org/x/exp/constraints" ) -type Set[T constraints.Ordered] map[T]struct{} +type Set[T comparable] map[T]struct{} // SetFromSlice constructs a Set from a slice. -func FromSlice[T constraints.Ordered](items []T) Set[T] { +func FromSlice[T comparable](items []T) Set[T] { // best pre-allocation if there are no duplicates res := make(map[T]struct{}, len(items)) for _, item := range items { @@ -52,7 +52,7 @@ func (s Set[T]) Remove(item ...T) { } } -func New[T constraints.Ordered](item ...T) Set[T] { +func New[T comparable](item ...T) Set[T] { res := make(Set[T], len(item)) res.Add(item...) return res @@ -70,7 +70,7 @@ func (s Set[T]) String() string { // RemoveCommon removes elements from both sets that are in both, // leaving only the delta. Useful for Notifier on Set so that // oldValue has what has been removed and newValue has what has been added. -func RemoveCommon[T constraints.Ordered](a, b Set[T]) { +func RemoveCommon[T comparable](a, b Set[T]) { if len(a) > len(b) { a, b = b, a } @@ -82,8 +82,8 @@ func RemoveCommon[T constraints.Ordered](a, b Set[T]) { } } -func (s Set[T]) Sorted() []T { - keys := make([]T, 0, len(s)) +func Sort[Q constraints.Ordered](s Set[Q]) []Q { + keys := make([]Q, 0, len(s)) for k := range s { keys = append(keys, k) } diff --git a/sets_test.go b/sets_test.go index 43ec4f0..fa32ef0 100644 --- a/sets_test.go +++ b/sets_test.go @@ -19,7 +19,7 @@ func TestArrayToSet(t *testing.T) { a := []string{"z", "a", "c", "b"} s := sets.FromSlice(a) assert.Equal(t, "a,b,c,z", s.String()) - assert.Equal(t, s.Sorted(), []string{"a", "b", "c", "z"}) + assert.Equal(t, sets.Sort(s), []string{"a", "b", "c", "z"}) } func TestRemoveCommon(t *testing.T) {