forked from egonelbre/exp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
70 lines (59 loc) · 1.13 KB
/
iterator.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package onearrpremul
import (
"io"
"github.com/egonelbre/exp/niterator/shape"
)
type Index struct {
Track uint32
Shape uint32
Stride uint32
Premul uint32
}
type Iterator struct {
*shape.AP
Track []Index
NextIndex uint32
Done bool
}
func NewIterator(ap *shape.AP) *Iterator {
track := make([]Index, len(ap.Shape))
for i := range ap.Shape {
track[i].Shape = uint32(ap.Shape[i])
track[i].Stride = uint32(ap.Stride[i])
track[i].Premul = uint32((ap.Shape[i] - 1) * ap.Stride[i])
}
return &Iterator{
AP: ap,
Track: track,
}
}
func (it *Iterator) IsDone() bool {
return it.Done
}
func (it *Iterator) Next() (int, error) {
if it.Done {
return 0, io.EOF
}
last := len(it.Track) - 1
next := it.NextIndex
result := next
// the following 3 lines causes the compiler to perform bounds check here,
// instead of being done in the loop
track := it.Track[:last+1]
for i := last; i >= 0; i-- {
x := &track[i]
x.Track++
if x.Track == x.Shape {
if i == 0 {
it.Done = true
}
x.Track = 0
next -= x.Premul
continue
}
next += x.Stride
break
}
it.NextIndex = next
return int(result), nil
}