Skip to content

Commit

Permalink
cpuset: Make 'ToSlice*' methods look like 'set' methods
Browse files Browse the repository at this point in the history
In 'set', conversions to slice are done also, but with different names:

ToSliceNoSort() -> UnsortedList()
ToSlice() -> List()

Reimplement List() in terms of UnsortedList to save some duplication.
  • Loading branch information
iancoolidge committed Jan 6, 2023
1 parent 0ac3d42 commit 8370a8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,17 @@ func (s CPUSet) Difference(s2 CPUSet) CPUSet {
return s.FilterNot(func(cpu int) bool { return s2.Contains(cpu) })
}

// ToSlice returns a slice of integers that contains all elements from
// this set.
func (s CPUSet) ToSlice() []int {
result := make([]int, 0, len(s.elems))
for cpu := range s.elems {
result = append(result, cpu)
}
// List returns a slice of integers that contains all elements from
// this set. The list is sorted.
func (s CPUSet) List() []int {
result := s.UnsortedList()
sort.Ints(result)
return result
}

// ToSliceNoSort returns a slice of integers that contains all elements from
// UnsortedList returns a slice of integers that contains all elements from
// this set.
func (s CPUSet) ToSliceNoSort() []int {
func (s CPUSet) UnsortedList() []int {
result := make([]int, 0, len(s.elems))
for cpu := range s.elems {
result = append(result, cpu)
Expand All @@ -192,7 +189,7 @@ func (s CPUSet) String() string {
return ""
}

elems := s.ToSlice()
elems := s.List()

type rng struct {
start int
Expand Down
4 changes: 2 additions & 2 deletions cpuset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestCPUSetDifference(t *testing.T) {
}
}

func TestCPUSetToSlice(t *testing.T) {
func TestCPUSetList(t *testing.T) {
testCases := []struct {
set CPUSet
expected []int
Expand All @@ -285,7 +285,7 @@ func TestCPUSetToSlice(t *testing.T) {
}

for _, c := range testCases {
result := c.set.ToSlice()
result := c.set.List()
if !reflect.DeepEqual(result, c.expected) {
t.Fatalf("expected set as slice to be [%v] (got [%v]), s: [%v]", c.expected, result, c.set)
}
Expand Down

0 comments on commit 8370a8e

Please sign in to comment.