Skip to content

Commit

Permalink
use new custom type
Browse files Browse the repository at this point in the history
use a new custom type CPUSet to make the code behave more
like the kubernetes original package.

Signed-off-by: Francesco Romani <fromani@redhat.com>
  • Loading branch information
ffromani committed May 6, 2024
1 parent 854e87f commit 6ee9d8c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
28 changes: 25 additions & 3 deletions cpuset_empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,29 @@

package cpuset

// Empty returns a empty cpuset, a slice of ints
func Empty() []int {
return []int{}
import "sort"

type CPUSet map[int]struct{}

// Empty returns a empty cpuset
func Empty() CPUSet {
return make(CPUSet)
}

func (cs CPUSet) Len() int {
return len(cs)
}

func (cs CPUSet) UnsortedList() []int {
r := make([]int, 0, len(cs))
for k := range cs {
r = append(r, k)
}
return r
}

func (cs CPUSet) List() []int {
r := cs.UnsortedList()
sort.Ints(r)
return r
}
8 changes: 3 additions & 5 deletions cpuset_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package cpuset

import (
"sort"
"strconv"
"strings"
)

// Parse takes a string representing a cpuset definition, and returns as sorted slice of ints
func Parse(s string) ([]int, error) {
func Parse(s string) (CPUSet, error) {
cpus := Empty()
if len(s) == 0 {
return cpus, nil
Expand All @@ -37,7 +36,7 @@ func Parse(s string) ([]int, error) {
if err != nil {
return cpus, err
}
cpus = append(cpus, cpuid)
cpus[cpuid] = struct{}{}
continue
}

Expand All @@ -52,10 +51,9 @@ func Parse(s string) ([]int, error) {
return cpus, err
}
for cpuid := cpuBegin; cpuid <= cpuEnd; cpuid++ {
cpus = append(cpus, cpuid)
cpus[cpuid] = struct{}{}
}
}

sort.Ints(cpus)
return cpus, nil
}
15 changes: 7 additions & 8 deletions cpuset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func TestParseEmpty(t *testing.T) {
func TestParseRange(t *testing.T) {
testCases := []tcase{
{"0,1,2,3", []int{0, 1, 2, 3}},
// we don't elide dupes
{"0,1,1,3", []int{0, 1, 1, 3}},
{"0,1,3", []int{0, 1, 3}},
}
for _, testCase := range testCases {
testCase.CheckParseSlices(t)
Expand Down Expand Up @@ -90,19 +89,19 @@ func TestParseRangeIntervalMalformed(t *testing.T) {
}
}

func mustParse(t *testing.T, s string) []int {
func mustParse(t *testing.T, s string) CPUSet {
cpus, err := Parse(s)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
return cpus
}

func checkEmpty(t *testing.T, cpus []int) {
func checkEmpty(t *testing.T, cpus CPUSet) {
if cpus == nil {
t.Errorf("empty must not be nil")
}
if len(cpus) != 0 {
if cpus.Len() != 0 {
t.Errorf("empty must have zero length")
}
}
Expand All @@ -113,22 +112,22 @@ type tcase struct {
}

func (tc tcase) CheckParseSlices(t *testing.T) {
cpus := mustParse(t, tc.s)
cpus := mustParse(t, tc.s).List()
if !reflect.DeepEqual(cpus, tc.v) {
t.Errorf("slices differ: expected=%v cpus=%v", tc.v, cpus)
}
}

func (tc tcase) CheckUnparseSlices(t *testing.T) {
cpus := Unparse(tc.v)
cpus := unparseInts(tc.v...)
if tc.s != cpus {
t.Errorf("strings differ: expected=%q cpus=%q", tc.s, cpus)
}
}

func (tc tcase) CheckParseUnparseSlices(t *testing.T) {
cpus := mustParse(t, tc.s)
cpuString := Unparse(cpus)
cpuString := unparseInts(cpus.List()...)
if cpuString != tc.s {
t.Errorf("parse/unparse mismatch: expected=%s cpuString=%s cpus=%q", tc.s, cpuString, cpus)
}
Expand Down
26 changes: 10 additions & 16 deletions cpuset_unparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package cpuset

import (
"fmt"
"sort"
"strings"
)

// Unparse takes a cpuset as (unsorted) slice of ints and returns a representing cpuset definition
func Unparse(v []int) string {
cpus := sorted(v)
func Unparse(cs CPUSet) string {
return unparseInts(cs.List()...)
}

func unparseInts(cpus ...int) string {
num := len(cpus)

if num == 0 {
Expand All @@ -34,13 +35,6 @@ func Unparse(v []int) string {
return fmt.Sprintf("%d", cpus[0])
}

makeAtom := func(cpus []int, begin, end int) string {
if begin < (end - 1) { // range
return fmt.Sprintf("%d-%d", cpus[begin], cpus[end-1])
}
return fmt.Sprintf("%d", cpus[begin])
}

var atoms []string
begin := 0 // of the potential range
end := 1 // of the potential range
Expand All @@ -59,9 +53,9 @@ func Unparse(v []int) string {
return strings.Join(atoms, ",")
}

func sorted(v []int) []int {
r := make([]int, len(v))
copy(r, v)
sort.Ints(r)
return r
func makeAtom(cpus []int, begin, end int) string {
if begin < (end - 1) { // range
return fmt.Sprintf("%d-%d", cpus[begin], cpus[end-1])
}
return fmt.Sprintf("%d", cpus[begin])
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/fromanirh/cpuset
module github.com/ffromani/cpuset

go 1.16
go 1.18

0 comments on commit 6ee9d8c

Please sign in to comment.