-
Notifications
You must be signed in to change notification settings - Fork 6
/
iterator.go
72 lines (58 loc) · 1.18 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
71
72
package onearrrevadvance
import (
"io"
"github.com/egonelbre/exp/niterator/shape"
)
type Index struct {
Track uint32
Advance uint32
Shape uint32
}
type Iterator struct {
*shape.AP
Track []Index
NextIndex uint32
Done bool
}
func NewIterator(ap *shape.AP) *Iterator {
track := make([]Index, len(ap.Shape))
last := len(ap.Shape) - 1
stride := ap.Stride[:last+1]
shape := ap.Shape[:last+1]
track[0].Track = uint32(shape[last])
track[0].Advance = uint32(stride[last])
track[0].Shape = uint32(shape[last])
for i := 1; i < last+1; i++ {
track[i].Track = uint32(shape[last-i])
track[i].Shape = uint32(shape[last-i])
track[i].Advance = uint32(stride[last-i] - stride[last-i+1]*shape[last-i+1])
}
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
}
next := it.NextIndex
result := next
track := it.Track
for i := range track {
x := &track[i]
x.Track--
next += x.Advance
if x.Track > 0 {
it.NextIndex = next
return int(result), nil
}
x.Track = x.Shape
}
it.Done = true
it.NextIndex = next
return int(result), nil
}