github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/niterator/onearrrev/iterator.go (about) 1 package onearrrev 2 3 import ( 4 "io" 5 6 "github.com/egonelbre/exp/niterator/shape" 7 ) 8 9 type Index struct { 10 Track uint32 11 Shape uint32 12 Stride uint32 13 } 14 15 type Iterator struct { 16 *shape.AP 17 18 Track []Index 19 NextIndex uint32 20 Done bool 21 } 22 23 func NewIterator(ap *shape.AP) *Iterator { 24 track := make([]Index, len(ap.Shape)) 25 for i := range ap.Shape { 26 track[3-i].Track = uint32(ap.Shape[i]) 27 track[3-i].Shape = uint32(ap.Shape[i]) 28 track[3-i].Stride = uint32(ap.Stride[i]) 29 } 30 31 return &Iterator{ 32 AP: ap, 33 Track: track, 34 } 35 } 36 37 func (it *Iterator) IsDone() bool { 38 return it.Done 39 } 40 41 func (it *Iterator) Next() (int, error) { 42 if it.Done { 43 return 0, io.EOF 44 } 45 46 next := it.NextIndex 47 result := next 48 track := it.Track 49 for i := range track { 50 x := &track[i] 51 x.Track-- 52 if x.Track > 0 { 53 next += x.Stride 54 it.NextIndex = next 55 return int(result), nil 56 } 57 x.Track = x.Shape 58 next -= (x.Shape - 1) * x.Stride 59 } 60 61 it.Done = true 62 it.NextIndex = next 63 return int(result), nil 64 }