github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/niterator/unrollinreversehardcode/iterator.go (about) 1 package unrollinreversehardcode 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 [4]Index 19 NextIndex uint32 20 Done bool 21 } 22 23 func NewIterator(ap *shape.AP) *Iterator { 24 track := [4]Index{} 25 26 for i := range ap.Shape { 27 track[3-i].Track = uint32(ap.Shape[i]) 28 //track[3-i].Shape = uint32(ap.Shape[i]) 29 //track[3-i].Stride = uint32(ap.Stride[i]) 30 } 31 32 return &Iterator{ 33 AP: ap, 34 Track: track, 35 } 36 } 37 38 func (it *Iterator) IsDone() bool { 39 return it.Done 40 } 41 42 func (it *Iterator) Next() (int, error) { 43 if it.Done { 44 return 0, io.EOF 45 } 46 47 next := it.NextIndex 48 result := int(next) 49 50 // {Track:100 Shape:100 Stride:1} 51 x := &it.Track[0] 52 x.Track-- 53 next += 1 54 if x.Track > 0 { 55 it.NextIndex = next 56 return result, nil 57 } 58 x.Track = 100 59 next -= 100 * 1 60 61 // {Track:100 Shape:100 Stride:100} 62 x = &it.Track[1] 63 x.Track-- 64 next += 100 65 if x.Track > 0 { 66 it.NextIndex = next 67 return result, nil 68 } 69 x.Track = 100 70 next -= 100 * 100 71 72 // {Track:100 Shape:100 Stride:10000} 73 x = &it.Track[2] 74 x.Track-- 75 next += 10000 76 if x.Track > 0 { 77 it.NextIndex = next 78 return result, nil 79 } 80 x.Track = 100 81 next -= 100 * 10000 82 83 // {Track:100 Shape:100 Stride:1000000} 84 x = &it.Track[3] 85 x.Track-- 86 next += 1000000 87 if x.Track > 0 { 88 it.NextIndex = next 89 return result, nil 90 } 91 it.Done = true 92 it.NextIndex = next 93 94 return result, nil 95 }