@@ -25,52 +25,28 @@ import (
2525 "strings"
2626)
2727
28- // Builder is a mutable builder for CPUSet. Functions that mutate instances
29- // of this type are not thread-safe.
30- type Builder struct {
31- result CPUSet
32- done bool
33- }
34-
35- // NewBuilder returns a mutable CPUSet builder.
36- func NewBuilder () * Builder {
37- return & Builder {
38- result : CPUSet {
39- elems : map [int ]struct {}{},
40- },
41- }
42- }
43-
44- // Add adds the supplied elements to the result. Calling Add after calling
45- // Result has no effect.
46- func (b * Builder ) Add (elems ... int ) {
47- if b .done {
48- return
49- }
50- for _ , elem := range elems {
51- b .result .elems [elem ] = struct {}{}
52- }
53- }
54-
55- // Result returns the result CPUSet containing all elements that were
56- // previously added to this builder. Subsequent calls to Add have no effect.
57- func (b * Builder ) Result () CPUSet {
58- b .done = true
59- return b .result
60- }
61-
6228// CPUSet is a thread-safe, immutable set-like data structure for CPU IDs.
6329type CPUSet struct {
6430 elems map [int ]struct {}
6531}
6632
6733// New returns a new CPUSet containing the supplied elements.
6834func New (cpus ... int ) CPUSet {
69- b := NewBuilder ()
35+ s := CPUSet {
36+ elems : map [int ]struct {}{},
37+ }
7038 for _ , c := range cpus {
71- b .Add (c )
39+ s .add (c )
40+ }
41+ return s
42+ }
43+
44+ // add adds the supplied elements to the CPUSet.
45+ // It is intended for internal use only, since it mutates the CPUSet.
46+ func (s CPUSet ) add (elems ... int ) {
47+ for _ , elem := range elems {
48+ s .elems [elem ] = struct {}{}
7249 }
73- return b .Result ()
7450}
7551
7652// Size returns the number of elements in this set.
@@ -98,13 +74,13 @@ func (s CPUSet) Equals(s2 CPUSet) bool {
9874// filter returns a new CPU set that contains all of the elements from this
9975// set that match the supplied predicate, without mutating the source set.
10076func (s CPUSet ) filter (predicate func (int ) bool ) CPUSet {
101- b := NewBuilder ()
77+ r := New ()
10278 for cpu := range s .elems {
10379 if predicate (cpu ) {
104- b . Add (cpu )
80+ r . add (cpu )
10581 }
10682 }
107- return b . Result ()
83+ return r
10884}
10985
11086// IsSubsetOf returns true if the supplied set contains all the elements
@@ -123,16 +99,16 @@ func (s CPUSet) IsSubsetOf(s2 CPUSet) bool {
12399// set and all of the elements from the supplied sets, without mutating
124100// either source set.
125101func (s CPUSet ) Union (s2 ... CPUSet ) CPUSet {
126- b := NewBuilder ()
102+ r := New ()
127103 for cpu := range s .elems {
128- b . Add (cpu )
104+ r . add (cpu )
129105 }
130106 for _ , cs := range s2 {
131107 for cpu := range cs .elems {
132- b . Add (cpu )
108+ r . add (cpu )
133109 }
134110 }
135- return b . Result ()
111+ return r
136112}
137113
138114// Intersection returns a new CPU set that contains all of the elements
@@ -214,13 +190,13 @@ func (s CPUSet) String() string {
214190//
215191// See: http://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS
216192func Parse (s string ) (CPUSet , error ) {
217- b := NewBuilder ()
218-
219193 // Handle empty string.
220194 if s == "" {
221- return b . Result (), nil
195+ return New (), nil
222196 }
223197
198+ result := New ()
199+
224200 // Split CPU list string:
225201 // "0-5,34,46-48" => ["0-5", "34", "46-48"]
226202 ranges := strings .Split (s , "," )
@@ -233,7 +209,7 @@ func Parse(s string) (CPUSet, error) {
233209 if err != nil {
234210 return New (), err
235211 }
236- b . Add (elem )
212+ result . add (elem )
237213 } else if len (boundaries ) == 2 {
238214 // Handle multi-element ranges like "0-5".
239215 start , err := strconv .Atoi (boundaries [0 ])
@@ -252,18 +228,18 @@ func Parse(s string) (CPUSet, error) {
252228 // Add all elements to the result.
253229 // e.g. "0-5", "46-48" => [0, 1, 2, 3, 4, 5, 46, 47, 48].
254230 for e := start ; e <= end ; e ++ {
255- b . Add (e )
231+ result . add (e )
256232 }
257233 }
258234 }
259- return b . Result () , nil
235+ return result , nil
260236}
261237
262238// Clone returns a copy of this CPU set.
263239func (s CPUSet ) Clone () CPUSet {
264- b := NewBuilder ()
240+ r := New ()
265241 for elem := range s .elems {
266- b . Add (elem )
242+ r . add (elem )
267243 }
268- return b . Result ()
244+ return r
269245}
0 commit comments