-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspan.go
More file actions
39 lines (36 loc) · 787 Bytes
/
span.go
File metadata and controls
39 lines (36 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package concurrency
type Span struct {
Start int
Count int
}
func Spans(total, spanSize int) []Span {
spans := make([]Span, 0, (total+spanSize-1)/spanSize)
var c int
for i := 0; i < total; i += c {
if i+spanSize <= total {
c = spanSize
spans = append(spans, Span{Start: i, Count: c})
} else {
c = total - i
if c > spanSize/2 || len(spans) == 0 {
spans = append(spans, Span{Start: i, Count: c})
} else {
spans[len(spans)-1].Count += c
}
}
}
return spans
}
func StrictSpans(total, spanSize int) []Span {
spans := make([]Span, 0, (total+spanSize-1)/spanSize)
var c int
for i := 0; i < total; i += c {
if i+spanSize <= total {
c = spanSize
} else {
c = total - i
}
spans = append(spans, Span{Start: i, Count: c})
}
return spans
}