From 6ee9d8c9f91829ec7864b440f02841358b8678ab Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Mon, 6 May 2024 10:52:19 +0200 Subject: [PATCH] use new custom type use a new custom type CPUSet to make the code behave more like the kubernetes original package. Signed-off-by: Francesco Romani --- cpuset_empty.go | 28 +++++++++++++++++++++++++--- cpuset_parse.go | 8 +++----- cpuset_test.go | 15 +++++++-------- cpuset_unparse.go | 26 ++++++++++---------------- go.mod | 4 ++-- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/cpuset_empty.go b/cpuset_empty.go index 1195f62..85931d9 100644 --- a/cpuset_empty.go +++ b/cpuset_empty.go @@ -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 } diff --git a/cpuset_parse.go b/cpuset_parse.go index 55aab63..9246889 100644 --- a/cpuset_parse.go +++ b/cpuset_parse.go @@ -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 @@ -37,7 +36,7 @@ func Parse(s string) ([]int, error) { if err != nil { return cpus, err } - cpus = append(cpus, cpuid) + cpus[cpuid] = struct{}{} continue } @@ -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 } diff --git a/cpuset_test.go b/cpuset_test.go index 6b7f207..c74b786 100644 --- a/cpuset_test.go +++ b/cpuset_test.go @@ -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) @@ -90,7 +89,7 @@ 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) @@ -98,11 +97,11 @@ func mustParse(t *testing.T, s string) []int { 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") } } @@ -113,14 +112,14 @@ 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) } @@ -128,7 +127,7 @@ func (tc tcase) CheckUnparseSlices(t *testing.T) { 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) } diff --git a/cpuset_unparse.go b/cpuset_unparse.go index aeb0791..975cf2f 100644 --- a/cpuset_unparse.go +++ b/cpuset_unparse.go @@ -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 { @@ -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 @@ -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]) } diff --git a/go.mod b/go.mod index e2f8e90..abf72ba 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/fromanirh/cpuset +module github.com/ffromani/cpuset -go 1.16 +go 1.18