Skip to content

Commit

Permalink
Implement set.SortedSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
adracus committed Oct 7, 2022
1 parent edf8e27 commit e373722
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/onsi/gomega v1.20.2
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
golang.org/x/exp v0.0.0-20221006183845-316c7553db56
k8s.io/api v0.24.3
k8s.io/apiextensions-apiserver v0.24.3
k8s.io/apimachinery v0.24.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221006183845-316c7553db56 h1:BrYbdKcCNjLyrN6aKqXy4hPw9qGI8IATkj4EWv9Q+kQ=
golang.org/x/exp v0.0.0-20221006183845-316c7553db56/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
18 changes: 18 additions & 0 deletions set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package set

import (
"sort"

"golang.org/x/exp/constraints"
)

// Empty is the presence marker in a Set.
type Empty struct{}

Expand Down Expand Up @@ -165,3 +171,15 @@ func (s Set[E]) PopAny() (E, bool) {
func (s Set[E]) Len() int {
return len(s)
}

// SortedSlice takes a Set with constraints.Ordered items and returns a sorted slice of the items.
func SortedSlice[E constraints.Ordered](set Set[E]) []E {
res := make([]E, 0, len(set))
for item := range set {
res = append(res, item)
}
sort.Slice(res, func(i, j int) bool {
return res[i] < res[j]
})
return res
}
19 changes: 17 additions & 2 deletions set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
package set_test

import (
. "github.com/onmetal/controller-utils/set"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

. "github.com/onmetal/controller-utils/set"
)

var _ = Describe("Set", func() {
Expand Down Expand Up @@ -153,4 +152,20 @@ var _ = Describe("Set", func() {
Expect(nilSet.Len()).To(Equal(0))
})
})

Describe("Slice", func() {
It("should return a slice of the set items in arbitrary order", func() {
s := New[int](5, 1, 10, 7, 3)

Expect(s.Slice()).To(ConsistOf(1, 3, 5, 7, 10))
})
})

Describe("SortedSlice", func() {
It("should return an ordered slice of the items", func() {
s := New[int](5, 1, 10, 7, 3)

Expect(SortedSlice(s)).To(Equal([]int{1, 3, 5, 7, 10}))
})
})
})

0 comments on commit e373722

Please sign in to comment.