Skip to content

Commit 3d9ba3c

Browse files
committed
go back to comparable for base type, make Sort a function as a result
1 parent 6e71e60 commit 3d9ba3c

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

sets.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"golang.org/x/exp/constraints"
1616
)
1717

18-
type Set[T constraints.Ordered] map[T]struct{}
18+
type Set[T comparable] map[T]struct{}
1919

2020
// SetFromSlice constructs a Set from a slice.
21-
func FromSlice[T constraints.Ordered](items []T) Set[T] {
21+
func FromSlice[T comparable](items []T) Set[T] {
2222
// best pre-allocation if there are no duplicates
2323
res := make(map[T]struct{}, len(items))
2424
for _, item := range items {
@@ -52,7 +52,7 @@ func (s Set[T]) Remove(item ...T) {
5252
}
5353
}
5454

55-
func New[T constraints.Ordered](item ...T) Set[T] {
55+
func New[T comparable](item ...T) Set[T] {
5656
res := make(Set[T], len(item))
5757
res.Add(item...)
5858
return res
@@ -70,7 +70,7 @@ func (s Set[T]) String() string {
7070
// RemoveCommon removes elements from both sets that are in both,
7171
// leaving only the delta. Useful for Notifier on Set so that
7272
// oldValue has what has been removed and newValue has what has been added.
73-
func RemoveCommon[T constraints.Ordered](a, b Set[T]) {
73+
func RemoveCommon[T comparable](a, b Set[T]) {
7474
if len(a) > len(b) {
7575
a, b = b, a
7676
}
@@ -82,8 +82,8 @@ func RemoveCommon[T constraints.Ordered](a, b Set[T]) {
8282
}
8383
}
8484

85-
func (s Set[T]) Sorted() []T {
86-
keys := make([]T, 0, len(s))
85+
func Sort[Q constraints.Ordered](s Set[Q]) []Q {
86+
keys := make([]Q, 0, len(s))
8787
for k := range s {
8888
keys = append(keys, k)
8989
}

sets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestArrayToSet(t *testing.T) {
1919
a := []string{"z", "a", "c", "b"}
2020
s := sets.FromSlice(a)
2121
assert.Equal(t, "a,b,c,z", s.String())
22-
assert.Equal(t, s.Sorted(), []string{"a", "b", "c", "z"})
22+
assert.Equal(t, sets.Sort(s), []string{"a", "b", "c", "z"})
2323
}
2424

2525
func TestRemoveCommon(t *testing.T) {

0 commit comments

Comments
 (0)